qSqlparser.h 9.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 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
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
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;
  int32_t orderColId;
} SOrderVal;

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

56 57 58 59 60
typedef struct SIntervalVal {
  SStrToken interval;
  SStrToken offset;
} SIntervalVal;

61 62
typedef struct SQuerySQL {
  struct tSQLExprList *pSelection;   // select clause
H
Haojun Liao 已提交
63
  SArray *             from;         // from clause  SArray<tVariantListItem>
64
  struct tSQLExpr *    pWhere;       // where clause [optional]
H
Haojun Liao 已提交
65 66
  SArray *             pGroupby;     // groupby clause, only for tags[optional], SArray<tVariantListItem>
  SArray *             pSortOrder;   // orderby [optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
67
  SStrToken            interval;     // interval [optional]
68
  SStrToken            offset;       // offset window [optional]
H
Haojun Liao 已提交
69
  SStrToken            sliding;      // sliding window [optional]
70 71
  SLimitVal            limit;        // limit offset [optional]
  SLimitVal            slimit;       // group limit offset [optional]
H
Haojun Liao 已提交
72
  SArray *             fillType;     // fill type[optional], SArray<tVariantListItem>
H
Haojun Liao 已提交
73
  SStrToken            selectToken;  // sql string
74 75 76
} SQuerySQL;

typedef struct SCreateTableSQL {
H
Haojun Liao 已提交
77
  struct SStrToken name;  // meter name, create table [meterName] xxx
78 79 80 81
  bool             existCheck;
  
  int8_t           type; // create normal table/from super table/ stream
  struct {
H
Haojun Liao 已提交
82 83
    SArray        *pTagColumns; // SArray<TAOS_FIELD>
    SArray        *pColumns;    // SArray<TAOS_FIELD>
84 85 86
  } colInfo;
  
  struct {
H
Haojun Liao 已提交
87 88 89
    SStrToken      stableName;  // super table name, for using clause
    SArray        *pTagVals;    // create by using metric, tag value
    STagData       tagdata;
90 91
  } usingInfo;
  
H
Haojun Liao 已提交
92
  SQuerySQL       *pSelect;
93 94 95
} SCreateTableSQL;

typedef struct SAlterTableSQL {
H
Haojun Liao 已提交
96
  SStrToken     name;
97 98
  int16_t       type;
  STagData      tagData;
H
Haojun Liao 已提交
99 100
  SArray       *pAddColumns; // SArray<TAOS_FIELD>
  SArray       *varList;  // set t=val or: change src dst, SArray<tVariantListItem>
101 102 103
} SAlterTableSQL;

typedef struct SCreateDBInfo {
H
Haojun Liao 已提交
104
  SStrToken dbname;
105 106
  int32_t   replica;
  int32_t   cacheBlockSize;
H
hjxilinx 已提交
107 108
  int32_t   maxTablesPerVnode;
  int32_t   numOfBlocks;
109
  int32_t   daysPerFile;
H
hjxilinx 已提交
110 111
  int32_t   minRowsPerBlock;
  int32_t   maxRowsPerBlock;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
112
  int32_t   fsyncPeriod;
113
  int64_t   commitTime;
H
hjxilinx 已提交
114
  int32_t   walLevel;
115
  int32_t   quorum;
116
  int32_t   compressionLevel;
H
Haojun Liao 已提交
117
  SStrToken precision;
118
  bool      ignoreExists;
Y
yihaoDeng 已提交
119
  int8_t    update; 
120
  
H
Haojun Liao 已提交
121
  SArray *keep;
122 123 124 125 126 127 128 129 130 131 132
} 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 已提交
133
  SStrToken stat;
134 135 136 137
} SCreateAcctSQL;

typedef struct SShowInfo {
  uint8_t showType;
H
Haojun Liao 已提交
138 139
  SStrToken prefix;
  SStrToken pattern;
140 141 142
} SShowInfo;

typedef struct SUserInfo {
H
Haojun Liao 已提交
143 144 145
  SStrToken user;
  SStrToken passwd;
  SStrToken privilege;
146 147 148 149 150 151
  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 已提交
152
  SStrToken *a;       /* one entry for element */
153 154 155 156 157 158
  bool  existsCheck;
  
  union {
    SCreateDBInfo  dbOpt;
    SCreateAcctSQL acctOpt;
    SShowInfo      showOpt;
H
Haojun Liao 已提交
159
    SStrToken      ip;
160 161 162 163 164 165 166 167 168 169 170 171
  };
  
  SUserInfo user;
  
} tDCLSQL;

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

typedef struct SSqlInfo {
H
Haojun Liao 已提交
172 173
  int32_t            type;
  bool               valid;
174 175 176
  
  union {
    SCreateTableSQL *pCreateTableInfo;
H
Haojun Liao 已提交
177 178
    SAlterTableSQL  *pAlterInfo;
    tDCLSQL         *pDCLInfo;
179 180
  };
  
H
Haojun Liao 已提交
181 182
  SSubclauseInfo     subclauseInfo;
  char               pzErrMsg[256];
183 184 185
} SSqlInfo;

typedef struct tSQLExpr {
H
Haojun Liao 已提交
186
  uint32_t         nSQLOptr;    // TK_FUNCTION: sql function, TK_LE: less than(binary expr)
187 188 189
  
  // 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 已提交
190 191 192 193 194
  SStrToken        operand;
  SStrToken        colInfo;     // field id
  tVariant         val;         // value only for string, float, int
  SStrToken        token;       // original sql expr string

H
[td-32]  
hjxilinx 已提交
195 196 197
  struct tSQLExpr *pLeft;       // left child
  struct tSQLExpr *pRight;      // right child
  struct tSQLExprList *pParam;  // function parameters
198 199 200 201 202 203 204 205
} 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;

206
// todo refactor by using SArray
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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;

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

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

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

void tSQLExprDestroy(tSQLExpr *);

H
Haojun Liao 已提交
236
tSQLExprList *tSQLExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SStrToken *pToken);
237 238 239

void tSQLExprListDestroy(tSQLExprList *pList);

H
Haojun Liao 已提交
240 241 242
SQuerySQL *tSetQuerySQLElems(SStrToken *pSelectToken, tSQLExprList *pSelection, SArray *pFrom, tSQLExpr *pWhere,
                             SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval,
                             SStrToken *pSliding, SArray *pFill, SLimitVal *pLimit, SLimitVal *pGLimit);
243

H
Haojun Liao 已提交
244 245
SCreateTableSQL *tSetCreateSQLElems(SArray *pCols, SArray *pTags, SStrToken *pMetricName,
                                    SArray *pTagVals, SQuerySQL *pSelect, int32_t type);
246

H
Haojun Liao 已提交
247
void tSQLExprNodeDestroy(tSQLExpr *pExpr);
248

H
Haojun Liao 已提交
249
SAlterTableSQL *tAlterTableSQLElems(SStrToken *pMeterName, SArray *pCols, SArray *pVals, int32_t type);
250 251 252 253

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

H
Haojun Liao 已提交
254
SSqlInfo *      setSQLInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SStrToken *pMeterName, int32_t type);
255 256 257 258
SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo);

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

H
Haojun Liao 已提交
259
void setCreatedTableName(SSqlInfo *pInfo, SStrToken *pMeterName, SStrToken *pIfNotExists);
260 261 262 263

void SQLInfoDestroy(SSqlInfo *pInfo);

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

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

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

H
Haojun Liao 已提交
271 272 273 274
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);
275 276 277 278

void setDefaultCreateDbOption(SCreateDBInfo *pDBInfo);

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

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

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

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

H
Haojun Liao 已提交
287
void tSQLSetColumnType(TAOS_FIELD *pField, SStrToken *pToken);
288 289 290

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

291
enum {
H
[td-32]  
hjxilinx 已提交
292 293
  TSQL_NODE_TYPE_EXPR  = 0x1,
  TSQL_NODE_TYPE_ID    = 0x2,
294 295 296 297
  TSQL_NODE_TYPE_VALUE = 0x4,
};

#define NON_ARITHMEIC_EXPR 0
H
[td-32]  
hjxilinx 已提交
298 299
#define NORMAL_ARITHMETIC  1
#define AGG_ARIGHTMEIC     2
300

301
SSqlInfo qSQLParse(const char *str);
302

303 304 305 306
#ifdef __cplusplus
}
#endif

H
hjxilinx 已提交
307
#endif  // TDENGINE_QSQLPARSER_H