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
#define SQLEXPR_TYPE_UNASSIGNED 0
#define SQLEXPR_TYPE_SCALAR 1
#define SQLEXPR_TYPE_AGG 2
#define SQLEXPR_TYPE_VALUE 3
H
Haojun Liao 已提交
35 36 37 38 39 40

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

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

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

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

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

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

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

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

90
typedef struct SSessionWindowVal {
H
Haojun Liao 已提交
91 92
  SStrToken          col;
  SStrToken          gap;
93 94
} SSessionWindowVal;

Y
TD-2570  
yihaoDeng 已提交
95 96 97 98
typedef struct SWindowStateVal {
  SStrToken          col;
} SWindowStateVal;

99
struct SRelationInfo;
H
Haojun Liao 已提交
100

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

H
Haojun Liao 已提交
118 119 120 121 122 123
typedef struct SRelElementPair {
  union {
    SStrToken  tableName;
    SArray    *pSubquery;
  };

H
Haojun Liao 已提交
124
  SStrToken aliasName;
H
Haojun Liao 已提交
125
} SRelElementPair;
H
Haojun Liao 已提交
126

127 128
typedef struct SRelationInfo {
  int32_t       type;        // nested query|table name list
H
Haojun Liao 已提交
129
  SArray       *list;        // SArray<SRelElementPair>
130
} SRelationInfo;
131

132
typedef struct SCreatedTableInfo {
H
Haojun Liao 已提交
133 134 135 136 137 138 139
  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
140 141
} SCreatedTableInfo;

H
Haojun Liao 已提交
142
typedef struct SCreateTableSql {
H
Haojun Liao 已提交
143 144 145
  SStrToken          name;  // table name, create table [name] xxx
  int8_t             type;  // create normal table/from super table/ stream
  bool               existCheck;
146

147
  struct {
H
Haojun Liao 已提交
148 149
    SArray          *pTagColumns; // SArray<TAOS_FIELD>
    SArray          *pColumns;    // SArray<TAOS_FIELD>
150
  } colInfo;
151

H
Haojun Liao 已提交
152
  SArray            *childTableInfo;        // SArray<SCreatedTableInfo>
H
Haojun Liao 已提交
153
  SSqlNode          *pSelect;
H
Haojun Liao 已提交
154
} SCreateTableSql;
155

H
Haojun Liao 已提交
156
typedef struct SAlterTableInfo {
H
Haojun Liao 已提交
157 158 159 160 161 162
  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 已提交
163
} SAlterTableInfo;
164

H
Haojun Liao 已提交
165
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  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 已提交
186
} SCreateDbInfo;
187

D
dapan1121 已提交
188 189 190
typedef struct SCreateFuncInfo {
  SStrToken name;
  SStrToken path;
191
  int32_t   type;
D
dapan1121 已提交
192
  int32_t   bufSize;
D
dapan1121 已提交
193
  TAOS_FIELD output;
D
dapan1121 已提交
194 195 196
} SCreateFuncInfo;


H
Haojun Liao 已提交
197
typedef struct SCreateAcctInfo {
H
Haojun Liao 已提交
198 199 200 201 202 203 204 205 206
  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 已提交
207
} SCreateAcctInfo;
208 209

typedef struct SShowInfo {
H
Haojun Liao 已提交
210 211 212
  uint8_t            showType;
  SStrToken          prefix;
  SStrToken          pattern;
213 214 215
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
216 217 218 219
  SStrToken          user;
  SStrToken          passwd;
  SStrToken          privilege;
  int16_t            type;
220 221
} SUserInfo;

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

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

H
Haojun Liao 已提交
250
typedef struct tSqlExpr {
H
Haojun Liao 已提交
251 252
  uint16_t           type;       // sql node type
  uint32_t           tokenId;    // TK_LE: less than(binary expr)
H
Haojun Liao 已提交
253

H
Haojun Liao 已提交
254 255 256 257 258
  // 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 已提交
259

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

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

H
Haojun Liao 已提交
278 279 280
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);
281

282
SRelationInfo *setTableNameList(SRelationInfo* pFromInfo, SStrToken *pName, SStrToken* pAlias);
H
Haojun Liao 已提交
283
void          *destroyRelationInfo(SRelationInfo* pFromInfo);
H
Haojun Liao 已提交
284
SRelationInfo *addSubqueryElem(SRelationInfo* pRelationInfo, SArray* pSub, SStrToken* pAlias);
D
dapan1121 已提交
285

H
Haojun Liao 已提交
286
// sql expr leaf node
287
tSqlExpr *tSqlExprCreateIdValue(SSqlInfo* pInfo, SStrToken *pToken, int32_t optrType);
H
Haojun Liao 已提交
288
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
D
cast  
dapan1121 已提交
289
tSqlExpr *tSqlExprCreateFuncWithParams(SSqlInfo *pInfo, tSqlExpr* col, TAOS_FIELD *colType, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
D
dapan1121 已提交
290
SArray *tStrTokenAppend(SArray *pList, SStrToken *pToken);
291

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

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

306
SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type);
307

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

311 312
void destroyAllSqlNode(SArray *pSqlNode);
void destroySqlNode(SSqlNode *pSql);
313
void freeCreateTableInfo(void* p);
314

315 316 317
SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type);
SArray   *setSubclause(SArray *pList, void *pSqlNode);
SArray   *appendSelectClause(SArray *pList, void *pSubclause);
318

319
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists);
320

H
Haojun Liao 已提交
321
void SqlInfoDestroy(SSqlInfo *pInfo);
322

H
Haojun Liao 已提交
323
void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
D
dapan1121 已提交
324
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType);
H
Haojun Liao 已提交
325
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
326

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

H
Haojun Liao 已提交
329
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo);
H
Haojun Liao 已提交
330 331 332
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);
333

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

H
Haojun Liao 已提交
336
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
D
dapan1121 已提交
337
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);
338 339

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

H
Haojun Liao 已提交
342 343
void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
void tSetColumnType(TAOS_FIELD *pField, SStrToken *type);
344

H
Haojun Liao 已提交
345 346 347 348 349 350 351
/**
 *
 * @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 *);
352

H
Haojun Liao 已提交
353 354 355 356 357 358
/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));
359

H
Haojun Liao 已提交
360 361 362 363 364
/**
 *
 * @param mallocProc  The parser allocator
 * @return
 */
365 366
void *ParseAlloc(void *(*mallocProc)(size_t));

H
Haojun Liao 已提交
367 368 369 370 371 372
/**
 *
 * @param str sql string
 * @return sql ast
 */
SSqlInfo qSqlParse(const char *str);
373

374 375 376 377
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
378
#endif  // TDENGINE_QSQLPARSER_H