parser.c 8.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
H
Haojun Liao 已提交
14 15
 */

X
Xiaoyu Wang 已提交
16
#include "parser.h"
X
Xiaoyu Wang 已提交
17
#include "os.h"
X
Xiaoyu Wang 已提交
18

X
Xiaoyu Wang 已提交
19 20
#include "parInt.h"
#include "parToken.h"
X
Xiaoyu Wang 已提交
21

22
bool qIsInsertValuesSql(const char* pStr, size_t length) {
D
stmt  
dapan1121 已提交
23 24 25
  if (NULL == pStr) {
    return false;
  }
X
Xiaoyu Wang 已提交
26

27 28
  const char* pSql = pStr;

X
Xiaoyu Wang 已提交
29
  int32_t index = 0;
30 31 32 33
  SToken  t = tStrGetToken((char*)pStr, &index, false);
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
    return false;
  }
X
Xiaoyu Wang 已提交
34 35

  do {
36
    pStr += index;
37
    index = 0;
38
    t = tStrGetToken((char*)pStr, &index, false);
39
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
40
      return true;
41 42
    } else if (TK_SELECT == t.type) {
      return false;
X
Xiaoyu Wang 已提交
43
    }
X
Xiaoyu Wang 已提交
44
    if (0 == t.type || 0 == t.n) {
D
dapan1121 已提交
45 46
      break;
    }
47 48
  } while (pStr - pSql < length);
  return false;
X
Xiaoyu Wang 已提交
49 50
}

51 52
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
53

54 55
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
56 57 58 59
    return TSDB_CODE_SUCCESS;
  }

  if (TSDB_CODE_SUCCESS == code) {
60
    code = translate(pCxt, pQuery, pMetaCache);
61 62 63
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = calculateConstant(pCxt, pQuery);
X
Xiaoyu Wang 已提交
64
  }
65 66 67 68 69
  return code;
}

static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) {
  int32_t code = parse(pCxt, pQuery);
70
  if (TSDB_CODE_SUCCESS == code) {
71
    code = analyseSemantic(pCxt, *pQuery, NULL);
X
Xiaoyu Wang 已提交
72 73 74 75
  }
  return code;
}

76
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
77 78
  int32_t code = parse(pCxt, pQuery);
  if (TSDB_CODE_SUCCESS == code) {
79
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
80 81 82 83
  }
  return code;
}

X
Xiaoyu Wang 已提交
84
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam) {
D
dapan1121 已提交
85 86 87
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
    taosMemoryFreeClear(pVal->datum.p);
  }
88

X
Xiaoyu Wang 已提交
89 90 91 92 93
  if (pParam->is_null && 1 == *(pParam->is_null)) {
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
    return TSDB_CODE_SUCCESS;
  }
94

X
Xiaoyu Wang 已提交
95 96 97
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
  pVal->node.resType.type = pParam->buffer_type;
  pVal->node.resType.bytes = inputSize;
98

X
Xiaoyu Wang 已提交
99 100 101 102 103 104 105 106 107
  switch (pParam->buffer_type) {
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pVal->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
D
dapan1121 已提交
108
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
X
Xiaoyu Wang 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
      break;
    case TSDB_DATA_TYPE_NCHAR: {
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pVal->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }

      int32_t output = 0;
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
                         &output)) {
        return errno;
      }
      varDataSetLen(pVal->datum.p, output);
D
dapan1121 已提交
123
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
X
Xiaoyu Wang 已提交
124 125
      break;
    }
D
dapan1121 已提交
126 127 128 129 130
    default: {
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
      if (code) {
        return code;
      }
X
Xiaoyu Wang 已提交
131
      break;
D
dapan1121 已提交
132
    }
X
Xiaoyu Wang 已提交
133 134 135 136 137
  }
  pVal->translate = true;
  return TSDB_CODE_SUCCESS;
}

138 139
static EDealRes rewriteQueryExprAliasImpl(SNode* pNode, void* pContext) {
  if (nodesIsExprNode(pNode) && QUERY_NODE_COLUMN != nodeType(pNode) && '\0' == ((SExprNode*)pNode)->userAlias[0]) {
140
    strcpy(((SExprNode*)pNode)->userAlias, ((SExprNode*)pNode)->aliasName);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    sprintf(((SExprNode*)pNode)->aliasName, "#%d", *(int32_t*)pContext);
    ++(*(int32_t*)pContext);
  }
  return DEAL_RES_CONTINUE;
}

static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
  switch (nodeType(pRoot)) {
    case QUERY_NODE_SELECT_STMT:
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
      break;
    case QUERY_NODE_SET_OPERATOR: {
      SSetOperator* pSetOper = (SSetOperator*)pRoot;
      rewriteQueryExprAlias(pSetOper->pLeft, pNo);
      rewriteQueryExprAlias(pSetOper->pRight, pNo);
      break;
    }
    default:
      break;
  }
}

static void rewriteExprAlias(SNode* pRoot) {
  int32_t no = 1;
  rewriteQueryExprAlias(pRoot, &no);
}

X
Xiaoyu Wang 已提交
168
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
X
Xiaoyu Wang 已提交
169
  int32_t code = TSDB_CODE_SUCCESS;
170
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
171
    code = parseInsertSql(pCxt, pQuery, NULL);
X
Xiaoyu Wang 已提交
172
  } else {
X
Xiaoyu Wang 已提交
173
    code = parseSqlIntoAst(pCxt, pQuery);
X
Xiaoyu Wang 已提交
174
  }
X
Xiaoyu Wang 已提交
175 176
  terrno = code;
  return code;
X
Xiaoyu Wang 已提交
177 178
}

179
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
180 181
  SParseMetaCache metaCache = {0};
  int32_t         code = TSDB_CODE_SUCCESS;
182
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
183
    code = parseInsertSyntax(pCxt, pQuery, &metaCache);
184
  } else {
185
    code = parseSqlSyntax(pCxt, pQuery, &metaCache);
186 187
  }
  if (TSDB_CODE_SUCCESS == code) {
188
    code = buildCatalogReq(&metaCache, pCatalogReq);
189
  }
190
  destoryParseMetaCache(&metaCache, true);
191 192 193 194
  terrno = code;
  return code;
}

195 196
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
                            const struct SMetaData* pMetaData, SQuery* pQuery) {
197 198 199 200 201
  SParseMetaCache metaCache = {0};
  int32_t         code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
  if (TSDB_CODE_SUCCESS == code) {
    if (NULL == pQuery->pRoot) {
      code = parseInsertSql(pCxt, &pQuery, &metaCache);
202 203
    } else {
      code = analyseSemantic(pCxt, pQuery, &metaCache);
204
    }
205
  }
206
  destoryParseMetaCache(&metaCache, false);
207 208
  terrno = code;
  return code;
209 210
}

211
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
212 213 214

int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
  return extractResultSchema(pRoot, numOfCols, pSchema);
D
stmt  
dapan1121 已提交
215
}
X
Xiaoyu Wang 已提交
216

217
int32_t qSetSTableIdForRsma(SNode* pStmt, int64_t uid) {
X
Xiaoyu Wang 已提交
218 219 220 221 222 223 224 225 226 227 228
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    SNode* pTable = ((SSelectStmt*)pStmt)->pFromTable;
    if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
      ((SRealTableNode*)pTable)->pMeta->uid = uid;
      ((SRealTableNode*)pTable)->pMeta->suid = uid;
      return TSDB_CODE_SUCCESS;
    }
  }
  return TSDB_CODE_FAILED;
}

X
Xiaoyu Wang 已提交
229 230
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }

231
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx) {
X
Xiaoyu Wang 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245
  int32_t code = TSDB_CODE_SUCCESS;

  if (colIdx < 0) {
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
    for (int32_t i = 0; i < size; ++i) {
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
    }
  } else {
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams);
  }

246
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
247
    nodesDestroyNode(pQuery->pRoot);
248 249 250 251 252 253 254 255
    pQuery->pRoot = nodesCloneNode(pQuery->pPrepareRoot);
    if (NULL == pQuery->pRoot) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
    rewriteExprAlias(pQuery->pRoot);
  }
X
Xiaoyu Wang 已提交
256 257 258 259
  return code;
}

int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
260
  int32_t code = translate(pCxt, pQuery, NULL);
X
Xiaoyu Wang 已提交
261 262 263 264 265
  if (TSDB_CODE_SUCCESS == code) {
    code = calculateConstant(pCxt, pQuery);
  }
  return code;
}