qSqlparser.h 10.2 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 53 54 55 56 57 58 59
extern char tTokenTypeSwitcher[13];

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

typedef struct SLimitVal {
  int64_t limit;
  int64_t offset;
} SLimitVal;

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

typedef struct tVariantListItem {
  tVariant pVar;
  uint8_t  sortOrder;
} tVariantListItem;

68 69 70 71 72
typedef struct SIntervalVal {
  SStrToken interval;
  SStrToken offset;
} SIntervalVal;

73 74 75 76 77
typedef struct SSessionWindowVal {
  SStrToken col;
  SStrToken gap;
} SSessionWindowVal;

H
Haojun Liao 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
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>
  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]
  SArray            *fillType;     // fill type[optional], SArray<tVariantListItem>
  SStrToken          sqlstr;       // sql string in select clause
} SQuerySqlNode;
92

93 94 95
typedef struct SCreatedTableInfo {
  SStrToken  name;        // table name token
  SStrToken  stableName;  // super table name token , for using clause
96
  SArray    *pTagNames;   // create by using super table, tag name
97 98 99 100 101 102
  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
} SCreatedTableInfo;

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

108
  struct {
109 110
    SArray    *pTagColumns; // SArray<TAOS_FIELD>
    SArray    *pColumns;    // SArray<TAOS_FIELD>
111
  } colInfo;
112 113

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

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

H
Haojun Liao 已提交
126
typedef struct SCreateDbInfo {
H
Haojun Liao 已提交
127
  SStrToken dbname;
128 129
  int32_t   replica;
  int32_t   cacheBlockSize;
H
hjxilinx 已提交
130 131
  int32_t   maxTablesPerVnode;
  int32_t   numOfBlocks;
132
  int32_t   daysPerFile;
H
hjxilinx 已提交
133 134
  int32_t   minRowsPerBlock;
  int32_t   maxRowsPerBlock;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
135
  int32_t   fsyncPeriod;
136
  int64_t   commitTime;
H
hjxilinx 已提交
137
  int32_t   walLevel;
138
  int32_t   quorum;
139
  int32_t   compressionLevel;
H
Haojun Liao 已提交
140
  SStrToken precision;
141
  bool      ignoreExists;
D
dapan1121 已提交
142 143
  int8_t    update;
  int8_t    cachelast; 
H
Haojun Liao 已提交
144
  SArray    *keep;
D
dapan1121 已提交
145 146
  int8_t    dbType;
  int16_t   partitions;
H
Haojun Liao 已提交
147
} SCreateDbInfo;
148

H
Haojun Liao 已提交
149
typedef struct SCreateAcctInfo {
150 151 152 153 154 155 156 157
  int32_t   maxUsers;
  int32_t   maxDbs;
  int32_t   maxTimeSeries;
  int32_t   maxStreams;
  int32_t   maxPointsPerSecond;
  int64_t   maxStorage;
  int64_t   maxQueryTime;
  int32_t   maxConnections;
H
Haojun Liao 已提交
158
  SStrToken stat;
H
Haojun Liao 已提交
159
} SCreateAcctInfo;
160 161

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

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

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

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

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

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

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

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

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

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

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

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

H
Haojun Liao 已提交
237 238 239 240 241
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 已提交
242

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

315 316 317 318
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
319
#endif  // TDENGINE_QSQLPARSER_H