qSqlparser.h 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
hjxilinx 已提交
16 17
#ifndef TDENGINE_QSQLPARSER_H
#define TDENGINE_QSQLPARSER_H
18 19 20 21 22 23 24 25

#ifdef __cplusplus
extern "C" {
#endif

#include "taos.h"
#include "taosmsg.h"
#include "tstoken.h"
H
Haojun Liao 已提交
26
#include "tstrbuild.h"
27 28
#include "tvariant.h"

H
Haojun Liao 已提交
29
#define ParseTOKENTYPE SStrToken
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
extern char tTokenTypeSwitcher[13];

#define toTSDBType(x)                          \
  do {                                         \
    if ((x) >= tListLen(tTokenTypeSwitcher)) { \
      (x) = TSDB_DATA_TYPE_BINARY;             \
    } else {                                   \
      (x) = tTokenTypeSwitcher[(x)];           \
    }                                          \
  } while (0)

typedef struct SLimitVal {
  int64_t limit;
  int64_t offset;
} SLimitVal;

typedef struct SOrderVal {
  uint32_t order;
H
Haojun Liao 已提交
48
  int32_t  orderColId;
49 50 51 52 53 54 55
} SOrderVal;

typedef struct tVariantListItem {
  tVariant pVar;
  uint8_t  sortOrder;
} tVariantListItem;

56 57 58 59 60
typedef struct SIntervalVal {
  SStrToken interval;
  SStrToken offset;
} SIntervalVal;

61 62 63 64 65
typedef struct SSessionWindowVal {
  SStrToken col;
  SStrToken gap;
} SSessionWindowVal;

66 67
typedef struct SQuerySQL {
  struct tSQLExprList *pSelection;   // select clause
H
Haojun Liao 已提交
68
  SArray *             from;         // from clause  SArray<tVariantListItem>
69
  struct tSQLExpr *    pWhere;       // where clause [optional]
H
Haojun Liao 已提交
70 71
  SArray *             pGroupby;     // groupby clause, only for tags[optional], SArray<tVariantListItem>
  SArray *             pSortOrder;   // orderby [optional], SArray<tVariantListItem>
72 73
  SIntervalVal         interval;     // (interval, interval_offset) [optional]
  SSessionWindowVal    sessionVal;   // session window [optional]
H
Haojun Liao 已提交
74
  SStrToken            sliding;      // sliding window [optional]
75 76
  SLimitVal            limit;        // limit offset [optional]
  SLimitVal            slimit;       // group limit offset [optional]
H
Haojun Liao 已提交
77
  SArray *             fillType;     // fill type[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
78
  SStrToken            selectToken;  // sql string
79 80
} SQuerySQL;

81 82 83
typedef struct SCreatedTableInfo {
  SStrToken  name;        // table name token
  SStrToken  stableName;  // super table name token , for using clause
84
  SArray    *pTagNames;   // create by using super table, tag name
85 86 87 88 89 90
  SArray    *pTagVals;    // create by using super table, tag value
  char      *fullname;    // table full name
  STagData   tagdata;     // true tag data, super table full name is in STagData
  int8_t     igExist;     // ignore if exists
} SCreatedTableInfo;

91
typedef struct SCreateTableSQL {
92 93 94 95
  SStrToken    name;  // table name, create table [name] xxx
  int8_t       type;  // create normal table/from super table/ stream
  bool         existCheck;

96
  struct {
97 98
    SArray    *pTagColumns; // SArray<TAOS_FIELD>
    SArray    *pColumns;    // SArray<TAOS_FIELD>
99
  } colInfo;
100 101 102

  SArray      *childTableInfo;        // SArray<SCreatedTableInfo>
  SQuerySQL   *pSelect;
103 104
} SCreateTableSQL;

H
Haojun Liao 已提交
105
typedef struct SAlterTableInfo {
H
Haojun Liao 已提交
106
  SStrToken     name;
D
dapan1121 已提交
107
  int16_t       tableType;
108 109
  int16_t       type;
  STagData      tagData;
H
Haojun Liao 已提交
110 111
  SArray       *pAddColumns; // SArray<TAOS_FIELD>
  SArray       *varList;  // set t=val or: change src dst, SArray<tVariantListItem>
H
Haojun Liao 已提交
112
} SAlterTableInfo;
113

H
Haojun Liao 已提交
114
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
115
  SStrToken dbname;
116 117
  int32_t   replica;
  int32_t   cacheBlockSize;
H
hjxilinx 已提交
118 119
  int32_t   maxTablesPerVnode;
  int32_t   numOfBlocks;
120
  int32_t   daysPerFile;
H
hjxilinx 已提交
121 122
  int32_t   minRowsPerBlock;
  int32_t   maxRowsPerBlock;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
123
  int32_t   fsyncPeriod;
124
  int64_t   commitTime;
H
hjxilinx 已提交
125
  int32_t   walLevel;
126
  int32_t   quorum;
127
  int32_t   compressionLevel;
H
Haojun Liao 已提交
128
  SStrToken precision;
129
  bool      ignoreExists;
D
dapan1121 已提交
130 131
  int8_t    update;
  int8_t    cachelast; 
H
Haojun Liao 已提交
132
  SArray    *keep;
D
dapan1121 已提交
133 134
  int8_t    dbType;
  int16_t   partitions;
H
Haojun Liao 已提交
135
} SCreateDbInfo;
136

H
Haojun Liao 已提交
137
typedef struct SCreateAcctInfo {
138 139 140 141 142 143 144 145
  int32_t   maxUsers;
  int32_t   maxDbs;
  int32_t   maxTimeSeries;
  int32_t   maxStreams;
  int32_t   maxPointsPerSecond;
  int64_t   maxStorage;
  int64_t   maxQueryTime;
  int32_t   maxConnections;
H
Haojun Liao 已提交
146
  SStrToken stat;
H
Haojun Liao 已提交
147
} SCreateAcctInfo;
148 149 150

typedef struct SShowInfo {
  uint8_t showType;
H
Haojun Liao 已提交
151 152
  SStrToken prefix;
  SStrToken pattern;
153 154 155
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
156 157 158
  SStrToken user;
  SStrToken passwd;
  SStrToken privilege;
159 160 161
  int16_t   type;
} SUserInfo;

H
Haojun Liao 已提交
162 163 164
typedef struct SMiscInfo {
  SArray    *a;         // SArray<SStrToken>
  bool       existsCheck;
D
dapan1121 已提交
165
  int16_t    dbType;
D
dapan1121 已提交
166
  int16_t    tableType;
H
Haojun Liao 已提交
167
  SUserInfo  user;
168
  union {
H
Haojun Liao 已提交
169 170 171 172
    SCreateDbInfo   dbOpt;
    SCreateAcctInfo acctOpt;
    SShowInfo       showOpt;
    SStrToken       id;
173
  };
H
Haojun Liao 已提交
174
} SMiscInfo;
175 176 177 178 179 180 181

typedef struct SSubclauseInfo {  // "UNION" multiple select sub-clause
  SQuerySQL **pClause;
  int32_t     numOfClause;
} SSubclauseInfo;

typedef struct SSqlInfo {
H
Haojun Liao 已提交
182 183
  int32_t            type;
  bool               valid;
H
Haojun Liao 已提交
184 185
  SSubclauseInfo     subclauseInfo;
  char               msg[256];
186
  union {
H
Haojun Liao 已提交
187 188 189
    SCreateTableSQL  *pCreateTableInfo;
    SAlterTableInfo  *pAlterInfo;
    SMiscInfo        *pMiscInfo;
190 191 192
  };
} SSqlInfo;

H
Haojun Liao 已提交
193 194 195 196 197 198 199 200 201 202 203
#define NON_ARITHMEIC_EXPR 0
#define NORMAL_ARITHMETIC  1
#define AGG_ARIGHTMEIC     2

enum SQL_NODE_TYPE {
  SQL_NODE_TABLE_COLUMN= 1,
  SQL_NODE_SQLFUNCTION = 2,
  SQL_NODE_VALUE       = 3,
  SQL_NODE_EXPR        = 4,
};

204
typedef struct tSQLExpr {
H
Haojun Liao 已提交
205 206 207 208
  uint16_t         type;       // sql node type
  uint32_t         tokenId;    // TK_FUNCTION: sql function, TK_LE: less than(binary expr)

  // the whole string of the function(col, param), while the function name is kept in token
H
Haojun Liao 已提交
209
  SStrToken        operand;
H
Haojun Liao 已提交
210 211 212 213
  uint32_t         functionId;  // function id

  SStrToken        colInfo;     // table column info
  tVariant         value;       // the use input value
H
Haojun Liao 已提交
214 215
  SStrToken        token;       // original sql expr string

H
[td-32]  
hjxilinx 已提交
216 217
  struct tSQLExpr *pLeft;       // left child
  struct tSQLExpr *pRight;      // right child
H
Haojun Liao 已提交
218
  struct tSQLExprList *pParam;  // function parameters list
219 220 221
} tSQLExpr;

// used in select clause. select <tSQLExprList> from xxx
H
Haojun Liao 已提交
222
typedef struct tSqlExprItem {
223 224
  tSQLExpr *pNode;      // The list of expressions
  char *    aliasName;  // alias name, null-terminated string
dengyihao's avatar
TD-2571  
dengyihao 已提交
225
  bool      distinct;
H
Haojun Liao 已提交
226
} tSqlExprItem;
227

228
// todo refactor by using SArray
229 230 231
typedef struct tSQLExprList {
  int32_t       nExpr;  /* Number of expressions on the list */
  int32_t       nAlloc; /* Number of entries allocated below */
H
Haojun Liao 已提交
232
  tSqlExprItem *a;      /* One entry for each expression */
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
} tSQLExprList;

/**
 *
 * @param yyp      The parser
 * @param yymajor  The major token code number
 * @param yyminor  The value for the token
 */
void Parse(void *yyp, int yymajor, ParseTOKENTYPE yyminor, SSqlInfo *);

/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));

H
Haojun Liao 已提交
250 251 252
SArray *tVariantListAppend(SArray *pList, tVariant *pVar, uint8_t sortOrder);
SArray *tVariantListInsert(SArray *pList, tVariant *pVar, uint8_t sortOrder, int32_t index);
SArray *tVariantListAppendToken(SArray *pList, SStrToken *pAliasToken, uint8_t sortOrder);
253

H
Haojun Liao 已提交
254
tSQLExpr *tSqlExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optrType);
255

D
dapan1121 已提交
256 257
tSQLExpr *tSqlExprClone(tSQLExpr *pSrc);

H
Haojun Liao 已提交
258
void tSqlExprDestroy(tSQLExpr *pExpr);
259

dengyihao's avatar
TD-2571  
dengyihao 已提交
260
tSQLExprList *tSqlExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SStrToken *pDistinct, SStrToken *pToken);
261

H
Haojun Liao 已提交
262
void tSqlExprListDestroy(tSQLExprList *pList);
263

264 265
SQuerySQL *tSetQuerySqlNode(SStrToken *pSelectToken, tSQLExprList *pSelection, SArray *pFrom, tSQLExpr *pWhere,
                             SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *pSession,
H
Haojun Liao 已提交
266
                             SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pGLimit);
267

H
Haojun Liao 已提交
268
SCreateTableSQL *tSetCreateSqlElems(SArray *pCols, SArray *pTags, SQuerySQL *pSelect, int32_t type);
269

H
Haojun Liao 已提交
270
void tSqlExprNodeDestroy(tSQLExpr *pExpr);
271

H
Haojun Liao 已提交
272
SAlterTableInfo *  tAlterTableSqlElems(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableTable);
273
SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNames, SArray *pTagVals, SStrToken *pToken, SStrToken* igExists);
274 275 276

void destroyAllSelectClause(SSubclauseInfo *pSql);
void doDestroyQuerySql(SQuerySQL *pSql);
277
void freeCreateTableInfo(void* p);
278

H
Haojun Liao 已提交
279
SSqlInfo       *      setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type);
280 281 282 283
SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo);

SSubclauseInfo *appendSelectClause(SSubclauseInfo *pInfo, void *pSubclause);

284
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists);
285

H
Haojun Liao 已提交
286
void SqlInfoDestroy(SSqlInfo *pInfo);
287 288

void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
D
dapan1121 已提交
289
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType);
H
Haojun Liao 已提交
290
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
291

H
Haojun Liao 已提交
292
void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SStrToken *pToken, SCreateDbInfo *pDB, SStrToken *pIgExists);
293

H
Haojun Liao 已提交
294
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo);
H
Haojun Liao 已提交
295 296 297
void setCreateUserSql(SSqlInfo *pInfo, SStrToken *pName, SStrToken *pPasswd);
void setKillSql(SSqlInfo *pInfo, int32_t type, SStrToken *ip);
void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SStrToken *pName, SStrToken* pPwd, SStrToken *pPrivilege);
298

H
Haojun Liao 已提交
299
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
D
dapan1121 已提交
300
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);
301 302

// prefix show db.tables;
H
Haojun Liao 已提交
303
void setDbName(SStrToken *pCpxName, SStrToken *pDb);
304

H
Haojun Liao 已提交
305
tSQLExpr *tSqlExprIdValueCreate(SStrToken *pToken, int32_t optrType);
306

H
Haojun Liao 已提交
307
tSQLExpr *tSqlExprCreateFunction(tSQLExprList *pList, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
308

H
Haojun Liao 已提交
309
void tSqlSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
310

H
Haojun Liao 已提交
311
void tSqlSetColumnType(TAOS_FIELD *pField, SStrToken *type);
312 313 314

void *ParseAlloc(void *(*mallocProc)(size_t));

315
SSqlInfo qSQLParse(const char *str);
316

317 318 319 320
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
321
#endif  // TDENGINE_QSQLPARSER_H