tscLocal.c 32.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16
#include "os.h"
H
hzcheng 已提交
17 18
#include "taosmsg.h"

H
hzcheng 已提交
19
#include "taosdef.h"
H
Haojun Liao 已提交
20
#include "tname.h"
S
slguan 已提交
21
#include "tscLog.h"
H
Haojun Liao 已提交
22
#include "tscUtil.h"
H
hzcheng 已提交
23
#include "tschemautil.h"
H
Haojun Liao 已提交
24
#include "tsclient.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "taos.h"
#include "tscSubquery.h"

#define STR_NOCASE_EQUAL(str1, len1, str2, len2) ((len1 == len2) && 0 == strncasecmp(str1, str2, len1)) 

typedef enum BuildType {
  SCREATE_BUILD_TABLE = 1, 
  SCREATE_BUILD_DB    = 2, 
} BuildType; 

typedef enum Stage {
  SCREATE_CALLBACK_QUERY    = 1,
  SCREATE_CALLBACK_RETRIEVE = 2,
} Stage;

// support 'show create table'   
typedef struct SCreateBuilder {
  char sTableName[TSDB_TABLE_FNAME_LEN];
  char buf[TSDB_TABLE_FNAME_LEN];
  SSqlObj *pParentSql; 
  SSqlObj *pInterSql;
  int32_t (*fp)(void *para, char* result);
  Stage callStage;
48 49
} SCreateBuilder;

H
Haojun Liao 已提交
50
static void tscSetLocalQueryResult(SSqlObj *pSql, const char *val, const char *columnName, int16_t type, size_t valueLength);
H
hjxilinx 已提交
51

H
hzcheng 已提交
52 53 54 55
static int32_t tscSetValueToResObj(SSqlObj *pSql, int32_t rowLen) {
  SSqlRes *pRes = &pSql->res;

  // one column for each row
H
Haojun Liao 已提交
56
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
57
  
H
hjxilinx 已提交
58
  STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
H
hjxilinx 已提交
59
  STableMeta *    pMeta = pTableMetaInfo->pTableMeta;
H
hzcheng 已提交
60 61 62 63 64 65 66

  /*
   * tagValueCnt is to denote the number of tags columns for meter, not metric. and is to show the column data.
   * for meter, which is created according to metric, the value of tagValueCnt is not 0, and the numOfTags must be 0.
   * for metric, the value of tagValueCnt must be 0, but the numOfTags is not 0
   */

H
hjxilinx 已提交
67 68
  int32_t numOfRows = tscGetNumOfColumns(pMeta);
  int32_t totalNumOfRows = numOfRows + tscGetNumOfTags(pMeta);
H
hzcheng 已提交
69

weixin_48148422's avatar
weixin_48148422 已提交
70
  if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
H
hjxilinx 已提交
71
    numOfRows = numOfRows + tscGetNumOfTags(pMeta);
H
hzcheng 已提交
72 73 74
  }

  tscInitResObjForLocalQuery(pSql, totalNumOfRows, rowLen);
H
hjxilinx 已提交
75
  SSchema *pSchema = tscGetTableSchema(pMeta);
H
hzcheng 已提交
76 77

  for (int32_t i = 0; i < numOfRows; ++i) {
H
hjxilinx 已提交
78
    TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 0);
H
hjxilinx 已提交
79
    char* dst = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * totalNumOfRows + pField->bytes * i;
H
Hui Li 已提交
80
    STR_WITH_MAXSIZE_TO_VARSTR(dst, pSchema[i].name, pField->bytes);
H
hzcheng 已提交
81

H
Haojun Liao 已提交
82
    char *type = tDataTypes[pSchema[i].type].name;
H
hzcheng 已提交
83

H
hjxilinx 已提交
84
    pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 1);
H
hjxilinx 已提交
85 86
    dst = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 1) * totalNumOfRows + pField->bytes * i;
    
H
Hui Li 已提交
87
    STR_WITH_MAXSIZE_TO_VARSTR(dst, type, pField->bytes);
H
hjxilinx 已提交
88
    
H
hzcheng 已提交
89
    int32_t bytes = pSchema[i].bytes;
H
hjxilinx 已提交
90 91 92 93 94 95
    if (pSchema[i].type == TSDB_DATA_TYPE_BINARY || pSchema[i].type == TSDB_DATA_TYPE_NCHAR) {
      bytes -= VARSTR_HEADER_SIZE;
      
      if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) {
        bytes = bytes / TSDB_NCHAR_SIZE;
      }
H
hzcheng 已提交
96 97
    }

H
hjxilinx 已提交
98
    pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 2);
99
    *(int32_t *)(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 2) * totalNumOfRows + pField->bytes * i) = bytes;
H
hzcheng 已提交
100

H
hjxilinx 已提交
101
    pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 3);
H
hjxilinx 已提交
102
    if (i >= tscGetNumOfColumns(pMeta) && tscGetNumOfTags(pMeta) != 0) {
103
      char* output = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 3) * totalNumOfRows + pField->bytes * i;
sangshuduo's avatar
sangshuduo 已提交
104
      const char *src = "TAG";
H
Hui Li 已提交
105
      STR_WITH_MAXSIZE_TO_VARSTR(output, src, pField->bytes);
H
hzcheng 已提交
106 107 108
    }
  }

weixin_48148422's avatar
weixin_48148422 已提交
109
  if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
H
hzcheng 已提交
110 111 112
    return 0;
  }

H
Haojun Liao 已提交
113
  // the following is handle display tags for table created according to super table
H
hzcheng 已提交
114 115
  for (int32_t i = numOfRows; i < totalNumOfRows; ++i) {
    // field name
H
hjxilinx 已提交
116
    TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 0);
117
    char* output = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * totalNumOfRows + pField->bytes * i;
H
Hui Li 已提交
118
    STR_WITH_MAXSIZE_TO_VARSTR(output, pSchema[i].name, pField->bytes);
H
hzcheng 已提交
119 120

    // type name
H
hjxilinx 已提交
121
    pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 1);
H
Haojun Liao 已提交
122
    char *type = tDataTypes[pSchema[i].type].name;
123 124 125
    
    output = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 1) * totalNumOfRows + pField->bytes * i;
    STR_WITH_MAXSIZE_TO_VARSTR(output, type, pField->bytes);
H
hzcheng 已提交
126 127 128

    // type length
    int32_t bytes = pSchema[i].bytes;
H
hjxilinx 已提交
129
    pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 2);
L
liu0x54 已提交
130 131 132 133 134 135
    if (pSchema[i].type == TSDB_DATA_TYPE_BINARY || pSchema[i].type == TSDB_DATA_TYPE_NCHAR) {
      bytes -= VARSTR_HEADER_SIZE;
      
      if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) {
        bytes = bytes / TSDB_NCHAR_SIZE;
      }
H
hzcheng 已提交
136 137
    }

138
    *(int32_t *)(pRes->data + tscFieldInfoGetOffset(pQueryInfo, 2) * totalNumOfRows + pField->bytes * i) = bytes;
H
hzcheng 已提交
139 140

    // tag value
H
hjxilinx 已提交
141
    pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 3);
142
    char *target = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 3) * totalNumOfRows + pField->bytes * i;
sangshuduo's avatar
sangshuduo 已提交
143
    const char *src = "TAG";
H
Hui Li 已提交
144
    STR_WITH_MAXSIZE_TO_VARSTR(target, src, pField->bytes);
H
hzcheng 已提交
145 146 147 148 149
  }

  return 0;
}

H
hjxilinx 已提交
150
static int32_t tscBuildTableSchemaResultFields(SSqlObj *pSql, int32_t numOfCols, int32_t typeColLength,
H
hzcheng 已提交
151 152
                                               int32_t noteColLength) {
  int32_t  rowLen = 0;
H
hjxilinx 已提交
153 154 155
  SColumnIndex index = {0};
  
  pSql->cmd.numOfCols = numOfCols;
H
hzcheng 已提交
156

H
Haojun Liao 已提交
157
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
158
  pQueryInfo->order.order = TSDB_ORDER_ASC;
H
hzcheng 已提交
159

B
Bomin Zhang 已提交
160 161
  TAOS_FIELD f = {.type = TSDB_DATA_TYPE_BINARY, .bytes = (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE};
  tstrncpy(f.name, "Field", sizeof(f.name));
H
hjxilinx 已提交
162
  
H
Haojun Liao 已提交
163
  SInternalField* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
H
Haojun Liao 已提交
164
  pInfo->pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY,
H
Haojun Liao 已提交
165
      (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE, -1000, (TSDB_COL_NAME_LEN - 1), false);
H
hjxilinx 已提交
166
  
B
Bomin Zhang 已提交
167
  rowLen += ((TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE);
H
hzcheng 已提交
168

S
TD-1057  
Shengliang Guan 已提交
169
  f.bytes = (int16_t)(typeColLength + VARSTR_HEADER_SIZE);
H
hjxilinx 已提交
170
  f.type = TSDB_DATA_TYPE_BINARY;
B
Bomin Zhang 已提交
171
  tstrncpy(f.name, "Type", sizeof(f.name));
H
hjxilinx 已提交
172 173
  
  pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
H
Haojun Liao 已提交
174
  pInfo->pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, (int16_t)(typeColLength + VARSTR_HEADER_SIZE),
H
Haojun Liao 已提交
175
      -1000, typeColLength, false);
H
hjxilinx 已提交
176
  
H
Hui Li 已提交
177
  rowLen += typeColLength + VARSTR_HEADER_SIZE;
H
hzcheng 已提交
178

H
hjxilinx 已提交
179 180
  f.bytes = sizeof(int32_t);
  f.type = TSDB_DATA_TYPE_INT;
B
Bomin Zhang 已提交
181
  tstrncpy(f.name, "Length", sizeof(f.name));
H
hjxilinx 已提交
182 183
  
  pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
H
Haojun Liao 已提交
184
  pInfo->pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_INT, sizeof(int32_t),
H
Haojun Liao 已提交
185
      -1000, sizeof(int32_t), false);
H
hjxilinx 已提交
186
  
H
hzcheng 已提交
187 188
  rowLen += sizeof(int32_t);

S
TD-1057  
Shengliang Guan 已提交
189
  f.bytes = (int16_t)(noteColLength + VARSTR_HEADER_SIZE);
H
hjxilinx 已提交
190
  f.type = TSDB_DATA_TYPE_BINARY;
B
Bomin Zhang 已提交
191
  tstrncpy(f.name, "Note", sizeof(f.name));
H
hjxilinx 已提交
192
  
H
hjxilinx 已提交
193
  pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
H
Haojun Liao 已提交
194
  pInfo->pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, (int16_t)(noteColLength + VARSTR_HEADER_SIZE),
H
Haojun Liao 已提交
195
      -1000, noteColLength, false);
H
hjxilinx 已提交
196
  
H
Hui Li 已提交
197
  rowLen += noteColLength + VARSTR_HEADER_SIZE;
H
hzcheng 已提交
198 199 200 201
  return rowLen;
}

static int32_t tscProcessDescribeTable(SSqlObj *pSql) {
H
Haojun Liao 已提交
202
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
203
  
H
hjxilinx 已提交
204
  assert(tscGetMetaInfo(pQueryInfo, 0)->pTableMeta != NULL);
H
hzcheng 已提交
205

H
hjxilinx 已提交
206
  const int32_t NUM_OF_DESC_TABLE_COLUMNS = 4;
207
  const int32_t TYPE_COLUMN_LENGTH = 20;
H
hzcheng 已提交
208 209
  const int32_t NOTE_COLUMN_MIN_LENGTH = 8;

210
  int32_t noteFieldLen = NOTE_COLUMN_MIN_LENGTH;
H
hzcheng 已提交
211

H
hjxilinx 已提交
212
  int32_t rowLen = tscBuildTableSchemaResultFields(pSql, NUM_OF_DESC_TABLE_COLUMNS, TYPE_COLUMN_LENGTH, noteFieldLen);
H
hjxilinx 已提交
213
  tscFieldInfoUpdateOffset(pQueryInfo);
H
hzcheng 已提交
214 215
  return tscSetValueToResObj(pSql, rowLen);
}
216
static int32_t tscGetNthFieldResult(TAOS_ROW row, TAOS_FIELD* fields, int *lengths, int idx, char *result) {
217
  const char *val = (const char*)row[idx];
218 219 220 221 222 223 224 225 226
  if (val == NULL) {
    sprintf(result, "%s", TSDB_DATA_NULL_STR);
    return -1;
  } 
  uint8_t type = fields[idx].type;
  int32_t length = lengths[idx]; 

  switch (type) {
    case TSDB_DATA_TYPE_BOOL: 
S
TD-1530  
Shengliang Guan 已提交
227
      sprintf(result, "%s", ((((int32_t)(*((char *)val))) == 1) ? "true" : "false"));
228 229
      break;
    case TSDB_DATA_TYPE_TINYINT:
S
TD-1530  
Shengliang Guan 已提交
230
      sprintf(result, "%d", *((int8_t *)val));
231 232
      break;
    case TSDB_DATA_TYPE_SMALLINT: 
S
TD-1530  
Shengliang Guan 已提交
233
      sprintf(result, "%d", *((int16_t *)val));
234 235
      break;
    case TSDB_DATA_TYPE_INT:
S
TD-1530  
Shengliang Guan 已提交
236
      sprintf(result, "%d", *((int32_t *)val));
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      break;
    case TSDB_DATA_TYPE_BIGINT: 
      sprintf(result, "%"PRId64, *((int64_t *)val)); 
      break;
    case TSDB_DATA_TYPE_FLOAT:
      sprintf(result, "%f", GET_FLOAT_VAL(val)); 
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      sprintf(result, "%f", GET_DOUBLE_VAL(val)); 
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_BINARY:
      memcpy(result, val, length); 
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      ///formatTimestamp(buf, *(int64_t*)val, TSDB_TIME_PRECISION_MICRO);
      //memcpy(result, val, strlen(buf));
      sprintf(result, "%"PRId64, *((int64_t *)val)); 
      break;
    default:
      break; 
  }
  return 0;
} 

void tscSCreateCallBack(void *param, TAOS_RES *tres, int code) {
  if (param == NULL || tres == NULL) {
    return;
  }  
  SCreateBuilder *builder = (SCreateBuilder *)(param);
  SSqlObj *pParentSql = builder->pParentSql;  
  SSqlObj *pSql = (SSqlObj *)tres; 

  SSqlRes *pRes = &pParentSql->res;
  pRes->code = taos_errno(pSql); 
  if (pRes->code != TSDB_CODE_SUCCESS) {
    taos_free_result(pSql);  
    free(builder);
H
Haojun Liao 已提交
275
    tscAsyncResultOnError(pParentSql);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    return;
  }

  if (builder->callStage == SCREATE_CALLBACK_QUERY) {
    taos_fetch_rows_a(tres, tscSCreateCallBack, param);    
    builder->callStage = SCREATE_CALLBACK_RETRIEVE;
  } else {
    char *result = calloc(1, TSDB_MAX_BINARY_LEN);
    pRes->code = builder->fp(builder, result);

    taos_free_result(pSql);  
    free(builder);
    free(result);

    if (pRes->code == TSDB_CODE_SUCCESS) {
      (*pParentSql->fp)(pParentSql->param, pParentSql, code);  
    } else {
H
Haojun Liao 已提交
293
      tscAsyncResultOnError(pParentSql);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    }
  }
}

TAOS_ROW tscFetchRow(void *param) {
  SCreateBuilder *builder = (SCreateBuilder *)param;
  if (builder == NULL) {
    return NULL;
  } 
  SSqlObj *pSql = builder->pInterSql;
  if (pSql == NULL || pSql->signature != pSql) {
    terrno = TSDB_CODE_TSC_DISCONNECTED;
    return NULL;
  }
  
  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;
  
H
Haojun Liao 已提交
312
  if (pRes->qId == 0 ||
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
      pCmd->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT ||
      pCmd->command == TSDB_SQL_INSERT) {
    return NULL;
  }

  // set the sql object owner
  tscSetSqlOwner(pSql);

  // current data set are exhausted, fetch more data from node
  if (pRes->row >= pRes->numOfRows && (pRes->completed != true || hasMoreVnodesToTry(pSql) || hasMoreClauseToTry(pSql)) &&
      (pCmd->command == TSDB_SQL_RETRIEVE ||
       pCmd->command == TSDB_SQL_RETRIEVE_LOCALMERGE ||
       pCmd->command == TSDB_SQL_TABLE_JOIN_RETRIEVE ||
       pCmd->command == TSDB_SQL_FETCH ||
       pCmd->command == TSDB_SQL_SHOW ||
       pCmd->command == TSDB_SQL_SHOW_CREATE_TABLE ||
       pCmd->command == TSDB_SQL_SHOW_CREATE_DATABASE ||
       pCmd->command == TSDB_SQL_SELECT ||
       pCmd->command == TSDB_SQL_DESCRIBE_TABLE ||
       pCmd->command == TSDB_SQL_SERV_STATUS ||
       pCmd->command == TSDB_SQL_CURRENT_DB ||
       pCmd->command == TSDB_SQL_SERV_VERSION ||
       pCmd->command == TSDB_SQL_CLI_VERSION ||
       pCmd->command == TSDB_SQL_CURRENT_USER )) {
    taos_fetch_rows_a(pSql, tscSCreateCallBack, param);
    return NULL;
  }

341
  void* data = doSetResultRowData(pSql);
342 343 344 345 346 347 348 349 350

  tscClearSqlOwner(pSql);
  return data;
}
static int32_t tscGetTableTagValue(SCreateBuilder *builder, char *result) {
  TAOS_ROW row = tscFetchRow(builder);
  SSqlObj* pSql = builder->pInterSql;

  if (row == NULL) {
351
   return TSDB_CODE_TSC_INVALID_TABLE_NAME;
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
  }

  int32_t* lengths = taos_fetch_lengths(pSql);  
  int num_fields = taos_num_fields(pSql);
  TAOS_FIELD *fields = taos_fetch_fields(pSql);

  char buf[TSDB_COL_NAME_LEN + 16]; 
  for (int i = 0; i < num_fields; i++) {
    memset(buf, 0, sizeof(buf));
    int32_t ret = tscGetNthFieldResult(row, fields, lengths, i, buf);

    if (i == 0) {
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s", "(");
    } 
    if ((fields[i].type == TSDB_DATA_TYPE_NCHAR 
        || fields[i].type == TSDB_DATA_TYPE_BINARY 
        || fields[i].type == TSDB_DATA_TYPE_TIMESTAMP) && 0 == ret) {
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "\"%s\",", buf);
    } else {
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s,", buf);
    }
    if (i == num_fields - 1) {
      sprintf(result + strlen(result) - 1, "%s", ")");
    }
  }  

  if (0 == strlen(result)) {
379
   return TSDB_CODE_TSC_INVALID_TABLE_NAME;
380 381 382 383 384 385 386 387
  }
  return TSDB_CODE_SUCCESS;
}


// build 'show create table/database' result fields 
static int32_t tscSCreateBuildResultFields(SSqlObj *pSql, BuildType type, const char *ddl) {
  int32_t  rowLen = 0;
S
Shengliang Guan 已提交
388
  int16_t  ddlLen = (int16_t)strlen(ddl); 
389 390 391
  SColumnIndex index = {0};
  pSql->cmd.numOfCols = 2;

H
Haojun Liao 已提交
392
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
393 394 395 396 397 398 399 400 401 402 403 404 405
  pQueryInfo->order.order = TSDB_ORDER_ASC;

  TAOS_FIELD f; 
  if (type == SCREATE_BUILD_TABLE) {
    f.type  = TSDB_DATA_TYPE_BINARY;
    f.bytes = (TSDB_COL_NAME_LEN - 1) + VARSTR_HEADER_SIZE;
    tstrncpy(f.name, "Table", sizeof(f.name));
  } else {
    f.type  = TSDB_DATA_TYPE_BINARY;
    f.bytes =  (TSDB_DB_NAME_LEN - 1) + VARSTR_HEADER_SIZE;
    tstrncpy(f.name, "Database", sizeof(f.name));
  } 

H
Haojun Liao 已提交
406
  SInternalField* pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
H
Haojun Liao 已提交
407
  pInfo->pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY, f.bytes, -1000, f.bytes - VARSTR_HEADER_SIZE, false);
408 409

  rowLen += f.bytes; 
H
hzcheng 已提交
410

411 412 413 414 415 416 417 418 419
  f.bytes = (int16_t)(ddlLen + VARSTR_HEADER_SIZE);
  f.type = TSDB_DATA_TYPE_BINARY;
  if (type == SCREATE_BUILD_TABLE) {
    tstrncpy(f.name, "Create Table", sizeof(f.name));
  } else {
    tstrncpy(f.name, "Create Database", sizeof(f.name));
  }

  pInfo = tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
H
Haojun Liao 已提交
420
  pInfo->pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index, TSDB_DATA_TYPE_BINARY,
H
Haojun Liao 已提交
421
      (int16_t)(ddlLen + VARSTR_HEADER_SIZE), -1000, ddlLen, false);
422 423 424 425 426 427 428 429

  rowLen += ddlLen + VARSTR_HEADER_SIZE;

  return rowLen;
}
static int32_t tscSCreateSetValueToResObj(SSqlObj *pSql, int32_t rowLen, const char *tableName, const char *ddl) {
  SSqlRes *pRes = &pSql->res;

H
Haojun Liao 已提交
430
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  int32_t numOfRows = 1;
  if (strlen(ddl) == 0) {
    
  }
  tscInitResObjForLocalQuery(pSql, numOfRows, rowLen);

  TAOS_FIELD *pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 0);
  char* dst = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 0) * numOfRows;
  STR_WITH_MAXSIZE_TO_VARSTR(dst, tableName, pField->bytes);

  pField = tscFieldInfoGetField(&pQueryInfo->fieldsInfo, 1);
  dst = pRes->data + tscFieldInfoGetOffset(pQueryInfo, 1) * numOfRows;
  STR_WITH_MAXSIZE_TO_VARSTR(dst, ddl, pField->bytes);
  return 0;
}
static int32_t tscSCreateBuildResult(SSqlObj *pSql, BuildType type, const char *str, const char *result) {
H
Haojun Liao 已提交
447
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  int32_t rowLen = tscSCreateBuildResultFields(pSql, type, result);

  tscFieldInfoUpdateOffset(pQueryInfo);
  return tscSCreateSetValueToResObj(pSql, rowLen, str, result);  
}
int32_t tscRebuildCreateTableStatement(void *param,char *result) {
  SCreateBuilder *builder = (SCreateBuilder *)param;
  int32_t code = TSDB_CODE_SUCCESS;

  char *buf = calloc(1,TSDB_MAX_BINARY_LEN);
  if (buf == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

  code = tscGetTableTagValue(builder, buf);
  if (code == TSDB_CODE_SUCCESS) {
    snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "CREATE TABLE %s USING %s TAGS %s", builder->buf, builder->sTableName, buf);
    code = tscSCreateBuildResult(builder->pParentSql, SCREATE_BUILD_TABLE, builder->buf, result);    
  }  
  free(buf);
  return code;
}

static int32_t tscGetDBInfo(SCreateBuilder *builder, char *result) {
  TAOS_ROW row = tscFetchRow(builder);
  if (row == NULL) {
474
   return TSDB_CODE_TSC_DB_NOT_SELECTED;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  }
  const char *showColumns[] = {"REPLICA", "QUORUM", "DAYS", "KEEP", "BLOCKS", NULL};

  SSqlObj *pSql = builder->pInterSql;
  TAOS_FIELD *fields = taos_fetch_fields(pSql);
  int num_fields = taos_num_fields(pSql);

  char buf[TSDB_DB_NAME_LEN + 64] = {0}; 
  do {
    int32_t* lengths = taos_fetch_lengths(pSql);  
    int32_t ret = tscGetNthFieldResult(row, fields, lengths, 0, buf);
    if (0 == ret && STR_NOCASE_EQUAL(buf, strlen(buf), builder->buf, strlen(builder->buf))) {
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "CREATE DATABASE %s", buf);  
      for (int i = 1; i < num_fields; i++) {
        for (int j = 0; showColumns[j] != NULL; j++) {
          if (STR_NOCASE_EQUAL(fields[i].name, strlen(fields[i].name), showColumns[j], strlen(showColumns[j]))) {
            memset(buf, 0, sizeof(buf));
            ret = tscGetNthFieldResult(row, fields, lengths, i, buf);
            if (ret == 0) {
              snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), " %s %s", showColumns[j], buf); 
            }
          }
        }
      }
      break;
    } 
    
    row = tscFetchRow(builder);
  } while (row != NULL);

  if (0 == strlen(result)) {
506
   return TSDB_CODE_TSC_DB_NOT_SELECTED;
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 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 551 552 553 554
  }

  return TSDB_CODE_SUCCESS;
}
int32_t tscRebuildCreateDBStatement(void *param,char *result) {
  SCreateBuilder *builder = (SCreateBuilder *)param;
  int32_t code = TSDB_CODE_SUCCESS;

  char *buf = calloc(1, TSDB_MAX_BINARY_LEN);
  if (buf == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
  code = tscGetDBInfo(param, buf);  
  if (code == TSDB_CODE_SUCCESS) {
    code = tscSCreateBuildResult(builder->pParentSql, SCREATE_BUILD_DB, builder->buf, buf);    
  }
  free(buf);
  return code;
}

static int32_t tscGetTableTagColumnName(SSqlObj *pSql, char **result) {
  char *buf = (char *)malloc(TSDB_MAX_BINARY_LEN);
  if (buf == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY; 
  }
  buf[0] = 0;

  STableMeta *pMeta = tscGetTableMetaInfoFromCmd(&pSql->cmd, 0, 0)->pTableMeta; 
  if (pMeta->tableType == TSDB_SUPER_TABLE || pMeta->tableType == TSDB_NORMAL_TABLE ||
      pMeta->tableType == TSDB_STREAM_TABLE) {
    free(buf);
    return TSDB_CODE_TSC_INVALID_VALUE;
  } 

  SSchema *pTagsSchema = tscGetTableTagSchema(pMeta);  
  int32_t numOfTags = tscGetNumOfTags(pMeta);
  for (int32_t i = 0; i < numOfTags; i++) {
    if (i != numOfTags - 1) {
      snprintf(buf + strlen(buf), TSDB_MAX_BINARY_LEN - strlen(buf), "%s,", pTagsSchema[i].name);  
    } else {
      snprintf(buf + strlen(buf), TSDB_MAX_BINARY_LEN - strlen(buf), "%s", pTagsSchema[i].name);
    }
  }   

  *result = buf;
  return TSDB_CODE_SUCCESS;
}  
static int32_t tscRebuildDDLForSubTable(SSqlObj *pSql, const char *tableName, char *ddl) {
H
Haojun Liao 已提交
555
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

  STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
  STableMeta *    pMeta = pTableMetaInfo->pTableMeta;

  SSqlObj *pInterSql = (SSqlObj *)calloc(1, sizeof(SSqlObj)); 
  if (pInterSql == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }  

  SCreateBuilder *param = (SCreateBuilder *)malloc(sizeof(SCreateBuilder));    
  if (param == NULL) {
    free(pInterSql);
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

S
Shengliang Guan 已提交
571
  char fullName[TSDB_TABLE_FNAME_LEN * 2] = {0};
572 573
  tNameGetDbName(&pTableMetaInfo->name, fullName);

574
  extractTableName(pMeta->sTableName, param->sTableName);
575
  snprintf(fullName + strlen(fullName), TSDB_TABLE_FNAME_LEN - strlen(fullName),  ".%s", param->sTableName);
576 577

  strncpy(param->buf, tNameGetTableName(&pTableMetaInfo->name), TSDB_TABLE_NAME_LEN);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

  param->pParentSql = pSql;
  param->pInterSql  = pInterSql;
  param->fp         = tscRebuildCreateTableStatement;
  param->callStage  = SCREATE_CALLBACK_QUERY;

  char *query = (char *)calloc(1, TSDB_MAX_BINARY_LEN); 
  if (query == NULL) {
    free(param);
    free(pInterSql);
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

  char *columns = NULL;
  int32_t code = tscGetTableTagColumnName(pSql, &columns) ;
  if (code != TSDB_CODE_SUCCESS) {
    free(param); 
    free(pInterSql);
Y
fix bug  
yihaoDeng 已提交
596
    free(query);
597 598 599 600 601 602 603 604 605 606
    return code;
  }

  snprintf(query + strlen(query), TSDB_MAX_BINARY_LEN - strlen(query), "SELECT %s FROM %s WHERE TBNAME IN(\'%s\')", columns, fullName, param->buf);
  doAsyncQuery(pSql->pTscObj, pInterSql, tscSCreateCallBack, param, query, strlen(query));
  free(query);
  free(columns);

  return TSDB_CODE_TSC_ACTION_IN_PROGRESS; 
}
607

608
static int32_t tscRebuildDDLForNormalTable(SSqlObj *pSql, const char *tableName, char *ddl) {
H
Haojun Liao 已提交
609
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
610 611 612 613 614 615 616 617 618 619 620
  STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
  STableMeta *    pMeta = pTableMetaInfo->pTableMeta;

  int32_t numOfRows = tscGetNumOfColumns(pMeta);
  SSchema *pSchema = tscGetTableSchema(pMeta);

  char *result = ddl;
  sprintf(result, "create table %s (", tableName);
  for (int32_t i = 0; i < numOfRows; ++i) {
    uint8_t type = pSchema[i].type;
    if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
621 622 623 624
      int32_t bytes = pSchema[i].bytes - VARSTR_HEADER_SIZE;
      if (type == TSDB_DATA_TYPE_NCHAR) {
        bytes =  bytes/TSDB_NCHAR_SIZE;
      }
H
Haojun Liao 已提交
625
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s %s(%d),", pSchema[i].name, tDataTypes[pSchema[i].type].name, bytes);
626
    } else {
H
Haojun Liao 已提交
627
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s %s,", pSchema[i].name, tDataTypes[pSchema[i].type].name);
628 629 630 631 632 633 634 635
    }
  }
  sprintf(result + strlen(result) - 1, "%s", ")");

  return TSDB_CODE_SUCCESS;
}
static int32_t tscRebuildDDLForSuperTable(SSqlObj *pSql, const char *tableName, char *ddl) {
  char *result = ddl;
H
Haojun Liao 已提交
636
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
637 638 639 640 641 642 643 644 645 646 647
  STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
  STableMeta *    pMeta = pTableMetaInfo->pTableMeta;

  int32_t numOfRows = tscGetNumOfColumns(pMeta);
  int32_t totalRows = numOfRows + tscGetNumOfTags(pMeta);
  SSchema *pSchema = tscGetTableSchema(pMeta);

  sprintf(result, "create table %s (", tableName);
  for (int32_t i = 0; i < numOfRows; ++i) {
    uint8_t type = pSchema[i].type;
    if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
648 649 650 651
      int32_t bytes = pSchema[i].bytes - VARSTR_HEADER_SIZE;
      if (type == TSDB_DATA_TYPE_NCHAR) {
        bytes =  bytes/TSDB_NCHAR_SIZE;
      }
H
Haojun Liao 已提交
652
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result),"%s %s(%d),", pSchema[i].name,tDataTypes[pSchema[i].type].name, bytes);
653
    } else {
H
Haojun Liao 已提交
654
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s %s,", pSchema[i].name, tDataTypes[type].name);
655 656
    }
  }
Y
fix bug  
yihaoDeng 已提交
657
  snprintf(result + strlen(result) - 1, TSDB_MAX_BINARY_LEN - strlen(result), "%s %s", ")", "TAGS (");
658 659 660 661

  for (int32_t i = numOfRows; i < totalRows; i++) {
    uint8_t type = pSchema[i].type;
    if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
662 663 664 665
      int32_t bytes = pSchema[i].bytes - VARSTR_HEADER_SIZE;
      if (type == TSDB_DATA_TYPE_NCHAR) {
        bytes =  bytes/TSDB_NCHAR_SIZE;
      }
H
Haojun Liao 已提交
666
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s %s(%d),", pSchema[i].name,tDataTypes[pSchema[i].type].name, bytes);
667
    } else {
H
Haojun Liao 已提交
668
      snprintf(result + strlen(result), TSDB_MAX_BINARY_LEN - strlen(result), "%s %s,", pSchema[i].name, tDataTypes[type].name);
669 670 671 672 673 674 675 676
    }
  }
  sprintf(result + strlen(result) - 1, "%s", ")");

  return TSDB_CODE_SUCCESS;
}

static int32_t tscProcessShowCreateTable(SSqlObj *pSql) {
H
Haojun Liao 已提交
677
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
678 679 680
  STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
  assert(pTableMetaInfo->pTableMeta != NULL);

681
  const char* tableName = tNameGetTableName(&pTableMetaInfo->name);
682

Y
fix bug  
yihaoDeng 已提交
683
  char *result = (char *)calloc(1, TSDB_MAX_BINARY_LEN);
684 685 686 687 688 689 690 691 692 693 694
  int32_t code = TSDB_CODE_SUCCESS;
  if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) {
    code = tscRebuildDDLForSuperTable(pSql, tableName, result);
  } else if (UTIL_TABLE_IS_NORMAL_TABLE(pTableMetaInfo)) {
    code = tscRebuildDDLForNormalTable(pSql, tableName, result);
  } else if (UTIL_TABLE_IS_CHILD_TABLE(pTableMetaInfo)) {
    code = tscRebuildDDLForSubTable(pSql, tableName, result);
  } else {
    code = TSDB_CODE_TSC_INVALID_VALUE;
  }

Y
fix bug  
yihaoDeng 已提交
695 696 697 698 699
  if (code == TSDB_CODE_SUCCESS) {
    code = tscSCreateBuildResult(pSql, SCREATE_BUILD_TABLE, tableName, result);
  } 
  free(result);
  return code;
700 701 702
}

static int32_t tscProcessShowCreateDatabase(SSqlObj *pSql) {
H
Haojun Liao 已提交
703
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
704 705 706 707 708 709 710 711 712 713 714 715 716

  STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);

  SSqlObj *pInterSql = (SSqlObj *)calloc(1, sizeof(SSqlObj)); 
  if (pInterSql == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }  

  SCreateBuilder *param = (SCreateBuilder *)malloc(sizeof(SCreateBuilder));    
  if (param == NULL) {
    free(pInterSql);
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
717 718 719

  strncpy(param->buf, tNameGetTableName(&pTableMetaInfo->name), TSDB_TABLE_NAME_LEN);

720 721 722 723 724 725 726 727 728
  param->pParentSql = pSql;
  param->pInterSql  = pInterSql;
  param->fp         = tscRebuildCreateDBStatement;
  param->callStage  = SCREATE_CALLBACK_QUERY;
   
  const char *query = "show databases";
  doAsyncQuery(pSql->pTscObj, pInterSql, tscSCreateCallBack, param, query, strlen(query));
  return TSDB_CODE_TSC_ACTION_IN_PROGRESS;
}
H
Haojun Liao 已提交
729
static int32_t tscProcessCurrentUser(SSqlObj *pSql) {
H
Haojun Liao 已提交
730
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
731

H
hjxilinx 已提交
732
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
H
Haojun Liao 已提交
733 734
  pExpr->resBytes = TSDB_USER_LEN + TSDB_DATA_TYPE_BINARY;
  pExpr->resType = TSDB_DATA_TYPE_BINARY;
735

H
Haojun Liao 已提交
736
  char* vx = calloc(1, pExpr->resBytes);
H
Haojun Liao 已提交
737 738 739 740
  if (vx == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

741 742
  size_t size = sizeof(pSql->pTscObj->user);
  STR_WITH_MAXSIZE_TO_VARSTR(vx, pSql->pTscObj->user, size);
743

H
Haojun Liao 已提交
744 745
  tscSetLocalQueryResult(pSql, vx, pExpr->aliasName, pExpr->resType, pExpr->resBytes);
  free(vx);
H
Haojun Liao 已提交
746 747

  return TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
748 749
}

H
Haojun Liao 已提交
750
static int32_t tscProcessCurrentDB(SSqlObj *pSql) {
B
Bomin Zhang 已提交
751
  char db[TSDB_DB_NAME_LEN] = {0};
D
dapan1121 已提交
752 753

  pthread_mutex_lock(&pSql->pTscObj->mutex);
H
hjxilinx 已提交
754
  extractDBName(pSql->pTscObj->db, db);
D
dapan1121 已提交
755
  pthread_mutex_unlock(&pSql->pTscObj->mutex);
756

H
Haojun Liao 已提交
757
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, pSql->cmd.clauseIndex);
758

H
hjxilinx 已提交
759
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
H
Haojun Liao 已提交
760
  pExpr->resType = TSDB_DATA_TYPE_BINARY;
761

H
Haojun Liao 已提交
762 763
  size_t t = strlen(db);
  pExpr->resBytes = TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE;
764

H
Haojun Liao 已提交
765
  char* vx = calloc(1, pExpr->resBytes);
H
Haojun Liao 已提交
766 767 768 769
  if (vx == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

H
Haojun Liao 已提交
770 771 772
  if (t == 0) {
    setVardataNull(vx, TSDB_DATA_TYPE_BINARY);
  } else {
S
TD-1057  
Shengliang Guan 已提交
773
    STR_WITH_SIZE_TO_VARSTR(vx, db, (VarDataLenT)t);
H
Haojun Liao 已提交
774
  }
775

H
Haojun Liao 已提交
776 777
  tscSetLocalQueryResult(pSql, vx, pExpr->aliasName, pExpr->resType, pExpr->resBytes);
  free(vx);
H
Haojun Liao 已提交
778 779

  return TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
780 781
}

H
Haojun Liao 已提交
782
static int32_t tscProcessServerVer(SSqlObj *pSql) {
H
hjxilinx 已提交
783
  const char* v = pSql->pTscObj->sversion;
H
Haojun Liao 已提交
784
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, pSql->cmd.clauseIndex);
785

H
hjxilinx 已提交
786
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
H
Haojun Liao 已提交
787
  pExpr->resType = TSDB_DATA_TYPE_BINARY;
788

H
Haojun Liao 已提交
789
  size_t t = strlen(v);
S
TD-1057  
Shengliang Guan 已提交
790
  pExpr->resBytes = (int16_t)(t + VARSTR_HEADER_SIZE);
791

H
Haojun Liao 已提交
792
  char* vx = calloc(1, pExpr->resBytes);
H
Haojun Liao 已提交
793 794
  if (vx == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
795

H
Haojun Liao 已提交
796 797
  }

S
TD-1057  
Shengliang Guan 已提交
798
  STR_WITH_SIZE_TO_VARSTR(vx, v, (VarDataLenT)t);
H
Haojun Liao 已提交
799
  tscSetLocalQueryResult(pSql, vx, pExpr->aliasName, pExpr->resType, pExpr->resBytes);
800

H
Haojun Liao 已提交
801 802
  free(vx);
  return TSDB_CODE_SUCCESS;
803

H
hjxilinx 已提交
804 805
}

H
Haojun Liao 已提交
806
static int32_t tscProcessClientVer(SSqlObj *pSql) {
H
Haojun Liao 已提交
807
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
808

H
hjxilinx 已提交
809
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
H
Haojun Liao 已提交
810
  pExpr->resType = TSDB_DATA_TYPE_BINARY;
811

H
Haojun Liao 已提交
812
  size_t t = strlen(version);
S
TD-1057  
Shengliang Guan 已提交
813
  pExpr->resBytes = (int16_t)(t + VARSTR_HEADER_SIZE);
814

H
Haojun Liao 已提交
815
  char* v = calloc(1, pExpr->resBytes);
H
Haojun Liao 已提交
816 817
  if (v == NULL) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
818

H
Haojun Liao 已提交
819 820
  }

S
TD-1057  
Shengliang Guan 已提交
821
  STR_WITH_SIZE_TO_VARSTR(v, version, (VarDataLenT)t);
H
Haojun Liao 已提交
822
  tscSetLocalQueryResult(pSql, v, pExpr->aliasName, pExpr->resType, pExpr->resBytes);
823

H
Haojun Liao 已提交
824 825
  free(v);
  return TSDB_CODE_SUCCESS;
826

H
hjxilinx 已提交
827 828
}

829 830 831
// TODO add test cases.
static int32_t checkForOnlineNode(SSqlObj* pSql) {
  int32_t* data = pSql->res.length;
832 833 834
  if (data == NULL) {
    return TSDB_CODE_SUCCESS;
  }
835 836 837 838 839 840

  int32_t total  = data[0];
  int32_t online = data[1];
  return (online < total)? TSDB_CODE_RPC_NETWORK_UNAVAIL:TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
841
static int32_t tscProcessServStatus(SSqlObj *pSql) {
H
hjxilinx 已提交
842
  STscObj* pObj = pSql->pTscObj;
843

844 845
  SSqlObj* pHb = (SSqlObj*)taosAcquireRef(tscObjRef, pObj->hbrid);
  if (pHb != NULL) {
846
    pSql->res.code = pHb->res.code;
847
    taosReleaseRef(tscObjRef, pObj->hbrid);
H
hjxilinx 已提交
848
  }
849

850 851 852 853 854 855 856 857
  if (pSql->res.code == TSDB_CODE_RPC_NETWORK_UNAVAIL) {
    return pSql->res.code;
  }

  pSql->res.code = checkForOnlineNode(pHb);
  if (pSql->res.code == TSDB_CODE_RPC_NETWORK_UNAVAIL) {
    return pSql->res.code;
  }
858

H
Haojun Liao 已提交
859
  SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd, 0);
H
hjxilinx 已提交
860
  SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
861

H
Haojun Liao 已提交
862 863
  int32_t val = 1;
  tscSetLocalQueryResult(pSql, (char*) &val, pExpr->aliasName, TSDB_DATA_TYPE_INT, sizeof(int32_t));
H
Haojun Liao 已提交
864
  return TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
865 866
}

H
Haojun Liao 已提交
867
void tscSetLocalQueryResult(SSqlObj *pSql, const char *val, const char *columnName, int16_t type, size_t valueLength) {
H
hjxilinx 已提交
868 869 870 871
  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;

  pCmd->numOfCols = 1;
872

H
Haojun Liao 已提交
873
  SQueryInfo* pQueryInfo = tscGetQueryInfo(pCmd, pCmd->clauseIndex);
874
  pQueryInfo->order.order = TSDB_ORDER_ASC;
875

H
hjxilinx 已提交
876
  tscFieldInfoClear(&pQueryInfo->fieldsInfo);
H
Haojun Liao 已提交
877
  pQueryInfo->fieldsInfo.internalField = taosArrayInit(1, sizeof(SInternalField));
878

S
TD-1057  
Shengliang Guan 已提交
879
  TAOS_FIELD f = tscCreateField((int8_t)type, columnName, (int16_t)valueLength);
H
hjxilinx 已提交
880
  tscFieldInfoAppend(&pQueryInfo->fieldsInfo, &f);
881

S
TD-1057  
Shengliang Guan 已提交
882
  tscInitResObjForLocalQuery(pSql, 1, (int32_t)valueLength);
H
hjxilinx 已提交
883

H
Haojun Liao 已提交
884
  SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, 0);
H
Haojun Liao 已提交
885
  pInfo->pExpr = taosArrayGetP(pQueryInfo->exprList, 0);
886

H
Haojun Liao 已提交
887
  memcpy(pRes->data, val, pInfo->field.bytes);
H
hjxilinx 已提交
888 889
}

H
hzcheng 已提交
890 891
int tscProcessLocalCmd(SSqlObj *pSql) {
  SSqlCmd *pCmd = &pSql->cmd;
H
Haojun Liao 已提交
892
  SSqlRes *pRes = &pSql->res;
H
hzcheng 已提交
893 894

  if (pCmd->command == TSDB_SQL_CFG_LOCAL) {
Y
TD-2568  
yihaoDeng 已提交
895 896 897 898 899 900
    if (taosCfgDynamicOptions(pCmd->payload)) {
       pRes->code = TSDB_CODE_SUCCESS;
    } else {
       pRes->code = TSDB_CODE_COM_INVALID_CFG_MSG; 
    } 
    pRes->numOfRows = 0;
H
hzcheng 已提交
901
  } else if (pCmd->command == TSDB_SQL_DESCRIBE_TABLE) {
H
Haojun Liao 已提交
902
    pRes->code = (uint8_t)tscProcessDescribeTable(pSql);
H
hzcheng 已提交
903
  } else if (pCmd->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT) {
S
slguan 已提交
904
    /*
H
hjxilinx 已提交
905 906
     * set the qhandle to be 1 in order to pass the qhandle check, and to call partial release function to
     * free allocated resources and remove the SqlObj from sql query linked list
S
slguan 已提交
907
     */
H
Haojun Liao 已提交
908
    pRes->qId = 0x1;
H
Haojun Liao 已提交
909
    pRes->numOfRows = 0;
910 911 912 913
  } else if (pCmd->command == TSDB_SQL_SHOW_CREATE_TABLE) {
    pRes->code = tscProcessShowCreateTable(pSql); 
  } else if (pCmd->command == TSDB_SQL_SHOW_CREATE_DATABASE) {
    pRes->code = tscProcessShowCreateDatabase(pSql); 
H
hzcheng 已提交
914
  } else if (pCmd->command == TSDB_SQL_RESET_CACHE) {
915
    taosHashEmpty(tscTableMetaInfo);
H
Haojun Liao 已提交
916
    pRes->code = TSDB_CODE_SUCCESS;
H
hjxilinx 已提交
917
  } else if (pCmd->command == TSDB_SQL_SERV_VERSION) {
H
Haojun Liao 已提交
918
    pRes->code = tscProcessServerVer(pSql);
H
hjxilinx 已提交
919
  } else if (pCmd->command == TSDB_SQL_CLI_VERSION) {
H
Haojun Liao 已提交
920
    pRes->code = tscProcessClientVer(pSql);
H
hjxilinx 已提交
921
  } else if (pCmd->command == TSDB_SQL_CURRENT_USER) {
H
Haojun Liao 已提交
922
    pRes->code = tscProcessCurrentUser(pSql);
H
hjxilinx 已提交
923
  } else if (pCmd->command == TSDB_SQL_CURRENT_DB) {
H
Haojun Liao 已提交
924
    pRes->code = tscProcessCurrentDB(pSql);
H
hjxilinx 已提交
925
  } else if (pCmd->command == TSDB_SQL_SERV_STATUS) {
H
Haojun Liao 已提交
926
    pRes->code = tscProcessServStatus(pSql);
H
hzcheng 已提交
927
  } else {
H
Haojun Liao 已提交
928
    pRes->code = TSDB_CODE_TSC_INVALID_SQL;
D
dapan1121 已提交
929
    tscError("0x%"PRIx64" not support command:%d", pSql->self, pCmd->command);
H
hzcheng 已提交
930 931
  }

S
slguan 已提交
932
  // keep the code in local variable in order to avoid invalid read in case of async query
933

H
Haojun Liao 已提交
934
  int32_t code = pRes->code;
935
  if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
936
    (*pSql->fp)(pSql->param, pSql, code);
937
  } else if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS){
938
  } else {
H
Haojun Liao 已提交
939
    tscAsyncResultOnError(pSql);
H
hzcheng 已提交
940 941 942
  }
  return code;
}