astGenerator.h 12.4 KB
Newer Older
H
Haojun Liao 已提交
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
/*
 * 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_ASTGENERATOR_H
#define TDENGINE_ASTGENERATOR_H

#ifdef __cplusplus
extern "C" {
#endif

#include "ttoken.h"
#include "tvariant.h"
#include "parser.h"

27
// #define ParseTOKENTYPE SToken
H
Haojun Liao 已提交
28 29 30 31 32 33 34 35 36 37 38 39

#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,
};

40 41 42 43 44 45 46 47
enum SQL_FROM_NODE_TYPE {
  SQL_FROM_NODE_SUBQUERY = 1,
  SQL_FROM_NODE_TABLES   = 2,
};

enum SQL_UNION_TYPE {
  SQL_TYPE_UNIONALL      = 1,
  SQL_TYPE_UNION         = 2,
H
Haojun Liao 已提交
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
};

extern char tTokenTypeSwitcher[13];

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

#define TPARSER_HAS_TOKEN(_t)      ((_t).n > 0)
#define TPARSER_SET_NONE_TOKEN(_t) ((_t).n = 0)

typedef struct SListItem {
  SVariant           pVar;
  uint8_t            sortOrder;
} SListItem;

typedef struct SIntervalVal {
  int32_t            token;
  SToken             interval;
  SToken             offset;
} SIntervalVal;

typedef struct SSessionWindowVal {
  SToken          col;
  SToken          gap;
} SSessionWindowVal;

typedef struct SWindowStateVal {
  SToken          col;
} SWindowStateVal;

struct SRelationInfo;

typedef struct SSqlNode {
  struct SRelationInfo  *from;     // from clause SArray<SSqlNode>
88
  struct SArray     *pSelNodeList; // select clause
H
Haojun Liao 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
  struct tSqlExpr   *pWhere;       // where clause [optional]
  SArray            *pGroupby;     // groupby clause, only for tags[optional], SArray<SListItem>
  SArray            *pSortOrder;   // orderby [optional],  SArray<SListItem>
  SArray            *fillType;     // fill type[optional], SArray<SListItem>
  SIntervalVal       interval;     // (interval, interval_offset) [optional]
  SSessionWindowVal  sessionVal;   // session window [optional]
  SWindowStateVal    windowstateVal; // window_state(col) [optional]
  SToken             sliding;      // sliding window [optional]
  SLimit             limit;        // limit offset [optional]
  SLimit             slimit;       // group limit offset [optional]
  SToken             sqlstr;       // sql string in select clause
  struct tSqlExpr   *pHaving;      // having clause [optional]
} SSqlNode;

103 104 105 106 107 108
typedef struct SSubclause {
  int32_t            unionType;
  SArray            *node;
} SSubclause;

typedef struct SRelElement {
H
Haojun Liao 已提交
109
  union {
110 111
    SToken      tableName;
    SSubclause *pSubquery;
H
Haojun Liao 已提交
112 113 114
  };

  SToken aliasName;
115
} SRelElement;
H
Haojun Liao 已提交
116 117 118

typedef struct SRelationInfo {
  int32_t           type;        // nested query|table name list
119
  SArray           *list;        // SArray<SRelElement>
H
Haojun Liao 已提交
120 121 122 123
} SRelationInfo;

typedef struct SCreatedTableInfo {
  SToken             name;        // table name token
S
Shengliang Guan 已提交
124
  SToken             stbName;     // super table name token , for using clause
H
Haojun Liao 已提交
125
  SArray            *pTagNames;   // create by using super table, tag name
H
Haojun Liao 已提交
126
  SArray            *pTagVals;    // create by using super table, tag value. SArray<SToken>
H
Haojun Liao 已提交
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
  char              *fullname;    // table full name
  int8_t             igExist;     // ignore if exists
} SCreatedTableInfo;

typedef struct SCreateTableSql {
  SToken             name;  // table name, create table [name] xxx
  int8_t             type;  // create normal table/from super table/ stream
  bool               existCheck;

  struct {
    SArray          *pTagColumns; // SArray<SField>
    SArray          *pColumns;    // SArray<SField>
  } colInfo;

  SArray            *childTableInfo;        // SArray<SCreatedTableInfo>
  SSqlNode          *pSelect;
} SCreateTableSql;

typedef struct SAlterTableInfo {
  SToken             name;
  int16_t            tableType;
  int16_t            type;
  STagData           tagData;
  SArray            *pAddColumns; // SArray<SField>
  SArray            *varList;     // set t=val or: change src dst, SArray<SListItem>
} SAlterTableInfo;

typedef struct SCreateDbInfo {
  SToken             dbname;
  int32_t            replica;
  int32_t            cacheBlockSize;
H
Haojun Liao 已提交
158
  int32_t            numOfVgroups;
H
Haojun Liao 已提交
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
  int32_t            numOfBlocks;
  int32_t            daysPerFile;
  int32_t            minRowsPerBlock;
  int32_t            maxRowsPerBlock;
  int32_t            fsyncPeriod;
  int64_t            commitTime;
  int32_t            walLevel;
  int32_t            quorum;
  int32_t            compressionLevel;
  SToken             precision;
  bool               ignoreExists;
  int8_t             update;
  int8_t             cachelast;
  SArray            *keep;
} SCreateDbInfo;

typedef struct SCreateFuncInfo {
  SToken             name;
  SToken             path;
  int32_t            type;
  int32_t            bufSize;
  SField             output;
} SCreateFuncInfo;

typedef struct SCreateAcctInfo {
  int32_t            maxUsers;
  int32_t            maxDbs;
  int32_t            maxTimeSeries;
  int32_t            maxStreams;
  int32_t            maxPointsPerSecond;
  int64_t            maxStorage;
  int64_t            maxQueryTime;
  int32_t            maxConnections;
  SToken             stat;
} SCreateAcctInfo;

typedef struct SShowInfo {
  uint8_t            showType;
  SToken             prefix;
  SToken             pattern;
} SShowInfo;

typedef struct SUserInfo {
  SToken             user;
  SToken             passwd;
  SToken             privilege;
  int16_t            type;
} SUserInfo;

typedef struct SMiscInfo {
  SArray            *a;         // SArray<SToken>
  bool               existsCheck;
  int16_t            dbType;
  int16_t            tableType;
  SUserInfo          user;
  union {
    SCreateDbInfo    dbOpt;
    SCreateAcctInfo  acctOpt;
    SCreateFuncInfo  funcOpt;
    SShowInfo        showOpt;
    SToken           id;
  };
} SMiscInfo;

typedef struct SSqlInfo {
  int32_t            type;
  bool               valid;
226
  SSubclause         sub;
H
Haojun Liao 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  char               msg[256];
  SArray            *funcs;
  union {
    SCreateTableSql *pCreateTableInfo;
    SAlterTableInfo *pAlterInfo;
    SMiscInfo       *pMiscInfo;
  };
} SSqlInfo;

typedef struct tSqlExpr {
  uint16_t           type;       // sql node type
  uint32_t           tokenId;    // TK_LE: less than(binary expr)

  // The complete string of the function(col, param), and the function name is kept in exprToken
  struct {
    SToken           operand;
243
    struct SArray   *paramList;   // function parameters list
H
Haojun Liao 已提交
244 245 246 247 248 249 250 251 252 253 254 255
  } Expr;

  SToken             columnName;  // table column info
  SVariant           value;       // the use input value
  SToken             exprToken;   // original sql expr string  or function name of sql function
  struct tSqlExpr   *pLeft;       // the left child
  struct tSqlExpr   *pRight;      // the right child
} tSqlExpr;

// used in select clause. select <SArray> from xxx
typedef struct tSqlExprItem {
  tSqlExpr          *pNode;      // The list of expressions
256
  int32_t            functionId;
H
Haojun Liao 已提交
257 258 259 260 261 262 263 264 265 266
  char              *aliasName;  // alias name, null-terminated string
  bool               distinct;
} tSqlExprItem;

SArray *tListItemAppend(SArray *pList, SVariant *pVar, uint8_t sortOrder);
SArray *tListItemInsert(SArray *pList, SVariant *pVar, uint8_t sortOrder, int32_t index);
SArray *tListItemAppendToken(SArray *pList, SToken *pAliasToken, uint8_t sortOrder);

SRelationInfo *setTableNameList(SRelationInfo *pRelationInfo, SToken *pName, SToken *pAlias);
void *         destroyRelationInfo(SRelationInfo *pFromInfo);
267
SRelationInfo *addSubquery(SRelationInfo *pRelationInfo, SSubclause *pSub, SToken *pAlias);
H
Haojun Liao 已提交
268 269 270 271

// sql expr leaf node
tSqlExpr *tSqlExprCreateIdValue(SToken *pToken, int32_t optrType);
tSqlExpr *tSqlExprCreateFunction(SArray *pParam, SToken *pFuncToken, SToken *endToken, int32_t optType);
272
SArray *  tRecordFuncName(SArray *pList, SToken *pToken);
H
Haojun Liao 已提交
273 274 275 276 277 278 279 280 281

tSqlExpr *tSqlExprCreate(tSqlExpr *pLeft, tSqlExpr *pRight, int32_t optrType);
tSqlExpr *tSqlExprClone(tSqlExpr *pSrc);
void      tSqlExprCompact(tSqlExpr **pExpr);
bool      tSqlExprIsLeaf(tSqlExpr *pExpr);
bool      tSqlExprIsParentOfLeaf(tSqlExpr *pExpr);
void      tSqlExprDestroy(tSqlExpr *pExpr);
SArray *  tSqlExprListAppend(SArray *pList, tSqlExpr *pNode, SToken *pDistinct, SToken *pToken);
void      tSqlExprListDestroy(SArray *pList);
282
void      tSqlExprEvaluate(tSqlExpr* pExpr);
H
Haojun Liao 已提交
283 284 285 286 287 288 289 290

SSqlNode *tSetQuerySqlNode(SToken *pSelectToken, SArray *pSelNodeList, SRelationInfo *pFrom, tSqlExpr *pWhere,
                           SArray *pGroupby, SArray *pSortOrder, SIntervalVal *pInterval, SSessionWindowVal *ps,
                           SWindowStateVal *pw, SToken *pSliding, SArray *pFill, SLimit *pLimit, SLimit *pgLimit, tSqlExpr *pHaving);
int32_t   tSqlExprCompare(tSqlExpr *left, tSqlExpr *right);

SCreateTableSql *tSetCreateTableInfo(SArray *pCols, SArray *pTags, SSqlNode *pSelect, int32_t type);

H
Haojun Liao 已提交
291
SAlterTableInfo * tSetAlterTableInfo(SToken *pTableName, SArray *pCols, SArray *pVals, int32_t type, int16_t tableType);
H
Haojun Liao 已提交
292 293
SCreatedTableInfo createNewChildTableInfo(SToken *pTableName, SArray *pTagNames, SArray *pTagVals, SToken *pToken,
                                          SToken *igExists);
294 295 296 297
/*!
 * test
 * @param pSqlNode
 */
298
void destroyAllSqlNode(struct SSubclause *pSqlNode);
H
Haojun Liao 已提交
299 300 301 302
void destroySqlNode(SSqlNode *pSql);
void freeCreateTableInfo(void* p);

SSqlInfo *setSqlInfo(SSqlInfo *pInfo, void *pSqlExprInfo, SToken *pTableName, int32_t type);
303 304
SSubclause* setSubclause(SSubclause* sub, void *pSqlNode);
SSubclause* appendSelectClause(SSubclause *sub, int32_t unionType, void *pSubclause);
H
Haojun Liao 已提交
305 306

void setCreatedTableName(SSqlInfo *pInfo, SToken *pTableNameToken, SToken *pIfNotExists);
H
Haojun Liao 已提交
307
void* destroyCreateTableSql(SCreateTableSql* pCreate);
308 309
void setDropFuncInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken);
void setCreateFuncInfo(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPath, SField *output, SToken* bufSize, int32_t funcType);
H
Haojun Liao 已提交
310

311
void destroySqlInfo(SSqlInfo *pInfo);
H
Haojun Liao 已提交
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

void setDCLSqlElems(SSqlInfo *pInfo, int32_t type, int32_t nParams, ...);
void setDropDbTableInfo(SSqlInfo *pInfo, int32_t type, SToken* pToken, SToken* existsCheck,int16_t dbType,int16_t tableType);
void setShowOptions(SSqlInfo *pInfo, int32_t type, SToken* prefix, SToken* pPatterns);

void setCreateDbInfo(SSqlInfo *pInfo, int32_t type, SToken *pToken, SCreateDbInfo *pDB, SToken *pIgExists);

void setCreateAcctSql(SSqlInfo *pInfo, int32_t type, SToken *pName, SToken *pPwd, SCreateAcctInfo *pAcctInfo);
void setCreateUserSql(SSqlInfo *pInfo, SToken *pName, SToken *pPasswd);
void setKillSql(SSqlInfo *pInfo, int32_t type, SToken *ip);
void setAlterUserSql(SSqlInfo *pInfo, int16_t type, SToken *pName, SToken* pPwd, SToken *pPrivilege);

void setCompactVnodeSql(SSqlInfo *pInfo, int32_t type, SArray *pParam);

void setDefaultCreateDbOption(SCreateDbInfo *pDBInfo);
void setDefaultCreateTopicOption(SCreateDbInfo *pDBInfo);

// prefix show db.tables;
void tSetDbName(SToken *pCpxName, SToken *pDb);

void tSetColumnInfo(struct SField *pField, SToken *pName, struct SField *pType);
void tSetColumnType(struct SField *pField, SToken *type);

/**
 * The main parse function.
 * @param yyp      The parser
 * @param yymajor  The major token code number
 * @param yyminor  The value for the token
 */
341
// void Parse(void *yyp, int yymajor, ParseTOKENTYPE yyminor, SSqlInfo *);
H
Haojun Liao 已提交
342 343 344 345 346 347

/**
 * Free the allocated resources in case of failure.
 * @param p         The parser to be deleted
 * @param freeProc  Function used to reclaim memory
 */
348
// void ParseFree(void *p, void (*freeProc)(void *));
H
Haojun Liao 已提交
349 350 351 352 353 354

/**
 * Allocated callback function.
 * @param mallocProc  The parser allocator
 * @return
 */
355
// void *ParseAlloc(void *(*mallocProc)(size_t));
H
Haojun Liao 已提交
356 357 358 359 360 361

/**
 *
 * @param str sql string
 * @return sql ast
 */
H
Haojun Liao 已提交
362
SSqlInfo doGenerateAST(const char *str);
H
Haojun Liao 已提交
363 364 365 366 367 368

#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_ASTGENERATOR_H