nodesMsgFuncs.c 113.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 "nodesUtil.h"
#include "plannodes.h"
18
#include "tdatablock.h"
19 20

#define NODES_MSG_DEFAULT_LEN 1024
X
Xiaoyu Wang 已提交
21
#define TLV_TYPE_ARRAY_ELEM   0
22

23
#define tlvForEach(pDecoder, pTlv, code) \
24
  while (TSDB_CODE_SUCCESS == code && TSDB_CODE_SUCCESS == (code = tlvGetNextTlv(pDecoder, &pTlv)) && NULL != pTlv)
25

26 27
typedef struct STlv {
  int16_t type;
28
  int32_t len;
29
  char    value[0];
X
Xiaoyu Wang 已提交
30
} __attribute__((packed, aligned(1))) STlv;
31 32 33 34 35

typedef struct STlvEncoder {
  int32_t allocSize;
  int32_t offset;
  char*   pBuf;
36
  int32_t tlvCount;
37 38 39 40 41 42 43 44 45 46
} STlvEncoder;

typedef struct STlvDecoder {
  int32_t     bufSize;
  int32_t     offset;
  const char* pBuf;
} STlvDecoder;

typedef int32_t (*FToMsg)(const void* pObj, STlvEncoder* pEncoder);
typedef int32_t (*FToObject)(STlvDecoder* pDecoder, void* pObj);
47
typedef void* (*FMakeObject)(int16_t type);
48 49 50 51
typedef int32_t (*FSetObject)(STlv* pTlv, void* pObj);

static int32_t nodeToMsg(const void* pObj, STlvEncoder* pEncoder);
static int32_t nodeListToMsg(const void* pObj, STlvEncoder* pEncoder);
52 53 54 55
static int32_t msgToNode(STlvDecoder* pDecoder, void** pObj);
static int32_t msgToNodeFromTlv(STlv* pTlv, void** pObj);
static int32_t msgToNodeList(STlvDecoder* pDecoder, void** pObj);
static int32_t msgToNodeListFromTlv(STlv* pTlv, void** pObj);
56 57 58 59

static int32_t initTlvEncoder(STlvEncoder* pEncoder) {
  pEncoder->allocSize = NODES_MSG_DEFAULT_LEN;
  pEncoder->offset = 0;
60
  pEncoder->tlvCount = 0;
61 62 63 64 65 66 67 68 69 70
  pEncoder->pBuf = taosMemoryMalloc(pEncoder->allocSize);
  return NULL == pEncoder->pBuf ? TSDB_CODE_OUT_OF_MEMORY : TSDB_CODE_SUCCESS;
}

static void clearTlvEncoder(STlvEncoder* pEncoder) { taosMemoryFree(pEncoder->pBuf); }

static void endTlvEncode(STlvEncoder* pEncoder, char** pMsg, int32_t* pLen) {
  *pMsg = pEncoder->pBuf;
  pEncoder->pBuf = NULL;
  *pLen = pEncoder->offset;
71
  // nodesWarn("encode tlv count = %d, tl size = %d", pEncoder->tlvCount, sizeof(STlv) * pEncoder->tlvCount);
72 73
}

74
static int32_t tlvEncodeImpl(STlvEncoder* pEncoder, int16_t type, const void* pValue, int32_t len) {
75 76
  int32_t tlvLen = sizeof(STlv) + len;
  if (pEncoder->offset + tlvLen > pEncoder->allocSize) {
77 78 79 80 81 82 83 84 85 86 87
    void* pNewBuf = taosMemoryRealloc(pEncoder->pBuf, pEncoder->allocSize * 2);
    if (NULL == pNewBuf) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pEncoder->pBuf = pNewBuf;
    pEncoder->allocSize = pEncoder->allocSize * 2;
  }
  STlv* pTlv = (STlv*)(pEncoder->pBuf + pEncoder->offset);
  pTlv->type = type;
  pTlv->len = len;
  memcpy(pTlv->value, pValue, len);
88
  pEncoder->offset += tlvLen;
89
  ++(pEncoder->tlvCount);
90 91 92
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106
static int32_t tlvEncodeValueImpl(STlvEncoder* pEncoder, const void* pValue, int32_t len) {
  if (pEncoder->offset + len > pEncoder->allocSize) {
    void* pNewBuf = taosMemoryRealloc(pEncoder->pBuf, pEncoder->allocSize * 2);
    if (NULL == pNewBuf) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pEncoder->pBuf = pNewBuf;
    pEncoder->allocSize = pEncoder->allocSize * 2;
  }
  memcpy(pEncoder->pBuf + pEncoder->offset, pValue, len);
  pEncoder->offset += len;
  return TSDB_CODE_SUCCESS;
}

107 108 109 110
static int32_t tlvEncodeI8(STlvEncoder* pEncoder, int16_t type, int8_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
111 112 113 114
static int32_t tlvEncodeValueI8(STlvEncoder* pEncoder, int8_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

115 116 117 118
static int32_t tlvEncodeI16(STlvEncoder* pEncoder, int16_t type, int16_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
119 120 121 122
static int32_t tlvEncodeValueI16(STlvEncoder* pEncoder, int16_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

123 124 125 126
static int32_t tlvEncodeI32(STlvEncoder* pEncoder, int16_t type, int32_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
127 128 129 130
static int32_t tlvEncodeValueI32(STlvEncoder* pEncoder, int32_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

131 132 133 134
static int32_t tlvEncodeI64(STlvEncoder* pEncoder, int16_t type, int64_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
135 136 137 138
static int32_t tlvEncodeValueI64(STlvEncoder* pEncoder, int64_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

139 140 141 142
static int32_t tlvEncodeU8(STlvEncoder* pEncoder, int16_t type, uint8_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
143 144 145 146
static int32_t tlvEncodeValueU8(STlvEncoder* pEncoder, uint8_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

147 148 149 150
static int32_t tlvEncodeU16(STlvEncoder* pEncoder, int16_t type, uint16_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
151 152 153 154
static int32_t tlvEncodeValueU16(STlvEncoder* pEncoder, uint16_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

155 156 157 158
static int32_t tlvEncodeU64(STlvEncoder* pEncoder, int16_t type, uint64_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
159 160 161 162
static int32_t tlvEncodeValueU64(STlvEncoder* pEncoder, uint64_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

163 164 165 166
static int32_t tlvEncodeDouble(STlvEncoder* pEncoder, int16_t type, double value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
167 168 169 170
static int32_t tlvEncodeValueDouble(STlvEncoder* pEncoder, double value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

171 172 173 174
static int32_t tlvEncodeEnum(STlvEncoder* pEncoder, int16_t type, int32_t value) {
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
175 176 177 178 179
static int32_t tlvEncodeValueEnum(STlvEncoder* pEncoder, int32_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

static int32_t tlvEncodeBool(STlvEncoder* pEncoder, int16_t type, int8_t value) {
180 181 182
  return tlvEncodeImpl(pEncoder, type, &value, sizeof(value));
}

X
Xiaoyu Wang 已提交
183 184 185 186
static int32_t tlvEncodeValueBool(STlvEncoder* pEncoder, int8_t value) {
  return tlvEncodeValueImpl(pEncoder, &value, sizeof(value));
}

187
static int32_t tlvEncodeCStr(STlvEncoder* pEncoder, int16_t type, const char* pValue) {
188 189 190
  if (NULL == pValue) {
    return TSDB_CODE_SUCCESS;
  }
191 192 193
  return tlvEncodeImpl(pEncoder, type, pValue, strlen(pValue));
}

X
Xiaoyu Wang 已提交
194 195 196 197 198 199 200 201 202
static int32_t tlvEncodeValueCStr(STlvEncoder* pEncoder, const char* pValue) {
  int16_t len = strlen(pValue);
  int32_t code = tlvEncodeValueImpl(pEncoder, &len, sizeof(len));
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueImpl(pEncoder, pValue, len);
  }
  return code;
}

203 204 205 206
static int32_t tlvEncodeBinary(STlvEncoder* pEncoder, int16_t type, const void* pValue, int32_t len) {
  return tlvEncodeImpl(pEncoder, type, pValue, len);
}

207 208 209 210 211 212 213 214 215 216 217 218 219
static int32_t tlvEncodeObj(STlvEncoder* pEncoder, int16_t type, FToMsg func, const void* pObj) {
  if (NULL == pObj) {
    return TSDB_CODE_SUCCESS;
  }

  int32_t start = pEncoder->offset;
  pEncoder->offset += sizeof(STlv);
  int32_t code = func(pObj, pEncoder);
  if (TSDB_CODE_SUCCESS == code) {
    STlv* pTlv = (STlv*)(pEncoder->pBuf + start);
    pTlv->type = type;
    pTlv->len = pEncoder->offset - start - sizeof(STlv);
  }
X
Xiaoyu Wang 已提交
220
  ++(pEncoder->tlvCount);
221 222 223
  return code;
}

224 225 226 227 228 229 230
static int32_t tlvEncodeObjArray(STlvEncoder* pEncoder, int16_t type, FToMsg func, const void* pArray, int32_t itemSize,
                                 int32_t num) {
  int32_t code = TSDB_CODE_SUCCESS;
  if (num > 0) {
    int32_t start = pEncoder->offset;
    pEncoder->offset += sizeof(STlv);
    for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < num; ++i) {
X
Xiaoyu Wang 已提交
231
      code = tlvEncodeObj(pEncoder, TLV_TYPE_ARRAY_ELEM, func, (const char*)pArray + i * itemSize);
232 233 234 235 236 237 238 239 240 241
    }
    if (TSDB_CODE_SUCCESS == code) {
      STlv* pTlv = (STlv*)(pEncoder->pBuf + start);
      pTlv->type = type;
      pTlv->len = pEncoder->offset - start - sizeof(STlv);
    }
  }
  return code;
}

X
Xiaoyu Wang 已提交
242 243 244 245 246 247 248 249 250
static int32_t tlvEncodeValueArray(STlvEncoder* pEncoder, FToMsg func, const void* pArray, int32_t itemSize,
                                   int32_t num) {
  int32_t code = tlvEncodeValueI32(pEncoder, num);
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < num; ++i) {
    code = func((const char*)pArray + i * itemSize, pEncoder);
  }
  return code;
}

251
static int32_t tlvGetNextTlv(STlvDecoder* pDecoder, STlv** pTlv) {
252 253 254 255 256
  if (pDecoder->offset == pDecoder->bufSize) {
    *pTlv = NULL;
    return TSDB_CODE_SUCCESS;
  }

257 258 259 260
  *pTlv = (STlv*)(pDecoder->pBuf + pDecoder->offset);
  if ((*pTlv)->len + pDecoder->offset > pDecoder->bufSize) {
    return TSDB_CODE_FAILED;
  }
261
  pDecoder->offset += sizeof(STlv) + (*pTlv)->len;
262 263 264
  return TSDB_CODE_SUCCESS;
}

265 266
static bool tlvDecodeEnd(STlvDecoder* pDecoder) { return pDecoder->offset == pDecoder->bufSize; }

267
static int32_t tlvDecodeImpl(STlv* pTlv, void* pValue, int32_t len) {
268 269 270 271 272 273 274
  if (pTlv->len != len) {
    return TSDB_CODE_FAILED;
  }
  memcpy(pValue, pTlv->value, len);
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
275 276 277 278 279 280 281 282 283
static int32_t tlvDecodeValueImpl(STlvDecoder* pDecoder, void* pValue, int32_t len) {
  if (pDecoder->offset + len > pDecoder->bufSize) {
    return TSDB_CODE_FAILED;
  }
  memcpy(pValue, pDecoder->pBuf + pDecoder->offset, len);
  pDecoder->offset += len;
  return TSDB_CODE_SUCCESS;
}

284 285
static int32_t tlvDecodeI8(STlv* pTlv, int8_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
286 287 288 289
static int32_t tlvDecodeValueI8(STlvDecoder* pDecoder, int8_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

290 291
static int32_t tlvDecodeI16(STlv* pTlv, int16_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
292 293 294 295
static int32_t tlvDecodeValueI16(STlvDecoder* pDecoder, int16_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

296 297
static int32_t tlvDecodeI32(STlv* pTlv, int32_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
298 299 300 301
static int32_t tlvDecodeValueI32(STlvDecoder* pDecoder, int32_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

302 303
static int32_t tlvDecodeI64(STlv* pTlv, int64_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
304 305 306 307
static int32_t tlvDecodeValueI64(STlvDecoder* pDecoder, int64_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

308 309
static int32_t tlvDecodeU8(STlv* pTlv, uint8_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
310 311 312 313
static int32_t tlvDecodeValueU8(STlvDecoder* pDecoder, uint8_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

314 315
static int32_t tlvDecodeU16(STlv* pTlv, uint16_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
316 317 318 319
static int32_t tlvDecodeValueU16(STlvDecoder* pDecoder, uint16_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

320 321
static int32_t tlvDecodeU64(STlv* pTlv, uint64_t* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
322 323 324 325
static int32_t tlvDecodeValueU64(STlvDecoder* pDecoder, uint64_t* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}

326 327
static int32_t tlvDecodeDouble(STlv* pTlv, double* pValue) { return tlvDecodeImpl(pTlv, pValue, sizeof(*pValue)); }

X
Xiaoyu Wang 已提交
328 329 330
static int32_t tlvDecodeValueDouble(STlvDecoder* pDecoder, double* pValue) {
  return tlvDecodeValueImpl(pDecoder, pValue, sizeof(*pValue));
}
331

X
Xiaoyu Wang 已提交
332
static int32_t convertIntegerType(int32_t value, void* pValue, int16_t len) {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  switch (len) {
    case 1:
      *(int8_t*)pValue = value;
      break;
    case 2:
      *(int16_t*)pValue = value;
      break;
    case 4:
      *(int32_t*)pValue = value;
      break;
    default:
      return TSDB_CODE_FAILED;
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
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
static int32_t tlvDecodeBool(STlv* pTlv, bool* pValue) {
  int8_t  value = 0;
  int32_t code = tlvDecodeI8(pTlv, &value);
  if (TSDB_CODE_SUCCESS == code) {
    code = convertIntegerType(value, pValue, sizeof(bool));
  }
  return code;
}

static int32_t tlvDecodeValueBool(STlvDecoder* pDecoder, bool* pValue) {
  int8_t  value = 0;
  int32_t code = tlvDecodeValueI8(pDecoder, &value);
  if (TSDB_CODE_SUCCESS == code) {
    code = convertIntegerType(value, pValue, sizeof(bool));
  }
  return code;
}

static int32_t tlvDecodeEnum(STlv* pTlv, void* pValue, int16_t len) {
  int32_t value = 0;
  int32_t code = tlvDecodeI32(pTlv, &value);
  if (TSDB_CODE_SUCCESS == code) {
    code = convertIntegerType(value, pValue, len);
  }
  return code;
}

static int32_t tlvDecodeValueEnum(STlvDecoder* pDecoder, void* pValue, int16_t len) {
  int32_t value = 0;
  int32_t code = tlvDecodeValueI32(pDecoder, &value);
  if (TSDB_CODE_SUCCESS == code) {
    code = convertIntegerType(value, pValue, len);
  }
  return code;
}

385 386 387 388 389
static int32_t tlvDecodeCStr(STlv* pTlv, char* pValue) {
  memcpy(pValue, pTlv->value, pTlv->len);
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
390 391 392 393 394 395 396 397 398
static int32_t tlvDecodeValueCStr(STlvDecoder* pDecoder, char* pValue) {
  int16_t len = 0;
  int32_t code = tlvDecodeValueI16(pDecoder, &len);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueImpl(pDecoder, pValue, len);
  }
  return code;
}

399 400 401 402 403
static int32_t tlvDecodeCStrP(STlv* pTlv, char** pValue) {
  *pValue = strndup(pTlv->value, pTlv->len);
  return NULL == *pValue ? TSDB_CODE_OUT_OF_MEMORY : TSDB_CODE_SUCCESS;
}

404 405 406 407 408 409 410 411 412
static int32_t tlvDecodeDynBinary(STlv* pTlv, void** pValue) {
  *pValue = taosMemoryMalloc(pTlv->len);
  if (NULL == *pValue) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  memcpy(*pValue, pTlv->value, pTlv->len);
  return TSDB_CODE_SUCCESS;
}

413 414 415 416 417
static int32_t tlvDecodeBinary(STlv* pTlv, void* pValue) {
  memcpy(pValue, pTlv->value, pTlv->len);
  return TSDB_CODE_SUCCESS;
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431
static int32_t tlvDecodeObjFromTlv(STlv* pTlv, FToObject func, void* pObj) {
  STlvDecoder decoder = {.bufSize = pTlv->len, .offset = 0, .pBuf = pTlv->value};
  return func(&decoder, pObj);
}

static int32_t tlvDecodeObj(STlvDecoder* pDecoder, FToObject func, void* pObj) {
  STlv*   pTlv = NULL;
  int32_t code = tlvGetNextTlv(pDecoder, &pTlv);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeObjFromTlv(pTlv, func, pObj);
  }
  return code;
}

432 433 434 435
static int32_t tlvDecodeObjArray(STlvDecoder* pDecoder, FToObject func, void* pArray, int32_t itemSize) {
  int32_t code = TSDB_CODE_SUCCESS;
  int32_t i = 0;
  STlv*   pTlv = NULL;
436
  tlvForEach(pDecoder, pTlv, code) { code = tlvDecodeObjFromTlv(pTlv, func, (char*)pArray + itemSize * i++); }
437 438 439
  return code;
}

X
Xiaoyu Wang 已提交
440 441 442 443 444 445 446 447 448
static int32_t tlvDecodeValueArray(STlvDecoder* pDecoder, FToObject func, void* pArray, int32_t itemSize,
                                   int32_t* pNum) {
  int32_t code = tlvDecodeValueI32(pDecoder, pNum);
  for (size_t i = 0; TSDB_CODE_SUCCESS == code && i < *pNum; ++i) {
    code = func(pDecoder, (char*)pArray + i * itemSize);
  }
  return code;
}

449 450 451 452 453
static int32_t tlvDecodeObjArrayFromTlv(STlv* pTlv, FToObject func, void* pArray, int32_t itemSize) {
  STlvDecoder decoder = {.bufSize = pTlv->len, .offset = 0, .pBuf = pTlv->value};
  return tlvDecodeObjArray(&decoder, func, pArray, itemSize);
}

454 455 456 457
static int32_t tlvDecodeDynObjFromTlv(STlv* pTlv, FMakeObject makeFunc, FToObject toFunc, void** pObj) {
  *pObj = makeFunc(pTlv->type);
  if (NULL == *pObj) {
    return TSDB_CODE_OUT_OF_MEMORY;
458
  }
459
  return tlvDecodeObjFromTlv(pTlv, toFunc, *pObj);
460 461
}

462 463
static int32_t tlvDecodeDynObj(STlvDecoder* pDecoder, FMakeObject makeFunc, FToObject toFunc, void** pObj) {
  STlv*   pTlv = NULL;
464
  int32_t code = tlvGetNextTlv(pDecoder, &pTlv);
465 466
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeDynObjFromTlv(pTlv, makeFunc, toFunc, pObj);
467 468 469 470 471 472
  }
  return code;
}

enum { DATA_TYPE_CODE_TYPE = 1, DATA_TYPE_CODE_PRECISION, DATA_TYPE_CODE_SCALE, DATA_TYPE_CODE_BYTES };

X
Xiaoyu Wang 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static int32_t dataTypeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataType* pNode = (const SDataType*)pObj;

  int32_t code = tlvEncodeValueI8(pEncoder, pNode->type);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU8(pEncoder, pNode->precision);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU8(pEncoder, pNode->scale);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->bytes);
  }

  return code;
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
static int32_t dataTypeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataType* pNode = (const SDataType*)pObj;

  int32_t code = tlvEncodeI8(pEncoder, DATA_TYPE_CODE_TYPE, pNode->type);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeU8(pEncoder, DATA_TYPE_CODE_PRECISION, pNode->precision);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeU8(pEncoder, DATA_TYPE_CODE_SCALE, pNode->scale);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, DATA_TYPE_CODE_BYTES, pNode->bytes);
  }

  return code;
}

X
Xiaoyu Wang 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
static int32_t msgToDataTypeInline(STlvDecoder* pDecoder, void* pObj) {
  SDataType* pNode = (SDataType*)pObj;

  int32_t code = tlvDecodeValueI8(pDecoder, &pNode->type);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueU8(pDecoder, &pNode->precision);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueU8(pDecoder, &pNode->scale);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->bytes);
  }

  return code;
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
static int32_t msgToDataType(STlvDecoder* pDecoder, void* pObj) {
  SDataType* pNode = (SDataType*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case DATA_TYPE_CODE_TYPE:
        code = tlvDecodeI8(pTlv, &pNode->type);
        break;
      case DATA_TYPE_CODE_PRECISION:
        code = tlvDecodeU8(pTlv, &pNode->precision);
        break;
      case DATA_TYPE_CODE_SCALE:
        code = tlvDecodeU8(pTlv, &pNode->scale);
        break;
      case DATA_TYPE_CODE_BYTES:
        code = tlvDecodeI32(pTlv, &pNode->bytes);
        break;
      default:
        break;
    }
  }

  return code;
}

551 552 553 554 555 556 557 558
enum { EXPR_CODE_RES_TYPE = 1 };

static int32_t exprNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SExprNode* pNode = (const SExprNode*)pObj;
  return tlvEncodeObj(pEncoder, EXPR_CODE_RES_TYPE, dataTypeToMsg, &pNode->resType);
}

static int32_t msgToExprNode(STlvDecoder* pDecoder, void* pObj) {
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  SExprNode* pNode = (SExprNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case EXPR_CODE_RES_TYPE:
        code = tlvDecodeObjFromTlv(pTlv, msgToDataType, &pNode->resType);
        break;
      default:
        break;
    }
  }

  return code;
574 575
}

X
Xiaoyu Wang 已提交
576
enum { COLUMN_CODE_INLINE_ATTRS = 1 };
577

X
Xiaoyu Wang 已提交
578
static int32_t columnNodeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
579 580
  const SColumnNode* pNode = (const SColumnNode*)pObj;

X
Xiaoyu Wang 已提交
581
  int32_t code = dataTypeInlineToMsg(&pNode->node.resType, pEncoder);
582
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
583
    code = tlvEncodeValueU64(pEncoder, pNode->tableId);
584 585
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
586
    code = tlvEncodeValueI8(pEncoder, pNode->tableType);
587 588
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
589
    code = tlvEncodeValueI16(pEncoder, pNode->colId);
590 591
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
592
    code = tlvEncodeValueEnum(pEncoder, pNode->colType);
593
  }
594
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
595
    code = tlvEncodeValueCStr(pEncoder, pNode->dbName);
596 597
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
598
    code = tlvEncodeValueCStr(pEncoder, pNode->tableName);
599 600
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
601
    code = tlvEncodeValueCStr(pEncoder, pNode->tableAlias);
602 603
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
604
    code = tlvEncodeValueCStr(pEncoder, pNode->colName);
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
607
    code = tlvEncodeValueI16(pEncoder, pNode->dataBlockId);
608 609
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    code = tlvEncodeValueI16(pEncoder, pNode->slotId);
  }

  return code;
}

static int32_t columnNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  return tlvEncodeObj(pEncoder, COLUMN_CODE_INLINE_ATTRS, columnNodeInlineToMsg, pObj);
}

static int32_t msgToColumnNodeInline(STlvDecoder* pDecoder, void* pObj) {
  SColumnNode* pNode = (SColumnNode*)pObj;

  int32_t code = msgToDataTypeInline(pDecoder, &pNode->node.resType);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueU64(pDecoder, &pNode->tableId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI8(pDecoder, &pNode->tableType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI16(pDecoder, &pNode->colId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueEnum(pDecoder, &pNode->colType, sizeof(pNode->colType));
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueCStr(pDecoder, pNode->dbName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueCStr(pDecoder, pNode->tableName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueCStr(pDecoder, pNode->tableAlias);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueCStr(pDecoder, pNode->colName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI16(pDecoder, &pNode->dataBlockId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI16(pDecoder, &pNode->slotId);
653 654 655 656 657
  }

  return code;
}

658
static int32_t msgToColumnNode(STlvDecoder* pDecoder, void* pObj) {
659 660
  SColumnNode* pNode = (SColumnNode*)pObj;

661 662 663 664
  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
665 666
      case COLUMN_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToColumnNodeInline, pNode);
667 668
        break;
      default:
669 670 671 672 673 674 675
        break;
    }
  }

  return code;
}

676 677 678 679 680 681 682 683 684
enum {
  VALUE_CODE_EXPR_BASE = 1,
  VALUE_CODE_LITERAL,
  VALUE_CODE_IS_DURATION,
  VALUE_CODE_TRANSLATE,
  VALUE_CODE_NOT_RESERVED,
  VALUE_CODE_IS_NULL,
  VALUE_CODE_DATUM
};
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

static int32_t datumToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SValueNode* pNode = (const SValueNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_NULL:
      break;
    case TSDB_DATA_TYPE_BOOL:
      code = tlvEncodeBool(pEncoder, VALUE_CODE_DATUM, pNode->datum.b);
      break;
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      code = tlvEncodeI64(pEncoder, VALUE_CODE_DATUM, pNode->datum.i);
      break;
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      code = tlvEncodeU64(pEncoder, VALUE_CODE_DATUM, pNode->datum.u);
      break;
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
      code = tlvEncodeDouble(pEncoder, VALUE_CODE_DATUM, pNode->datum.d);
      break;
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
    case TSDB_DATA_TYPE_NCHAR:
      code = tlvEncodeBinary(pEncoder, VALUE_CODE_DATUM, pNode->datum.p, varDataTLen(pNode->datum.p));
      break;
    case TSDB_DATA_TYPE_JSON:
      code = tlvEncodeBinary(pEncoder, VALUE_CODE_DATUM, pNode->datum.p, getJsonValueLen(pNode->datum.p));
      break;
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }

  return code;
}

static int32_t valueNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SValueNode* pNode = (const SValueNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, VALUE_CODE_EXPR_BASE, exprNodeToMsg, pNode);
735 736 737
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, VALUE_CODE_LITERAL, pNode->literal);
  }
738
  if (TSDB_CODE_SUCCESS == code) {
739 740 741 742 743 744 745
    code = tlvEncodeBool(pEncoder, VALUE_CODE_IS_DURATION, pNode->isDuration);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, VALUE_CODE_TRANSLATE, pNode->translate);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, VALUE_CODE_NOT_RESERVED, pNode->notReserved);
746 747
  }
  if (TSDB_CODE_SUCCESS == code) {
748 749 750
    code = tlvEncodeBool(pEncoder, VALUE_CODE_IS_NULL, pNode->isNull);
  }
  if (TSDB_CODE_SUCCESS == code && !pNode->isNull) {
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    code = datumToMsg(pNode, pEncoder);
  }

  return code;
}

static int32_t msgToDatum(STlv* pTlv, void* pObj) {
  SValueNode* pNode = (SValueNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_NULL:
      break;
    case TSDB_DATA_TYPE_BOOL:
      code = tlvDecodeBool(pTlv, &pNode->datum.b);
      *(bool*)&pNode->typeData = pNode->datum.b;
      break;
    case TSDB_DATA_TYPE_TINYINT:
      code = tlvDecodeI64(pTlv, &pNode->datum.i);
      *(int8_t*)&pNode->typeData = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      code = tlvDecodeI64(pTlv, &pNode->datum.i);
      *(int16_t*)&pNode->typeData = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_INT:
      code = tlvDecodeI64(pTlv, &pNode->datum.i);
      *(int32_t*)&pNode->typeData = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_BIGINT:
      code = tlvDecodeI64(pTlv, &pNode->datum.i);
      *(int64_t*)&pNode->typeData = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      code = tlvDecodeI64(pTlv, &pNode->datum.i);
      *(int64_t*)&pNode->typeData = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      code = tlvDecodeU64(pTlv, &pNode->datum.u);
      *(uint8_t*)&pNode->typeData = pNode->datum.u;
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      code = tlvDecodeU64(pTlv, &pNode->datum.u);
      *(uint16_t*)&pNode->typeData = pNode->datum.u;
      break;
    case TSDB_DATA_TYPE_UINT:
      code = tlvDecodeU64(pTlv, &pNode->datum.u);
      *(uint32_t*)&pNode->typeData = pNode->datum.u;
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      code = tlvDecodeU64(pTlv, &pNode->datum.u);
      *(uint64_t*)&pNode->typeData = pNode->datum.u;
      break;
    case TSDB_DATA_TYPE_FLOAT:
      code = tlvDecodeDouble(pTlv, &pNode->datum.d);
      *(float*)&pNode->typeData = pNode->datum.d;
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      code = tlvDecodeDouble(pTlv, &pNode->datum.d);
      *(double*)&pNode->typeData = pNode->datum.d;
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
814 815 816 817 818 819 820
    case TSDB_DATA_TYPE_VARBINARY: {
      pNode->datum.p = taosMemoryCalloc(1, pNode->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pNode->datum.p) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        break;
      }
      code = tlvDecodeBinary(pTlv, pNode->datum.p);
821
      if (TSDB_CODE_SUCCESS == code) {
822
        varDataSetLen(pNode->datum.p, pTlv->len - VARSTR_HEADER_SIZE);
823 824
      }
      break;
825
    }
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    case TSDB_DATA_TYPE_JSON:
      code = tlvDecodeDynBinary(pTlv, (void**)&pNode->datum.p);
      break;
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }

  return code;
}

static int32_t msgToValueNode(STlvDecoder* pDecoder, void* pObj) {
  SValueNode* pNode = (SValueNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case VALUE_CODE_EXPR_BASE:
        code = tlvDecodeObjFromTlv(pTlv, msgToExprNode, &pNode->node);
        break;
849 850 851
      case VALUE_CODE_LITERAL:
        code = tlvDecodeCStrP(pTlv, &pNode->literal);
        break;
852 853 854 855 856 857 858 859 860
      case VALUE_CODE_IS_DURATION:
        code = tlvDecodeBool(pTlv, &pNode->isDuration);
        break;
      case VALUE_CODE_TRANSLATE:
        code = tlvDecodeBool(pTlv, &pNode->translate);
        break;
      case VALUE_CODE_NOT_RESERVED:
        code = tlvDecodeBool(pTlv, &pNode->notReserved);
        break;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
      case VALUE_CODE_IS_NULL:
        code = tlvDecodeBool(pTlv, &pNode->isNull);
        break;
      case VALUE_CODE_DATUM:
        code = msgToDatum(pTlv, pNode);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { OPERATOR_CODE_EXPR_BASE = 1, OPERATOR_CODE_OP_TYPE, OPERATOR_CODE_LEFT, OPERATOR_CODE_RIGHT };

static int32_t operatorNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SOperatorNode* pNode = (const SOperatorNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, OPERATOR_CODE_EXPR_BASE, exprNodeToMsg, pNode);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, OPERATOR_CODE_OP_TYPE, pNode->opType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, OPERATOR_CODE_LEFT, nodeToMsg, pNode->pLeft);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, OPERATOR_CODE_RIGHT, nodeToMsg, pNode->pRight);
  }

  return code;
}

static int32_t msgToOperatorNode(STlvDecoder* pDecoder, void* pObj) {
  SOperatorNode* pNode = (SOperatorNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case OPERATOR_CODE_EXPR_BASE:
        code = tlvDecodeObjFromTlv(pTlv, msgToExprNode, &pNode->node);
        break;
      case OPERATOR_CODE_OP_TYPE:
        code = tlvDecodeEnum(pTlv, &pNode->opType, sizeof(pNode->opType));
        break;
      case OPERATOR_CODE_LEFT:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pLeft);
        break;
      case OPERATOR_CODE_RIGHT:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pRight);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { LOGIC_COND_CODE_EXPR_BASE = 1, LOGIC_COND_CODE_COND_TYPE, LOGIC_COND_CODE_PARAMETERS };

static int32_t logicConditionNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SLogicConditionNode* pNode = (const SLogicConditionNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, LOGIC_COND_CODE_EXPR_BASE, exprNodeToMsg, pNode);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, LOGIC_COND_CODE_COND_TYPE, pNode->condType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, LOGIC_COND_CODE_PARAMETERS, nodeListToMsg, pNode->pParameterList);
  }

  return code;
}

static int32_t msgToLogicConditionNode(STlvDecoder* pDecoder, void* pObj) {
  SLogicConditionNode* pNode = (SLogicConditionNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case LOGIC_COND_CODE_EXPR_BASE:
        code = tlvDecodeObjFromTlv(pTlv, msgToExprNode, &pNode->node);
        break;
      case LOGIC_COND_CODE_COND_TYPE:
        code = tlvDecodeEnum(pTlv, &pNode->condType, sizeof(pNode->condType));
        break;
      case LOGIC_COND_CODE_PARAMETERS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pParameterList);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  FUNCTION_CODE_EXPR_BASE = 1,
963
  FUNCTION_CODE_FUNCTION_NAME,
964 965 966 967 968 969 970 971 972 973
  FUNCTION_CODE_FUNCTION_ID,
  FUNCTION_CODE_FUNCTION_TYPE,
  FUNCTION_CODE_PARAMETERS,
  FUNCTION_CODE_UDF_BUF_SIZE
};

static int32_t functionNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SFunctionNode* pNode = (const SFunctionNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, FUNCTION_CODE_EXPR_BASE, exprNodeToMsg, pNode);
974 975 976
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, FUNCTION_CODE_FUNCTION_NAME, pNode->functionName);
  }
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, FUNCTION_CODE_FUNCTION_ID, pNode->funcId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, FUNCTION_CODE_FUNCTION_TYPE, pNode->funcType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, FUNCTION_CODE_PARAMETERS, nodeListToMsg, pNode->pParameterList);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, FUNCTION_CODE_UDF_BUF_SIZE, pNode->udfBufSize);
  }

  return code;
}

static int32_t msgToFunctionNode(STlvDecoder* pDecoder, void* pObj) {
  SFunctionNode* pNode = (SFunctionNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case FUNCTION_CODE_EXPR_BASE:
        code = tlvDecodeObjFromTlv(pTlv, msgToExprNode, &pNode->node);
        break;
1003 1004 1005
      case FUNCTION_CODE_FUNCTION_NAME:
        code = tlvDecodeCStr(pTlv, pNode->functionName);
        break;
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
      case FUNCTION_CODE_FUNCTION_ID:
        code = tlvDecodeI32(pTlv, &pNode->funcId);
        break;
      case FUNCTION_CODE_FUNCTION_TYPE:
        code = tlvDecodeI32(pTlv, &pNode->funcType);
        break;
      case FUNCTION_CODE_PARAMETERS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pParameterList);
        break;
      case FUNCTION_CODE_UDF_BUF_SIZE:
        code = tlvDecodeI32(pTlv, &pNode->udfBufSize);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { ORDER_BY_EXPR_CODE_EXPR = 1, ORDER_BY_EXPR_CODE_ORDER, ORDER_BY_EXPR_CODE_NULL_ORDER };

static int32_t orderByExprNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SOrderByExprNode* pNode = (const SOrderByExprNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, ORDER_BY_EXPR_CODE_EXPR, nodeToMsg, pNode->pExpr);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, ORDER_BY_EXPR_CODE_ORDER, pNode->order);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, ORDER_BY_EXPR_CODE_NULL_ORDER, pNode->nullOrder);
  }

  return code;
}

static int32_t msgToOrderByExprNode(STlvDecoder* pDecoder, void* pObj) {
  SOrderByExprNode* pNode = (SOrderByExprNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case ORDER_BY_EXPR_CODE_EXPR:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pExpr);
        break;
      case ORDER_BY_EXPR_CODE_ORDER:
        code = tlvDecodeEnum(pTlv, &pNode->order, sizeof(pNode->order));
        break;
      case ORDER_BY_EXPR_CODE_NULL_ORDER:
        code = tlvDecodeEnum(pTlv, &pNode->nullOrder, sizeof(pNode->nullOrder));
        break;
      default:
        break;
    }
  }

  return code;
}

enum { LIMIT_CODE_LIMIT = 1, LIMIT_CODE_OFFSET };

static int32_t limitNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SLimitNode* pNode = (const SLimitNode*)pObj;

  int32_t code = tlvEncodeI64(pEncoder, LIMIT_CODE_LIMIT, pNode->limit);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, LIMIT_CODE_OFFSET, pNode->offset);
  }

  return code;
}

static int32_t msgToLimitNode(STlvDecoder* pDecoder, void* pObj) {
  SLimitNode* pNode = (SLimitNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case LIMIT_CODE_LIMIT:
        code = tlvDecodeI64(pTlv, &pNode->limit);
        break;
      case LIMIT_CODE_OFFSET:
        code = tlvDecodeI64(pTlv, &pNode->offset);
        break;
      default:
        break;
    }
  }

  return code;
}

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
enum { NAME_CODE_TYPE = 1, NAME_CODE_ACCT_ID, NAME_CODE_DB_NAME, NAME_CODE_TABLE_NAME };

static int32_t nameToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SName* pNode = (const SName*)pObj;

  int32_t code = tlvEncodeU8(pEncoder, NAME_CODE_TYPE, pNode->type);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, NAME_CODE_ACCT_ID, pNode->acctId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, NAME_CODE_DB_NAME, pNode->dbname);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, NAME_CODE_TABLE_NAME, pNode->tname);
  }

  return code;
}

static int32_t msgToName(STlvDecoder* pDecoder, void* pObj) {
  SName* pNode = (SName*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case NAME_CODE_TYPE:
        code = tlvDecodeU8(pTlv, &pNode->type);
        break;
      case NAME_CODE_ACCT_ID:
        code = tlvDecodeI32(pTlv, &pNode->acctId);
        break;
      case NAME_CODE_DB_NAME:
        code = tlvDecodeCStr(pTlv, pNode->dbname);
        break;
      case NAME_CODE_TABLE_NAME:
        code = tlvDecodeCStr(pTlv, pNode->tname);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { TIME_WINDOW_CODE_START_KEY = 1, TIME_WINDOW_CODE_END_KEY };

static int32_t timeWindowToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const STimeWindow* pNode = (const STimeWindow*)pObj;

  int32_t code = tlvEncodeI64(pEncoder, TIME_WINDOW_CODE_START_KEY, pNode->skey);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, TIME_WINDOW_CODE_END_KEY, pNode->ekey);
  }

  return code;
}

static int32_t msgToTimeWindow(STlvDecoder* pDecoder, void* pObj) {
  STimeWindow* pNode = (STimeWindow*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case TIME_WINDOW_CODE_START_KEY:
        code = tlvDecodeI64(pTlv, &pNode->skey);
        break;
      case TIME_WINDOW_CODE_END_KEY:
        code = tlvDecodeI64(pTlv, &pNode->ekey);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { NODE_LIST_CODE_DATA_TYPE = 1, NODE_LIST_CODE_NODE_LIST };

static int32_t nodeListNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SNodeListNode* pNode = (const SNodeListNode*)pObj;

X
Xiaoyu Wang 已提交
1185
  int32_t code = tlvEncodeObj(pEncoder, NODE_LIST_CODE_DATA_TYPE, dataTypeInlineToMsg, &pNode->dataType);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, NODE_LIST_CODE_NODE_LIST, nodeListToMsg, pNode->pNodeList);
  }

  return code;
}

static int32_t msgToNodeListNode(STlvDecoder* pDecoder, void* pObj) {
  SNodeListNode* pNode = (SNodeListNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case NODE_LIST_CODE_DATA_TYPE:
X
Xiaoyu Wang 已提交
1201
        code = tlvDecodeObjFromTlv(pTlv, msgToDataTypeInline, &pNode->dataType);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
        break;
      case NODE_LIST_CODE_NODE_LIST:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pNodeList);
        break;
      default:
        break;
    }
  }

  return code;
}

X
Xiaoyu Wang 已提交
1214
enum { TARGET_CODE_INLINE_ATTRS = 1, TARGET_CODE_EXPR };
1215

X
Xiaoyu Wang 已提交
1216
static int32_t targetNodeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
1217 1218
  const STargetNode* pNode = (const STargetNode*)pObj;

X
Xiaoyu Wang 已提交
1219
  int32_t code = tlvEncodeValueI16(pEncoder, pNode->dataBlockId);
1220
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1221
    code = tlvEncodeValueI16(pEncoder, pNode->slotId);
1222
  }
X
Xiaoyu Wang 已提交
1223 1224 1225 1226 1227 1228 1229 1230

  return code;
}

static int32_t targetNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const STargetNode* pNode = (const STargetNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, TARGET_CODE_INLINE_ATTRS, targetNodeInlineToMsg, pNode);
1231 1232 1233 1234 1235 1236 1237
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, TARGET_CODE_EXPR, nodeToMsg, pNode->pExpr);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
static int32_t msgToTargetNodeInline(STlvDecoder* pDecoder, void* pObj) {
  STargetNode* pNode = (STargetNode*)pObj;

  int32_t code = tlvDecodeValueI16(pDecoder, &pNode->dataBlockId);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI16(pDecoder, &pNode->slotId);
  }

  return code;
}

1249 1250 1251 1252 1253 1254 1255
static int32_t msgToTargetNode(STlvDecoder* pDecoder, void* pObj) {
  STargetNode* pNode = (STargetNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
1256 1257
      case TARGET_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToTargetNodeInline, pNode);
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
        break;
      case TARGET_CODE_EXPR:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pExpr);
        break;
      default:
        break;
    }
  }

  return code;
}

X
Xiaoyu Wang 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
enum { DATA_BLOCK_DESC_CODE_INLINE_ATTRS = 1, DATA_BLOCK_DESC_CODE_SLOTS };

static int32_t dataBlockDescNodeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataBlockDescNode* pNode = (const SDataBlockDescNode*)pObj;

  int32_t code = tlvEncodeValueI16(pEncoder, pNode->dataBlockId);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->totalRowSize);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->outputRowSize);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU8(pEncoder, pNode->precision);
  }

  return code;
}
1288 1289 1290 1291

static int32_t dataBlockDescNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataBlockDescNode* pNode = (const SDataBlockDescNode*)pObj;

X
Xiaoyu Wang 已提交
1292
  int32_t code = tlvEncodeObj(pEncoder, DATA_BLOCK_DESC_CODE_INLINE_ATTRS, dataBlockDescNodeInlineToMsg, pNode);
1293 1294 1295
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, DATA_BLOCK_DESC_CODE_SLOTS, nodeListToMsg, pNode->pSlots);
  }
X
Xiaoyu Wang 已提交
1296 1297 1298 1299 1300 1301 1302 1303

  return code;
}

static int32_t msgToDataBlockDescNodeInline(STlvDecoder* pDecoder, void* pObj) {
  SDataBlockDescNode* pNode = (SDataBlockDescNode*)pObj;

  int32_t code = tlvDecodeValueI16(pDecoder, &pNode->dataBlockId);
1304
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1305
    code = tlvDecodeValueI32(pDecoder, &pNode->totalRowSize);
1306 1307
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1308
    code = tlvDecodeValueI32(pDecoder, &pNode->outputRowSize);
1309 1310
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1311
    code = tlvDecodeValueU8(pDecoder, &pNode->precision);
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
  }

  return code;
}

static int32_t msgToDataBlockDescNode(STlvDecoder* pDecoder, void* pObj) {
  SDataBlockDescNode* pNode = (SDataBlockDescNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
1324 1325
      case DATA_BLOCK_DESC_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToDataBlockDescNodeInline, pNode);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
        break;
      case DATA_BLOCK_DESC_CODE_SLOTS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pSlots);
        break;
      default:
        break;
    }
  }

  return code;
}

X
Xiaoyu Wang 已提交
1338
enum { SLOT_DESC_CODE_INLINE_ATTRS = 1 };
1339

X
Xiaoyu Wang 已提交
1340
static int32_t slotDescNodeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
1341 1342
  const SSlotDescNode* pNode = (const SSlotDescNode*)pObj;

X
Xiaoyu Wang 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
  int32_t code = tlvEncodeValueI16(pEncoder, pNode->slotId);
  if (TSDB_CODE_SUCCESS == code) {
    code = dataTypeInlineToMsg(&pNode->dataType, pEncoder);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueBool(pEncoder, pNode->reserve);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueBool(pEncoder, pNode->output);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueBool(pEncoder, pNode->tag);
  }

  return code;
}

static int32_t slotDescNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  return tlvEncodeObj(pEncoder, SLOT_DESC_CODE_INLINE_ATTRS, slotDescNodeInlineToMsg, pObj);
}

static int32_t msgToSlotDescNodeInline(STlvDecoder* pDecoder, void* pObj) {
  SSlotDescNode* pNode = (SSlotDescNode*)pObj;

  int32_t code = tlvDecodeValueI16(pDecoder, &pNode->slotId);
1368
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1369
    code = msgToDataTypeInline(pDecoder, &pNode->dataType);
1370 1371
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1372
    code = tlvDecodeValueBool(pDecoder, &pNode->reserve);
1373 1374
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1375
    code = tlvDecodeValueBool(pDecoder, &pNode->output);
1376 1377
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1378
    code = tlvDecodeValueBool(pDecoder, &pNode->tag);
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
  }

  return code;
}

static int32_t msgToSlotDescNode(STlvDecoder* pDecoder, void* pObj) {
  SSlotDescNode* pNode = (SSlotDescNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
1391 1392
      case SLOT_DESC_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToSlotDescNodeInline, pNode);
1393 1394 1395 1396 1397 1398 1399 1400 1401
        break;
      default:
        break;
    }
  }

  return code;
}

1402 1403
enum { EP_CODE_FQDN = 1, EP_CODE_port };

X
Xiaoyu Wang 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
static int32_t epInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SEp* pNode = (const SEp*)pObj;

  int32_t code = tlvEncodeValueCStr(pEncoder, pNode->fqdn);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU16(pEncoder, pNode->port);
  }

  return code;
}

1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
static int32_t epToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SEp* pNode = (const SEp*)pObj;

  int32_t code = tlvEncodeCStr(pEncoder, EP_CODE_FQDN, pNode->fqdn);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeU16(pEncoder, EP_CODE_port, pNode->port);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
static int32_t msgToEpInline(STlvDecoder* pDecoder, void* pObj) {
  SEp* pNode = (SEp*)pObj;

  int32_t code = tlvDecodeValueCStr(pDecoder, pNode->fqdn);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueU16(pDecoder, &pNode->port);
  }

  return code;
}

1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
static int32_t msgToEp(STlvDecoder* pDecoder, void* pObj) {
  SEp* pNode = (SEp*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case EP_CODE_FQDN:
        code = tlvDecodeCStr(pTlv, pNode->fqdn);
        break;
      case EP_CODE_port:
        code = tlvDecodeU16(pTlv, &pNode->port);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { EP_SET_CODE_IN_USE = 1, EP_SET_CODE_NUM_OF_EPS, EP_SET_CODE_EPS };

X
Xiaoyu Wang 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
static int32_t epSetInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SEpSet* pNode = (const SEpSet*)pObj;

  int32_t code = tlvEncodeValueI8(pEncoder, pNode->inUse);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueArray(pEncoder, epInlineToMsg, pNode->eps, sizeof(SEp), pNode->numOfEps);
  }

  return code;
}

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
static int32_t epSetToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SEpSet* pNode = (const SEpSet*)pObj;

  int32_t code = tlvEncodeI8(pEncoder, EP_SET_CODE_IN_USE, pNode->inUse);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, EP_SET_CODE_NUM_OF_EPS, pNode->numOfEps);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObjArray(pEncoder, EP_SET_CODE_EPS, epToMsg, pNode->eps, sizeof(SEp), pNode->numOfEps);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
static int32_t msgToEpSetInline(STlvDecoder* pDecoder, void* pObj) {
  SEpSet* pNode = (SEpSet*)pObj;

  int32_t code = tlvDecodeValueI8(pDecoder, &pNode->inUse);
  if (TSDB_CODE_SUCCESS == code) {
    int32_t numOfEps = 0;
    code = tlvDecodeValueArray(pDecoder, msgToEpInline, pNode->eps, sizeof(SEp), &numOfEps);
    pNode->numOfEps = numOfEps;
  }

  return code;
}

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
static int32_t msgToEpSet(STlvDecoder* pDecoder, void* pObj) {
  SEpSet* pNode = (SEpSet*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case EP_SET_CODE_IN_USE:
        code = tlvDecodeI8(pTlv, &pNode->inUse);
        break;
      case EP_SET_CODE_NUM_OF_EPS:
        code = tlvDecodeI8(pTlv, &pNode->numOfEps);
        break;
      case EP_SET_CODE_EPS:
        code = tlvDecodeObjArrayFromTlv(pTlv, msgToEp, pNode->eps, sizeof(SEp));
        break;
      default:
        break;
    }
  }

  return code;
}

enum { QUERY_NODE_ADDR_CODE_NODE_ID = 1, QUERY_NODE_ADDR_CODE_EP_SET };

X
Xiaoyu Wang 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
static int32_t queryNodeAddrInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SQueryNodeAddr* pNode = (const SQueryNodeAddr*)pObj;

  int32_t code = tlvEncodeValueI32(pEncoder, pNode->nodeId);
  if (TSDB_CODE_SUCCESS == code) {
    code = epSetInlineToMsg(&pNode->epSet, pEncoder);
  }

  return code;
}

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
static int32_t queryNodeAddrToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SQueryNodeAddr* pNode = (const SQueryNodeAddr*)pObj;

  int32_t code = tlvEncodeI32(pEncoder, QUERY_NODE_ADDR_CODE_NODE_ID, pNode->nodeId);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, QUERY_NODE_ADDR_CODE_EP_SET, epSetToMsg, &pNode->epSet);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
static int32_t msgToQueryNodeAddrInline(STlvDecoder* pDecoder, void* pObj) {
  SQueryNodeAddr* pNode = (SQueryNodeAddr*)pObj;

  int32_t code = tlvDecodeValueI32(pDecoder, &pNode->nodeId);
  if (TSDB_CODE_SUCCESS == code) {
    code = msgToEpSetInline(pDecoder, &pNode->epSet);
  }

  return code;
}

1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
static int32_t msgToQueryNodeAddr(STlvDecoder* pDecoder, void* pObj) {
  SQueryNodeAddr* pNode = (SQueryNodeAddr*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case QUERY_NODE_ADDR_CODE_NODE_ID:
        code = tlvDecodeI32(pTlv, &pNode->nodeId);
        break;
      case QUERY_NODE_ADDR_CODE_EP_SET:
        code = tlvDecodeObjFromTlv(pTlv, msgToEpSet, &pNode->epSet);
        break;
    }
  }

  return code;
}

X
Xiaoyu Wang 已提交
1576
enum { DOWNSTREAM_SOURCE_CODE_INLINE_ATTRS = 1 };
1577

X
Xiaoyu Wang 已提交
1578
static int32_t downstreamSourceNodeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
1579 1580
  const SDownstreamSourceNode* pNode = (const SDownstreamSourceNode*)pObj;

X
Xiaoyu Wang 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
  int32_t code = queryNodeAddrInlineToMsg(&pNode->addr, pEncoder);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU64(pEncoder, pNode->taskId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU64(pEncoder, pNode->schedId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->execId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->fetchMsgType);
  }

  return code;
}

static int32_t downstreamSourceNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  return tlvEncodeObj(pEncoder, DOWNSTREAM_SOURCE_CODE_INLINE_ATTRS, downstreamSourceNodeInlineToMsg, pObj);
}

static int32_t msgToDownstreamSourceNodeInlineToMsg(STlvDecoder* pDecoder, void* pObj) {
  SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)pObj;

  int32_t code = msgToQueryNodeAddrInline(pDecoder, &pNode->addr);
1606
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1607
    code = tlvDecodeValueU64(pDecoder, &pNode->taskId);
1608 1609
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1610
    code = tlvDecodeValueU64(pDecoder, &pNode->schedId);
1611 1612
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1613
    code = tlvDecodeValueI32(pDecoder, &pNode->execId);
1614 1615
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1616
    code = tlvDecodeValueI32(pDecoder, &pNode->fetchMsgType);
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
  }

  return code;
}

static int32_t msgToDownstreamSourceNode(STlvDecoder* pDecoder, void* pObj) {
  SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
1629 1630
      case DOWNSTREAM_SOURCE_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToDownstreamSourceNodeInlineToMsg, pNode);
1631 1632 1633 1634 1635 1636 1637 1638 1639
        break;
      default:
        break;
    }
  }

  return code;
}

1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
enum {
  PHY_NODE_CODE_OUTPUT_DESC = 1,
  PHY_NODE_CODE_CONDITIONS,
  PHY_NODE_CODE_CHILDREN,
  PHY_NODE_CODE_LIMIT,
  PHY_NODE_CODE_SLIMIT
};

static int32_t physiNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SPhysiNode* pNode = (const SPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_NODE_CODE_OUTPUT_DESC, nodeToMsg, pNode->pOutputDataBlockDesc);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_NODE_CODE_CONDITIONS, nodeToMsg, pNode->pConditions);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_NODE_CODE_CHILDREN, nodeListToMsg, pNode->pChildren);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_NODE_CODE_LIMIT, nodeToMsg, pNode->pLimit);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_NODE_CODE_SLIMIT, nodeToMsg, pNode->pSlimit);
  }

  return code;
}

static int32_t msgToPhysiNode(STlvDecoder* pDecoder, void* pObj) {
  SPhysiNode* pNode = (SPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_NODE_CODE_OUTPUT_DESC:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pOutputDataBlockDesc);
        break;
      case PHY_NODE_CODE_CONDITIONS:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pConditions);
        break;
      case PHY_NODE_CODE_CHILDREN:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pChildren);
        break;
      case PHY_NODE_CODE_LIMIT:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pLimit);
        break;
      case PHY_NODE_CODE_SLIMIT:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pSlimit);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_SCAN_CODE_BASE_NODE = 1,
  PHY_SCAN_CODE_SCAN_COLS,
  PHY_SCAN_CODE_SCAN_PSEUDO_COLS,
  PHY_SCAN_CODE_BASE_UID,
  PHY_SCAN_CODE_BASE_SUID,
  PHY_SCAN_CODE_BASE_TABLE_TYPE,
  PHY_SCAN_CODE_BASE_TABLE_NAME
};

static int32_t physiScanNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SScanPhysiNode* pNode = (const SScanPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_SCAN_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SCAN_CODE_SCAN_COLS, nodeListToMsg, pNode->pScanCols);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SCAN_CODE_SCAN_PSEUDO_COLS, nodeListToMsg, pNode->pScanPseudoCols);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeU64(pEncoder, PHY_SCAN_CODE_BASE_UID, pNode->uid);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeU64(pEncoder, PHY_SCAN_CODE_BASE_SUID, pNode->suid);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_SCAN_CODE_BASE_TABLE_TYPE, pNode->tableType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SCAN_CODE_BASE_TABLE_NAME, nameToMsg, &pNode->tableName);
  }

  return code;
}

static int32_t msgToPhysiScanNode(STlvDecoder* pDecoder, void* pObj) {
  SScanPhysiNode* pNode = (SScanPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_SCAN_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_SCAN_CODE_SCAN_COLS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pScanCols);
        break;
      case PHY_SCAN_CODE_SCAN_PSEUDO_COLS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pScanPseudoCols);
        break;
      case PHY_SCAN_CODE_BASE_UID:
        code = tlvDecodeU64(pTlv, &pNode->uid);
        break;
      case PHY_SCAN_CODE_BASE_SUID:
        code = tlvDecodeU64(pTlv, &pNode->suid);
        break;
      case PHY_SCAN_CODE_BASE_TABLE_TYPE:
        code = tlvDecodeI8(pTlv, &pNode->tableType);
        break;
      case PHY_SCAN_CODE_BASE_TABLE_NAME:
        code = tlvDecodeObjFromTlv(pTlv, msgToName, &pNode->tableName);
        break;
      default:
        break;
    }
  }

  return code;
}

1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
enum { PHY_LAST_ROW_SCAN_CODE_SCAN = 1, PHY_LAST_ROW_SCAN_CODE_GROUP_TAGS, PHY_LAST_ROW_SCAN_CODE_GROUP_SORT };

static int32_t physiLastRowScanNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SLastRowScanPhysiNode* pNode = (const SLastRowScanPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_LAST_ROW_SCAN_CODE_SCAN, physiScanNodeToMsg, &pNode->scan);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_LAST_ROW_SCAN_CODE_GROUP_TAGS, nodeListToMsg, pNode->pGroupTags);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_LAST_ROW_SCAN_CODE_GROUP_SORT, pNode->groupSort);
  }

  return code;
}

static int32_t msgToPhysiLastRowScanNode(STlvDecoder* pDecoder, void* pObj) {
  SLastRowScanPhysiNode* pNode = (SLastRowScanPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_LAST_ROW_SCAN_CODE_SCAN:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiScanNode, &pNode->scan);
        break;
      case PHY_LAST_ROW_SCAN_CODE_GROUP_TAGS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pGroupTags);
        break;
      case PHY_LAST_ROW_SCAN_CODE_GROUP_SORT:
        code = tlvDecodeBool(pTlv, &pNode->groupSort);
        break;
      default:
        break;
    }
  }

  return code;
}

1810 1811
enum {
  PHY_TABLE_SCAN_CODE_SCAN = 1,
X
Xiaoyu Wang 已提交
1812
  PHY_TABLE_SCAN_CODE_INLINE_ATTRS,
1813
  PHY_TABLE_SCAN_CODE_DYN_SCAN_FUNCS,
X
Xiaoyu Wang 已提交
1814
  PHY_TABLE_SCAN_CODE_GROUP_TAGS
1815 1816
};

X
Xiaoyu Wang 已提交
1817
static int32_t physiTableScanNodeInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
1818 1819
  const STableScanPhysiNode* pNode = (const STableScanPhysiNode*)pObj;

X
Xiaoyu Wang 已提交
1820 1821 1822 1823
  int32_t code = tlvEncodeValueU8(pEncoder, pNode->scanSeq[0]);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueU8(pEncoder, pNode->scanSeq[1]);
  }
1824
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1825
    code = tlvEncodeValueI64(pEncoder, pNode->scanRange.skey);
1826 1827
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1828
    code = tlvEncodeValueI64(pEncoder, pNode->scanRange.ekey);
1829 1830
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1831
    code = tlvEncodeValueDouble(pEncoder, pNode->ratio);
1832 1833
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1834
    code = tlvEncodeValueI32(pEncoder, pNode->dataRequired);
1835 1836
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
    code = tlvEncodeValueBool(pEncoder, pNode->groupSort);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI64(pEncoder, pNode->interval);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI64(pEncoder, pNode->offset);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI64(pEncoder, pNode->sliding);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI8(pEncoder, pNode->intervalUnit);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI8(pEncoder, pNode->slidingUnit);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI8(pEncoder, pNode->triggerType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI64(pEncoder, pNode->watermark);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI8(pEncoder, pNode->igExpired);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueBool(pEncoder, pNode->assignBlockUid);
  }

  return code;
}

static int32_t physiTableScanNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const STableScanPhysiNode* pNode = (const STableScanPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_TABLE_SCAN_CODE_SCAN, physiScanNodeToMsg, &pNode->scan);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_TABLE_SCAN_CODE_INLINE_ATTRS, physiTableScanNodeInlineToMsg, pNode);
1876 1877 1878 1879 1880 1881 1882
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_TABLE_SCAN_CODE_DYN_SCAN_FUNCS, nodeListToMsg, pNode->pDynamicScanFuncs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_TABLE_SCAN_CODE_GROUP_TAGS, nodeListToMsg, pNode->pGroupTags);
  }
X
Xiaoyu Wang 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905

  return code;
}

static int32_t msgToPhysiTableScanNodeInline(STlvDecoder* pDecoder, void* pObj) {
  STableScanPhysiNode* pNode = (STableScanPhysiNode*)pObj;

  int32_t code = tlvDecodeValueU8(pDecoder, pNode->scanSeq);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueU8(pDecoder, pNode->scanSeq + 1);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI64(pDecoder, &pNode->scanRange.skey);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI64(pDecoder, &pNode->scanRange.ekey);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueDouble(pDecoder, &pNode->ratio);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->dataRequired);
  }
1906
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1907
    code = tlvDecodeValueBool(pDecoder, &pNode->groupSort);
1908 1909
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1910
    code = tlvDecodeValueI64(pDecoder, &pNode->interval);
1911 1912
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1913
    code = tlvDecodeValueI64(pDecoder, &pNode->offset);
1914 1915
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1916
    code = tlvDecodeValueI64(pDecoder, &pNode->sliding);
1917 1918
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1919
    code = tlvDecodeValueI8(pDecoder, &pNode->intervalUnit);
1920 1921
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1922
    code = tlvDecodeValueI8(pDecoder, &pNode->slidingUnit);
1923 1924
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1925
    code = tlvDecodeValueI8(pDecoder, &pNode->triggerType);
1926 1927
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1928
    code = tlvDecodeValueI64(pDecoder, &pNode->watermark);
1929 1930
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1931
    code = tlvDecodeValueI8(pDecoder, &pNode->igExpired);
1932 1933
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1934
    code = tlvDecodeValueBool(pDecoder, &pNode->assignBlockUid);
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
  }

  return code;
}

static int32_t msgToPhysiTableScanNode(STlvDecoder* pDecoder, void* pObj) {
  STableScanPhysiNode* pNode = (STableScanPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_TABLE_SCAN_CODE_SCAN:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiScanNode, &pNode->scan);
        break;
X
Xiaoyu Wang 已提交
1950 1951
      case PHY_TABLE_SCAN_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiTableScanNodeInline, pNode);
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
        break;
      case PHY_TABLE_SCAN_CODE_DYN_SCAN_FUNCS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pDynamicScanFuncs);
        break;
      case PHY_TABLE_SCAN_CODE_GROUP_TAGS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pGroupTags);
        break;
      default:
        break;
    }
  }

  return code;
}

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
enum {
  PHY_SYSTABLE_SCAN_CODE_SCAN = 1,
  PHY_SYSTABLE_SCAN_CODE_MGMT_EP_SET,
  PHY_SYSTABLE_SCAN_CODE_SHOW_REWRITE,
  PHY_SYSTABLE_SCAN_CODE_ACCOUNT_ID,
  PHY_SYSTABLE_SCAN_CODE_SYS_INFO
};

static int32_t physiSysTableScanNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSystemTableScanPhysiNode* pNode = (const SSystemTableScanPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_SYSTABLE_SCAN_CODE_SCAN, physiScanNodeToMsg, &pNode->scan);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SYSTABLE_SCAN_CODE_MGMT_EP_SET, epSetToMsg, &pNode->mgmtEpSet);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_SYSTABLE_SCAN_CODE_SHOW_REWRITE, pNode->showRewrite);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, PHY_SYSTABLE_SCAN_CODE_ACCOUNT_ID, pNode->accountId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_SYSTABLE_SCAN_CODE_SYS_INFO, pNode->sysInfo);
  }

  return code;
}

static int32_t msgToPhysiSysTableScanNode(STlvDecoder* pDecoder, void* pObj) {
  SSystemTableScanPhysiNode* pNode = (SSystemTableScanPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_SYSTABLE_SCAN_CODE_SCAN:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiScanNode, &pNode->scan);
        break;
      case PHY_SYSTABLE_SCAN_CODE_MGMT_EP_SET:
        code = tlvDecodeObjFromTlv(pTlv, msgToEpSet, &pNode->mgmtEpSet);
        break;
      case PHY_SYSTABLE_SCAN_CODE_SHOW_REWRITE:
        code = tlvDecodeBool(pTlv, &pNode->showRewrite);
        break;
      case PHY_SYSTABLE_SCAN_CODE_ACCOUNT_ID:
        code = tlvDecodeI32(pTlv, &pNode->accountId);
        break;
      case PHY_SYSTABLE_SCAN_CODE_SYS_INFO:
        code = tlvDecodeBool(pTlv, &pNode->sysInfo);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_PROJECT_CODE_BASE_NODE = 1,
  PHY_PROJECT_CODE_PROJECTIONS,
  PHY_PROJECT_CODE_MERGE_DATA_BLOCK,
  PHY_PROJECT_CODE_IGNORE_GROUP_ID
};

static int32_t physiProjectNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SProjectPhysiNode* pNode = (const SProjectPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_PROJECT_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_PROJECT_CODE_PROJECTIONS, nodeListToMsg, pNode->pProjections);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_PROJECT_CODE_MERGE_DATA_BLOCK, pNode->mergeDataBlock);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_PROJECT_CODE_IGNORE_GROUP_ID, pNode->ignoreGroupId);
  }

  return code;
}

static int32_t msgToPhysiProjectNode(STlvDecoder* pDecoder, void* pObj) {
  SProjectPhysiNode* pNode = (SProjectPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_PROJECT_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_PROJECT_CODE_PROJECTIONS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pProjections);
        break;
      case PHY_PROJECT_CODE_MERGE_DATA_BLOCK:
        code = tlvDecodeBool(pTlv, &pNode->mergeDataBlock);
        break;
      case PHY_PROJECT_CODE_IGNORE_GROUP_ID:
        code = tlvDecodeBool(pTlv, &pNode->ignoreGroupId);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_SORT_MERGE_JOIN_CODE_BASE_NODE = 1,
  PHY_SORT_MERGE_JOIN_CODE_JOIN_TYPE,
  PHY_SORT_MERGE_JOIN_CODE_MERGE_CONDITION,
  PHY_SORT_MERGE_JOIN_CODE_ON_CONDITIONS,
  PHY_SORT_MERGE_JOIN_CODE_TARGETS,
  PHY_SORT_MERGE_JOIN_CODE_INPUT_TS_ORDER
};

static int32_t physiJoinNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSortMergeJoinPhysiNode* pNode = (const SSortMergeJoinPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_SORT_MERGE_JOIN_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_SORT_MERGE_JOIN_CODE_JOIN_TYPE, pNode->joinType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SORT_MERGE_JOIN_CODE_MERGE_CONDITION, nodeToMsg, pNode->pMergeCondition);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SORT_MERGE_JOIN_CODE_ON_CONDITIONS, nodeToMsg, pNode->pOnConditions);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SORT_MERGE_JOIN_CODE_TARGETS, nodeListToMsg, pNode->pTargets);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_SORT_MERGE_JOIN_CODE_INPUT_TS_ORDER, pNode->inputTsOrder);
  }

  return code;
}

static int32_t msgToPhysiJoinNode(STlvDecoder* pDecoder, void* pObj) {
  SSortMergeJoinPhysiNode* pNode = (SSortMergeJoinPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_SORT_MERGE_JOIN_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_SORT_MERGE_JOIN_CODE_JOIN_TYPE:
        code = tlvDecodeEnum(pTlv, &pNode->joinType, sizeof(pNode->joinType));
        break;
      case PHY_SORT_MERGE_JOIN_CODE_MERGE_CONDITION:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pMergeCondition);
        break;
      case PHY_SORT_MERGE_JOIN_CODE_ON_CONDITIONS:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pOnConditions);
        break;
      case PHY_SORT_MERGE_JOIN_CODE_TARGETS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pTargets);
        break;
      case PHY_SORT_MERGE_JOIN_CODE_INPUT_TS_ORDER:
        code = tlvDecodeEnum(pTlv, &pNode->inputTsOrder, sizeof(pNode->inputTsOrder));
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_AGG_CODE_BASE_NODE = 1,
  PHY_AGG_CODE_EXPR,
  PHY_AGG_CODE_GROUP_KEYS,
  PHY_AGG_CODE_AGG_FUNCS,
  PHY_AGG_CODE_MERGE_DATA_BLOCK
};

static int32_t physiAggNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SAggPhysiNode* pNode = (const SAggPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_AGG_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_AGG_CODE_EXPR, nodeListToMsg, pNode->pExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_AGG_CODE_GROUP_KEYS, nodeListToMsg, pNode->pGroupKeys);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_AGG_CODE_AGG_FUNCS, nodeListToMsg, pNode->pAggFuncs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_AGG_CODE_MERGE_DATA_BLOCK, pNode->mergeDataBlock);
  }

  return code;
}

static int32_t msgToPhysiAggNode(STlvDecoder* pDecoder, void* pObj) {
  SAggPhysiNode* pNode = (SAggPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_AGG_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_AGG_CODE_EXPR:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pExprs);
        break;
      case PHY_AGG_CODE_GROUP_KEYS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pGroupKeys);
        break;
      case PHY_AGG_CODE_AGG_FUNCS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pAggFuncs);
        break;
      case PHY_AGG_CODE_MERGE_DATA_BLOCK:
        code = tlvDecodeBool(pTlv, &pNode->mergeDataBlock);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_EXCHANGE_CODE_BASE_NODE = 1,
  PHY_EXCHANGE_CODE_SRC_GROUP_ID,
  PHY_EXCHANGE_CODE_SINGLE_CHANNEL,
  PHY_EXCHANGE_CODE_SRC_ENDPOINTS
};

static int32_t physiExchangeNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SExchangePhysiNode* pNode = (const SExchangePhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_EXCHANGE_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, PHY_EXCHANGE_CODE_SRC_GROUP_ID, pNode->srcGroupId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_EXCHANGE_CODE_SINGLE_CHANNEL, pNode->singleChannel);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_EXCHANGE_CODE_SRC_ENDPOINTS, nodeListToMsg, pNode->pSrcEndPoints);
  }

  return code;
}

static int32_t msgToPhysiExchangeNode(STlvDecoder* pDecoder, void* pObj) {
  SExchangePhysiNode* pNode = (SExchangePhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_EXCHANGE_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_EXCHANGE_CODE_SRC_GROUP_ID:
        code = tlvDecodeI32(pTlv, &pNode->srcGroupId);
        break;
      case PHY_EXCHANGE_CODE_SINGLE_CHANNEL:
        code = tlvDecodeBool(pTlv, &pNode->singleChannel);
        break;
      case PHY_EXCHANGE_CODE_SRC_ENDPOINTS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pSrcEndPoints);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_MERGE_CODE_BASE_NODE = 1,
  PHY_MERGE_CODE_MERGE_KEYS,
  PHY_MERGE_CODE_TARGETS,
  PHY_MERGE_CODE_NUM_OF_CHANNELS,
  PHY_MERGE_CODE_SRC_GROUP_ID,
  PHY_MERGE_CODE_GROUP_SORT
};

static int32_t physiMergeNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SMergePhysiNode* pNode = (const SMergePhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_MERGE_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_MERGE_CODE_MERGE_KEYS, nodeListToMsg, pNode->pMergeKeys);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_MERGE_CODE_TARGETS, nodeListToMsg, pNode->pTargets);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, PHY_MERGE_CODE_NUM_OF_CHANNELS, pNode->numOfChannels);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, PHY_MERGE_CODE_SRC_GROUP_ID, pNode->srcGroupId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_MERGE_CODE_GROUP_SORT, pNode->groupSort);
  }

  return code;
}

static int32_t msgToPhysiMergeNode(STlvDecoder* pDecoder, void* pObj) {
  SMergePhysiNode* pNode = (SMergePhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_MERGE_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_MERGE_CODE_MERGE_KEYS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pMergeKeys);
        break;
      case PHY_MERGE_CODE_TARGETS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pTargets);
        break;
      case PHY_MERGE_CODE_NUM_OF_CHANNELS:
        code = tlvDecodeI32(pTlv, &pNode->numOfChannels);
        break;
      case PHY_MERGE_CODE_SRC_GROUP_ID:
        code = tlvDecodeI32(pTlv, &pNode->srcGroupId);
        break;
      case PHY_MERGE_CODE_GROUP_SORT:
        code = tlvDecodeBool(pTlv, &pNode->groupSort);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { PHY_SORT_CODE_BASE_NODE = 1, PHY_SORT_CODE_EXPR, PHY_SORT_CODE_SORT_KEYS, PHY_SORT_CODE_TARGETS };

static int32_t physiSortNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSortPhysiNode* pNode = (const SSortPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_SORT_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SORT_CODE_EXPR, nodeListToMsg, pNode->pExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SORT_CODE_SORT_KEYS, nodeListToMsg, pNode->pSortKeys);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_SORT_CODE_TARGETS, nodeListToMsg, pNode->pTargets);
  }

  return code;
}

static int32_t msgToPhysiSortNode(STlvDecoder* pDecoder, void* pObj) {
  SSortPhysiNode* pNode = (SSortPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_SORT_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_SORT_CODE_EXPR:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pExprs);
        break;
      case PHY_SORT_CODE_SORT_KEYS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pSortKeys);
        break;
      case PHY_SORT_CODE_TARGETS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pTargets);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_WINDOW_CODE_BASE_NODE = 1,
  PHY_WINDOW_CODE_EXPR,
  PHY_WINDOW_CODE_FUNCS,
  PHY_WINDOW_CODE_TS_PK,
  PHY_WINDOW_CODE_TS_END,
  PHY_WINDOW_CODE_TRIGGER_TYPE,
  PHY_WINDOW_CODE_WATERMARK,
  PHY_WINDOW_CODE_IG_EXPIRED,
  PHY_WINDOW_CODE_INPUT_TS_ORDER,
  PHY_WINDOW_CODE_OUTPUT_TS_ORDER,
  PHY_WINDOW_CODE_MERGE_DATA_BLOCK
};

static int32_t physiWindowNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SWinodwPhysiNode* pNode = (const SWinodwPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_WINDOW_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_WINDOW_CODE_EXPR, nodeListToMsg, pNode->pExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_WINDOW_CODE_FUNCS, nodeListToMsg, pNode->pFuncs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_WINDOW_CODE_TS_PK, nodeToMsg, pNode->pTspk);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_WINDOW_CODE_TS_END, nodeToMsg, pNode->pTsEnd);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_WINDOW_CODE_TRIGGER_TYPE, pNode->triggerType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, PHY_WINDOW_CODE_WATERMARK, pNode->watermark);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_WINDOW_CODE_IG_EXPIRED, pNode->igExpired);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_WINDOW_CODE_INPUT_TS_ORDER, pNode->inputTsOrder);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_WINDOW_CODE_OUTPUT_TS_ORDER, pNode->outputTsOrder);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeBool(pEncoder, PHY_WINDOW_CODE_MERGE_DATA_BLOCK, pNode->mergeDataBlock);
  }

  return code;
}

static int32_t msgToPhysiWindowNode(STlvDecoder* pDecoder, void* pObj) {
  SWinodwPhysiNode* pNode = (SWinodwPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_WINDOW_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_WINDOW_CODE_EXPR:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pExprs);
        break;
      case PHY_WINDOW_CODE_FUNCS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pFuncs);
        break;
      case PHY_WINDOW_CODE_TS_PK:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pTspk);
        break;
      case PHY_WINDOW_CODE_TS_END:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pTsEnd);
        break;
      case PHY_WINDOW_CODE_TRIGGER_TYPE:
        code = tlvDecodeI8(pTlv, &pNode->triggerType);
        break;
      case PHY_WINDOW_CODE_WATERMARK:
        code = tlvDecodeI64(pTlv, &pNode->watermark);
        break;
      case PHY_WINDOW_CODE_IG_EXPIRED:
        code = tlvDecodeI8(pTlv, &pNode->igExpired);
        break;
      case PHY_WINDOW_CODE_INPUT_TS_ORDER:
        code = tlvDecodeEnum(pTlv, &pNode->inputTsOrder, sizeof(pNode->inputTsOrder));
        break;
      case PHY_WINDOW_CODE_OUTPUT_TS_ORDER:
        code = tlvDecodeEnum(pTlv, &pNode->outputTsOrder, sizeof(pNode->outputTsOrder));
        break;
      case PHY_WINDOW_CODE_MERGE_DATA_BLOCK:
        code = tlvDecodeBool(pTlv, &pNode->mergeDataBlock);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_INTERVAL_CODE_WINDOW = 1,
  PHY_INTERVAL_CODE_INTERVAL,
  PHY_INTERVAL_CODE_OFFSET,
  PHY_INTERVAL_CODE_SLIDING,
  PHY_INTERVAL_CODE_INTERVAL_UNIT,
  PHY_INTERVAL_CODE_SLIDING_UNIT
};

static int32_t physiIntervalNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SIntervalPhysiNode* pNode = (const SIntervalPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_INTERVAL_CODE_WINDOW, physiWindowNodeToMsg, &pNode->window);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, PHY_INTERVAL_CODE_INTERVAL, pNode->interval);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, PHY_INTERVAL_CODE_OFFSET, pNode->offset);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, PHY_INTERVAL_CODE_SLIDING, pNode->sliding);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_INTERVAL_CODE_INTERVAL_UNIT, pNode->intervalUnit);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_INTERVAL_CODE_SLIDING_UNIT, pNode->slidingUnit);
  }

  return code;
}

static int32_t msgToPhysiIntervalNode(STlvDecoder* pDecoder, void* pObj) {
  SIntervalPhysiNode* pNode = (SIntervalPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_INTERVAL_CODE_WINDOW:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiWindowNode, &pNode->window);
        break;
      case PHY_INTERVAL_CODE_INTERVAL:
        code = tlvDecodeI64(pTlv, &pNode->interval);
        break;
      case PHY_INTERVAL_CODE_OFFSET:
        code = tlvDecodeI64(pTlv, &pNode->offset);
        break;
      case PHY_INTERVAL_CODE_SLIDING:
        code = tlvDecodeI64(pTlv, &pNode->sliding);
        break;
      case PHY_INTERVAL_CODE_INTERVAL_UNIT:
        code = tlvDecodeI8(pTlv, &pNode->intervalUnit);
        break;
      case PHY_INTERVAL_CODE_SLIDING_UNIT:
        code = tlvDecodeI8(pTlv, &pNode->slidingUnit);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_FILL_CODE_BASE_NODE = 1,
  PHY_FILL_CODE_MODE,
  PHY_FILL_CODE_FILL_EXPRS,
  PHY_FILL_CODE_NOT_FILL_EXPRS,
  PHY_FILL_CODE_WSTART,
  PHY_FILL_CODE_VALUES,
  PHY_FILL_CODE_TIME_RANGE,
  PHY_FILL_CODE_INPUT_TS_ORDER
};

static int32_t physiFillNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SFillPhysiNode* pNode = (const SFillPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_FILL_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_FILL_CODE_MODE, pNode->mode);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_FILL_CODE_FILL_EXPRS, nodeListToMsg, pNode->pFillExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_FILL_CODE_NOT_FILL_EXPRS, nodeListToMsg, pNode->pNotFillExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_FILL_CODE_WSTART, nodeToMsg, pNode->pWStartTs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_FILL_CODE_VALUES, nodeToMsg, pNode->pValues);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_FILL_CODE_TIME_RANGE, timeWindowToMsg, &pNode->timeRange);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_FILL_CODE_INPUT_TS_ORDER, pNode->inputTsOrder);
  }

  return code;
}

static int32_t msgToPhysiFillNode(STlvDecoder* pDecoder, void* pObj) {
  SFillPhysiNode* pNode = (SFillPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_FILL_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_FILL_CODE_MODE:
        code = tlvDecodeEnum(pTlv, &pNode->mode, sizeof(pNode->mode));
        break;
      case PHY_FILL_CODE_FILL_EXPRS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pFillExprs);
        break;
      case PHY_FILL_CODE_NOT_FILL_EXPRS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pNotFillExprs);
        break;
      case PHY_FILL_CODE_WSTART:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pWStartTs);
        break;
      case PHY_FILL_CODE_VALUES:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pValues);
        break;
      case PHY_FILL_CODE_TIME_RANGE:
        code = tlvDecodeObjFromTlv(pTlv, msgToTimeWindow, (void**)&pNode->timeRange);
        break;
      case PHY_FILL_CODE_INPUT_TS_ORDER:
        code = tlvDecodeEnum(pTlv, &pNode->inputTsOrder, sizeof(pNode->inputTsOrder));
        break;
      default:
        break;
    }
  }

  return code;
}

enum { PHY_SESSION_CODE_WINDOW = 1, PHY_SESSION_CODE_GAP };

static int32_t physiSessionWindowNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSessionWinodwPhysiNode* pNode = (const SSessionWinodwPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_SESSION_CODE_WINDOW, physiWindowNodeToMsg, &pNode->window);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, PHY_SESSION_CODE_GAP, pNode->gap);
  }

  return code;
}

static int32_t msgToPhysiSessionWindowNode(STlvDecoder* pDecoder, void* pObj) {
  SSessionWinodwPhysiNode* pNode = (SSessionWinodwPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_SESSION_CODE_WINDOW:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiWindowNode, &pNode->window);
        break;
      case PHY_SESSION_CODE_GAP:
        code = tlvDecodeI64(pTlv, &pNode->gap);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { PHY_STATE_CODE_WINDOW = 1, PHY_STATE_CODE_KEY };

static int32_t physiStateWindowNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SStateWinodwPhysiNode* pNode = (const SStateWinodwPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_STATE_CODE_WINDOW, physiWindowNodeToMsg, &pNode->window);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_STATE_CODE_KEY, nodeToMsg, pNode->pStateKey);
  }

  return code;
}

static int32_t msgToPhysiStateWindowNode(STlvDecoder* pDecoder, void* pObj) {
  SStateWinodwPhysiNode* pNode = (SStateWinodwPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_STATE_CODE_WINDOW:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiWindowNode, &pNode->window);
        break;
      case PHY_STATE_CODE_KEY:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pStateKey);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { PHY_PARTITION_CODE_BASE_NODE = 1, PHY_PARTITION_CODE_EXPR, PHY_PARTITION_CODE_KEYS, PHY_PARTITION_CODE_TARGETS };

static int32_t physiPartitionNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SPartitionPhysiNode* pNode = (const SPartitionPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_PARTITION_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_PARTITION_CODE_EXPR, nodeListToMsg, pNode->pExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_PARTITION_CODE_KEYS, nodeListToMsg, pNode->pPartitionKeys);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_PARTITION_CODE_TARGETS, nodeListToMsg, pNode->pTargets);
  }

  return code;
}

static int32_t msgToPhysiPartitionNode(STlvDecoder* pDecoder, void* pObj) {
  SPartitionPhysiNode* pNode = (SPartitionPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_PARTITION_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_PARTITION_CODE_EXPR:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pExprs);
        break;
      case PHY_PARTITION_CODE_KEYS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pPartitionKeys);
        break;
      case PHY_PARTITION_CODE_TARGETS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pTargets);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { PHY_INDEF_ROWS_FUNC_CODE_BASE_NODE = 1, PHY_INDEF_ROWS_FUNC_CODE_EXPRS, PHY_INDEF_ROWS_FUNC_CODE_FUNCS };

static int32_t physiIndefRowsFuncNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SIndefRowsFuncPhysiNode* pNode = (const SIndefRowsFuncPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_INDEF_ROWS_FUNC_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INDEF_ROWS_FUNC_CODE_EXPRS, nodeListToMsg, pNode->pExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INDEF_ROWS_FUNC_CODE_FUNCS, nodeListToMsg, pNode->pFuncs);
  }

  return code;
}

static int32_t msgToPhysiIndefRowsFuncNode(STlvDecoder* pDecoder, void* pObj) {
  SIndefRowsFuncPhysiNode* pNode = (SIndefRowsFuncPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_INDEF_ROWS_FUNC_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_INDEF_ROWS_FUNC_CODE_EXPRS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pExprs);
        break;
      case PHY_INDEF_ROWS_FUNC_CODE_FUNCS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pFuncs);
        break;
      default:
        break;
    }
  }

  return code;
}

enum {
  PHY_INERP_FUNC_CODE_BASE_NODE = 1,
  PHY_INERP_FUNC_CODE_EXPR,
  PHY_INERP_FUNC_CODE_FUNCS,
  PHY_INERP_FUNC_CODE_TIME_RANGE,
  PHY_INERP_FUNC_CODE_INTERVAL,
  PHY_INERP_FUNC_CODE_INTERVAL_UNIT,
  PHY_INERP_FUNC_CODE_FILL_MODE,
  PHY_INERP_FUNC_CODE_FILL_VALUES,
  PHY_INERP_FUNC_CODE_TIME_SERIES
};

static int32_t physiInterpFuncNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SInterpFuncPhysiNode* pNode = (const SInterpFuncPhysiNode*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, PHY_INERP_FUNC_CODE_BASE_NODE, physiNodeToMsg, &pNode->node);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INERP_FUNC_CODE_EXPR, nodeListToMsg, pNode->pExprs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INERP_FUNC_CODE_FUNCS, nodeListToMsg, pNode->pFuncs);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INERP_FUNC_CODE_TIME_RANGE, timeWindowToMsg, &pNode->timeRange);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI64(pEncoder, PHY_INERP_FUNC_CODE_INTERVAL, pNode->interval);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_INERP_FUNC_CODE_INTERVAL_UNIT, pNode->intervalUnit);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeEnum(pEncoder, PHY_INERP_FUNC_CODE_FILL_MODE, pNode->fillMode);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INERP_FUNC_CODE_FILL_VALUES, nodeToMsg, pNode->pFillValues);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_INERP_FUNC_CODE_TIME_SERIES, nodeToMsg, pNode->pTimeSeries);
  }

  return code;
}

static int32_t msgToPhysiInterpFuncNode(STlvDecoder* pDecoder, void* pObj) {
  SInterpFuncPhysiNode* pNode = (SInterpFuncPhysiNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_INERP_FUNC_CODE_BASE_NODE:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysiNode, &pNode->node);
        break;
      case PHY_INERP_FUNC_CODE_EXPR:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pExprs);
        break;
      case PHY_INERP_FUNC_CODE_FUNCS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pFuncs);
        break;
      case PHY_INERP_FUNC_CODE_TIME_RANGE:
        code = tlvDecodeObjFromTlv(pTlv, msgToTimeWindow, &pNode->timeRange);
        break;
      case PHY_INERP_FUNC_CODE_INTERVAL:
        code = tlvDecodeI64(pTlv, &pNode->interval);
        break;
      case PHY_INERP_FUNC_CODE_INTERVAL_UNIT:
        code = tlvDecodeI8(pTlv, &pNode->intervalUnit);
        break;
      case PHY_INERP_FUNC_CODE_FILL_MODE:
        code = tlvDecodeEnum(pTlv, &pNode->fillMode, sizeof(pNode->fillMode));
        break;
      case PHY_INERP_FUNC_CODE_FILL_VALUES:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pFillValues);
        break;
      case PHY_INERP_FUNC_CODE_TIME_SERIES:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pTimeSeries);
        break;
      default:
        break;
    }
  }

  return code;
}

enum { PHY_DATA_SINK_CODE_INPUT_DESC = 1 };

static int32_t physicDataSinkNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataSinkNode* pNode = (const SDataSinkNode*)pObj;
  return tlvEncodeObj(pEncoder, PHY_DATA_SINK_CODE_INPUT_DESC, nodeToMsg, pNode->pInputDataBlockDesc);
}

static int32_t msgToPhysicDataSinkNode(STlvDecoder* pDecoder, void* pObj) {
  SDataSinkNode* pNode = (SDataSinkNode*)pObj;
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case PHY_DATA_SINK_CODE_INPUT_DESC:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pInputDataBlockDesc);
        break;
      default:
        break;
    }
  }

  return code;
}

2870
enum { PHY_DISPATCH_CODE_SINK = 1 };
2871 2872 2873

static int32_t physiDispatchNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataDispatcherNode* pNode = (const SDataDispatcherNode*)pObj;
2874
  return tlvEncodeObj(pEncoder, PHY_DISPATCH_CODE_SINK, physicDataSinkNodeToMsg, &pNode->sink);
2875 2876 2877 2878 2879 2880 2881 2882 2883
}

static int32_t msgToPhysiDispatchNode(STlvDecoder* pDecoder, void* pObj) {
  SDataDispatcherNode* pNode = (SDataDispatcherNode*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
2884
      case PHY_DISPATCH_CODE_SINK:
2885 2886 2887
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysicDataSinkNode, &pNode->sink);
        break;
      default:
2888 2889
        break;
    }
2890 2891
  }

2892
  return code;
2893 2894
}

2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
enum {
  PHY_QUERY_INSERT_CODE_SINK = 1,
  PHY_QUERY_INSERT_CODE_COLS,
  PHY_QUERY_INSERT_CODE_TABLE_ID,
  PHY_QUERY_INSERT_CODE_STABLE_ID,
  PHY_QUERY_INSERT_CODE_TABLE_TYPE,
  PHY_QUERY_INSERT_CODE_TABLE_NAME,
  PHY_QUERY_INSERT_CODE_VG_ID,
  PHY_QUERY_INSERT_CODE_EP_SET
};
2905

2906 2907
static int32_t physiQueryInsertNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SQueryInserterNode* pNode = (const SQueryInserterNode*)pObj;
2908

2909
  int32_t code = tlvEncodeObj(pEncoder, PHY_QUERY_INSERT_CODE_SINK, physicDataSinkNodeToMsg, &pNode->sink);
2910
  if (TSDB_CODE_SUCCESS == code) {
2911
    code = tlvEncodeObj(pEncoder, PHY_QUERY_INSERT_CODE_COLS, nodeListToMsg, pNode->pCols);
2912 2913
  }
  if (TSDB_CODE_SUCCESS == code) {
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
    code = tlvEncodeU64(pEncoder, PHY_QUERY_INSERT_CODE_TABLE_ID, pNode->tableId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeU64(pEncoder, PHY_QUERY_INSERT_CODE_STABLE_ID, pNode->stableId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_QUERY_INSERT_CODE_TABLE_TYPE, pNode->tableType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, PHY_QUERY_INSERT_CODE_TABLE_NAME, pNode->tableName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI32(pEncoder, PHY_QUERY_INSERT_CODE_VG_ID, pNode->vgId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_QUERY_INSERT_CODE_EP_SET, epSetToMsg, &pNode->epSet);
2930 2931 2932 2933 2934
  }

  return code;
}

2935 2936
static int32_t msgToPhysiQueryInsertNode(STlvDecoder* pDecoder, void* pObj) {
  SQueryInserterNode* pNode = (SQueryInserterNode*)pObj;
2937 2938 2939 2940 2941

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
2942 2943
      case PHY_QUERY_INSERT_CODE_SINK:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysicDataSinkNode, &pNode->sink);
2944
        break;
2945 2946
      case PHY_QUERY_INSERT_CODE_COLS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pCols);
2947
        break;
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
      case PHY_QUERY_INSERT_CODE_TABLE_ID:
        code = tlvDecodeU64(pTlv, &pNode->tableId);
        break;
      case PHY_QUERY_INSERT_CODE_STABLE_ID:
        code = tlvDecodeU64(pTlv, &pNode->stableId);
        break;
      case PHY_QUERY_INSERT_CODE_TABLE_TYPE:
        code = tlvDecodeI8(pTlv, &pNode->tableType);
        break;
      case PHY_QUERY_INSERT_CODE_TABLE_NAME:
        code = tlvDecodeCStr(pTlv, pNode->tableName);
        break;
      case PHY_QUERY_INSERT_CODE_VG_ID:
        code = tlvDecodeI32(pTlv, &pNode->vgId);
        break;
      case PHY_QUERY_INSERT_CODE_EP_SET:
        code = tlvDecodeObjFromTlv(pTlv, msgToEpSet, &pNode->epSet);
2965 2966 2967 2968 2969 2970 2971 2972 2973
        break;
      default:
        break;
    }
  }

  return code;
}

2974 2975 2976 2977 2978 2979 2980 2981 2982
enum {
  PHY_DELETER_CODE_SINK = 1,
  PHY_DELETER_CODE_TABLE_ID,
  PHY_DELETER_CODE_TABLE_TYPE,
  PHY_DELETER_CODE_TABLE_FNAME,
  PHY_DELETER_CODE_TS_COL_NAME,
  PHY_DELETER_CODE_DELETE_TIME_RANGE,
  PHY_DELETER_CODE_AFFECTED_ROWS
};
2983

2984 2985
static int32_t physiDeleteNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SDataDeleterNode* pNode = (const SDataDeleterNode*)pObj;
2986

2987
  int32_t code = tlvEncodeObj(pEncoder, PHY_DELETER_CODE_SINK, physicDataSinkNodeToMsg, &pNode->sink);
2988
  if (TSDB_CODE_SUCCESS == code) {
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
    code = tlvEncodeU64(pEncoder, PHY_DELETER_CODE_TABLE_ID, pNode->tableId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeI8(pEncoder, PHY_DELETER_CODE_TABLE_TYPE, pNode->tableType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, PHY_DELETER_CODE_TABLE_FNAME, pNode->tableFName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeCStr(pEncoder, PHY_DELETER_CODE_TS_COL_NAME, pNode->tsColName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_DELETER_CODE_DELETE_TIME_RANGE, timeWindowToMsg, &pNode->deleteTimeRange);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, PHY_DELETER_CODE_AFFECTED_ROWS, nodeToMsg, pNode->pAffectedRows);
3005 3006 3007 3008 3009
  }

  return code;
}

3010 3011
static int32_t msgToPhysiDeleteNode(STlvDecoder* pDecoder, void* pObj) {
  SDataDeleterNode* pNode = (SDataDeleterNode*)pObj;
3012 3013 3014 3015 3016

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
3017 3018
      case PHY_DELETER_CODE_SINK:
        code = tlvDecodeObjFromTlv(pTlv, msgToPhysicDataSinkNode, &pNode->sink);
3019
        break;
3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
      case PHY_DELETER_CODE_TABLE_ID:
        code = tlvDecodeU64(pTlv, &pNode->tableId);
        break;
      case PHY_DELETER_CODE_TABLE_TYPE:
        code = tlvDecodeI8(pTlv, &pNode->tableType);
        break;
      case PHY_DELETER_CODE_TABLE_FNAME:
        code = tlvDecodeCStr(pTlv, pNode->tableFName);
        break;
      case PHY_DELETER_CODE_TS_COL_NAME:
        code = tlvDecodeCStr(pTlv, pNode->tsColName);
        break;
      case PHY_DELETER_CODE_DELETE_TIME_RANGE:
        code = tlvDecodeObjFromTlv(pTlv, msgToTimeWindow, &pNode->deleteTimeRange);
        break;
      case PHY_DELETER_CODE_AFFECTED_ROWS:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pAffectedRows);
3037 3038 3039 3040 3041 3042 3043 3044 3045
        break;
      default:
        break;
    }
  }

  return code;
}

3046
enum { SUBPLAN_ID_CODE_QUERY_ID = 1, SUBPLAN_ID_CODE_GROUP_ID, SUBPLAN_ID_CODE_SUBPLAN_ID };
3047

X
Xiaoyu Wang 已提交
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
static int32_t subplanIdInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSubplanId* pNode = (const SSubplanId*)pObj;

  int32_t code = tlvEncodeValueU64(pEncoder, pNode->queryId);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->groupId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeValueI32(pEncoder, pNode->subplanId);
  }

  return code;
}

3062 3063
static int32_t subplanIdToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSubplanId* pNode = (const SSubplanId*)pObj;
3064

3065
  int32_t code = tlvEncodeU64(pEncoder, SUBPLAN_ID_CODE_QUERY_ID, pNode->queryId);
3066
  if (TSDB_CODE_SUCCESS == code) {
3067
    code = tlvEncodeI32(pEncoder, SUBPLAN_ID_CODE_GROUP_ID, pNode->groupId);
3068 3069
  }
  if (TSDB_CODE_SUCCESS == code) {
3070 3071 3072 3073 3074 3075
    code = tlvEncodeI32(pEncoder, SUBPLAN_ID_CODE_SUBPLAN_ID, pNode->subplanId);
  }

  return code;
}

X
Xiaoyu Wang 已提交
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
static int32_t msgToSubplanIdInline(STlvDecoder* pDecoder, void* pObj) {
  SSubplanId* pNode = (SSubplanId*)pObj;

  int32_t code = tlvDecodeValueU64(pDecoder, &pNode->queryId);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->groupId);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->subplanId);
  }

  return code;
}

3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
static int32_t msgToSubplanId(STlvDecoder* pDecoder, void* pObj) {
  SSubplanId* pNode = (SSubplanId*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
      case SUBPLAN_ID_CODE_QUERY_ID:
        code = tlvDecodeU64(pTlv, &pNode->queryId);
        break;
      case SUBPLAN_ID_CODE_GROUP_ID:
        code = tlvDecodeI32(pTlv, &pNode->groupId);
        break;
      case SUBPLAN_ID_CODE_SUBPLAN_ID:
        code = tlvDecodeI32(pTlv, &pNode->subplanId);
        break;
      default:
        break;
    }
3109
  }
3110 3111 3112 3113

  return code;
}

3114
enum {
X
Xiaoyu Wang 已提交
3115
  SUBPLAN_CODE_INLINE_ATTRS = 1,
3116 3117 3118 3119 3120 3121
  SUBPLAN_CODE_ROOT_NODE,
  SUBPLAN_CODE_DATA_SINK,
  SUBPLAN_CODE_TAG_COND,
  SUBPLAN_CODE_TAG_INDEX_COND
};

X
Xiaoyu Wang 已提交
3122
static int32_t subplanInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
3123 3124
  const SSubplan* pNode = (const SSubplan*)pObj;

X
Xiaoyu Wang 已提交
3125
  int32_t code = subplanIdInlineToMsg(&pNode->id, pEncoder);
3126
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3127
    code = tlvEncodeValueEnum(pEncoder, pNode->subplanType);
3128 3129
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3130
    code = tlvEncodeValueI32(pEncoder, pNode->msgType);
3131 3132
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3133
    code = tlvEncodeValueI32(pEncoder, pNode->level);
3134 3135
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3136
    code = tlvEncodeValueCStr(pEncoder, pNode->dbFName);
3137 3138
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3139
    code = tlvEncodeValueCStr(pEncoder, pNode->user);
3140 3141
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3142
    code = queryNodeAddrInlineToMsg(&pNode->execNode, pEncoder);
3143
  }
X
Xiaoyu Wang 已提交
3144 3145 3146 3147 3148 3149 3150 3151

  return code;
}

static int32_t subplanToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SSubplan* pNode = (const SSubplan*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, SUBPLAN_CODE_INLINE_ATTRS, subplanInlineToMsg, pNode);
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, SUBPLAN_CODE_ROOT_NODE, nodeToMsg, pNode->pNode);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, SUBPLAN_CODE_DATA_SINK, nodeToMsg, pNode->pDataSink);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, SUBPLAN_CODE_TAG_COND, nodeToMsg, pNode->pTagCond);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, SUBPLAN_CODE_TAG_INDEX_COND, nodeToMsg, pNode->pTagIndexCond);
  }

  return code;
}

X
Xiaoyu Wang 已提交
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
static int32_t msgToSubplanInline(STlvDecoder* pDecoder, void* pObj) {
  SSubplan* pNode = (SSubplan*)pObj;

  int32_t code = msgToSubplanIdInline(pDecoder, &pNode->id);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueEnum(pDecoder, &pNode->subplanType, sizeof(pNode->subplanType));
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->msgType);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->level);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueCStr(pDecoder, pNode->dbFName);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueCStr(pDecoder, pNode->user);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = msgToQueryNodeAddrInline(pDecoder, &pNode->execNode);
  }

  return code;
}

3194 3195 3196 3197 3198 3199 3200
static int32_t msgToSubplan(STlvDecoder* pDecoder, void* pObj) {
  SSubplan* pNode = (SSubplan*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
3201 3202
      case SUBPLAN_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToSubplanInline, pNode);
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
        break;
      case SUBPLAN_CODE_ROOT_NODE:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pNode);
        break;
      case SUBPLAN_CODE_DATA_SINK:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pDataSink);
        break;
      case SUBPLAN_CODE_TAG_COND:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pTagCond);
        break;
      case SUBPLAN_CODE_TAG_INDEX_COND:
        code = msgToNodeFromTlv(pTlv, (void**)&pNode->pTagIndexCond);
        break;
      default:
        break;
    }
  }

  return code;
}

X
Xiaoyu Wang 已提交
3224
enum { QUERY_PLAN_CODE_INLINE_ATTRS = 1, QUERY_PLAN_CODE_SUBPLANS };
3225

X
Xiaoyu Wang 已提交
3226
static int32_t queryPlanInlineToMsg(const void* pObj, STlvEncoder* pEncoder) {
3227 3228
  const SQueryPlan* pNode = (const SQueryPlan*)pObj;

X
Xiaoyu Wang 已提交
3229
  int32_t code = tlvEncodeValueU64(pEncoder, pNode->queryId);
3230
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3231
    code = tlvEncodeValueI32(pEncoder, pNode->numOfSubplans);
3232
  }
X
Xiaoyu Wang 已提交
3233 3234 3235 3236 3237 3238 3239 3240

  return code;
}

static int32_t queryPlanToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SQueryPlan* pNode = (const SQueryPlan*)pObj;

  int32_t code = tlvEncodeObj(pEncoder, QUERY_PLAN_CODE_INLINE_ATTRS, queryPlanInlineToMsg, pNode);
3241 3242 3243 3244 3245 3246 3247
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvEncodeObj(pEncoder, QUERY_PLAN_CODE_SUBPLANS, nodeListToMsg, pNode->pSubplans);
  }

  return code;
}

X
Xiaoyu Wang 已提交
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
static int32_t msgToQueryPlanInline(STlvDecoder* pDecoder, void* pObj) {
  SQueryPlan* pNode = (SQueryPlan*)pObj;

  int32_t code = tlvDecodeValueU64(pDecoder, &pNode->queryId);
  if (TSDB_CODE_SUCCESS == code) {
    code = tlvDecodeValueI32(pDecoder, &pNode->numOfSubplans);
  }

  return code;
}

3259 3260 3261 3262 3263 3264 3265
static int32_t msgToQueryPlan(STlvDecoder* pDecoder, void* pObj) {
  SQueryPlan* pNode = (SQueryPlan*)pObj;

  int32_t code = TSDB_CODE_SUCCESS;
  STlv*   pTlv = NULL;
  tlvForEach(pDecoder, pTlv, code) {
    switch (pTlv->type) {
X
Xiaoyu Wang 已提交
3266 3267
      case QUERY_PLAN_CODE_INLINE_ATTRS:
        code = tlvDecodeObjFromTlv(pTlv, msgToQueryPlanInline, pNode);
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
        break;
      case QUERY_PLAN_CODE_SUBPLANS:
        code = msgToNodeListFromTlv(pTlv, (void**)&pNode->pSubplans);
        break;
      default:
        break;
    }
  }

  return code;
}

3280
static int32_t specificNodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
X
Xiaoyu Wang 已提交
3281
  // nodesWarn("specificNodeToMsg node = %s, before tlv count = %d", nodesNodeName(nodeType(pObj)), pEncoder->tlvCount);
3282
  int32_t code = TSDB_CODE_SUCCESS;
3283 3284
  switch (nodeType(pObj)) {
    case QUERY_NODE_COLUMN:
3285 3286
      code = columnNodeToMsg(pObj, pEncoder);
      break;
3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
    case QUERY_NODE_VALUE:
      code = valueNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_OPERATOR:
      code = operatorNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_LOGIC_CONDITION:
      code = logicConditionNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_FUNCTION:
      code = functionNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      code = orderByExprNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_LIMIT:
      code = limitNodeToMsg(pObj, pEncoder);
      break;
3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316
    case QUERY_NODE_NODE_LIST:
      code = nodeListNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_TARGET:
      code = targetNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      code = dataBlockDescNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_SLOT_DESC:
      code = slotDescNodeToMsg(pObj, pEncoder);
      break;
3317 3318
    case QUERY_NODE_DOWNSTREAM_SOURCE:
      return downstreamSourceNodeToMsg(pObj, pEncoder);
3319 3320 3321 3322 3323 3324 3325 3326 3327
    case QUERY_NODE_LEFT_VALUE:
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
      code = physiScanNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
      code = physiLastRowScanNodeToMsg(pObj, pEncoder);
      break;
3328
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
3329 3330
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
3331 3332
      code = physiTableScanNodeToMsg(pObj, pEncoder);
      break;
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
      code = physiSysTableScanNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
      code = physiProjectNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
      code = physiJoinNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
      code = physiAggNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
      code = physiExchangeNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
      code = physiMergeNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT:
      code = physiSortNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
      code = physiIntervalNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
      code = physiFillNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
      code = physiSessionWindowNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
      code = physiStateWindowNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
      code = physiPartitionNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC:
      code = physiIndefRowsFuncNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
      code = physiInterpFuncNodeToMsg(pObj, pEncoder);
      break;
3385 3386 3387
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
      code = physiDispatchNodeToMsg(pObj, pEncoder);
      break;
3388 3389 3390 3391 3392 3393
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT:
      code = physiQueryInsertNodeToMsg(pObj, pEncoder);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_DELETE:
      code = physiDeleteNodeToMsg(pObj, pEncoder);
      break;
3394
    case QUERY_NODE_PHYSICAL_SUBPLAN:
3395 3396
      code = subplanToMsg(pObj, pEncoder);
      break;
3397
    case QUERY_NODE_PHYSICAL_PLAN:
3398 3399
      code = queryPlanToMsg(pObj, pEncoder);
      break;
3400 3401
    default:
      break;
3402 3403 3404 3405
  }
  if (TSDB_CODE_SUCCESS != code) {
    nodesError("specificNodeToMsg error node = %s", nodesNodeName(nodeType(pObj)));
  }
X
Xiaoyu Wang 已提交
3406
  // nodesWarn("specificNodeToMsg node = %s, after tlv count = %d", nodesNodeName(nodeType(pObj)), pEncoder->tlvCount);
3407
  return code;
3408 3409 3410
}

static int32_t msgToSpecificNode(STlvDecoder* pDecoder, void* pObj) {
3411
  int32_t code = TSDB_CODE_SUCCESS;
3412 3413
  switch (nodeType(pObj)) {
    case QUERY_NODE_COLUMN:
3414 3415
      code = msgToColumnNode(pDecoder, pObj);
      break;
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
    case QUERY_NODE_VALUE:
      code = msgToValueNode(pDecoder, pObj);
      break;
    case QUERY_NODE_OPERATOR:
      code = msgToOperatorNode(pDecoder, pObj);
      break;
    case QUERY_NODE_LOGIC_CONDITION:
      code = msgToLogicConditionNode(pDecoder, pObj);
      break;
    case QUERY_NODE_FUNCTION:
      code = msgToFunctionNode(pDecoder, pObj);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      code = msgToOrderByExprNode(pDecoder, pObj);
      break;
    case QUERY_NODE_LIMIT:
      code = msgToLimitNode(pDecoder, pObj);
      break;
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
    case QUERY_NODE_NODE_LIST:
      code = msgToNodeListNode(pDecoder, pObj);
      break;
    case QUERY_NODE_TARGET:
      code = msgToTargetNode(pDecoder, pObj);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      code = msgToDataBlockDescNode(pDecoder, pObj);
      break;
    case QUERY_NODE_SLOT_DESC:
      code = msgToSlotDescNode(pDecoder, pObj);
      break;
3446 3447
    case QUERY_NODE_DOWNSTREAM_SOURCE:
      return msgToDownstreamSourceNode(pDecoder, pObj);
3448 3449 3450 3451 3452 3453 3454 3455 3456
    case QUERY_NODE_LEFT_VALUE:
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
      code = msgToPhysiScanNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
      code = msgToPhysiLastRowScanNode(pDecoder, pObj);
      break;
3457
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
3458 3459
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
3460 3461
      code = msgToPhysiTableScanNode(pDecoder, pObj);
      break;
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
      code = msgToPhysiSysTableScanNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
      code = msgToPhysiProjectNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
      code = msgToPhysiJoinNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
      code = msgToPhysiAggNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
      code = msgToPhysiExchangeNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
      code = msgToPhysiMergeNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT:
      code = msgToPhysiSortNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
      code = msgToPhysiIntervalNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
      code = msgToPhysiFillNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
      code = msgToPhysiSessionWindowNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
      code = msgToPhysiStateWindowNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
      code = msgToPhysiPartitionNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC:
      code = msgToPhysiIndefRowsFuncNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
      code = msgToPhysiInterpFuncNode(pDecoder, pObj);
      break;
3514 3515 3516
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
      code = msgToPhysiDispatchNode(pDecoder, pObj);
      break;
3517 3518 3519 3520 3521 3522
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT:
      code = msgToPhysiQueryInsertNode(pDecoder, pObj);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_DELETE:
      code = msgToPhysiDeleteNode(pDecoder, pObj);
      break;
3523
    case QUERY_NODE_PHYSICAL_SUBPLAN:
3524 3525
      code = msgToSubplan(pDecoder, pObj);
      break;
3526
    case QUERY_NODE_PHYSICAL_PLAN:
3527 3528
      code = msgToQueryPlan(pDecoder, pObj);
      break;
3529 3530
    default:
      break;
3531 3532 3533 3534 3535
  }
  if (TSDB_CODE_SUCCESS != code) {
    nodesError("msgToSpecificNode error node = %s", nodesNodeName(nodeType(pObj)));
  }
  return code;
3536 3537 3538 3539 3540 3541 3542
}

static int32_t nodeToMsg(const void* pObj, STlvEncoder* pEncoder) {
  return tlvEncodeObj(pEncoder, nodeType(pObj), specificNodeToMsg, pObj);
}

static int32_t msgToNode(STlvDecoder* pDecoder, void** pObj) {
3543 3544 3545 3546 3547 3548
  return tlvDecodeDynObj(pDecoder, (FMakeObject)nodesMakeNode, msgToSpecificNode, pObj);
}

static int32_t msgToNodeFromTlv(STlv* pTlv, void** pObj) {
  STlvDecoder decoder = {.bufSize = pTlv->len, .offset = 0, .pBuf = pTlv->value};
  return msgToNode(&decoder, pObj);
3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
}

static int32_t nodeListToMsg(const void* pObj, STlvEncoder* pEncoder) {
  const SNodeList* pList = (const SNodeList*)pObj;

  SNode* pNode = NULL;
  FOREACH(pNode, pList) {
    int32_t code = nodeToMsg(pNode, pEncoder);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
  }

  return TSDB_CODE_SUCCESS;
}

3565 3566
static int32_t msgToNodeList(STlvDecoder* pDecoder, void** pObj) {
  SNodeList* pList = nodesMakeList();
3567 3568 3569 3570 3571 3572 3573 3574 3575

  int32_t code = TSDB_CODE_SUCCESS;
  while (TSDB_CODE_SUCCESS == code && !tlvDecodeEnd(pDecoder)) {
    SNode* pNode = NULL;
    code = msgToNode(pDecoder, (void**)&pNode);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListAppend(pList, pNode);
    }
  }
3576 3577 3578 3579 3580
  if (TSDB_CODE_SUCCESS == code) {
    *pObj = pList;
  } else {
    nodesDestroyList(pList);
  }
3581 3582 3583 3584 3585 3586 3587 3588
  return code;
}

static int32_t msgToNodeListFromTlv(STlv* pTlv, void** pObj) {
  STlvDecoder decoder = {.bufSize = pTlv->len, .offset = 0, .pBuf = pTlv->value};
  return msgToNodeList(&decoder, pObj);
}

3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
int32_t nodesNodeToMsg(const SNode* pNode, char** pMsg, int32_t* pLen) {
  if (NULL == pNode || NULL == pMsg || NULL == pLen) {
    terrno = TSDB_CODE_FAILED;
    return TSDB_CODE_FAILED;
  }

  STlvEncoder encoder;
  int32_t     code = initTlvEncoder(&encoder);
  if (TSDB_CODE_SUCCESS == code) {
    code = nodeToMsg(pNode, &encoder);
  }
  if (TSDB_CODE_SUCCESS == code) {
    endTlvEncode(&encoder, pMsg, pLen);
  }
  clearTlvEncoder(&encoder);

  terrno = code;
  return code;
}

int32_t nodesMsgToNode(const char* pMsg, int32_t len, SNode** pNode) {
  if (NULL == pMsg || NULL == pNode) {
    return TSDB_CODE_SUCCESS;
  }

  STlvDecoder decoder = {.bufSize = len, .offset = 0, .pBuf = pMsg};
  int32_t     code = msgToNode(&decoder, (void**)pNode);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyNode(*pNode);
    *pNode = NULL;
  }

  terrno = code;
  return code;
}