parser.h 8.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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 _TD_PARSER_H_
#define _TD_PARSER_H_

#ifdef __cplusplus
extern "C" {
#endif

23 24
#include "catalog.h"
#include "common.h"
25
#include "tname.h"
26
#include "tvariant.h"
27
#include "function.h"
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
typedef struct SField {
  char     name[TSDB_COL_NAME_LEN];
  uint8_t  type;
  int16_t  bytes;
} SField;

typedef struct SFieldInfo {
  int16_t     numOfOutput;   // number of column in result
  SField     *final;
  SArray     *internalField; // SArray<SInternalField>
} SFieldInfo;

typedef struct SCond {
  uint64_t uid;
  int32_t  len;  // length of tag query condition data
  char *   cond;
} SCond;

typedef struct SJoinNode {
  uint64_t uid;
  int16_t  tagColId;
  SArray*  tsJoin;
  SArray*  tagJoin;
} SJoinNode;

typedef struct SJoinInfo {
  bool       hasJoin;
  SJoinNode *joinTables[TSDB_MAX_JOIN_TABLE_NUM];
} SJoinInfo;

typedef struct STagCond {
  int16_t   relType;     // relation between tbname list and query condition, including : TK_AND or TK_OR
  SCond     tbnameCond;  // tbname query condition, only support tbname query condition on one table
  SJoinInfo joinInfo;    // join condition, only support two tables join currently
  SArray   *pCond;       // for different table, the query condition must be seperated
} STagCond;

typedef struct STableMetaInfo {
  STableMeta    *pTableMeta;      // table meta, cached in client side and acquired by name
  SVgroupsInfo  *vgroupList;
  SName         name;
  char          aliasName[TSDB_TABLE_NAME_LEN];    // alias name of table specified in query sql
  SArray       *tagColList;                        // SArray<SColumn*>, involved tag columns
} STableMetaInfo;

typedef struct SQueryStmtInfo {
  int16_t          command;       // the command may be different for each subclause, so keep it seperately.
  uint32_t         type;          // query/insert type
  STimeWindow      window;        // the whole query time window
  SInterval        interval;      // tumble time window
  SSessionWindow   sessionWindow; // session time window
80
  SStateWindow     stateWindow;   // state window query
81 82 83
  SGroupbyExpr     groupbyExpr;   // groupby tags info
  SArray *         colList;       // SArray<SColumn*>
  SFieldInfo       fieldsInfo;
84
  SArray**         exprList;      // SArray<SExprInfo*>
85 86 87 88
  SLimit           limit;
  SLimit           slimit;
  STagCond         tagCond;
  SArray *         colCond;
89
  SArray *         order;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  int16_t          numOfTables;
  int16_t          curTableIdx;
  STableMetaInfo **pTableMetaInfo;
  struct STSBuf   *tsBuf;

  int16_t          fillType;      // final result fill type
  int64_t *        fillVal;       // default value for fill
  int32_t          numOfFillVal;  // fill value size

  char *           msg;           // pointer to the pCmd->payload to keep error message temporarily
  int64_t          clauseLimit;   // limit for current sub clause

  int64_t          prjOffset;     // offset value in the original sql expression, only applied at client side
  int64_t          vgroupLimit;    // table limit in case of super table projection query + global order + limit

  int32_t          udColumnId;    // current user-defined constant output field column id, monotonically decreases from TSDB_UD_COLUMN_INDEX
  int32_t          bufLen;
  char*            buf;
  SArray          *pUdfInfo;

  struct SQueryStmtInfo *sibling;     // sibling
  struct SQueryStmtInfo *pDownstream;
H
Haojun Liao 已提交
112 113
  SMultiFunctionsDesc    info;
  SArray            *pUpstream;   // SArray<struct SQueryStmtInfo>
114
  int32_t            havingFieldNum;
115
  int32_t            exprListLevelIndex;
116
} SQueryStmtInfo;
117

118 119 120 121 122 123
typedef struct SColumnIndex {
  int16_t tableIndex;
  int16_t columnIndex;
  int16_t type;               // normal column/tag/ user input constant column
} SColumnIndex;

124 125 126 127 128 129 130 131 132 133
struct SInsertStmtInfo;

/**
 * True will be returned if the input sql string is insert, false otherwise.
 * @param pStr    sql string
 * @param length  length of the sql string
 * @return
 */
bool qIsInsertSql(const char* pStr, size_t length);

134
typedef struct SParseContext {
135
  const char* pAcctId;
136
  const char* pDbname;
137 138 139 140
  void *pRpc;
  const char* pClusterId;
  const SEpSet* pEpSet;
  int64_t id;          // query id, generated by uuid generator
141
  int8_t schemaAttached; // denote if submit block is built with table schema or not
142 143
  const char* pSql;    // sql string
  size_t sqlLen;       // length of the sql string
144 145 146 147
  char* pMsg;           // extended error message if exists to help avoid the problem in sql statement.
  int32_t msgLen;      // max length of the msg
} SParseContext;

148 149 150 151 152 153 154 155
/**
 * Parse the sql statement and then return the SQueryStmtInfo as the result of bounded AST.
 * @param pSql     sql string
 * @param length   length of the sql string
 * @param id       operator id, generated by uuid generator
 * @param msg      extended error message if exists.
 * @return         error code
 */
H
Haojun Liao 已提交
156
int32_t qParseQuerySql(const char* pStr, size_t length, struct SQueryStmtInfo** pQueryInfo, int64_t id, char* msg, int32_t msgLen);
157

158 159 160 161 162
typedef enum {
  PAYLOAD_TYPE_KV = 0,
  PAYLOAD_TYPE_RAW = 1,
} EPayloadType;

163 164 165 166
typedef struct SVgDataBlocks {
  int64_t     vgId;         // virtual group id
  int32_t     numOfTables;  // number of tables in current submit block
  uint32_t    size;
167
  char       *pData;        // SMsgDesc + SSubmitMsg + SSubmitBlk + ...
168 169
} SVgDataBlocks;

170
typedef struct SInsertStmtInfo {
171
  SArray*     pDataBlocks;         // data block for each vgroup, SArray<SVgDataBlocks*>.
172 173 174 175 176 177
  int8_t      schemaAttache;        // denote if submit block is built with table schema or not
  uint8_t     payloadType;         // EPayloadType. 0: K-V payload for non-prepare insert, 1: rawPayload for prepare insert
  uint32_t    insertType;          // insert data from [file|sql statement| bound statement]
  const char* sql;                 // current sql statement position
} SInsertStmtInfo;

178 179 180 181 182 183
/**
 * Parse the insert sql statement.
 * @param pStr            sql string
 * @param length          length of the sql string
 * @param id              operator id, generated by uuid generator.
 * @param msg             extended error message if exists to help avoid the problem in sql statement.
184
 * @return                data in binary format to submit to vnode directly.
185
 */
186
 int32_t qParseInsertSql(SParseContext* pContext, struct SInsertStmtInfo** pInfo);
187 188 189 190 191 192 193 194 195 196 197

/**
 * Convert a normal sql statement to only query tags information to enable that the subscribe client can be aware quickly of the true vgroup ids that
 * involved in the subscribe procedure.
 * @param pSql
 * @param length
 * @param pConvertSql
 * @return
 */
int32_t qParserConvertSql(const char* pStr, size_t length, char** pConvertSql);

198 199 200 201
void assignExprInfo(SExprInfo* dst, const SExprInfo* src);
void columnListCopy(SArray* dst, const SArray* src, uint64_t uid);
void columnListDestroy(SArray* pColumnList);

202
void dropAllExprInfo(SArray** pExprInfo, int32_t numOfLevel);
203 204 205 206 207 208 209

typedef struct SSourceParam {
  SArray            *pExprNodeList; //Array<struct tExprNode*>
  SArray            *pColumnList;   //Array<struct SColumn>
  int32_t    num;
} SSourceParam;

210
SExprInfo* createExprInfo(STableMetaInfo* pTableMetaInfo, const char* funcName, SSourceParam* pSource, SSchema* pResSchema, int16_t interSize);
211
int32_t copyExprInfoList(SArray* dst, const SArray* src, uint64_t uid, bool deepcopy);
H
Haojun Liao 已提交
212
int32_t getExprFunctionLevel(SQueryStmtInfo* pQueryInfo);
213 214

STableMetaInfo* getMetaInfo(SQueryStmtInfo* pQueryInfo, int32_t tableIndex);
H
Haojun Liao 已提交
215
SSchema *getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex);
216
SSchema createSchema(uint8_t type, int16_t bytes, int16_t colId, const char* name);
H
Haojun Liao 已提交
217

218
int32_t getNewResColId();
219
void addIntoSourceParam(SSourceParam* pSourceParam, tExprNode* pNode, SColumn* pColumn);
220

H
refact  
Hongze Cheng 已提交
221 222 223 224 225
#ifdef __cplusplus
}
#endif

#endif /*_TD_PARSER_H_*/