tsql.h 10.6 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 24 25 26 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 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 295 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/*
 * 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"
#include "tsqldef.h"
#include "ttypes.h"

#define TK_SPACE   200
#define TK_COMMENT 201
#define TK_ILLEGAL 202
#define TK_HEX     203
#define TK_OCT     204

#define TSQL_SO_ASC  1
#define TSQL_SO_DESC 0

#define MAX_TOKEN_LEN 30

#define TSQL_TBNAME   "TBNAME"
#define TSQL_TBNAME_L "tbname"

#define TSQL_STABLE_QTYPE_COND 1
#define TSQL_STABLE_QTYPE_SET  2

// token type
enum {
  TSQL_NODE_TYPE_EXPR = 0x1,
  TSQL_NODE_TYPE_ID = 0x2,
  TSQL_NODE_TYPE_VALUE = 0x4,
};

extern char tTokenTypeSwitcher[12];

#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 {
  int32_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;

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

// sql operation type
enum TSQL_TYPE {
  TSQL_CREATE_NORMAL_METER = 0x01,
  TSQL_CREATE_NORMAL_METRIC = 0x02,
  TSQL_CREATE_METER_FROM_METRIC = 0x04,
  TSQL_CREATE_STREAM = 0x08,
  TSQL_QUERY_METER = 0x10,
  TSQL_INSERT = 0x20,

  DROP_DNODE = 0x40,
  DROP_DATABASE = 0x41,
  DROP_TABLE = 0x42,
  DROP_USER = 0x43,
  DROP_ACCOUNT = 0x44,

  USE_DATABASE = 0x50,

  // show operation
  SHOW_DATABASES = 0x60,
  SHOW_TABLES = 0x61,
  SHOW_STABLES = 0x62,
  SHOW_MNODES = 0x63,
  SHOW_DNODES = 0x64,
  SHOW_ACCOUNTS = 0x65,
  SHOW_USERS = 0x66,
  SHOW_VGROUPS = 0x67,
  SHOW_QUERIES = 0x68,
  SHOW_STREAMS = 0x69,
  SHOW_CONFIGS = 0x6a,
  SHOW_SCORES = 0x6b,
  SHOW_MODULES = 0x6c,
  SHOW_CONNECTIONS = 0x6d,
  SHOW_GRANTS = 0x6e,

  // create dnode
  CREATE_DNODE = 0x80,
  CREATE_DATABASE = 0x81,
  CREATE_USER = 0x82,
  CREATE_ACCOUNT = 0x83,

  DESCRIBE_TABLE = 0x90,

  ALTER_USER_PASSWD = 0xA0,
  ALTER_USER_PRIVILEGES = 0xA1,
  ALTER_DNODE = 0xA2,
  ALTER_LOCAL = 0xA3,
  ALTER_DATABASE = 0xA4,
  ALTER_ACCT = 0xA5,

  // reset operation
  RESET_QUERY_CACHE = 0xB0,

  // alter tags
  ALTER_TABLE_TAGS_ADD = 0xC0,
  ALTER_TABLE_TAGS_DROP = 0xC1,
  ALTER_TABLE_TAGS_CHG = 0xC2,
  ALTER_TABLE_TAGS_SET = 0xC4,

  // alter table column
  ALTER_TABLE_ADD_COLUMN = 0xD0,
  ALTER_TABLE_DROP_COLUMN = 0xD1,

  KILL_QUERY = 0xD2,
  KILL_STREAM = 0xD3,
  KILL_CONNECTION = 0xD4,
};

typedef struct SQuerySQL {
  struct tSQLExprList *pSelection;   // select clause
  struct SSQLToken     from;         // from clause
  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]
  SLimitVal            glimit;       // group limit offset [optional]
  tVariantList *       fillType;     // fill type[optional]
  SSQLToken            selectToken;  // sql string
} SQuerySQL;

typedef struct SCreateTableSQL {
  struct SSQLToken name;  // meter name, create table [meterName] xxx
  bool             existCheck;

  struct {
    tFieldList *pTagColumns;  // for normal table, pTagColumns = NULL;
    tFieldList *pColumns;
  } colInfo;

  struct {
    SSQLToken     metricName;  // metric name, for using clause
    tVariantList *pTagVals;    // create by using metric, tag value
  } usingInfo;

  SQuerySQL *pSelect;

} SCreateTableSQL;

typedef struct SAlterTableSQL {
  SSQLToken     name;
  tFieldList *  pAddColumns;
  SSQLToken     dropTagToken;
  tVariantList *varList;  // set t=val or: change src dst
} SAlterTableSQL;

typedef struct SInsertSQL {
  SSQLToken                name;
  struct tSQLExprListList *pValue;
} SInsertSQL;

typedef struct SCreateDBSQL {
  SSQLToken dbname;
  int32_t   nReplica;
  int32_t   nDays;

  tVariantList *keep;
  int32_t       nRowsInFileBlock;
  int32_t       nCacheBlockSize;
  float         nCacheNumOfBlocks;
  int32_t       numOfBlocksPerTable;

  int32_t   nTablesPerVnode;
  int64_t   commitTime;
  int32_t   commitLog;
  int32_t   compressionLevel;
  SSQLToken precision;  // time precision[ms by default/us]
} SCreateDBSQL;

typedef struct SCreateAcctSQL {
  int32_t   users;
  int32_t   dbs;
  int32_t   tseries;
  int32_t   streams;
  int32_t   pps;
  int64_t   storage;
  int64_t   qtime;
  int32_t   conns;
  SSQLToken stat;
} SCreateAcctSQL;

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 */

  union {
    SCreateDBSQL   dbOpt;
    SCreateAcctSQL acctOpt;
  };
} tDCLSQL;

typedef struct SSqlInfo {
  int32_t sqlType;
  bool    validSql;

  union {
    SCreateTableSQL *pCreateTableInfo;
    SInsertSQL *     pInsertInfo;
    SAlterTableSQL * pAlterInfo;
    SQuerySQL *      pQueryInfo;
    tDCLSQL *        pDCLInfo;
  };

  char pzErrMsg[256];
} 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
  // field name,
  // since the function name is kept in nSQLOptr already
  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);

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

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

void tSQLExprListDestroy(tSQLExprList *pList);

int32_t tSQLSyntaxNodeToString(tSQLExpr *pNode, char *dst);

SQuerySQL *tSetQuerySQLElems(SSQLToken *pSelectToken, tSQLExprList *pSelection, SSQLToken *pFrom, tSQLExpr *pWhere,
                             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);

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

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

void tSetInsertSQLElems(SSqlInfo *pInfo, SSQLToken *pName, tSQLExprListList *pList);

void destroyQuerySql(SQuerySQL *pSql);

void setSQLInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SSQLToken *pMeterName, int32_t type);

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

void SQLInfoDestroy(SSqlInfo *pInfo);

void setDCLSQLElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);

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

void setCreateDBSQL(SSqlInfo *pInfo, int32_t type, SSQLToken *pToken, SCreateDBSQL *pDB, SSQLToken *pIgExists);

void setCreateAcctSQL(SSqlInfo *pInfo, int32_t type, SSQLToken *pName, SSQLToken *pPwd, SCreateAcctSQL *pAcctInfo);

// 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