qSqlparser.h 13.8 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
#include "tvariant.h"
28
#include "tname.h"
29

H
Haojun Liao 已提交
30
#define ParseTOKENTYPE SStrToken
H
Haojun Liao 已提交
31

32 33 34 35
#define SQLEXPR_TYPE_UNASSIGNED 0
#define SQLEXPR_TYPE_SCALAR 1
#define SQLEXPR_TYPE_AGG 2
#define SQLEXPR_TYPE_VALUE 3
H
Haojun Liao 已提交
36 37 38 39 40 41

enum SQL_NODE_TYPE {
  SQL_NODE_TABLE_COLUMN= 1,
  SQL_NODE_SQLFUNCTION = 2,
  SQL_NODE_VALUE       = 3,
  SQL_NODE_EXPR        = 4,
D
cast  
dapan1121 已提交
42
  SQL_NODE_DATA_TYPE   = 5,
H
Haojun Liao 已提交
43 44
};

H
Haojun Liao 已提交
45
enum SQL_NODE_FROM_TYPE {
46 47
  SQL_NODE_FROM_SUBQUERY   = 1,
  SQL_NODE_FROM_TABLELIST  = 2,
H
Haojun Liao 已提交
48 49
};

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

56 57 58 59 60 61 62 63 64 65 66
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 已提交
67 68
#define TPARSER_HAS_TOKEN(_t)      ((_t).n > 0)
#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0)
H
Haojun Liao 已提交
69

70
typedef struct SLimitVal {
H
Haojun Liao 已提交
71 72
  int64_t            limit;
  int64_t            offset;
73 74 75
} SLimitVal;

typedef struct SOrderVal {
H
Haojun Liao 已提交
76 77
  uint32_t           order;
  int32_t            orderColId;
78 79 80
} SOrderVal;

typedef struct tVariantListItem {
H
Haojun Liao 已提交
81 82
  tVariant           pVar;
  uint8_t            sortOrder;
83 84
} tVariantListItem;

wmmhello's avatar
wmmhello 已提交
85
typedef struct CommonItem {
wmmhello's avatar
wmmhello 已提交
86
  union {
wmmhello's avatar
wmmhello 已提交
87
    tVariant           pVar;
wmmhello's avatar
wmmhello 已提交
88 89 90
    struct tSqlExpr    *jsonExp;
  };
  bool               isJsonExp;
wmmhello's avatar
wmmhello 已提交
91 92 93
  uint8_t            sortOrder;
} CommonItem;

94
typedef struct SIntervalVal {
95
  int32_t            token;
H
Haojun Liao 已提交
96 97
  SStrToken          interval;
  SStrToken          offset;
98 99
} SIntervalVal;

100 101
typedef struct tSqlExpr tSqlExprTimestamp;

D
dapan1121 已提交
102
typedef struct SRangeVal {
103 104
  tSqlExprTimestamp *start;
  tSqlExprTimestamp *end;
D
dapan1121 已提交
105 106
} SRangeVal;

107
typedef struct SSessionWindowVal {
H
Haojun Liao 已提交
108 109
  SStrToken          col;
  SStrToken          gap;
110 111
} SSessionWindowVal;

Y
TD-2570  
yihaoDeng 已提交
112 113 114 115
typedef struct SWindowStateVal {
  SStrToken          col;
} SWindowStateVal;

116
struct SRelationInfo;
H
Haojun Liao 已提交
117

118
typedef struct SSqlNode {
H
Haojun Liao 已提交
119
  struct SArray     *pSelNodeList; // select clause
120
  struct SRelationInfo  *from;     // from clause SArray<SSqlNode>
H
Haojun Liao 已提交
121 122
  struct tSqlExpr   *pWhere;       // where clause [optional]
  SArray            *pGroupby;     // groupby clause, only for tags[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
123
  SArray            *pSortOrder;   // orderby [optional],  SArray<tVariantListItem>
H
Haojun Liao 已提交
124
  SArray            *fillType;     // fill type[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
125 126
  SIntervalVal       interval;     // (interval, interval_offset) [optional]
  SSessionWindowVal  sessionVal;   // session window [optional]
Y
TD-2570  
yihaoDeng 已提交
127
  SWindowStateVal    windowstateVal; // window_state(col) [optional]
H
Haojun Liao 已提交
128 129 130
  SStrToken          sliding;      // sliding window [optional]
  SLimitVal          limit;        // limit offset [optional]
  SLimitVal          slimit;       // group limit offset [optional]
H
Haojun Liao 已提交
131
  SStrToken          sqlstr;       // sql string in select clause
D
dapan1121 已提交
132
  struct tSqlExpr   *pHaving;      // having clause [optional]
D
dapan1121 已提交
133
  SRangeVal          pRange;       // range clause [optional]
134
} SSqlNode;
135

H
Haojun Liao 已提交
136 137 138 139 140 141
typedef struct SRelElementPair {
  union {
    SStrToken  tableName;
    SArray    *pSubquery;
  };

H
Haojun Liao 已提交
142
  SStrToken aliasName;
H
Haojun Liao 已提交
143
} SRelElementPair;
H
Haojun Liao 已提交
144

145 146
typedef struct SRelationInfo {
  int32_t       type;        // nested query|table name list
H
Haojun Liao 已提交
147
  SArray       *list;        // SArray<SRelElementPair>
148
} SRelationInfo;
149

150
typedef struct SCreatedTableInfo {
H
Haojun Liao 已提交
151 152 153 154 155 156 157
  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
158 159
} SCreatedTableInfo;

H
Haojun Liao 已提交
160
typedef struct SCreateTableSql {
H
Haojun Liao 已提交
161
  SStrToken          name;  // table name, create table [name] xxx
162 163
  SStrToken          to;    // create stream to anohter table  
  SStrToken          split; // split columns  
H
Haojun Liao 已提交
164 165
  int8_t             type;  // create normal table/from super table/ stream
  bool               existCheck;
166
  SName              toSName;
167

168
  struct {
H
Haojun Liao 已提交
169 170
    SArray          *pTagColumns; // SArray<TAOS_FIELD>
    SArray          *pColumns;    // SArray<TAOS_FIELD>
171
  } colInfo;
172

H
Haojun Liao 已提交
173
  SArray            *childTableInfo;        // SArray<SCreatedTableInfo>
H
Haojun Liao 已提交
174
  SSqlNode          *pSelect;
H
Haojun Liao 已提交
175
} SCreateTableSql;
176

H
Haojun Liao 已提交
177
typedef struct SAlterTableInfo {
H
Haojun Liao 已提交
178 179 180 181 182
  SStrToken          name;
  int16_t            tableType;
  int16_t            type;
  SArray            *pAddColumns; // SArray<TAOS_FIELD>
  SArray            *varList;     // set t=val or: change src dst, SArray<tVariantListItem>
H
Haojun Liao 已提交
183
} SAlterTableInfo;
184

H
Haojun Liao 已提交
185
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  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 已提交
206
} SCreateDbInfo;
207

D
dapan1121 已提交
208 209 210
typedef struct SCreateFuncInfo {
  SStrToken name;
  SStrToken path;
211
  int32_t   type;
D
dapan1121 已提交
212
  int32_t   bufSize;
D
dapan1121 已提交
213
  TAOS_FIELD output;
D
dapan1121 已提交
214 215 216
} SCreateFuncInfo;


H
Haojun Liao 已提交
217
typedef struct SCreateAcctInfo {
H
Haojun Liao 已提交
218 219 220 221 222 223 224 225 226
  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 已提交
227
} SCreateAcctInfo;
228 229

typedef struct SShowInfo {
H
Haojun Liao 已提交
230 231 232
  uint8_t            showType;
  SStrToken          prefix;
  SStrToken          pattern;
233 234 235
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
236 237
  SStrToken          user;
  SStrToken          passwd;
238
  SStrToken          tags;   // format like tag1,tag2,tag3 ... splite with ','
H
Haojun Liao 已提交
239 240
  SStrToken          privilege;
  int16_t            type;
241 242
} SUserInfo;

H
Haojun Liao 已提交
243
typedef struct SMiscInfo {
H
Haojun Liao 已提交
244 245 246 247 248
  SArray            *a;         // SArray<SStrToken>
  bool               existsCheck;
  int16_t            dbType;
  int16_t            tableType;
  SUserInfo          user;
249
  union {
H
Haojun Liao 已提交
250 251
    SCreateDbInfo   dbOpt;
    SCreateAcctInfo acctOpt;
D
dapan1121 已提交
252
    SCreateFuncInfo funcOpt;
H
Haojun Liao 已提交
253 254
    SShowInfo       showOpt;
    SStrToken       id;
255
  };
H
Haojun Liao 已提交
256
} SMiscInfo;
257

258 259 260 261 262 263
typedef struct SDelData {
  bool      existsCheck;
  SStrToken tableName;
  struct tSqlExpr* pWhere;
} SDelData;

264
typedef struct SSqlInfo {
H
Haojun Liao 已提交
265 266
  int32_t            type;
  bool               valid;
267
  SArray            *list;    // todo refactor
H
Haojun Liao 已提交
268
  char               msg[256];
D
dapan1121 已提交
269
  SArray            *funcs;
270
  union {
H
Haojun Liao 已提交
271 272 273
    SCreateTableSql *pCreateTableInfo;
    SAlterTableInfo *pAlterInfo;
    SMiscInfo       *pMiscInfo;
274
    SDelData        *pDelData;
275 276 277
  };
} SSqlInfo;

H
Haojun Liao 已提交
278
typedef struct tSqlExpr {
H
Haojun Liao 已提交
279 280
  uint16_t           type;       // sql node type
  uint32_t           tokenId;    // TK_LE: less than(binary expr)
H
Haojun Liao 已提交
281

H
Haojun Liao 已提交
282 283 284 285 286
  // 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 已提交
287

288
  int32_t            functionId;  // function id, todo remove it
H
Haojun Liao 已提交
289
  SStrToken          columnName;  // table column info
D
cast  
dapan1121 已提交
290
  TAOS_FIELD         dataType;  // data type
H
Haojun Liao 已提交
291
  tVariant           value;       // the use input value
H
Haojun Liao 已提交
292 293
  SStrToken          exprToken;   // original sql expr string
  uint32_t           flags;       // todo remove it
D
fix bug  
dapan1121 已提交
294
  
H
Haojun Liao 已提交
295 296
  struct tSqlExpr   *pLeft;       // left child
  struct tSqlExpr   *pRight;      // right child
H
Haojun Liao 已提交
297
} tSqlExpr;
298

H
Haojun Liao 已提交
299
// used in select clause. select <SArray> from xxx
H
Haojun Liao 已提交
300
typedef struct tSqlExprItem {
H
Haojun Liao 已提交
301 302 303
  tSqlExpr          *pNode;      // The list of expressions
  char              *aliasName;  // alias name, null-terminated string
  bool               distinct;
H
Haojun Liao 已提交
304
} tSqlExprItem;
305

wmmhello's avatar
wmmhello 已提交
306
SArray *commonItemAppend(SArray *pList, tVariant *pVar, tSqlExpr *jsonExp, bool isJsonExp, uint8_t sortOrder);
D
dapan1121 已提交
307

H
Haojun Liao 已提交
308 309
SArray *tVariantListAppend(SArray *pList, tVariant *pVar, uint8_t sortOrder);
SArray *tVariantListInsert(SArray *pList, tVariant *pVar, uint8_t sortOrder, int32_t index);
310
SArray *tVariantListAppendToken(SArray *pList, SStrToken *pAliasToken, uint8_t sortOrder, bool needRmquoteEscape);
311

312
SRelationInfo *setTableNameList(SRelationInfo* pFromInfo, SStrToken *pName, SStrToken* pAlias);
H
Haojun Liao 已提交
313
void          *destroyRelationInfo(SRelationInfo* pFromInfo);
H
Haojun Liao 已提交
314
SRelationInfo *addSubqueryElem(SRelationInfo* pRelationInfo, SArray* pSub, SStrToken* pAlias);
D
dapan1121 已提交
315

H
Haojun Liao 已提交
316
// sql expr leaf node
D
dapan1121 已提交
317
tSqlExpr *tSqlExprCreateTimestamp(SStrToken *pToken, int32_t optrType);
318
tSqlExpr *tSqlExprCreateIdValue(SSqlInfo* pInfo, SStrToken *pToken, int32_t optrType);
H
Haojun Liao 已提交
319
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
D
cast  
dapan1121 已提交
320
tSqlExpr *tSqlExprCreateFuncWithParams(SSqlInfo *pInfo, tSqlExpr* col, TAOS_FIELD *colType, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
D
dapan1121 已提交
321
SArray *tStrTokenAppend(SArray *pList, SStrToken *pToken);
322

H
Haojun Liao 已提交
323 324
tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType);
tSqlExpr *tSqlExprClone(tSqlExpr *pSrc);
H
Haojun Liao 已提交
325 326 327
void      tSqlExprCompact(tSqlExpr** pExpr);
bool      tSqlExprIsLeaf(tSqlExpr* pExpr);
bool      tSqlExprIsParentOfLeaf(tSqlExpr* pExpr);
H
Haojun Liao 已提交
328 329 330
void      tSqlExprDestroy(tSqlExpr *pExpr);
SArray   *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken);
void      tSqlExprListDestroy(SArray *pList);
331

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

337
SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type);
338

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

342 343
void destroyAllSqlNode(SArray *pSqlNode);
void destroySqlNode(SSqlNode *pSql);
344
void freeCreateTableInfo(void* p);
345

346 347 348
SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type);
SArray   *setSubclause(SArray *pList, void *pSqlNode);
SArray   *appendSelectClause(SArray *pList, void *pSubclause);
349

350
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists);
351
void setCreatedStreamOpt(SSqlInfo *pInfo, SStrToken *pTo, SStrToken *pSplit);
352

H
Haojun Liao 已提交
353
void SqlInfoDestroy(SSqlInfo *pInfo);
354

H
Haojun Liao 已提交
355
void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
D
dapan1121 已提交
356
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType);
H
Haojun Liao 已提交
357
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
358

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

H
Haojun Liao 已提交
361
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo);
362
void setCreateUserSql(SSqlInfo *pInfo, SStrToken *pName, SStrToken *pPasswd, SStrToken *pTags);
H
Haojun Liao 已提交
363
void setKillSql(SSqlInfo *pInfo, int32_t type, SStrToken *ip);
364
void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SStrToken *pName, SStrToken* pPwd, SStrToken *pPrivilege, SStrToken *pTags);
365

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

H
Haojun Liao 已提交
368
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
D
dapan1121 已提交
369
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);
370 371

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

H
Haojun Liao 已提交
374 375
void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
void tSetColumnType(TAOS_FIELD *pField, SStrToken *type);
376

377 378 379
// malloc new SDelData and set with args
SDelData *tGetDelData(SStrToken *pTableName, SStrToken* existsCheck, tSqlExpr* pWhere);

H
Haojun Liao 已提交
380 381 382 383 384 385 386
/**
 *
 * @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 *);
387

H
Haojun Liao 已提交
388 389 390 391 392 393
/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));
394

H
Haojun Liao 已提交
395 396 397 398 399
/**
 *
 * @param mallocProc  The parser allocator
 * @return
 */
400 401
void *ParseAlloc(void *(*mallocProc)(size_t));

H
Haojun Liao 已提交
402 403 404 405 406 407
/**
 *
 * @param str sql string
 * @return sql ast
 */
SSqlInfo qSqlParse(const char *str);
408

409 410 411 412
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
413
#endif  // TDENGINE_QSQLPARSER_H