astCreateFuncs.c 28.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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

#include "astCreateFuncs.h"

19 20 21 22 23 24 25 26
#define CHECK_OUT_OF_MEM(p) \
  do { \
    if (NULL == (p)) { \
      pCxt->valid = false; \
      return NULL; \
    } \
  } while (0)

27 28 29 30 31 32 33 34
#define CHECK_RAW_EXPR_NODE(node) \
  do { \
    if (NULL == (node) || QUERY_NODE_RAW_EXPR != nodeType(node)) { \
      pCxt->valid = false; \
      return NULL; \
    } \
  } while (0)

35 36
SToken nil_token = { .type = TK_NIL, .n = 0, .z = NULL };

37 38 39
typedef SDatabaseOptions* (*FSetDatabaseOption)(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal);
static FSetDatabaseOption setDbOptionFuncs[DB_OPTION_MAX];

X
Xiaoyu Wang 已提交
40 41 42
typedef STableOptions* (*FSetTableOption)(SAstCreateContext* pCxt, STableOptions* pOptions, const SToken* pVal);
static FSetTableOption setTableOptionFuncs[TABLE_OPTION_MAX];

43 44 45 46
static SDatabaseOptions* setDbBlocks(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_TOTAL_BLOCKS || val > TSDB_MAX_TOTAL_BLOCKS) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
47
        "invalid db option totalBlocks: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_TOTAL_BLOCKS, TSDB_MAX_TOTAL_BLOCKS);
48 49 50 51 52 53 54 55 56 57 58
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->numOfBlocks = val;
  return pOptions;
}

static SDatabaseOptions* setDbCache(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
59
        "invalid db option cacheBlockSize: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE);
60 61 62 63 64 65 66 67 68 69 70
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->cacheBlockSize = val;
  return pOptions;
}

static SDatabaseOptions* setDbCacheLast(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_CACHE_LAST_ROW || val > TSDB_MAX_DB_CACHE_LAST_ROW) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
71
        "invalid db option cacheLast: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_DB_CACHE_LAST_ROW, TSDB_MAX_DB_CACHE_LAST_ROW);
72 73 74 75 76 77 78 79 80 81 82
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->cachelast = val;
  return pOptions;
}

static SDatabaseOptions* setDbComp(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_COMP_LEVEL || val > TSDB_MAX_COMP_LEVEL) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
83
        "invalid db option compression: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_COMP_LEVEL, TSDB_MAX_COMP_LEVEL);
84 85 86 87 88 89 90 91 92 93 94
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->compressionLevel = val;
  return pOptions;
}

static SDatabaseOptions* setDbDays(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DAYS_PER_FILE || val > TSDB_MAX_DAYS_PER_FILE) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
95
        "invalid db option daysPerFile: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_DAYS_PER_FILE, TSDB_MAX_DAYS_PER_FILE);
96 97 98 99 100 101 102 103 104 105 106
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->daysPerFile = val;
  return pOptions;
}

static SDatabaseOptions* setDbFsync(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_FSYNC_PERIOD || val > TSDB_MAX_FSYNC_PERIOD) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
107
        "invalid db option fsyncPeriod: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_FSYNC_PERIOD, TSDB_MAX_FSYNC_PERIOD);
108 109 110 111 112 113 114 115 116 117 118
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->fsyncPeriod = val;
  return pOptions;
}

static SDatabaseOptions* setDbMaxRows(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_MAX_ROW_FBLOCK || val > TSDB_MAX_MAX_ROW_FBLOCK) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
119
        "invalid db option maxRowsPerBlock: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_MAX_ROW_FBLOCK, TSDB_MAX_MAX_ROW_FBLOCK);
120 121 122 123 124 125 126 127 128 129 130
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->maxRowsPerBlock = val;
  return pOptions;
}

static SDatabaseOptions* setDbMinRows(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_MIN_ROW_FBLOCK || val > TSDB_MAX_MIN_ROW_FBLOCK) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
131
        "invalid db option minRowsPerBlock: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_MIN_ROW_FBLOCK, TSDB_MAX_MIN_ROW_FBLOCK);
132 133 134 135 136 137 138 139 140 141 142
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->minRowsPerBlock = val;
  return pOptions;
}

static SDatabaseOptions* setDbKeep(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_KEEP || val > TSDB_MAX_KEEP) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
143
        "invalid db option keep: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_KEEP, TSDB_MAX_KEEP);
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
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->keep = val;
  return pOptions;
}

static SDatabaseOptions* setDbPrecision(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  if (0 == strncmp(pVal->z, TSDB_TIME_PRECISION_MILLI_STR, pVal->n) && strlen(TSDB_TIME_PRECISION_MILLI_STR) == pVal->n) {
    pOptions->precision = TSDB_TIME_PRECISION_MILLI;
  } else if (0 == strncmp(pVal->z, TSDB_TIME_PRECISION_MICRO_STR, pVal->n) && strlen(TSDB_TIME_PRECISION_MICRO_STR) == pVal->n) {
    pOptions->precision = TSDB_TIME_PRECISION_MICRO;
  } else if (0 == strncmp(pVal->z, TSDB_TIME_PRECISION_NANO_STR, pVal->n) && strlen(TSDB_TIME_PRECISION_NANO_STR) == pVal->n) {
    pOptions->precision = TSDB_TIME_PRECISION_NANO;
  } else {
    char tmp[10];
    strncpy(tmp, pVal->z, pVal->n);
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid db option precision: %s", tmp);
    pCxt->valid = false;
  }
  return pOptions;
}

static SDatabaseOptions* setDbQuorum(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_QUORUM_OPTION || val > TSDB_MAX_DB_QUORUM_OPTION) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
171
        "invalid db option quorum: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_DB_QUORUM_OPTION, TSDB_MAX_DB_QUORUM_OPTION);
172 173 174 175 176 177 178 179 180 181 182
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->quorum = val;
  return pOptions;
}

static SDatabaseOptions* setDbReplica(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_REPLICA_OPTION || val > TSDB_MAX_DB_REPLICA_OPTION) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
183
        "invalid db option replications: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_DB_REPLICA_OPTION, TSDB_MAX_DB_REPLICA_OPTION);
184 185 186 187 188 189 190 191 192 193 194
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->replica = val;
  return pOptions;
}

static SDatabaseOptions* setDbTtl(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_TTL_OPTION) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
195
        "invalid db option ttl: %"PRId64", should be greater than or equal to %d", val, TSDB_MIN_DB_TTL_OPTION);
196 197 198 199 200 201 202 203 204 205
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->ttl = val;
  return pOptions;
}

static SDatabaseOptions* setDbWal(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_WAL_LEVEL || val > TSDB_MAX_WAL_LEVEL) {
H
Haojun Liao 已提交
206
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid db option walLevel: %"PRId64", only 1-2 allowed", val);
207 208 209 210 211 212 213 214 215 216 217
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->walLevel = val;
  return pOptions;
}

static SDatabaseOptions* setDbVgroups(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_VNODES_PER_DB || val > TSDB_MAX_VNODES_PER_DB) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
218
        "invalid db option vgroups: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_VNODES_PER_DB, TSDB_MAX_VNODES_PER_DB);
219 220 221 222 223 224 225 226 227 228
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->numOfVgroups = val;
  return pOptions;
}

static SDatabaseOptions* setDbSingleStable(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_SINGLE_STABLE_OPTION || val > TSDB_MAX_DB_SINGLE_STABLE_OPTION) {
H
Haojun Liao 已提交
229
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid db option singleStable: %"PRId64", only 0-1 allowed", val);
230 231 232 233 234 235 236 237 238 239
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->singleStable = val;
  return pOptions;
}

static SDatabaseOptions* setDbStreamMode(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_STREAM_MODE_OPTION || val > TSDB_MAX_DB_STREAM_MODE_OPTION) {
H
Haojun Liao 已提交
240
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid db option streamMode: %"PRId64", only 0-1 allowed", val);
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
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->streamMode = val;
  return pOptions;
}

static void initSetDatabaseOptionFp() {
  setDbOptionFuncs[DB_OPTION_BLOCKS] = setDbBlocks;
  setDbOptionFuncs[DB_OPTION_CACHE] = setDbCache;
  setDbOptionFuncs[DB_OPTION_CACHELAST] = setDbCacheLast;
  setDbOptionFuncs[DB_OPTION_COMP] = setDbComp;
  setDbOptionFuncs[DB_OPTION_DAYS] = setDbDays;
  setDbOptionFuncs[DB_OPTION_FSYNC] = setDbFsync;
  setDbOptionFuncs[DB_OPTION_MAXROWS] = setDbMaxRows;
  setDbOptionFuncs[DB_OPTION_MINROWS] = setDbMinRows;
  setDbOptionFuncs[DB_OPTION_KEEP] = setDbKeep;
  setDbOptionFuncs[DB_OPTION_PRECISION] = setDbPrecision;
  setDbOptionFuncs[DB_OPTION_QUORUM] = setDbQuorum;
  setDbOptionFuncs[DB_OPTION_REPLICA] = setDbReplica;
  setDbOptionFuncs[DB_OPTION_TTL] = setDbTtl;
  setDbOptionFuncs[DB_OPTION_WAL] = setDbWal;
  setDbOptionFuncs[DB_OPTION_VGROUPS] = setDbVgroups;
  setDbOptionFuncs[DB_OPTION_SINGLESTABLE] = setDbSingleStable;
  setDbOptionFuncs[DB_OPTION_STREAMMODE] = setDbStreamMode;
}

X
Xiaoyu Wang 已提交
268 269 270 271
static STableOptions* setTableKeep(SAstCreateContext* pCxt, STableOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_KEEP || val > TSDB_MAX_KEEP) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
272
        "invalid table option keep: %"PRId64" valid range: [%d, %d]", val, TSDB_MIN_KEEP, TSDB_MAX_KEEP);
X
Xiaoyu Wang 已提交
273 274 275 276 277 278 279 280 281 282 283
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->keep = val;
  return pOptions;
}

static STableOptions* setTableTtl(SAstCreateContext* pCxt, STableOptions* pOptions, const SToken* pVal) {
  int64_t val = strtol(pVal->z, NULL, 10);
  if (val < TSDB_MIN_DB_TTL_OPTION) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
284
        "invalid table option ttl: %"PRId64", should be greater than or equal to %d", val, TSDB_MIN_DB_TTL_OPTION);
X
Xiaoyu Wang 已提交
285 286 287 288 289 290 291 292 293 294
    pCxt->valid = false;
    return pOptions;
  }
  pOptions->ttl = val;
  return pOptions;
}

static STableOptions* setTableComment(SAstCreateContext* pCxt, STableOptions* pOptions, const SToken* pVal) {
  if (pVal->n >= sizeof(pOptions->comments)) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen,
H
Haojun Liao 已提交
295
        "invalid table option comment, length cannot exceed %d", (int32_t)(sizeof(pOptions->comments) - 1));
X
Xiaoyu Wang 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308
    pCxt->valid = false;
    return pOptions;
  }
  strncpy(pOptions->comments, pVal->z, pVal->n);
  return pOptions;
}

static void initSetTableOptionFp() {
  setTableOptionFuncs[TABLE_OPTION_KEEP] = setTableKeep;
  setTableOptionFuncs[TABLE_OPTION_TTL] = setTableTtl;
  setTableOptionFuncs[TABLE_OPTION_COMMENT] = setTableComment;
}

309 310 311 312 313 314
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
  pCxt->pQueryCxt = pParseCxt;
  pCxt->notSupport = false;
  pCxt->valid = true;
  pCxt->pRootNode = NULL;
  initSetDatabaseOptionFp();
X
Xiaoyu Wang 已提交
315
  initSetTableOptionFp();
316 317
}

318 319 320 321 322 323 324
static bool checkDbName(SAstCreateContext* pCxt, const SToken* pDbName) {
  if (NULL == pDbName) {
    return true;
  }
  pCxt->valid = pDbName->n < TSDB_DB_NAME_LEN ? true : false;
  return pCxt->valid;
}
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static bool checkTableName(SAstCreateContext* pCxt, const SToken* pTableName) {
  if (NULL == pTableName) {
    return true;
  }
  pCxt->valid = pTableName->n < TSDB_TABLE_NAME_LEN ? true : false;
  return pCxt->valid;
}

static bool checkColumnName(SAstCreateContext* pCxt, const SToken* pColumnName) {
  if (NULL == pColumnName) {
    return true;
  }
  pCxt->valid = pColumnName->n < TSDB_COL_NAME_LEN ? true : false;
  return pCxt->valid;
}

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
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
  SRawExprNode* target = (SRawExprNode*)nodesMakeNode(QUERY_NODE_RAW_EXPR);
  CHECK_OUT_OF_MEM(target);
  target->p = pToken->z;
  target->n = pToken->n;
  target->pNode = pNode;
  return (SNode*)target;
}

SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
  SRawExprNode* target = (SRawExprNode*)nodesMakeNode(QUERY_NODE_RAW_EXPR);
  CHECK_OUT_OF_MEM(target);
  target->p = pStart->z;
  target->n = (pEnd->z + pEnd->n) - pStart->z;
  target->pNode = pNode;
  return (SNode*)target;
}

SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
  CHECK_RAW_EXPR_NODE(pNode);
  SNode* tmp = ((SRawExprNode*)pNode)->pNode;
  tfree(pNode);
  return tmp;
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
368 369 370 371
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
    pCxt->valid = false;
    return nil_token;
  }
372 373 374 375 376
  SRawExprNode* target = (SRawExprNode*)pNode;
  SToken t = { .type = 0, .z = target->p, .n = target->n};
  return t;
}

377 378 379
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
X
Xiaoyu Wang 已提交
380 381 382 383
  if (TSDB_CODE_SUCCESS != nodesListAppend(list, pNode)) {
    pCxt->valid = false;
  }
  return list;
384 385 386
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
387 388 389 390
  if (TSDB_CODE_SUCCESS != nodesListAppend(pList, pNode)) {
    pCxt->valid = false;
  }
  return pList;
391 392
}

393 394
SNode* createColumnNode(SAstCreateContext* pCxt, const SToken* pTableAlias, const SToken* pColumnName) {
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
395 396 397 398
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
399 400
  if (NULL != pTableAlias) {
    strncpy(col->tableAlias, pTableAlias->z, pTableAlias->n);
401 402
  }
  strncpy(col->colName, pColumnName->z, pColumnName->n);
403
  return (SNode*)col;
404
}
405

406 407 408
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
409 410 411
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->node.resType.type = dataType;
412 413 414 415
  val->node.resType.bytes = tDataTypes[dataType].bytes;
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
416 417 418 419 420 421
  return (SNode*)val;
}

SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
422 423 424 425 426 427
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
428
  return (SNode*)val;
429 430
}

431 432 433 434 435
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  nodesListAppend(cond->pParameterList, pParam1);
  nodesListAppend(cond->pParameterList, pParam2);
  return (SNode*)cond;
}

SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
  SOperatorNode* op = (SOperatorNode*)nodesMakeNode(QUERY_NODE_OPERATOR);
  CHECK_OUT_OF_MEM(op);
  op->opType = type;
  op->pLeft = pLeft;
  op->pRight = pRight;
  return (SNode*)op;
}

SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
      createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft), createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pExpr, pRight));
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR,
      createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft), createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pExpr, pRight));
}

SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strncpy(func->functionName, pFuncName->z, pFuncName->n);
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
473 474
}

475 476 477 478 479 480 481
SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const SToken* pTableName, const SToken* pTableAlias) {
  if (!checkDbName(pCxt, pDbName) || !checkTableName(pCxt, pTableName)) {
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
482
    strncpy(realTable->table.dbName, pDbName->z, pDbName->n);
483 484 485 486 487 488 489
  } else {
    strcpy(realTable->table.dbName, pCxt->pQueryCxt->db);
  }
  if (NULL != pTableAlias && TK_NIL != pTableAlias->type) {
    strncpy(realTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
  } else {
    strncpy(realTable->table.tableAlias, pTableName->z, pTableName->n);
490 491
  }
  strncpy(realTable->table.tableName, pTableName->z, pTableName->n);
492
  return (SNode*)realTable;
493
}
494

495 496 497 498
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
499 500 501
  if (NULL != pTableAlias && TK_NIL != pTableAlias->type) {
    strncpy(tempTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
  }
502
  return (SNode*)tempTable;
503 504
}

505 506 507 508
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
509 510
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
511
  joinTable->pOnCond = pJoinCond;
512
  return (SNode*)joinTable;
513
}
514

515
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
516 517 518 519
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
  // limitNode->limit = limit;
  // limitNode->offset = offset;
520
  return (SNode*)limitNode;
521 522
}

523 524 525
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
526
  orderByExpr->pExpr = pExpr;
527
  orderByExpr->order = order;
528 529 530
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
531
  orderByExpr->nullOrder = nullOrder;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  return (SNode*)orderByExpr;
}

SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, const SToken* pVal) {
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
  session->pCol = pCol;
  // session->gap = getInteger(pVal);
  return (SNode*)session;
}

SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pCol) {
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
  state->pCol = pCol;
  return (SNode*)state;
}

SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding, SNode* pFill) {
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
  interval->pInterval = pInterval;
  interval->pOffset = pOffset;
  interval->pSliding = pSliding;
  interval->pFill = pFill;
  return (SNode*)interval;
}

SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
  return (SNode*)fill;
566
}
567

568 569 570 571 572 573 574 575 576
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
  SGroupingSetNode* groupingSet = (SGroupingSetNode*)nodesMakeNode(QUERY_NODE_GROUPING_SET);
  CHECK_OUT_OF_MEM(groupingSet);
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
  groupingSet->pParameterList = nodesMakeList();
  nodesListAppend(groupingSet->pParameterList, pNode);
  return (SNode*)groupingSet;
}

577
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
X
Xiaoyu Wang 已提交
578 579 580 581 582
  if (NULL == pNode || !pCxt->valid) {
    return pNode;
  }
  uint32_t maxLen = sizeof(((SExprNode*)pNode)->aliasName);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, pAlias->n > maxLen ? maxLen : pAlias->n);
583
  return pNode;
584 585
}

586 587
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
588
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
589 590 591
  }
  return pStmt;
}
592

593 594
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
595
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
596 597
  }
  return pStmt;
598 599
}

600 601
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
602
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
603 604 605
  }
  return pStmt;
}
606

607 608
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
609
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
610 611
  }
  return pStmt;
612 613
}

614 615
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
616
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
617 618 619 620 621 622
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
623
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
624 625 626
  }
  return pStmt;
}
627

628 629
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
630
    ((SSelectStmt*)pStmt)->pSlimit = pSlimit;
631 632
  }
  return pStmt;
633 634
}

635 636
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
637
    ((SSelectStmt*)pStmt)->pLimit = pLimit;
638
  }
639
  return pStmt;
640 641 642 643
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
644
  CHECK_OUT_OF_MEM(select);
645
  select->isDistinct = isDistinct;
646 647 648
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
  return (SNode*)select;
649 650 651
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
652 653 654
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
655 656 657
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
  return (SNode*)setOp;
658 659
}

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
SDatabaseOptions* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
  SDatabaseOptions* pOptions = calloc(1, sizeof(SDatabaseOptions));
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->numOfBlocks = TSDB_DEFAULT_TOTAL_BLOCKS;
  pOptions->cacheBlockSize = TSDB_DEFAULT_CACHE_BLOCK_SIZE;
  pOptions->cachelast = TSDB_DEFAULT_CACHE_LAST_ROW;
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAX_ROW_FBLOCK;
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MIN_ROW_FBLOCK;
  pOptions->keep = TSDB_DEFAULT_KEEP;
  pOptions->precision = TSDB_TIME_PRECISION_MILLI;
  pOptions->quorum = TSDB_DEFAULT_DB_QUORUM_OPTION;
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA_OPTION;
  pOptions->ttl = TSDB_DEFAULT_DB_TTL_OPTION;
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE_OPTION;
  pOptions->streamMode = TSDB_DEFAULT_DB_STREAM_MODE_OPTION;
  return pOptions;
}

SDatabaseOptions* setDatabaseOption(SAstCreateContext* pCxt, SDatabaseOptions* pOptions, EDatabaseOptionType type, const SToken* pVal) {
  return setDbOptionFuncs[type](pCxt, pOptions, pVal);
}

SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pDbName, SDatabaseOptions* pOptions) {
  if (!checkDbName(pCxt, pDbName)) {
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->ignoreExists = ignoreExists;
  pStmt->options = *pOptions;
  return (SNode*)pStmt;
697
}
X
Xiaoyu Wang 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

STableOptions* createDefaultTableOptions(SAstCreateContext* pCxt) {
  STableOptions* pOptions = calloc(1, sizeof(STableOptions));
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->keep = TSDB_DEFAULT_KEEP;
  pOptions->ttl = TSDB_DEFAULT_DB_TTL_OPTION;
  return pOptions;
}

STableOptions* setTableOption(SAstCreateContext* pCxt, STableOptions* pOptions, ETableOptionType type, const SToken* pVal) {
  return setTableOptionFuncs[type](pCxt, pOptions, pVal);
}

SNode* createColumnDefNode(SAstCreateContext* pCxt, const SToken* pColName, SDataType dataType, const SToken* pComment) {
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
  strncpy(pCol->colName, pColName->z, pColName->n);
  pCol->dataType = dataType;
  if (NULL != pComment) {
717
    strncpy(pCol->comments, pComment->z, pComment->n);
X
Xiaoyu Wang 已提交
718 719 720 721
  }
  return (SNode*)pCol;
}

722
SDataType createDataType(uint8_t type) {
723
  SDataType dt = { .type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes };
724 725 726 727
  return dt;
}

SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
728
  SDataType dt = { .type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes };
729 730 731
  return dt;
}

X
Xiaoyu Wang 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744
SNode* createCreateTableStmt(SAstCreateContext* pCxt,
    bool ignoreExists, const STokenPair* pFullTableName, SNodeList* pCols, STableOptions* pOptions) {
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NIL != pFullTableName->first.type) {
    strncpy(pStmt->dbName, pFullTableName->first.z, pFullTableName->first.n);
  }
  strncpy(pStmt->tableName, pFullTableName->second.z, pFullTableName->second.n);
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
  pStmt->options = *pOptions;
  return (SNode*)pStmt;
}
745 746 747 748 749 750 751 752 753

SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  return (SNode*)pStmt;
}

SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
754
  SNode* pStmt = nodesMakeNode(type);;
755 756 757
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}