tscSQLParser.h 11.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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/>.
 */

#ifndef TDENGINE_TSQL_H
#define TDENGINE_TSQL_H

#ifdef __cplusplus
extern "C" {
#endif

#include "taos.h"
24
#include "taosmsg.h"
H
hzcheng 已提交
25 26
#include "tsqldef.h"
#include "ttypes.h"
27 28 29 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 68 69 70

enum _sql_cmd {
  TSDB_SQL_SELECT = 1,
  TSDB_SQL_FETCH,
  TSDB_SQL_INSERT,
  
  TSDB_SQL_MGMT,  // the SQL below is for mgmt node
  TSDB_SQL_CREATE_DB,
  TSDB_SQL_CREATE_TABLE,
  TSDB_SQL_DROP_DB,
  TSDB_SQL_DROP_TABLE,
  TSDB_SQL_CREATE_ACCT,
  TSDB_SQL_CREATE_USER,  //10
  TSDB_SQL_DROP_ACCT,
  TSDB_SQL_DROP_USER,
  TSDB_SQL_ALTER_USER,
  TSDB_SQL_ALTER_ACCT,
  TSDB_SQL_ALTER_TABLE,
  TSDB_SQL_ALTER_DB,
  TSDB_SQL_CREATE_MNODE,
  TSDB_SQL_DROP_MNODE,
  TSDB_SQL_CREATE_DNODE,
  TSDB_SQL_DROP_DNODE,  // 20
  TSDB_SQL_CFG_DNODE,
  TSDB_SQL_CFG_MNODE,
  TSDB_SQL_SHOW,
  TSDB_SQL_RETRIEVE,
  TSDB_SQL_KILL_QUERY,
  TSDB_SQL_KILL_STREAM,
  TSDB_SQL_KILL_CONNECTION,
  
  TSDB_SQL_READ,  // SQL below is for read operation
  TSDB_SQL_CONNECT,
  TSDB_SQL_USE_DB,    // 30
  TSDB_SQL_META,
  TSDB_SQL_METRIC,
  TSDB_SQL_MULTI_META,
  TSDB_SQL_HB,
  
  TSDB_SQL_LOCAL,  // SQL below for client local
  TSDB_SQL_DESCRIBE_TABLE,
  TSDB_SQL_RETRIEVE_METRIC,
  TSDB_SQL_METRIC_JOIN_RETRIEVE,
  TSDB_SQL_RETRIEVE_TAGS,
H
hjxilinx 已提交
71
  
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  /*
   * build empty result instead of accessing dnode to fetch result
   * reset the client cache
   */
  TSDB_SQL_RETRIEVE_EMPTY_RESULT,  //40
  
  TSDB_SQL_RESET_CACHE,
  TSDB_SQL_SERV_STATUS,
  TSDB_SQL_CURRENT_DB,
  TSDB_SQL_SERV_VERSION,
  TSDB_SQL_CLI_VERSION,
  TSDB_SQL_CURRENT_USER,
  TSDB_SQL_CFG_LOCAL,
  
  TSDB_SQL_MAX   //48
};
H
hzcheng 已提交
88 89

enum {
90 91 92
  TSQL_NODE_TYPE_EXPR = 0x1,
  TSQL_NODE_TYPE_ID = 0x2,
  TSQL_NODE_TYPE_VALUE = 0x4,
H
hzcheng 已提交
93 94
};

H
hjxilinx 已提交
95 96 97 98
#define NON_ARITHMEIC_EXPR 0
#define NORMAL_ARITHMETIC 1
#define AGG_ARIGHTMEIC    2

99
extern char tTokenTypeSwitcher[13];
H
hzcheng 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

#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 {
H
hjxilinx 已提交
116
  uint32_t order;
H
hzcheng 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  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;

typedef struct tFieldList {
  int32_t     nField;
  int32_t     nAlloc;
  TAOS_FIELD *p;
} tFieldList;

137
// create table operation type
H
hzcheng 已提交
138
enum TSQL_TYPE {
139 140 141 142
  TSQL_CREATE_TABLE = 0x1,
  TSQL_CREATE_STABLE = 0x2,
  TSQL_CREATE_TABLE_FROM_STABLE = 0x3,
  TSQL_CREATE_STREAM = 0x4,
H
hzcheng 已提交
143 144 145 146
};

typedef struct SQuerySQL {
  struct tSQLExprList *pSelection;   // select clause
S
slguan 已提交
147
  tVariantList *       from;         // from clause
H
hzcheng 已提交
148 149 150 151 152 153
  struct tSQLExpr *    pWhere;       // where clause [optional]
  tVariantList *       pGroupby;     // groupby clause, only for tags[optional]
  tVariantList *       pSortOrder;   // orderby [optional]
  SSQLToken            interval;     // interval [optional]
  SSQLToken            sliding;      // sliding window [optional]
  SLimitVal            limit;        // limit offset [optional]
S
slguan 已提交
154
  SLimitVal            slimit;       // group limit offset [optional]
H
hzcheng 已提交
155 156 157 158 159 160 161
  tVariantList *       fillType;     // fill type[optional]
  SSQLToken            selectToken;  // sql string
} SQuerySQL;

typedef struct SCreateTableSQL {
  struct SSQLToken name;  // meter name, create table [meterName] xxx
  bool             existCheck;
162 163
  
  int8_t           type; // create normal table/from super table/ stream
H
hzcheng 已提交
164 165 166 167 168 169
  struct {
    tFieldList *pTagColumns;  // for normal table, pTagColumns = NULL;
    tFieldList *pColumns;
  } colInfo;

  struct {
170
    SSQLToken     stableName;  // super table name, for using clause
H
hzcheng 已提交
171
    tVariantList *pTagVals;    // create by using metric, tag value
172
    STagData      tagdata;
H
hzcheng 已提交
173 174 175 176 177 178 179
  } usingInfo;

  SQuerySQL *pSelect;
} SCreateTableSQL;

typedef struct SAlterTableSQL {
  SSQLToken     name;
180 181 182
  int16_t       type;
  STagData      tagData;
  
H
hzcheng 已提交
183 184 185 186
  tFieldList *  pAddColumns;
  tVariantList *varList;  // set t=val or: change src dst
} SAlterTableSQL;

187
typedef struct SCreateDBInfo {
H
hzcheng 已提交
188
  SSQLToken dbname;
189 190 191 192 193
  int32_t   replica;
  int32_t   cacheBlockSize;
  int32_t   tablesPerVnode;
  int32_t   daysPerFile;
  int32_t   rowPerFileBlock;
H
hzcheng 已提交
194

195 196
  float   numOfAvgCacheBlocks;
  int32_t numOfBlocksPerTable;
H
hzcheng 已提交
197 198 199 200

  int64_t   commitTime;
  int32_t   commitLog;
  int32_t   compressionLevel;
201 202 203 204
  SSQLToken precision;

  tVariantList *keep;
} SCreateDBInfo;
H
hzcheng 已提交
205 206

typedef struct SCreateAcctSQL {
207 208 209 210 211 212 213 214
  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
hzcheng 已提交
215 216 217
  SSQLToken stat;
} SCreateAcctSQL;

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
typedef struct SShowInfo {
  uint8_t showType;
  SSQLToken prefix;
  SSQLToken pattern;
} SShowInfo;

typedef struct SUserInfo {
  SSQLToken user;
  SSQLToken passwd;
//  bool      hasPasswd;
  
  SSQLToken privilege;
//  bool      hasPrivilege;
  
  int16_t   type;
} SUserInfo;

H
hzcheng 已提交
235 236 237 238
typedef struct tDCLSQL {
  int32_t    nTokens; /* Number of expressions on the list */
  int32_t    nAlloc;  /* Number of entries allocated below */
  SSQLToken *a;       /* one entry for element */
239
  bool  existsCheck;
H
hzcheng 已提交
240 241

  union {
S
slguan 已提交
242
    SCreateDBInfo  dbOpt;
H
hzcheng 已提交
243
    SCreateAcctSQL acctOpt;
244 245
    SShowInfo      showOpt;
    SSQLToken ip;
H
hzcheng 已提交
246
  };
247 248 249
  
  SUserInfo user;
  
H
hzcheng 已提交
250 251
} tDCLSQL;

252 253 254 255 256
typedef struct SSubclauseInfo {  // "UNION" multiple select sub-clause
  SQuerySQL **pClause;
  int32_t     numOfClause;
} SSubclauseInfo;

H
hzcheng 已提交
257
typedef struct SSqlInfo {
258 259
  int32_t type;
  bool    valid;
H
hzcheng 已提交
260 261 262 263 264 265 266

  union {
    SCreateTableSQL *pCreateTableInfo;
    SAlterTableSQL * pAlterInfo;
    tDCLSQL *        pDCLInfo;
  };

267 268
  SSubclauseInfo subclauseInfo;
  char           pzErrMsg[256];
H
hzcheng 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
} SSqlInfo;

typedef struct tSQLExpr {
  /*
   * for single operand:
   * TK_ALL
   * TK_ID
   * TK_SUM
   * TK_AVG
   * TK_MIN
   * TK_MAX
   * TK_FIRST
   * TK_LAST
   * TK_BOTTOM
   * TK_TOP
   * TK_STDDEV
   * TK_PERCENTILE
   *
   * for binary operand:
   * TK_LESS
   * TK_LARGE
   * TK_EQUAL etc...
   */
  uint32_t nSQLOptr;  // TK_FUNCTION: sql function, TK_LE: less than(binary expr)

  // the full sql string of function(col, param), which is actually the raw
H
hjxilinx 已提交
295
  // field name, since the function name is kept in nSQLOptr already
H
hzcheng 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  SSQLToken            operand;
  struct tSQLExprList *pParam;  // function parameters

  SSQLToken colInfo;  // field id
  tVariant  val;      // value only for string, float, int

  struct tSQLExpr *pLeft;   // left child
  struct tSQLExpr *pRight;  // right child
} 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;

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;

#define ParseTOKENTYPE SSQLToken

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

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

S
slguan 已提交
347 348
tVariantList *tVariantListAppendToken(tVariantList *pList, SSQLToken *pAliasToken, uint8_t sortOrder);
void          tVariantListDestroy(tVariantList *pList);
H
hzcheng 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361

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

void tFieldListDestroy(tFieldList *pList);

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

void tSQLExprDestroy(tSQLExpr *);

tSQLExprList *tSQLExprListAppend(tSQLExprList *pList, tSQLExpr *pNode, SSQLToken *pToken);

void tSQLExprListDestroy(tSQLExprList *pList);

S
slguan 已提交
362
SQuerySQL *tSetQuerySQLElems(SSQLToken *pSelectToken, tSQLExprList *pSelection, tVariantList *pFrom, tSQLExpr *pWhere,
H
hzcheng 已提交
363 364 365 366 367
                             tVariantList *pGroupby, tVariantList *pSortOrder, SSQLToken *pInterval,
                             SSQLToken *pSliding, tVariantList *pFill, SLimitVal *pLimit, SLimitVal *pGLimit);

SCreateTableSQL *tSetCreateSQLElems(tFieldList *pCols, tFieldList *pTags, SSQLToken *pMetricName,
                                    tVariantList *pTagVals, SQuerySQL *pSelect, int32_t type);
368 369

void      tSQLExprNodeDestroy(tSQLExpr *pExpr);
S
slguan 已提交
370
tSQLExpr *tSQLExprNodeClone(tSQLExpr *pExpr);
H
hzcheng 已提交
371 372 373 374 375

SAlterTableSQL *tAlterTableSQLElems(SSQLToken *pMeterName, tFieldList *pCols, tVariantList *pVals, int32_t type);

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

376 377
void destroyAllSelectClause(SSubclauseInfo *pSql);
void doDestroyQuerySql(SQuerySQL *pSql);
H
hzcheng 已提交
378

379 380
SSqlInfo *      setSQLInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SSQLToken *pMeterName, int32_t type);
SSubclauseInfo *setSubclause(SSubclauseInfo *pClause, void *pSqlExprInfo);
H
hzcheng 已提交
381

382
SSubclauseInfo *appendSelectClause(SSubclauseInfo *pInfo, void *pSubclause);
H
hzcheng 已提交
383 384 385 386 387 388

void setCreatedMeterName(SSqlInfo *pInfo, SSQLToken *pMeterName, SSQLToken *pIfNotExists);

void SQLInfoDestroy(SSqlInfo *pInfo);

void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
389 390
void setDropDBTableInfo(SSqlInfo *pInfo, int32_t type, SSQLToken* pToken, SSQLToken* existsCheck);
void setShowOptions(SSqlInfo *pInfo, int32_t type, SSQLToken* prefix, SSQLToken* pPatterns);
H
hzcheng 已提交
391 392 393

tDCLSQL *tTokenListAppend(tDCLSQL *pTokenList, SSQLToken *pToken);

394
void setCreateDBSQL(SSqlInfo *pInfo, int32_t type, SSQLToken *pToken, SCreateDBInfo *pDB, SSQLToken *pIgExists);
H
hzcheng 已提交
395 396

void setCreateAcctSQL(SSqlInfo *pInfo, int32_t type, SSQLToken *pName, SSQLToken *pPwd, SCreateAcctSQL *pAcctInfo);
397 398 399 400
void setCreateUserSQL(SSqlInfo *pInfo, SSQLToken *pName, SSQLToken *pPasswd);
void setKillSQL(SSqlInfo *pInfo, int32_t type, SSQLToken *ip);
void setAlterUserSQL(SSqlInfo *pInfo, int16_t type, SSQLToken *pName, SSQLToken* pPwd, SSQLToken *pPrivilege);

H
hjxilinx 已提交
401
void setDefaultCreateDbOption(SCreateDBInfo *pDBInfo);
H
hzcheng 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

// prefix show db.tables;
void setDBName(SSQLToken *pCpxName, SSQLToken *pDB);

tSQLExpr *tSQLExprIdValueCreate(SSQLToken *pToken, int32_t optType);

tSQLExpr *tSQLExprCreateFunction(tSQLExprList *pList, SSQLToken *pFuncToken, SSQLToken *endToken, int32_t optType);

void tSQLSetColumnInfo(TAOS_FIELD *pField, SSQLToken *pName, TAOS_FIELD *pType);

void tSQLSetColumnType(TAOS_FIELD *pField, SSQLToken *pToken);

int32_t tSQLParse(SSqlInfo *pSQLInfo, const char *pSql);

#ifdef __cplusplus
}
#endif

#endif