qSqlparser.h 12.6 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

#ifdef __cplusplus
extern "C" {
#endif

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

H
Haojun Liao 已提交
29
#define ParseTOKENTYPE SStrToken
H
Haojun Liao 已提交
30 31 32 33 34 35 36 37 38 39 40 41

#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,
};

H
Haojun Liao 已提交
42
enum SQL_NODE_FROM_TYPE {
43 44
  SQL_NODE_FROM_SUBQUERY   = 1,
  SQL_NODE_FROM_TABLELIST  = 2,
H
Haojun Liao 已提交
45 46
};

D
fix bug  
dapan1121 已提交
47
enum SQL_EXPR_FLAG {
D
dapan1121 已提交
48
  EXPR_FLAG_TS_ERROR = 1,
49
  EXPR_FLAG_NS_TIMESTAMP = 2,
D
dapan1121 已提交
50
  EXPR_FLAG_TIMESTAMP_VAR = 3,
H
Haojun Liao 已提交
51 52
};

53 54 55 56 57 58 59 60 61 62 63
extern char tTokenTypeSwitcher[13];

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

H
Haojun Liao 已提交
64 65
#define TPARSER_HAS_TOKEN(_t)      ((_t).n > 0)
#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0)
H
Haojun Liao 已提交
66

67
typedef struct SLimitVal {
H
Haojun Liao 已提交
68 69
  int64_t            limit;
  int64_t            offset;
70 71 72
} SLimitVal;

typedef struct SOrderVal {
H
Haojun Liao 已提交
73 74
  uint32_t           order;
  int32_t            orderColId;
75 76 77
} SOrderVal;

typedef struct tVariantListItem {
H
Haojun Liao 已提交
78 79
  tVariant           pVar;
  uint8_t            sortOrder;
80 81
} tVariantListItem;

82
typedef struct SIntervalVal {
83
  int32_t            token;
H
Haojun Liao 已提交
84 85
  SStrToken          interval;
  SStrToken          offset;
86 87
} SIntervalVal;

D
dapan1121 已提交
88 89 90 91 92
typedef struct SRangeVal {
  void          *start;
  void          *end;
} SRangeVal;

93
typedef struct SSessionWindowVal {
H
Haojun Liao 已提交
94 95
  SStrToken          col;
  SStrToken          gap;
96 97
} SSessionWindowVal;

Y
TD-2570  
yihaoDeng 已提交
98 99 100 101
typedef struct SWindowStateVal {
  SStrToken          col;
} SWindowStateVal;

102
struct SRelationInfo;
H
Haojun Liao 已提交
103

104
typedef struct SSqlNode {
H
Haojun Liao 已提交
105
  struct SArray     *pSelNodeList; // select clause
106
  struct SRelationInfo  *from;     // from clause SArray<SSqlNode>
H
Haojun Liao 已提交
107 108
  struct tSqlExpr   *pWhere;       // where clause [optional]
  SArray            *pGroupby;     // groupby clause, only for tags[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
109
  SArray            *pSortOrder;   // orderby [optional],  SArray<tVariantListItem>
H
Haojun Liao 已提交
110
  SArray            *fillType;     // fill type[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
111 112
  SIntervalVal       interval;     // (interval, interval_offset) [optional]
  SSessionWindowVal  sessionVal;   // session window [optional]
Y
TD-2570  
yihaoDeng 已提交
113
  SWindowStateVal    windowstateVal; // window_state(col) [optional]
H
Haojun Liao 已提交
114 115 116
  SStrToken          sliding;      // sliding window [optional]
  SLimitVal          limit;        // limit offset [optional]
  SLimitVal          slimit;       // group limit offset [optional]
H
Haojun Liao 已提交
117
  SStrToken          sqlstr;       // sql string in select clause
D
dapan1121 已提交
118
  struct tSqlExpr   *pHaving;      // having clause [optional]
D
dapan1121 已提交
119
  SRangeVal          pRange;       // range clause [optional]
120
} SSqlNode;
121

H
Haojun Liao 已提交
122 123 124 125 126 127
typedef struct SRelElementPair {
  union {
    SStrToken  tableName;
    SArray    *pSubquery;
  };

H
Haojun Liao 已提交
128
  SStrToken aliasName;
H
Haojun Liao 已提交
129
} SRelElementPair;
H
Haojun Liao 已提交
130

131 132
typedef struct SRelationInfo {
  int32_t       type;        // nested query|table name list
H
Haojun Liao 已提交
133
  SArray       *list;        // SArray<SRelElementPair>
134
} SRelationInfo;
135

136
typedef struct SCreatedTableInfo {
H
Haojun Liao 已提交
137 138 139 140 141 142 143
  SStrToken          name;        // table name token
  SStrToken          stableName;  // super table name token , for using clause
  SArray            *pTagNames;   // create by using super table, tag name
  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
144 145
} SCreatedTableInfo;

H
Haojun Liao 已提交
146
typedef struct SCreateTableSql {
H
Haojun Liao 已提交
147 148 149
  SStrToken          name;  // table name, create table [name] xxx
  int8_t             type;  // create normal table/from super table/ stream
  bool               existCheck;
150

151
  struct {
H
Haojun Liao 已提交
152 153
    SArray          *pTagColumns; // SArray<TAOS_FIELD>
    SArray          *pColumns;    // SArray<TAOS_FIELD>
154
  } colInfo;
155

H
Haojun Liao 已提交
156
  SArray            *childTableInfo;        // SArray<SCreatedTableInfo>
H
Haojun Liao 已提交
157
  SSqlNode          *pSelect;
H
Haojun Liao 已提交
158
} SCreateTableSql;
159

H
Haojun Liao 已提交
160
typedef struct SAlterTableInfo {
H
Haojun Liao 已提交
161 162 163 164 165 166
  SStrToken          name;
  int16_t            tableType;
  int16_t            type;
  STagData           tagData;
  SArray            *pAddColumns; // SArray<TAOS_FIELD>
  SArray            *varList;     // set t=val or: change src dst, SArray<tVariantListItem>
H
Haojun Liao 已提交
167
} SAlterTableInfo;
168

H
Haojun Liao 已提交
169
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  SStrToken          dbname;
  int32_t            replica;
  int32_t            cacheBlockSize;
  int32_t            maxTablesPerVnode;
  int32_t            numOfBlocks;
  int32_t            daysPerFile;
  int32_t            minRowsPerBlock;
  int32_t            maxRowsPerBlock;
  int32_t            fsyncPeriod;
  int64_t            commitTime;
  int32_t            walLevel;
  int32_t            quorum;
  int32_t            compressionLevel;
  SStrToken          precision;
  bool               ignoreExists;
  int8_t             update;
  int8_t             cachelast;
  SArray            *keep;
  int8_t             dbType;
  int16_t            partitions;
H
Haojun Liao 已提交
190
} SCreateDbInfo;
191

D
dapan1121 已提交
192 193 194
typedef struct SCreateFuncInfo {
  SStrToken name;
  SStrToken path;
195
  int32_t   type;
D
dapan1121 已提交
196
  int32_t   bufSize;
D
dapan1121 已提交
197
  TAOS_FIELD output;
D
dapan1121 已提交
198 199 200
} SCreateFuncInfo;


H
Haojun Liao 已提交
201
typedef struct SCreateAcctInfo {
H
Haojun Liao 已提交
202 203 204 205 206 207 208 209 210
  int32_t            maxUsers;
  int32_t            maxDbs;
  int32_t            maxTimeSeries;
  int32_t            maxStreams;
  int32_t            maxPointsPerSecond;
  int64_t            maxStorage;
  int64_t            maxQueryTime;
  int32_t            maxConnections;
  SStrToken          stat;
H
Haojun Liao 已提交
211
} SCreateAcctInfo;
212 213

typedef struct SShowInfo {
H
Haojun Liao 已提交
214 215 216
  uint8_t            showType;
  SStrToken          prefix;
  SStrToken          pattern;
217 218 219
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
220 221 222 223
  SStrToken          user;
  SStrToken          passwd;
  SStrToken          privilege;
  int16_t            type;
224 225
} SUserInfo;

H
Haojun Liao 已提交
226
typedef struct SMiscInfo {
H
Haojun Liao 已提交
227 228 229 230 231
  SArray            *a;         // SArray<SStrToken>
  bool               existsCheck;
  int16_t            dbType;
  int16_t            tableType;
  SUserInfo          user;
232
  union {
H
Haojun Liao 已提交
233 234
    SCreateDbInfo   dbOpt;
    SCreateAcctInfo acctOpt;
D
dapan1121 已提交
235
    SCreateFuncInfo funcOpt;
H
Haojun Liao 已提交
236 237
    SShowInfo       showOpt;
    SStrToken       id;
238
  };
H
Haojun Liao 已提交
239
} SMiscInfo;
240 241

typedef struct SSqlInfo {
H
Haojun Liao 已提交
242 243
  int32_t            type;
  bool               valid;
244
  SArray            *list;    // todo refactor
H
Haojun Liao 已提交
245
  char               msg[256];
D
dapan1121 已提交
246
  SArray            *funcs;
247
  union {
H
Haojun Liao 已提交
248 249 250
    SCreateTableSql *pCreateTableInfo;
    SAlterTableInfo *pAlterInfo;
    SMiscInfo       *pMiscInfo;
251 252 253
  };
} SSqlInfo;

H
Haojun Liao 已提交
254
typedef struct tSqlExpr {
H
Haojun Liao 已提交
255 256
  uint16_t           type;       // sql node type
  uint32_t           tokenId;    // TK_LE: less than(binary expr)
H
Haojun Liao 已提交
257

H
Haojun Liao 已提交
258 259 260 261 262
  // the whole string of the function(col, param), while the function name is kept in exprToken
  struct {
    SStrToken        operand;
    struct SArray   *paramList;      // function parameters list
  } Expr;
H
Haojun Liao 已提交
263

264
  int32_t            functionId;  // function id, todo remove it
H
Haojun Liao 已提交
265
  SStrToken          columnName;  // table column info
H
Haojun Liao 已提交
266
  tVariant           value;       // the use input value
H
Haojun Liao 已提交
267 268
  SStrToken          exprToken;   // original sql expr string
  uint32_t           flags;       // todo remove it
D
fix bug  
dapan1121 已提交
269
  
H
Haojun Liao 已提交
270 271
  struct tSqlExpr   *pLeft;       // left child
  struct tSqlExpr   *pRight;      // right child
H
Haojun Liao 已提交
272
} tSqlExpr;
273

H
Haojun Liao 已提交
274
// used in select clause. select <SArray> from xxx
H
Haojun Liao 已提交
275
typedef struct tSqlExprItem {
H
Haojun Liao 已提交
276 277 278
  tSqlExpr          *pNode;      // The list of expressions
  char              *aliasName;  // alias name, null-terminated string
  bool               distinct;
H
Haojun Liao 已提交
279
} tSqlExprItem;
280

D
dapan1121 已提交
281

H
Haojun Liao 已提交
282 283 284
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);
285

286
SRelationInfo *setTableNameList(SRelationInfo* pFromInfo, SStrToken *pName, SStrToken* pAlias);
H
Haojun Liao 已提交
287
void          *destroyRelationInfo(SRelationInfo* pFromInfo);
H
Haojun Liao 已提交
288
SRelationInfo *addSubqueryElem(SRelationInfo* pRelationInfo, SArray* pSub, SStrToken* pAlias);
D
dapan1121 已提交
289

H
Haojun Liao 已提交
290
// sql expr leaf node
D
dapan1121 已提交
291
tSqlExpr *tSqlExprCreateTimestamp(SStrToken *pToken, int32_t optrType);
292
tSqlExpr *tSqlExprCreateIdValue(SSqlInfo* pInfo, SStrToken *pToken, int32_t optrType);
H
Haojun Liao 已提交
293
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
D
dapan1121 已提交
294
SArray *tStrTokenAppend(SArray *pList, SStrToken *pToken);
295

H
Haojun Liao 已提交
296 297
tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType);
tSqlExpr *tSqlExprClone(tSqlExpr *pSrc);
H
Haojun Liao 已提交
298 299 300
void      tSqlExprCompact(tSqlExpr** pExpr);
bool      tSqlExprIsLeaf(tSqlExpr* pExpr);
bool      tSqlExprIsParentOfLeaf(tSqlExpr* pExpr);
H
Haojun Liao 已提交
301 302 303
void      tSqlExprDestroy(tSqlExpr *pExpr);
SArray   *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken);
void      tSqlExprListDestroy(SArray *pList);
304

305
SSqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere,
Y
TD-2570  
yihaoDeng 已提交
306
                                SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps, SWindowStateVal *pw,
D
dapan1121 已提交
307
                                SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pgLimit, tSqlExpr *pHaving, SRangeVal *pRange);
H
Haojun Liao 已提交
308
int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right);
309

310
SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type);
311

H
Haojun Liao 已提交
312
SAlterTableInfo *tSetAlterTableInfo(SStrToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableTable);
313
SCreatedTableInfo createNewChildTableInfo(SStrToken *pTableName, SArray *pTagNames, SArray *pTagVals, SStrToken *pToken, SStrToken* igExists);
314

315 316
void destroyAllSqlNode(SArray *pSqlNode);
void destroySqlNode(SSqlNode *pSql);
317
void freeCreateTableInfo(void* p);
318

319 320 321
SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type);
SArray   *setSubclause(SArray *pList, void *pSqlNode);
SArray   *appendSelectClause(SArray *pList, void *pSubclause);
322

323
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists);
324

H
Haojun Liao 已提交
325
void SqlInfoDestroy(SSqlInfo *pInfo);
326

H
Haojun Liao 已提交
327
void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
D
dapan1121 已提交
328
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType);
H
Haojun Liao 已提交
329
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
330

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

H
Haojun Liao 已提交
333
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo);
H
Haojun Liao 已提交
334 335 336
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);
337

Y
yihaoDeng 已提交
338 339
void setCompactVnodeSql(SSqlInfo *pInfo, int32_t type, SArray *pParam);

H
Haojun Liao 已提交
340
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
D
dapan1121 已提交
341
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);
342 343

// prefix show db.tables;
H
Haojun Liao 已提交
344
void tSetDbName(SStrToken *pCpxName, SStrToken *pDb);
345

H
Haojun Liao 已提交
346 347
void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
void tSetColumnType(TAOS_FIELD *pField, SStrToken *type);
348

H
Haojun Liao 已提交
349 350 351 352 353 354 355
/**
 *
 * @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 *);
356

H
Haojun Liao 已提交
357 358 359 360 361 362
/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));
363

H
Haojun Liao 已提交
364 365 366 367 368
/**
 *
 * @param mallocProc  The parser allocator
 * @return
 */
369 370
void *ParseAlloc(void *(*mallocProc)(size_t));

H
Haojun Liao 已提交
371 372 373 374 375 376
/**
 *
 * @param str sql string
 * @return sql ast
 */
SSqlInfo qSqlParse(const char *str);
377

378 379 380 381
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
382
#endif  // TDENGINE_QSQLPARSER_H