querynodes.h 10.3 KB
Newer Older
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 _TD_QUERY_NODES_H_
#define _TD_QUERY_NODES_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "nodes.h"
X
Xiaoyu Wang 已提交
24
#include "tmsg.h"
D
dapan1121 已提交
25
#include "tvariant.h"
26

X
Xiaoyu Wang 已提交
27
#define TABLE_TOTAL_COL_NUM(pMeta) ((pMeta)->tableInfo.numOfColumns + (pMeta)->tableInfo.numOfTags)
X
Xiaoyu Wang 已提交
28 29 30 31
#define TABLE_META_SIZE(pMeta) \
  (NULL == (pMeta) ? 0 : (sizeof(STableMeta) + TABLE_TOTAL_COL_NUM((pMeta)) * sizeof(SSchema)))
#define VGROUPS_INFO_SIZE(pInfo) \
  (NULL == (pInfo) ? 0 : (sizeof(SVgroupsInfo) + (pInfo)->numOfVgroups * sizeof(SVgroupInfo)))
X
Xiaoyu Wang 已提交
32

33 34
typedef struct SRawExprNode {
  ENodeType nodeType;
X
Xiaoyu Wang 已提交
35 36 37
  char*     p;
  uint32_t  n;
  SNode*    pNode;
38 39 40 41 42 43 44 45 46 47
} SRawExprNode;

typedef struct SDataType {
  uint8_t type;
  uint8_t precision;
  uint8_t scale;
  int32_t bytes;
} SDataType;

typedef struct SExprNode {
X
Xiaoyu Wang 已提交
48
  ENodeType type;
49
  SDataType resType;
X
Xiaoyu Wang 已提交
50
  char      aliasName[TSDB_COL_NAME_LEN];
51
  char      userAlias[TSDB_COL_NAME_LEN];
X
Xiaoyu Wang 已提交
52
  SArray*   pAssociation;
53 54
} SExprNode;

X
Xiaoyu Wang 已提交
55
typedef enum EColumnType { COLUMN_TYPE_COLUMN = 1, COLUMN_TYPE_TAG } EColumnType;
56 57

typedef struct SColumnNode {
X
Xiaoyu Wang 已提交
58 59 60 61 62 63 64 65 66 67 68 69
  SExprNode   node;  // QUERY_NODE_COLUMN
  uint64_t    tableId;
  int8_t      tableType;
  col_id_t    colId;
  EColumnType colType;  // column or tag
  char        dbName[TSDB_DB_NAME_LEN];
  char        tableName[TSDB_TABLE_NAME_LEN];
  char        tableAlias[TSDB_TABLE_NAME_LEN];
  char        colName[TSDB_COL_NAME_LEN];
  SNode*      pProjectRef;
  int16_t     dataBlockId;
  int16_t     slotId;
X
Xiaoyu Wang 已提交
70 71
} SColumnNode;

X
Xiaoyu Wang 已提交
72 73
typedef struct STargetNode {
  ENodeType type;
X
Xiaoyu Wang 已提交
74 75 76
  int16_t   dataBlockId;
  int16_t   slotId;
  SNode*    pExpr;
X
Xiaoyu Wang 已提交
77 78
} STargetNode;

79
typedef struct SValueNode {
X
Xiaoyu Wang 已提交
80 81 82 83 84
  SExprNode node;  // QUERY_NODE_VALUE
  char*     literal;
  bool      isDuration;
  bool      translate;
  int16_t   placeholderNo;
85
  union {
X
Xiaoyu Wang 已提交
86 87
    bool     b;
    int64_t  i;
88
    uint64_t u;
X
Xiaoyu Wang 已提交
89 90
    double   d;
    char*    p;
91
  } datum;
D
dapan1121 已提交
92
  int64_t typeData;
X
Xiaoyu Wang 已提交
93
  char    unit;
94 95 96
} SValueNode;

typedef struct SOperatorNode {
X
Xiaoyu Wang 已提交
97
  SExprNode     node;  // QUERY_NODE_OPERATOR
98
  EOperatorType opType;
X
Xiaoyu Wang 已提交
99 100
  SNode*        pLeft;
  SNode*        pRight;
101 102 103
} SOperatorNode;

typedef struct SLogicConditionNode {
X
Xiaoyu Wang 已提交
104
  SExprNode           node;  // QUERY_NODE_LOGIC_CONDITION
105
  ELogicConditionType condType;
X
Xiaoyu Wang 已提交
106
  SNodeList*          pParameterList;
107 108 109
} SLogicConditionNode;

typedef struct SNodeListNode {
X
Xiaoyu Wang 已提交
110 111
  ENodeType  type;  // QUERY_NODE_NODE_LIST
  SDataType  dataType;
112 113 114 115
  SNodeList* pNodeList;
} SNodeListNode;

typedef struct SFunctionNode {
X
Xiaoyu Wang 已提交
116 117 118 119
  SExprNode  node;  // QUERY_NODE_FUNCTION
  char       functionName[TSDB_FUNC_NAME_LEN];
  int32_t    funcId;
  int32_t    funcType;
120
  SNodeList* pParameterList;
X
Xiaoyu Wang 已提交
121
  int32_t    udfBufSize;
122 123 124
} SFunctionNode;

typedef struct STableNode {
X
Xiaoyu Wang 已提交
125
  SExprNode node;
X
Xiaoyu Wang 已提交
126 127 128 129
  char      dbName[TSDB_DB_NAME_LEN];
  char      tableName[TSDB_TABLE_NAME_LEN];
  char      tableAlias[TSDB_TABLE_NAME_LEN];
  uint8_t   precision;
130 131 132 133 134
} STableNode;

struct STableMeta;

typedef struct SRealTableNode {
X
Xiaoyu Wang 已提交
135
  STableNode         table;  // QUERY_NODE_REAL_TABLE
136
  struct STableMeta* pMeta;
X
Xiaoyu Wang 已提交
137 138 139
  SVgroupsInfo*      pVgroupList;
  char               qualDbName[TSDB_DB_NAME_LEN];  // SHOW qualDbName.TABLES
  double             ratio;
140 141 142
} SRealTableNode;

typedef struct STempTableNode {
X
Xiaoyu Wang 已提交
143 144
  STableNode table;  // QUERY_NODE_TEMP_TABLE
  SNode*     pSubquery;
145 146
} STempTableNode;

X
Xiaoyu Wang 已提交
147
typedef enum EJoinType { JOIN_TYPE_INNER = 1 } EJoinType;
148 149

typedef struct SJoinTableNode {
X
Xiaoyu Wang 已提交
150 151 152 153 154
  STableNode table;  // QUERY_NODE_JOIN_TABLE
  EJoinType  joinType;
  SNode*     pLeft;
  SNode*     pRight;
  SNode*     pOnCond;
155 156
} SJoinTableNode;

X
Xiaoyu Wang 已提交
157
typedef enum EGroupingSetType { GP_TYPE_NORMAL = 1 } EGroupingSetType;
158 159

typedef struct SGroupingSetNode {
X
Xiaoyu Wang 已提交
160
  ENodeType        type;  // QUERY_NODE_GROUPING_SET
161
  EGroupingSetType groupingSetType;
X
Xiaoyu Wang 已提交
162
  SNodeList*       pParameterList;
163 164
} SGroupingSetNode;

X
Xiaoyu Wang 已提交
165
typedef enum EOrder { ORDER_ASC = 1, ORDER_DESC } EOrder;
166

X
Xiaoyu Wang 已提交
167
typedef enum ENullOrder { NULL_ORDER_DEFAULT = 1, NULL_ORDER_FIRST, NULL_ORDER_LAST } ENullOrder;
168 169

typedef struct SOrderByExprNode {
X
Xiaoyu Wang 已提交
170 171 172
  ENodeType  type;  // QUERY_NODE_ORDER_BY_EXPR
  SNode*     pExpr;
  EOrder     order;
173 174 175 176
  ENullOrder nullOrder;
} SOrderByExprNode;

typedef struct SLimitNode {
X
Xiaoyu Wang 已提交
177 178 179
  ENodeType type;  // QUERY_NODE_LIMIT
  int64_t   limit;
  int64_t   offset;
180 181 182
} SLimitNode;

typedef struct SStateWindowNode {
X
Xiaoyu Wang 已提交
183 184 185
  ENodeType type;  // QUERY_NODE_STATE_WINDOW
  SNode*    pCol;  // timestamp primary key
  SNode*    pExpr;
186 187 188
} SStateWindowNode;

typedef struct SSessionWindowNode {
X
Xiaoyu Wang 已提交
189 190 191
  ENodeType    type;  // QUERY_NODE_SESSION_WINDOW
  SColumnNode* pCol;  // timestamp primary key
  SValueNode*  pGap;  // gap between two session window(in microseconds)
192 193 194
} SSessionWindowNode;

typedef struct SIntervalWindowNode {
X
Xiaoyu Wang 已提交
195 196 197 198 199 200
  ENodeType type;       // QUERY_NODE_INTERVAL_WINDOW
  SNode*    pCol;       // timestamp primary key
  SNode*    pInterval;  // SValueNode
  SNode*    pOffset;    // SValueNode
  SNode*    pSliding;   // SValueNode
  SNode*    pFill;
201 202 203 204 205 206 207 208 209 210 211 212
} SIntervalWindowNode;

typedef enum EFillMode {
  FILL_MODE_NONE = 1,
  FILL_MODE_VALUE,
  FILL_MODE_PREV,
  FILL_MODE_NULL,
  FILL_MODE_LINEAR,
  FILL_MODE_NEXT
} EFillMode;

typedef struct SFillNode {
X
Xiaoyu Wang 已提交
213 214 215 216 217
  ENodeType   type;  // QUERY_NODE_FILL
  EFillMode   mode;
  SNode*      pValues;    // SNodeListNode
  SNode*      pWStartTs;  // _wstartts pseudo column
  STimeWindow timeRange;
218 219 220
} SFillNode;

typedef struct SSelectStmt {
X
Xiaoyu Wang 已提交
221 222 223 224 225 226 227 228 229 230
  ENodeType   type;  // QUERY_NODE_SELECT_STMT
  bool        isDistinct;
  SNodeList*  pProjectionList;
  SNode*      pFromTable;
  SNode*      pWhere;
  SNodeList*  pPartitionByList;
  SNode*      pWindow;
  SNodeList*  pGroupByList;  // SGroupingSetNode
  SNode*      pHaving;
  SNodeList*  pOrderByList;  // SOrderByExprNode
X
Xiaoyu Wang 已提交
231 232
  SLimitNode* pLimit;
  SLimitNode* pSlimit;
X
Xiaoyu Wang 已提交
233 234 235
  char        stmtName[TSDB_TABLE_NAME_LEN];
  uint8_t     precision;
  bool        isEmptyResult;
X
Xiaoyu Wang 已提交
236
  bool        isTimeOrderQuery;
X
Xiaoyu Wang 已提交
237
  bool        hasAggFuncs;
X
Xiaoyu Wang 已提交
238
  bool        hasRepeatScanFuncs;
239 240
} SSelectStmt;

X
Xiaoyu Wang 已提交
241
typedef enum ESetOperatorType { SET_OP_TYPE_UNION_ALL = 1, SET_OP_TYPE_UNION } ESetOperatorType;
242 243

typedef struct SSetOperator {
X
Xiaoyu Wang 已提交
244
  ENodeType        type;  // QUERY_NODE_SET_OPERATOR
245
  ESetOperatorType opType;
X
Xiaoyu Wang 已提交
246 247 248 249 250
  SNodeList*       pProjectionList;
  SNode*           pLeft;
  SNode*           pRight;
  SNodeList*       pOrderByList;  // SOrderByExprNode
  SNode*           pLimit;
251
  char             stmtName[TSDB_TABLE_NAME_LEN];
252 253
} SSetOperator;

X
Xiaoyu Wang 已提交
254 255 256 257 258 259 260
typedef enum ESqlClause {
  SQL_CLAUSE_FROM = 1,
  SQL_CLAUSE_WHERE,
  SQL_CLAUSE_PARTITION_BY,
  SQL_CLAUSE_WINDOW,
  SQL_CLAUSE_GROUP_BY,
  SQL_CLAUSE_HAVING,
X
Xiaoyu Wang 已提交
261
  SQL_CLAUSE_DISTINCT,
X
Xiaoyu Wang 已提交
262 263 264 265
  SQL_CLAUSE_SELECT,
  SQL_CLAUSE_ORDER_BY
} ESqlClause;

X
Xiaoyu Wang 已提交
266 267 268 269 270 271 272 273 274
typedef enum {
  PAYLOAD_TYPE_KV = 0,
  PAYLOAD_TYPE_RAW = 1,
} EPayloadType;

typedef struct SVgDataBlocks {
  SVgroupInfo vg;
  int32_t     numOfTables;  // number of tables in current submit block
  uint32_t    size;
X
Xiaoyu Wang 已提交
275
  char*       pData;  // SMsgDesc + SSubmitReq + SSubmitBlk + ...
X
Xiaoyu Wang 已提交
276 277 278
} SVgDataBlocks;

typedef struct SVnodeModifOpStmt {
X
Xiaoyu Wang 已提交
279 280 281 282 283 284
  ENodeType   nodeType;
  ENodeType   sqlNodeType;
  SArray*     pDataBlocks;  // data block for each vgroup, SArray<SVgDataBlocks*>.
  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
X
Xiaoyu Wang 已提交
285 286
} SVnodeModifOpStmt;

287 288
typedef struct SExplainOptions {
  ENodeType type;
X
Xiaoyu Wang 已提交
289 290
  bool      verbose;
  double    ratio;
291 292 293
} SExplainOptions;

typedef struct SExplainStmt {
X
Xiaoyu Wang 已提交
294 295
  ENodeType        type;
  bool             analyze;
296
  SExplainOptions* pOptions;
X
Xiaoyu Wang 已提交
297
  SNode*           pQuery;
298 299
} SExplainStmt;

X
Xiaoyu Wang 已提交
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
typedef struct SCmdMsgInfo {
  int16_t msgType;
  SEpSet  epSet;
  void*   pMsg;
  int32_t msgLen;
  void*   pExtension;  // todo remove it soon
} SCmdMsgInfo;

typedef enum EQueryExecMode {
  QUERY_EXEC_MODE_LOCAL = 1,
  QUERY_EXEC_MODE_RPC,
  QUERY_EXEC_MODE_SCHEDULE,
  QUERY_EXEC_MODE_EMPTY_RESULT
} EQueryExecMode;

typedef struct SQuery {
  ENodeType      type;
  EQueryExecMode execMode;
  bool           haveResultSet;
  SNode*         pRoot;
  int32_t        numOfResCols;
  SSchema*       pResSchema;
  int8_t         precision;
  SCmdMsgInfo*   pCmdMsg;
  int32_t        msgType;
  SArray*        pDbList;
  SArray*        pTableList;
  bool           showRewrite;
  int32_t        placeholderNum;
X
Xiaoyu Wang 已提交
329
  SArray*        pPlaceholderValues;
330
  SNode*         pPrepareRoot;
X
Xiaoyu Wang 已提交
331 332
} SQuery;

X
Xiaoyu Wang 已提交
333 334 335
void nodesWalkSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeWalker walker, void* pContext);
void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewriter rewriter, void* pContext);

336 337 338
typedef enum ECollectColType { COLLECT_COL_TYPE_COL = 1, COLLECT_COL_TYPE_TAG, COLLECT_COL_TYPE_ALL } ECollectColType;
int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, ECollectColType type,
                            SNodeList** pCols);
X
Xiaoyu Wang 已提交
339 340

typedef bool (*FFuncClassifier)(int32_t funcId);
X
Xiaoyu Wang 已提交
341
int32_t nodesCollectFuncs(SSelectStmt* pSelect, ESqlClause clause, FFuncClassifier classifier, SNodeList** pFuncs);
X
Xiaoyu Wang 已提交
342

343 344
int32_t nodesCollectSpecialNodes(SSelectStmt* pSelect, ESqlClause clause, ENodeType type, SNodeList** pNodes);

345 346
bool nodesIsExprNode(const SNode* pNode);

X
Xiaoyu Wang 已提交
347
bool nodesIsUnaryOp(const SOperatorNode* pOp);
348 349 350
bool nodesIsArithmeticOp(const SOperatorNode* pOp);
bool nodesIsComparisonOp(const SOperatorNode* pOp);
bool nodesIsJsonOp(const SOperatorNode* pOp);
X
Xiaoyu Wang 已提交
351
bool nodesIsRegularOp(const SOperatorNode* pOp);
352 353 354

bool nodesIsTimeorderQuery(const SNode* pQuery);
bool nodesIsTimelineQuery(const SNode* pQuery);
X
Xiaoyu Wang 已提交
355

X
Xiaoyu Wang 已提交
356 357 358 359 360
void*   nodesGetValueFromNode(SValueNode* pNode);
int32_t nodesSetValueNodeValue(SValueNode* pNode, void* value);
char*   nodesGetStrValueFromNode(SValueNode* pNode);
char*   getFillModeString(EFillMode mode);
void    valueNodeToVariant(const SValueNode* pNode, SVariant* pVal);
361 362 363 364 365

#ifdef __cplusplus
}
#endif

D
dapan1121 已提交
366
#endif /*_TD_QUERY_NODES_H_*/