parInsertSml.c 10.3 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * 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 "parInsertUtil.h"
#include "parInt.h"
#include "parToken.h"
#include "ttime.h"

int32_t qCreateSName(SName* pName, const char* pTableName, int32_t acctId, char* dbName, char* msgBuf,
                     int32_t msgBufLen) {
  SMsgBuf msg = {.buf = msgBuf, .len = msgBufLen};
  SToken  sToken;
  int32_t code = 0;
  char*   tbName = NULL;

  NEXT_TOKEN(pTableName, sToken);

  if (sToken.n == 0) {
    return buildInvalidOperationMsg(&msg, "empty table name");
  }

  code = insCreateSName(pName, &sToken, acctId, dbName, &msg);
  if (code) {
    return code;
  }

  NEXT_TOKEN(pTableName, sToken);

  if (sToken.n > 0) {
    return buildInvalidOperationMsg(&msg, "table name format is wrong");
  }

  return TSDB_CODE_SUCCESS;
}

48 49 50 51
static int32_t smlBoundColumnData(SArray* cols, SBoundColInfo* pBoundInfo, SSchema* pSchema, bool isTag) {
  bool* pUseCols = taosMemoryCalloc(pBoundInfo->numOfCols, sizeof(bool));
  if (NULL == pUseCols) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
52 53
  }

54 55 56 57
  pBoundInfo->numOfBound = 0;
  int16_t lastColIdx = -1;  // last column found
  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
58 59 60 61
  for (int i = 0; i < taosArrayGetSize(cols); ++i) {
    SSmlKv*  kv = taosArrayGetP(cols, i);
    SToken   sToken = {.n = kv->keyLen, .z = (char*)kv->key};
    col_id_t t = lastColIdx + 1;
62 63
    col_id_t index = ((t == 0 && !isTag) ? 0 : insFindCol(&sToken, t, pBoundInfo->numOfCols, pSchema));
    uDebug("SML, index:%d, t:%d, ncols:%d", index, t, pBoundInfo->numOfCols);
X
Xiaoyu Wang 已提交
64 65 66
    if (index < 0 && t > 0) {
      index = insFindCol(&sToken, 0, t, pSchema);
    }
67

X
Xiaoyu Wang 已提交
68 69
    if (index < 0) {
      uError("smlBoundColumnData. index:%d", index);
70 71
      code = TSDB_CODE_SML_INVALID_DATA;
      goto end;
X
Xiaoyu Wang 已提交
72
    }
73
    if (pUseCols[index]) {
X
Xiaoyu Wang 已提交
74
      uError("smlBoundColumnData. already set. index:%d", index);
75 76
      code = TSDB_CODE_SML_INVALID_DATA;
      goto end;
X
Xiaoyu Wang 已提交
77 78
    }
    lastColIdx = index;
79 80 81
    pUseCols[index] = true;
    pBoundInfo->pColIndex[pBoundInfo->numOfBound] = index;
    ++pBoundInfo->numOfBound;
X
Xiaoyu Wang 已提交
82 83
  }

84 85
end:
  taosMemoryFree(pUseCols);
X
Xiaoyu Wang 已提交
86

87
  return code;
X
Xiaoyu Wang 已提交
88 89 90 91 92 93 94 95 96 97 98 99
}

/**
 * @brief No json tag for schemaless
 *
 * @param cols
 * @param tags
 * @param pSchema
 * @param ppTag
 * @param msg
 * @return int32_t
 */
100
static int32_t smlBuildTagRow(SArray* cols, SBoundColInfo* tags, SSchema* pSchema, STag** ppTag, SArray** tagName,
X
Xiaoyu Wang 已提交
101 102 103 104 105 106 107 108 109 110 111 112
                              SMsgBuf* msg) {
  SArray* pTagArray = taosArrayInit(tags->numOfBound, sizeof(STagVal));
  if (!pTagArray) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
  *tagName = taosArrayInit(8, TSDB_COL_NAME_LEN);
  if (!*tagName) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }

  int32_t code = TSDB_CODE_SUCCESS;
  for (int i = 0; i < tags->numOfBound; ++i) {
113
    SSchema* pTagSchema = &pSchema[tags->pColIndex[i]];
X
Xiaoyu Wang 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    SSmlKv*  kv = taosArrayGetP(cols, i);

    taosArrayPush(*tagName, pTagSchema->name);
    STagVal val = {.cid = pTagSchema->colId, .type = pTagSchema->type};
    //    strcpy(val.colName, pTagSchema->name);
    if (pTagSchema->type == TSDB_DATA_TYPE_BINARY) {
      val.pData = (uint8_t*)kv->value;
      val.nData = kv->length;
    } else if (pTagSchema->type == TSDB_DATA_TYPE_NCHAR) {
      int32_t output = 0;
      void*   p = taosMemoryCalloc(1, kv->length * TSDB_NCHAR_SIZE);
      if (p == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto end;
      }
      if (!taosMbsToUcs4(kv->value, kv->length, (TdUcs4*)(p), kv->length * TSDB_NCHAR_SIZE, &output)) {
        if (errno == E2BIG) {
          taosMemoryFree(p);
          code = generateSyntaxErrMsg(msg, TSDB_CODE_PAR_VALUE_TOO_LONG, pTagSchema->name);
          goto end;
        }
        char buf[512] = {0};
        snprintf(buf, tListLen(buf), " taosMbsToUcs4 error:%s", strerror(errno));
        taosMemoryFree(p);
        code = buildSyntaxErrMsg(msg, buf, kv->value);
        goto end;
      }
      val.pData = p;
      val.nData = output;
    } else {
      memcpy(&val.i64, &(kv->value), kv->length);
    }
    taosArrayPush(pTagArray, &val);
  }

  code = tTagNew(pTagArray, 1, false, ppTag);
end:
  for (int i = 0; i < taosArrayGetSize(pTagArray); ++i) {
    STagVal* p = (STagVal*)taosArrayGet(pTagArray, i);
    if (p->type == TSDB_DATA_TYPE_NCHAR) {
      taosMemoryFree(p->pData);
    }
  }
  taosArrayDestroy(pTagArray);
  return code;
}

wmmhello's avatar
wmmhello 已提交
161
int32_t smlBindData(SQuery* query, SArray* tags, SArray* colsSchema, SArray* cols, bool format, STableMeta* pTableMeta,
162
                    char* tableName, const char* sTableName, int32_t sTableNameLen, int32_t ttl, char* msgBuf, int16_t msgBufLen) {
X
Xiaoyu Wang 已提交
163 164 165
  SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen};

  SSchema* pTagsSchema = getTableTagSchema(pTableMeta);
166 167 168 169 170 171
  SBoundColInfo bindTags = {0};
  SVCreateTbReq *pCreateTblReq = NULL;
  SArray* tagName = NULL;

  insInitBoundColsInfo(getNumOfTags(pTableMeta), &bindTags);
  int ret = smlBoundColumnData(tags, &bindTags, pTagsSchema, true);
X
Xiaoyu Wang 已提交
172 173
  if (ret != TSDB_CODE_SUCCESS) {
    buildInvalidOperationMsg(&pBuf, "bound tags error");
174
    goto end;
X
Xiaoyu Wang 已提交
175
  }
176

X
Xiaoyu Wang 已提交
177
  STag*   pTag = NULL;
178 179

  ret = smlBuildTagRow(tags, &bindTags, pTagsSchema, &pTag, &tagName, &pBuf);
X
Xiaoyu Wang 已提交
180
  if (ret != TSDB_CODE_SUCCESS) {
181
    goto end;
X
Xiaoyu Wang 已提交
182 183
  }

184 185 186 187 188 189
  pCreateTblReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
  if (NULL == pCreateTblReq) {
    ret = TSDB_CODE_OUT_OF_MEMORY;
    goto end;
  }
  insBuildCreateTbReq(pCreateTblReq, tableName, pTag, pTableMeta->suid, NULL, tagName,
190
                      pTableMeta->tableInfo.numOfTags, ttl);
X
Xiaoyu Wang 已提交
191

192 193
  pCreateTblReq->ctb.stbName = taosMemoryCalloc(1, sTableNameLen + 1);
  memcpy(pCreateTblReq->ctb.stbName, sTableName, sTableNameLen);
X
Xiaoyu Wang 已提交
194

195
  STableDataCxt* pTableCxt = NULL;
wmmhello's avatar
wmmhello 已提交
196
  ret = insGetTableDataCxt(((SVnodeModifOpStmt *)(query->pRoot))->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid),
197
                           pTableMeta, &pCreateTblReq, &pTableCxt, false);
X
Xiaoyu Wang 已提交
198
  if (ret != TSDB_CODE_SUCCESS) {
199 200
    buildInvalidOperationMsg(&pBuf, "insGetTableDataCxt error");
    goto end;
X
Xiaoyu Wang 已提交
201 202 203
  }

  SSchema* pSchema = getTableColumnSchema(pTableMeta);
204
  ret = smlBoundColumnData(colsSchema, &pTableCxt->boundColsInfo, pSchema, false);
X
Xiaoyu Wang 已提交
205 206
  if (ret != TSDB_CODE_SUCCESS) {
    buildInvalidOperationMsg(&pBuf, "bound cols error");
207
    goto end;
X
Xiaoyu Wang 已提交
208 209
  }

210 211 212 213 214
  ret = initTableColSubmitData(pTableCxt);
  if (ret != TSDB_CODE_SUCCESS) {
    buildInvalidOperationMsg(&pBuf, "initTableColSubmitData error");
    goto end;
  }
X
Xiaoyu Wang 已提交
215 216 217

  int32_t rowNum = taosArrayGetSize(cols);
  if (rowNum <= 0) {
218 219
    ret = buildInvalidOperationMsg(&pBuf, "cols size <= 0");
    goto end;
X
Xiaoyu Wang 已提交
220
  }
221

X
Xiaoyu Wang 已提交
222 223 224 225
  for (int32_t r = 0; r < rowNum; ++r) {
    void*  rowData = taosArrayGetP(cols, r);

    // 1. set the parsed value from sql string
226 227 228
    for (int c = 0; c < pTableCxt->boundColsInfo.numOfBound; ++c) {
      SSchema* pColSchema = &pSchema[pTableCxt->boundColsInfo.pColIndex[c]];
      SColVal* pVal = taosArrayGet(pTableCxt->pValues, pTableCxt->boundColsInfo.pColIndex[c]);
X
Xiaoyu Wang 已提交
229
      SSmlKv* kv = NULL;
230
      if (!format){
X
Xiaoyu Wang 已提交
231 232 233 234
        void** p = taosHashGet(rowData, pColSchema->name, strlen(pColSchema->name));
        if (p) kv = *p;
      }

235 236 237 238 239 240 241 242
      if (kv == NULL) {
        continue;
      }
      if (pColSchema->type == TSDB_DATA_TYPE_TIMESTAMP) {
        kv->i = convertTimePrecision(kv->i, TSDB_TIME_PRECISION_NANO, pTableMeta->tableInfo.precision);
      }
      if (kv->type == TSDB_DATA_TYPE_NCHAR){
        int32_t len = 0;
wmmhello's avatar
wmmhello 已提交
243
        char*   pUcs4 = taosMemoryCalloc(1, pColSchema->bytes - VARSTR_HEADER_SIZE);
244 245 246
        if (NULL == pUcs4) {
          ret = TSDB_CODE_OUT_OF_MEMORY;
          goto end;
X
Xiaoyu Wang 已提交
247
        }
wmmhello's avatar
wmmhello 已提交
248
        if (!taosMbsToUcs4(kv->value, kv->length, (TdUcs4*)pUcs4, pColSchema->bytes - VARSTR_HEADER_SIZE, &len)) {
249 250 251 252 253 254 255
          if (errno == E2BIG) {
            buildInvalidOperationMsg(&pBuf, "value too long");
            ret = TSDB_CODE_PAR_VALUE_TOO_LONG;
            goto end;
          }
          ret = buildInvalidOperationMsg(&pBuf, strerror(errno));
          goto end;
X
Xiaoyu Wang 已提交
256
        }
257 258 259 260 261
        pVal->value.pData = pUcs4;
        pVal->value.nData = len;
      } else if(kv->type == TSDB_DATA_TYPE_BINARY) {
          pVal->value.nData = kv->length;
          pVal->value.pData = (uint8_t *)kv->value;
X
Xiaoyu Wang 已提交
262
      } else {
263
        memcpy(&pVal->value.val, &(kv->value), kv->length);
X
Xiaoyu Wang 已提交
264
      }
265
      pVal->flag = CV_FLAG_VALUE;
X
Xiaoyu Wang 已提交
266
    }
wmmhello's avatar
wmmhello 已提交
267 268 269 270 271 272 273 274

    SRow** pRow = taosArrayReserve(pTableCxt->pData->aRowP, 1);
    ret = tRowBuild(pTableCxt->pValues, pTableCxt->pSchema, pRow);
    if (TSDB_CODE_SUCCESS != ret) {
      buildInvalidOperationMsg(&pBuf, "tRowBuild error");
      goto end;
    }
    insCheckTableDataOrder(pTableCxt, TD_ROW_KEY(*pRow));
X
Xiaoyu Wang 已提交
275 276
  }

277 278 279 280 281
end:
  destroyBoundColInfo(&bindTags);
  taosMemoryFree(pCreateTblReq);
  taosArrayDestroy(tagName);
  return ret;
X
Xiaoyu Wang 已提交
282 283
}

wmmhello's avatar
wmmhello 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
SQuery* smlInitHandle() {
  SQuery *pQuery = (SQuery *)nodesMakeNode(QUERY_NODE_QUERY);
  if (NULL == pQuery) {
    uError("create pQuery error");
    return  NULL;
  }
  pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
  pQuery->haveResultSet = false;
  pQuery->msgType = TDMT_VND_SUBMIT;
  SVnodeModifOpStmt *stmt = (SVnodeModifOpStmt*)nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT);
  if (NULL == stmt) {
    uError("create SVnodeModifOpStmt error");
    qDestroyQuery(pQuery);
    return NULL;
  }
  stmt->pTableBlockHashObj = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
  stmt->freeHashFunc = insDestroyTableDataCxtHashMap;
  stmt->freeArrayFunc = insDestroyVgroupDataCxtList;
X
Xiaoyu Wang 已提交
302

wmmhello's avatar
wmmhello 已提交
303 304
  pQuery->pRoot = (SNode *)stmt;
  return pQuery;
X
Xiaoyu Wang 已提交
305 306
}

wmmhello's avatar
wmmhello 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320
int32_t smlBuildOutput(SQuery * handle, SHashObj* pVgHash) {
  SVnodeModifOpStmt *pStmt = (SVnodeModifOpStmt*)(handle)->pRoot;
  // merge according to vgId
  int32_t code = insMergeTableDataCxt(pStmt->pTableBlockHashObj, &pStmt->pVgDataBlocks);
  if (code != TSDB_CODE_SUCCESS) {
    uError("insMergeTableDataCxt failed");
    return code;
  }
  code = insBuildVgDataBlocks(pVgHash, pStmt->pVgDataBlocks, &pStmt->pDataBlocks);
  if (code != TSDB_CODE_SUCCESS) {
    uError("insBuildVgDataBlocks failed");
    return code;
  }
  return code;
X
Xiaoyu Wang 已提交
321
}