qSqlparser.h 10.5 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

#ifdef __cplusplus
extern "C" {
#endif

H
[td-32]  
hjxilinx 已提交
23
#include <tstrbuild.h>
24 25 26 27 28
#include "taos.h"
#include "taosmsg.h"
#include "tstoken.h"
#include "tvariant.h"

H
Haojun Liao 已提交
29
#define ParseTOKENTYPE SStrToken
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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 tFieldList {
  int32_t     nField;
  int32_t     nAlloc;
  TAOS_FIELD *p;
} tFieldList;

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

typedef struct SOrderVal {
  uint32_t order;
  int32_t orderColId;
} SOrderVal;

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

typedef struct tVariantList {
  int32_t           nExpr;  /* Number of expressions on the list */
  int32_t           nAlloc; /* Number of entries allocated below */
  tVariantListItem *a;      /* One entry for each expression */
} tVariantList;

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

73 74 75 76 77 78
typedef struct SQuerySQL {
  struct tSQLExprList *pSelection;   // select clause
  tVariantList *       from;         // from clause
  struct tSQLExpr *    pWhere;       // where clause [optional]
  tVariantList *       pGroupby;     // groupby clause, only for tags[optional]
  tVariantList *       pSortOrder;   // orderby [optional]
H
Haojun Liao 已提交
79
  SStrToken            interval;     // interval [optional]
80
  SStrToken            offset;       // offset window [optional]
H
Haojun Liao 已提交
81
  SStrToken            sliding;      // sliding window [optional]
82 83 84
  SLimitVal            limit;        // limit offset [optional]
  SLimitVal            slimit;       // group limit offset [optional]
  tVariantList *       fillType;     // fill type[optional]
H
Haojun Liao 已提交
85
  SStrToken            selectToken;  // sql string
86 87 88
} SQuerySQL;

typedef struct SCreateTableSQL {
H
Haojun Liao 已提交
89
  struct SStrToken name;  // meter name, create table [meterName] xxx
90 91 92 93 94 95 96 97 98
  bool             existCheck;
  
  int8_t           type; // create normal table/from super table/ stream
  struct {
    tFieldList *pTagColumns;  // for normal table, pTagColumns = NULL;
    tFieldList *pColumns;
  } colInfo;
  
  struct {
H
Haojun Liao 已提交
99
    SStrToken     stableName;  // super table name, for using clause
100 101 102 103 104 105 106 107
    tVariantList *pTagVals;    // create by using metric, tag value
    STagData      tagdata;
  } usingInfo;
  
  SQuerySQL *pSelect;
} SCreateTableSQL;

typedef struct SAlterTableSQL {
H
Haojun Liao 已提交
108
  SStrToken     name;
109 110 111 112 113 114 115 116
  int16_t       type;
  STagData      tagData;
  
  tFieldList *  pAddColumns;
  tVariantList *varList;  // set t=val or: change src dst
} SAlterTableSQL;

typedef struct SCreateDBInfo {
H
Haojun Liao 已提交
117
  SStrToken dbname;
118 119
  int32_t   replica;
  int32_t   cacheBlockSize;
H
hjxilinx 已提交
120 121
  int32_t   maxTablesPerVnode;
  int32_t   numOfBlocks;
122
  int32_t   daysPerFile;
H
hjxilinx 已提交
123 124
  int32_t   minRowsPerBlock;
  int32_t   maxRowsPerBlock;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
125
  int32_t   fsyncPeriod;
126
  int64_t   commitTime;
H
hjxilinx 已提交
127
  int32_t   walLevel;
128
  int32_t   quorum;
129
  int32_t   compressionLevel;
H
Haojun Liao 已提交
130
  SStrToken precision;
131
  bool      ignoreExists;
132 133 134 135 136 137 138 139 140 141 142 143 144
  
  tVariantList *keep;
} SCreateDBInfo;

typedef struct SCreateAcctSQL {
  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 已提交
145
  SStrToken stat;
146 147 148 149
} SCreateAcctSQL;

typedef struct SShowInfo {
  uint8_t showType;
H
Haojun Liao 已提交
150 151
  SStrToken prefix;
  SStrToken pattern;
152 153 154
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
155 156 157
  SStrToken user;
  SStrToken passwd;
  SStrToken privilege;
158 159 160 161 162 163
  int16_t   type;
} SUserInfo;

typedef struct tDCLSQL {
  int32_t    nTokens; /* Number of expressions on the list */
  int32_t    nAlloc;  /* Number of entries allocated below */
H
Haojun Liao 已提交
164
  SStrToken *a;       /* one entry for element */
165 166 167 168 169 170
  bool  existsCheck;
  
  union {
    SCreateDBInfo  dbOpt;
    SCreateAcctSQL acctOpt;
    SShowInfo      showOpt;
H
Haojun Liao 已提交
171
    SStrToken ip;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  };
  
  SUserInfo user;
  
} tDCLSQL;

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

typedef struct SSqlInfo {
  int32_t type;
  bool    valid;
  
  union {
    SCreateTableSQL *pCreateTableInfo;
    SAlterTableSQL * pAlterInfo;
    tDCLSQL *        pDCLInfo;
  };
  
  SSubclauseInfo subclauseInfo;
  char           pzErrMsg[256];
} SSqlInfo;

typedef struct tSQLExpr {
H
[td-32]  
hjxilinx 已提交
198 199
  // TK_FUNCTION: sql function, TK_LE: less than(binary expr)
  uint32_t nSQLOptr;
200 201 202
  
  // the full sql string of function(col, param), which is actually the raw
  // field name, since the function name is kept in nSQLOptr already
H
Haojun Liao 已提交
203 204
  SStrToken operand;
  SStrToken colInfo;            // field id
H
[td-32]  
hjxilinx 已提交
205
  tVariant  val;                // value only for string, float, int
206
  
H
[td-32]  
hjxilinx 已提交
207 208 209
  struct tSQLExpr *pLeft;       // left child
  struct tSQLExpr *pRight;      // right child
  struct tSQLExprList *pParam;  // function parameters
H
Haojun Liao 已提交
210
  SStrToken token;              // original sql expr string
211 212 213 214 215 216 217 218
} tSQLExpr;

// used in select clause. select <tSQLExprList> from xxx
typedef struct tSQLExprItem {
  tSQLExpr *pNode;      // The list of expressions
  char *    aliasName;  // alias name, null-terminated string
} tSQLExprItem;

219
// todo refactor by using SArray
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
typedef struct tSQLExprList {
  int32_t       nExpr;  /* Number of expressions on the list */
  int32_t       nAlloc; /* Number of entries allocated below */
  tSQLExprItem *a;      /* One entry for each expression */
} tSQLExprList;

typedef struct tSQLExprListList {
  int32_t        nList;  /* Number of expressions on the list */
  int32_t        nAlloc; /* Number of entries allocated below */
  tSQLExprList **a;      /* one entry for each row */
} tSQLExprListList;


/**
 *
 * @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 *);

/**
 *
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
void ParseFree(void *p, void (*freeProc)(void *));

tVariantList *tVariantListAppend(tVariantList *pList, tVariant *pVar, uint8_t sortOrder);

tVariantList *tVariantListInsert(tVariantList *pList, tVariant *pVar, uint8_t sortOrder, int32_t index);

H
Haojun Liao 已提交
252
tVariantList *tVariantListAppendToken(tVariantList *pList, SStrToken *pAliasToken, uint8_t sortOrder);
253 254 255 256 257 258 259 260 261 262
void          tVariantListDestroy(tVariantList *pList);

tFieldList *tFieldListAppend(tFieldList *pList, TAOS_FIELD *pField);

void tFieldListDestroy(tFieldList *pList);

tSQLExpr *tSQLExprCreate(tSQLExpr *pLeft, tSQLExpr *pRight, int32_t optType);

void tSQLExprDestroy(tSQLExpr *);

H
Haojun Liao 已提交
263
tSQLExprList *tSQLExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SStrToken *pToken);
264 265 266

void tSQLExprListDestroy(tSQLExprList *pList);

H
Haojun Liao 已提交
267
SQuerySQL *tSetQuerySQLElems(SStrToken *pSelectToken, tSQLExprList *pSelection, tVariantList *pFrom, tSQLExpr *pWhere,
268
                             tVariantList *pGroupby, tVariantList *pSortOrder, SIntervalVal *pInterval,
H
Haojun Liao 已提交
269
                             SStrToken *pSliding, tVariantList *pFill, SLimitVal *pLimit, SLimitVal *pGLimit);
270

H
Haojun Liao 已提交
271
SCreateTableSQL *tSetCreateSQLElems(tFieldList *pCols, tFieldList *pTags, SStrToken *pMetricName,
272 273 274 275 276
                                    tVariantList *pTagVals, SQuerySQL *pSelect, int32_t type);

void      tSQLExprNodeDestroy(tSQLExpr *pExpr);
tSQLExpr *tSQLExprNodeClone(tSQLExpr *pExpr);

H
Haojun Liao 已提交
277
SAlterTableSQL *tAlterTableSQLElems(SStrToken *pMeterName, tFieldList *pCols, tVariantList *pVals, int32_t type);
278 279 280 281 282 283

tSQLExprListList *tSQLListListAppend(tSQLExprListList *pList, tSQLExprList *pExprList);

void destroyAllSelectClause(SSubclauseInfo *pSql);
void doDestroyQuerySql(SQuerySQL *pSql);

H
Haojun Liao 已提交
284
SSqlInfo *      setSQLInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pMeterName, int32_t type);
285 286 287 288
SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo);

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

H
Haojun Liao 已提交
289
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pMeterName, SStrToken *pIfNotExists);
290 291 292 293

void SQLInfoDestroy(SSqlInfo *pInfo);

void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
H
Haojun Liao 已提交
294 295
void setDropDBTableInfo(SSqlInfo *pInfo, int32_t type, SStrToken* pToken, SStrToken* existsCheck);
void setShowOptions(SSqlInfo *pInfo, int32_t type, SStrToken* prefix, SStrToken* pPatterns);
296

H
Haojun Liao 已提交
297
tDCLSQL *tTokenListAppend(tDCLSQL *pTokenList, SStrToken *pToken);
298

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

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

void setDefaultCreateDbOption(SCreateDBInfo *pDBInfo);

// prefix show db.tables;
H
Haojun Liao 已提交
309
void setDBName(SStrToken *pCpxName, SStrToken *pDB);
310

H
Haojun Liao 已提交
311
tSQLExpr *tSQLExprIdValueCreate(SStrToken *pToken, int32_t optType);
312

H
Haojun Liao 已提交
313
tSQLExpr *tSQLExprCreateFunction(tSQLExprList *pList, SStrToken *pFuncToken, SStrToken *endToken, int32_t optType);
314

H
Haojun Liao 已提交
315
void tSQLSetColumnInfo(TAOS_FIELD *pField, SStrToken *pName, TAOS_FIELD *pType);
316

H
Haojun Liao 已提交
317
void tSQLSetColumnType(TAOS_FIELD *pField, SStrToken *pToken);
318 319 320

void *ParseAlloc(void *(*mallocProc)(size_t));

H
[td-32]  
hjxilinx 已提交
321 322 323
// convert the sql filter expression into binary data
int32_t tSQLExprToBinary(tSQLExpr* pExpr, SStringBuilder* sb);

324
enum {
H
[td-32]  
hjxilinx 已提交
325 326
  TSQL_NODE_TYPE_EXPR  = 0x1,
  TSQL_NODE_TYPE_ID    = 0x2,
327 328 329 330
  TSQL_NODE_TYPE_VALUE = 0x4,
};

#define NON_ARITHMEIC_EXPR 0
H
[td-32]  
hjxilinx 已提交
331 332
#define NORMAL_ARITHMETIC  1
#define AGG_ARIGHTMEIC     2
333

334
SSqlInfo qSQLParse(const char *str);
335

336 337 338 339
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
340
#endif  // TDENGINE_QSQLPARSER_H