qSqlparser.h 10.9 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,
};

42 43 44 45 46 47 48 49 50 51 52
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 已提交
53 54
#define TPARSER_HAS_TOKEN(_t) ((_t).n > 0)

55
typedef struct SLimitVal {
H
Haojun Liao 已提交
56 57
  int64_t            limit;
  int64_t            offset;
58 59 60
} SLimitVal;

typedef struct SOrderVal {
H
Haojun Liao 已提交
61 62
  uint32_t           order;
  int32_t            orderColId;
63 64 65
} SOrderVal;

typedef struct tVariantListItem {
H
Haojun Liao 已提交
66 67
  tVariant           pVar;
  uint8_t            sortOrder;
68 69
} tVariantListItem;

70
typedef struct SIntervalVal {
H
Haojun Liao 已提交
71 72
  SStrToken          interval;
  SStrToken          offset;
73 74
} SIntervalVal;

75
typedef struct SSessionWindowVal {
H
Haojun Liao 已提交
76 77
  SStrToken          col;
  SStrToken          gap;
78 79
} SSessionWindowVal;

H
Haojun Liao 已提交
80 81 82 83 84 85 86
typedef struct SQuerySqlNode {
  struct SArray     *pSelectList;  // select clause
  SArray            *from;         // from clause  SArray<SQuerySqlNode>
  struct tSqlExpr   *pWhere;       // where clause [optional]
  SArray            *pGroupby;     // groupby clause, only for tags[optional], SArray<tVariantListItem>
  SArray            *pSortOrder;   // orderby [optional], SArray<tVariantListItem>
  SArray            *fillType;     // fill type[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
87 88 89 90 91
  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 已提交
92 93
  SStrToken          sqlstr;       // sql string in select clause
} SQuerySqlNode;
94

95
typedef struct SCreatedTableInfo {
H
Haojun Liao 已提交
96 97 98 99 100 101 102
  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
103 104
} SCreatedTableInfo;

H
Haojun Liao 已提交
105
typedef struct SCreateTableSql {
H
Haojun Liao 已提交
106 107 108
  SStrToken          name;  // table name, create table [name] xxx
  int8_t             type;  // create normal table/from super table/ stream
  bool               existCheck;
109

110
  struct {
H
Haojun Liao 已提交
111 112
    SArray          *pTagColumns; // SArray<TAOS_FIELD>
    SArray          *pColumns;    // SArray<TAOS_FIELD>
113
  } colInfo;
114

H
Haojun Liao 已提交
115 116
  SArray            *childTableInfo;        // SArray<SCreatedTableInfo>
  SQuerySqlNode     *pSelect;
H
Haojun Liao 已提交
117
} SCreateTableSql;
118

H
Haojun Liao 已提交
119
typedef struct SAlterTableInfo {
H
Haojun Liao 已提交
120 121 122 123 124 125
  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 已提交
126
} SAlterTableInfo;
127

H
Haojun Liao 已提交
128
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  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 已提交
149
} SCreateDbInfo;
150

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

typedef struct SShowInfo {
H
Haojun Liao 已提交
164 165 166
  uint8_t            showType;
  SStrToken          prefix;
  SStrToken          pattern;
167 168 169
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
170 171 172 173
  SStrToken          user;
  SStrToken          passwd;
  SStrToken          privilege;
  int16_t            type;
174 175
} SUserInfo;

H
Haojun Liao 已提交
176
typedef struct SMiscInfo {
H
Haojun Liao 已提交
177 178 179 180 181
  SArray            *a;         // SArray<SStrToken>
  bool               existsCheck;
  int16_t            dbType;
  int16_t            tableType;
  SUserInfo          user;
182
  union {
H
Haojun Liao 已提交
183 184 185 186
    SCreateDbInfo    dbOpt;
    SCreateAcctInfo  acctOpt;
    SShowInfo        showOpt;
    SStrToken        id;
187
  };
H
Haojun Liao 已提交
188
} SMiscInfo;
189 190

typedef struct SSubclauseInfo {  // "UNION" multiple select sub-clause
H
Haojun Liao 已提交
191 192
  SQuerySqlNode    **pClause;
  int32_t            numOfClause;
193 194 195
} SSubclauseInfo;

typedef struct SSqlInfo {
H
Haojun Liao 已提交
196 197
  int32_t            type;
  bool               valid;
H
Haojun Liao 已提交
198 199
  SSubclauseInfo     subclauseInfo;
  char               msg[256];
200
  union {
H
Haojun Liao 已提交
201 202 203
    SCreateTableSql *pCreateTableInfo;
    SAlterTableInfo *pAlterInfo;
    SMiscInfo       *pMiscInfo;
204 205 206
  };
} SSqlInfo;

H
Haojun Liao 已提交
207
typedef struct tSqlExpr {
H
Haojun Liao 已提交
208 209
  uint16_t           type;       // sql node type
  uint32_t           tokenId;    // TK_LE: less than(binary expr)
H
Haojun Liao 已提交
210 211

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

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

H
Haojun Liao 已提交
219 220 221
  struct tSqlExpr   *pLeft;       // left child
  struct tSqlExpr   *pRight;      // right child
  struct SArray     *pParam;      // function parameters list
H
Haojun Liao 已提交
222
} tSqlExpr;
223

H
Haojun Liao 已提交
224
// used in select clause. select <SArray> from xxx
H
Haojun Liao 已提交
225
typedef struct tSqlExprItem {
H
Haojun Liao 已提交
226 227 228
  tSqlExpr          *pNode;      // The list of expressions
  char              *aliasName;  // alias name, null-terminated string
  bool               distinct;
H
Haojun Liao 已提交
229
} tSqlExprItem;
230

H
Haojun Liao 已提交
231 232 233
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);
234

H
Haojun Liao 已提交
235 236 237
// sql expr leaf node
tSqlExpr *tSqlExprCreateIdValue(SStrToken *pToken, int32_t optrType);
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
238

H
Haojun Liao 已提交
239 240 241 242 243
tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType);
tSqlExpr *tSqlExprClone(tSqlExpr *pSrc);
void      tSqlExprDestroy(tSqlExpr *pExpr);
SArray   *tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SStrToken *pDistinct, SStrToken *pToken);
void      tSqlExprListDestroy(SArray *pList);
D
dapan1121 已提交
244

H
Haojun Liao 已提交
245
SQuerySqlNode *tSetQuerySqlNode(SStrToken *pSelectToken, SArray *pSelectList, SArray *pFrom, tSqlExpr *pWhere,
H
Haojun Liao 已提交
246 247
                                SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps,
                                SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pgLimit);
248

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

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

void destroyAllSelectClause(SSubclauseInfo *pSql);
H
Haojun Liao 已提交
255
void destroyQuerySqlNode(SQuerySqlNode *pSql);
256
void freeCreateTableInfo(void* p);
257

H
Haojun Liao 已提交
258
SSqlInfo       *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pTableName, int32_t type);
259 260 261 262
SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo);

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

263
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pTableNameToken, SStrToken *pIfNotExists);
264

H
Haojun Liao 已提交
265
void SqlInfoDestroy(SSqlInfo *pInfo);
266

H
Haojun Liao 已提交
267
void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
D
dapan1121 已提交
268
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck,int16_t dbType,int16_t tableType);
H
Haojun Liao 已提交
269
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
270

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

H
Haojun Liao 已提交
273
void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SStrToken *pName, SStrToken *pPwd, SCreateAcctInfo *pAcctInfo);
H
Haojun Liao 已提交
274 275 276
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);
277

H
Haojun Liao 已提交
278
void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
D
dapan1121 已提交
279
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);
280 281

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

H
Haojun Liao 已提交
284 285
void tSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
void tSetColumnType(TAOS_FIELD *pField, SStrToken *type);
286

H
Haojun Liao 已提交
287 288 289 290 291 292 293
/**
 *
 * @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 *);
294

H
Haojun Liao 已提交
295 296 297 298 299 300
/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));
301

H
Haojun Liao 已提交
302 303 304 305 306
/**
 *
 * @param mallocProc  The parser allocator
 * @return
 */
307 308
void *ParseAlloc(void *(*mallocProc)(size_t));

H
Haojun Liao 已提交
309 310 311 312 313 314
/**
 *
 * @param str sql string
 * @return sql ast
 */
SSqlInfo qSqlParse(const char *str);
315

316 317 318 319
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
320
#endif  // TDENGINE_QSQLPARSER_H