qSqlparser.h 11.7 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
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 43 44 45 46
enum SQL_NODE_FROM_TYPE {
  SQL_NODE_FROM_SUBQUERY  = 1,
  SQL_NODE_FROM_NAMELIST  = 2,
};

47 48 49 50 51 52 53 54 55 56 57
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 已提交
58 59
#define TPARSER_HAS_TOKEN(_t)      ((_t).n > 0)
#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0)
H
Haojun Liao 已提交
60

61
typedef struct SLimitVal {
H
Haojun Liao 已提交
62 63
  int64_t            limit;
  int64_t            offset;
64 65 66
} SLimitVal;

typedef struct SOrderVal {
H
Haojun Liao 已提交
67 68
  uint32_t           order;
  int32_t            orderColId;
69 70 71
} SOrderVal;

typedef struct tVariantListItem {
H
Haojun Liao 已提交
72 73
  tVariant           pVar;
  uint8_t            sortOrder;
74 75
} tVariantListItem;

76
typedef struct SIntervalVal {
H
Haojun Liao 已提交
77 78
  SStrToken          interval;
  SStrToken          offset;
79 80
} SIntervalVal;

81
typedef struct SSessionWindowVal {
H
Haojun Liao 已提交
82 83
  SStrToken          col;
  SStrToken          gap;
84 85
} SSessionWindowVal;

H
Haojun Liao 已提交
86 87
struct SFromInfo;

H
Haojun Liao 已提交
88 89
typedef struct SQuerySqlNode {
  struct SArray     *pSelectList;  // select clause
H
Haojun Liao 已提交
90
  struct SFromInfo  *from;         // from clause SArray<SQuerySqlNode>
H
Haojun Liao 已提交
91 92
  struct tSqlExpr   *pWhere;       // where clause [optional]
  SArray            *pGroupby;     // groupby clause, only for tags[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
93
  SArray            *pSortOrder;   // orderby [optional],  SArray<tVariantListItem>
H
Haojun Liao 已提交
94
  SArray            *fillType;     // fill type[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
95 96 97 98 99
  SIntervalVal       interval;     // (interval, interval_offset) [optional]
  SSessionWindowVal  sessionVal;   // session window [optional]
  SStrToken          sliding;      // sliding window [optional]
  SLimitVal          limit;        // limit offset [optional]
  SLimitVal          slimit;       // group limit offset [optional]
H
Haojun Liao 已提交
100 101
  SStrToken          sqlstr;       // sql string in select clause
} SQuerySqlNode;
102

H
Haojun Liao 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
typedef struct STableNamePair {
  SStrToken name;
  SStrToken aliasName;
} STableNamePair;

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

typedef struct SFromInfo {
  int32_t            type;        // nested query|table name list
  union {
    SSubclauseInfo  *pNode;
    SArray          *tableList;   // SArray<STableNamePair>
  };
} SFromInfo;

121
typedef struct SCreatedTableInfo {
H
Haojun Liao 已提交
122 123 124 125 126 127 128
  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
129 130
} SCreatedTableInfo;

H
Haojun Liao 已提交
131
typedef struct SCreateTableSql {
H
Haojun Liao 已提交
132 133 134
  SStrToken          name;  // table name, create table [name] xxx
  int8_t             type;  // create normal table/from super table/ stream
  bool               existCheck;
135

136
  struct {
H
Haojun Liao 已提交
137 138
    SArray          *pTagColumns; // SArray<TAOS_FIELD>
    SArray          *pColumns;    // SArray<TAOS_FIELD>
139
  } colInfo;
140

H
Haojun Liao 已提交
141 142
  SArray            *childTableInfo;        // SArray<SCreatedTableInfo>
  SQuerySqlNode     *pSelect;
H
Haojun Liao 已提交
143
} SCreateTableSql;
144

H
Haojun Liao 已提交
145
typedef struct SAlterTableInfo {
H
Haojun Liao 已提交
146 147 148 149 150 151
  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 已提交
152
} SAlterTableInfo;
153

H
Haojun Liao 已提交
154
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  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 已提交
175
} SCreateDbInfo;
176

H
Haojun Liao 已提交
177
typedef struct SCreateAcctInfo {
H
Haojun Liao 已提交
178 179 180 181 182 183 184 185 186
  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 已提交
187
} SCreateAcctInfo;
188 189

typedef struct SShowInfo {
H
Haojun Liao 已提交
190 191 192
  uint8_t            showType;
  SStrToken          prefix;
  SStrToken          pattern;
193 194 195
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
196 197 198 199
  SStrToken          user;
  SStrToken          passwd;
  SStrToken          privilege;
  int16_t            type;
200 201
} SUserInfo;

H
Haojun Liao 已提交
202
typedef struct SMiscInfo {
H
Haojun Liao 已提交
203 204 205 206 207
  SArray            *a;         // SArray<SStrToken>
  bool               existsCheck;
  int16_t            dbType;
  int16_t            tableType;
  SUserInfo          user;
208
  union {
H
Haojun Liao 已提交
209 210 211 212
    SCreateDbInfo    dbOpt;
    SCreateAcctInfo  acctOpt;
    SShowInfo        showOpt;
    SStrToken        id;
213
  };
H
Haojun Liao 已提交
214
} SMiscInfo;
215 216

typedef struct SSqlInfo {
H
Haojun Liao 已提交
217 218
  int32_t            type;
  bool               valid;
H
Haojun Liao 已提交
219 220
  SSubclauseInfo     subclauseInfo;
  char               msg[256];
221
  union {
H
Haojun Liao 已提交
222 223 224
    SCreateTableSql *pCreateTableInfo;
    SAlterTableInfo *pAlterInfo;
    SMiscInfo       *pMiscInfo;
225 226 227
  };
} SSqlInfo;

H
Haojun Liao 已提交
228
typedef struct tSqlExpr {
H
Haojun Liao 已提交
229 230
  uint16_t           type;       // sql node type
  uint32_t           tokenId;    // TK_LE: less than(binary expr)
H
Haojun Liao 已提交
231 232

  // the whole string of the function(col, param), while the function name is kept in token
H
Haojun Liao 已提交
233 234
  SStrToken          operand;
  uint32_t           functionId;  // function id
H
Haojun Liao 已提交
235

H
Haojun Liao 已提交
236 237 238
  SStrToken          colInfo;     // table column info
  tVariant           value;       // the use input value
  SStrToken          token;       // original sql expr string
H
Haojun Liao 已提交
239

H
Haojun Liao 已提交
240 241 242
  struct tSqlExpr   *pLeft;       // left child
  struct tSqlExpr   *pRight;      // right child
  struct SArray     *pParam;      // function parameters list
H
Haojun Liao 已提交
243
} tSqlExpr;
244

H
Haojun Liao 已提交
245
// used in select clause. select <SArray> from xxx
H
Haojun Liao 已提交
246
typedef struct tSqlExprItem {
H
Haojun Liao 已提交
247 248 249
  tSqlExpr          *pNode;      // The list of expressions
  char              *aliasName;  // alias name, null-terminated string
  bool               distinct;
H
Haojun Liao 已提交
250
} tSqlExprItem;
251

H
Haojun Liao 已提交
252 253 254
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);
255

H
Haojun Liao 已提交
256 257 258 259
SFromInfo *setTableNameList(SFromInfo* pFromInfo, SStrToken *pName, SStrToken* pAlias);
SFromInfo *setSubquery(SFromInfo* pFromInfo, SQuerySqlNode *pSqlNode);
void      *destroyFromInfo(SFromInfo* pFromInfo);

H
Haojun Liao 已提交
260 261 262
// sql expr leaf node
tSqlExpr *tSqlExprCreateIdValue(SStrToken *pToken, int32_t optrType);
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
263

H
Haojun Liao 已提交
264 265
tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType);
tSqlExpr *tSqlExprClone(tSqlExpr *pSrc);
H
Haojun Liao 已提交
266 267 268
void      tSqlExprCompact(tSqlExpr** pExpr);
bool      tSqlExprIsLeaf(tSqlExpr* pExpr);
bool      tSqlExprIsParentOfLeaf(tSqlExpr* pExpr);
H
Haojun Liao 已提交
269 270 271
void      tSqlExprDestroy(tSqlExpr *pExpr);
SArray   *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken);
void      tSqlExprListDestroy(SArray *pList);
D
dapan1121 已提交
272

H
Haojun Liao 已提交
273
SQuerySqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelectList, SFromInfo *pFrom, tSqlExpr *pWhere,
H
Haojun Liao 已提交
274 275
                                SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps,
                                SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pgLimit);
276

H
Haojun Liao 已提交
277
SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SQuerySqlNode *pSelect, int32_t type);
278

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

void destroyAllSelectClause(SSubclauseInfo *pSql);
H
Haojun Liao 已提交
283
void destroyQuerySqlNode(SQuerySqlNode *pSql);
284
void freeCreateTableInfo(void* p);
285

H
Haojun Liao 已提交
286
SSqlInfo       *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type);
287 288 289 290
SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo);

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

291
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists);
292

H
Haojun Liao 已提交
293
void SqlInfoDestroy(SSqlInfo *pInfo);
294

H
Haojun Liao 已提交
295
void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
D
dapan1121 已提交
296
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType);
H
Haojun Liao 已提交
297
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
298

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

H
Haojun Liao 已提交
301
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo);
H
Haojun Liao 已提交
302 303 304
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);
305

H
Haojun Liao 已提交
306
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
D
dapan1121 已提交
307
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);
308 309

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

H
Haojun Liao 已提交
312 313
void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
void tSetColumnType(TAOS_FIELD *pField, SStrToken *type);
314

H
Haojun Liao 已提交
315 316 317 318 319 320 321
/**
 *
 * @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 *);
322

H
Haojun Liao 已提交
323 324 325 326 327 328
/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));
329

H
Haojun Liao 已提交
330 331 332 333 334
/**
 *
 * @param mallocProc  The parser allocator
 * @return
 */
335 336
void *ParseAlloc(void *(*mallocProc)(size_t));

H
Haojun Liao 已提交
337 338 339 340 341 342
/**
 *
 * @param str sql string
 * @return sql ast
 */
SSqlInfo qSqlParse(const char *str);
343

344 345 346 347
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
348
#endif  // TDENGINE_QSQLPARSER_H