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

16
#include "astGenerator.h"
17
#include "parserInt.h"
18
#include "parserUtil.h"
19
#include "ttoken.h"
20
#include "function.h"
21
#include "insertParser.h"
22

X
Xiaoyu Wang 已提交
23
bool isInsertSql(const char* pStr, size_t length) {
24 25 26
  int32_t index = 0;

  do {
27
    SToken t0 = tStrGetToken((char*) pStr, &index, false);
28 29 30 31
    if (t0.type != TK_LP) {
      return t0.type == TK_INSERT || t0.type == TK_IMPORT;
    }
  } while (1);
32 33
}

H
Haojun Liao 已提交
34 35
bool qIsDdlQuery(const SQueryNode* pQueryNode) {
  return TSDB_SQL_INSERT != pQueryNode->type && TSDB_SQL_SELECT != pQueryNode->type && TSDB_SQL_CREATE_TABLE != pQueryNode->type;
X
Xiaoyu Wang 已提交
36 37 38 39
}

int32_t parseQuerySql(SParseContext* pCxt, SQueryNode** pQuery) {
  SSqlInfo info = doGenerateAST(pCxt->pSql);
40
  if (!info.valid) {
X
Xiaoyu Wang 已提交
41
    strncpy(pCxt->pMsg, info.msg, pCxt->msgLen);
42
    terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
43
    return terrno;
44 45
  }

46
  if (!isDqlSqlStatement(&info)) {
47
    if (info.type == TSDB_SQL_CREATE_TABLE) {
48 49
      SVnodeModifOpStmtInfo * pModifStmtInfo = qParserValidateCreateTbSqlNode(&info, &pCxt->ctx, pCxt->pMsg, pCxt->msgLen);
      if (pModifStmtInfo == NULL) {
50 51 52
        return terrno;
      }

53
      *pQuery = (SQueryNode*)pModifStmtInfo;
54 55 56 57 58 59
    } else {
      SDclStmtInfo* pDcl = qParserValidateDclSqlNode(&info, &pCxt->ctx, pCxt->pMsg, pCxt->msgLen);
      if (pDcl == NULL) {
        return terrno;
      }

X
Xiaoyu Wang 已提交
60
      *pQuery = (SQueryNode*)pDcl;
61
      pDcl->nodeType = info.type;
62 63
    }
  } else {
H
Haojun Liao 已提交
64 65
    SQueryStmtInfo* pQueryInfo = calloc(1, sizeof(SQueryStmtInfo));
    if (pQueryInfo == NULL) {
66
      terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; // set correct error code.
H
Haojun Liao 已提交
67 68 69
      return terrno;
    }

70
    int32_t code = qParserValidateSqlNode(pCxt->ctx.pCatalog, &info, pQueryInfo, pCxt->ctx.requestId, pCxt->pMsg, pCxt->msgLen);
71
    if (code == TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
72
      *pQuery = (SQueryNode*)pQueryInfo;
73
    }
74 75
  }

H
Haojun Liao 已提交
76
  destroySqlInfo(&info);
77
  return TSDB_CODE_SUCCESS;
78 79
}

X
Xiaoyu Wang 已提交
80 81
int32_t qParseQuerySql(SParseContext* pCxt, SQueryNode** pQuery) {
  if (isInsertSql(pCxt->pSql, pCxt->sqlLen)) {
H
Haojun Liao 已提交
82
    return parseInsertSql(pCxt, (SVnodeModifOpStmtInfo**)pQuery);
X
Xiaoyu Wang 已提交
83 84 85
  } else {
    return parseQuerySql(pCxt, pQuery);
  }
86 87 88 89 90 91
}

int32_t qParserConvertSql(const char* pStr, size_t length, char** pConvertSql) {
  return 0;
}

92
static int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf);
93 94 95 96 97

static int32_t tnameComparFn(const void* p1, const void* p2) {
  SName* pn1 = (SName*)p1;
  SName* pn2 = (SName*)p2;

H
Haojun Liao 已提交
98
  int32_t ret = pn1->acctId - pn2->acctId;
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  if (ret != 0) {
    return ret > 0? 1:-1;
  } else {
    ret = strncmp(pn1->dbname, pn2->dbname, tListLen(pn1->dbname));
    if (ret != 0) {
      return ret > 0? 1:-1;
    } else {
      ret = strncmp(pn1->tname, pn2->tname, tListLen(pn1->tname));
      if (ret != 0) {
        return ret > 0? 1:-1;
      } else {
        return 0;
      }
    }
  }
}

116
static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf) {
117 118 119
  int32_t numOfSub = (int32_t)taosArrayGetSize(pSqlNode->from->list);

  for (int32_t j = 0; j < numOfSub; ++j) {
120
    SRelElement* sub = taosArrayGet(pSqlNode->from->list, j);
121

122
    int32_t num = (int32_t)taosArrayGetSize(sub->pSubquery->node);
123
    for (int32_t i = 0; i < num; ++i) {
124 125
      SSqlNode* p = taosArrayGetP(sub->pSubquery->node, i);
      if (p->from->type == SQL_FROM_NODE_TABLES) {
126
        int32_t code = getTableNameFromSqlNode(p, tableNameList, pMsgBuf);
127 128 129 130
        if (code != TSDB_CODE_SUCCESS) {
          return code;
        }
      } else {
131
        getTableNameFromSubquery(p, tableNameList, pMsgBuf);
132 133 134 135 136 137 138
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}

139
int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf) {
140 141 142
  const char* msg1 = "invalid table name";

  int32_t numOfTables = (int32_t) taosArrayGetSize(pSqlNode->from->list);
143
  assert(pSqlNode->from->type == SQL_FROM_NODE_TABLES);
144 145

  for(int32_t j = 0; j < numOfTables; ++j) {
146
    SRelElement* item = taosArrayGet(pSqlNode->from->list, j);
147 148

    SToken* t = &item->tableName;
149
    if (t->type == TK_INTEGER || t->type == TK_FLOAT || t->type == TK_STRING) {
150
      return buildInvalidOperationMsg(pMsgBuf, msg1);
151 152 153
    }

    if (parserValidateIdToken(t) != TSDB_CODE_SUCCESS) {
154
      return buildInvalidOperationMsg(pMsgBuf, msg1);
155 156 157
    }

    SName name = {0};
158
    strndequote(name.tname, t->z, t->n);
159 160 161 162 163 164
    taosArrayPush(tableNameList, &name);
  }

  return TSDB_CODE_SUCCESS;
}

165 166 167
static void freePtrElem(void* p) {
  tfree(*(char**)p);
}
168

D
dapan1121 已提交
169
int32_t qParserExtractRequestedMetaInfo(const SSqlInfo* pSqlInfo, SCatalogReq* pMetaInfo, char* msg, int32_t msgBufLen) {
170
  int32_t code  = TSDB_CODE_SUCCESS;
171
  SMsgBuf msgBuf = {.buf = msg, .len = msgBufLen};
172

173 174
  pMetaInfo->pTableName = taosArrayInit(4, sizeof(SName));
  pMetaInfo->pUdf = taosArrayInit(4, POINTER_BYTES);
175

176
  size_t size = taosArrayGetSize(pSqlInfo->sub.node);
177
  for (int32_t i = 0; i < size; ++i) {
178
    SSqlNode* pSqlNode = taosArrayGetP(pSqlInfo->sub.node, i);
179
    if (pSqlNode->from == NULL) {
180
      return buildInvalidOperationMsg(&msgBuf, "invalid from clause");
181 182
    }

183
    // load the table meta in the FROM clause
184
    if (pSqlNode->from->type == SQL_FROM_NODE_TABLES) {
185
      code = getTableNameFromSqlNode(pSqlNode, pMetaInfo->pTableName, &msgBuf);
186
      if (code != TSDB_CODE_SUCCESS) {
187
        return code;
188 189
      }
    } else {
190
      code = getTableNameFromSubquery(pSqlNode, pMetaInfo->pTableName, &msgBuf);
191
      if (code != TSDB_CODE_SUCCESS) {
192
        return code;
193 194 195 196
      }
    }
  }

197 198
  taosArraySort(pMetaInfo->pTableName, tnameComparFn);
  taosArrayRemoveDuplicate(pMetaInfo->pTableName, tnameComparFn, NULL);
199 200

  size_t funcSize = 0;
201 202
  if (pSqlInfo->funcs) {
    funcSize = taosArrayGetSize(pSqlInfo->funcs);
203 204 205 206
  }

  if (funcSize > 0) {
    for (size_t i = 0; i < funcSize; ++i) {
207 208
      SToken* t = taosArrayGet(pSqlInfo->funcs, i);
      assert(t != NULL);
209 210

      if (t->n >= TSDB_FUNC_NAME_LEN) {
211
        return buildSyntaxErrMsg(&msgBuf, "too long function name", t->z);
212 213
      }

214
      // Let's assume that it is an UDF/UDAF, if it is not a built-in function.
215 216
      bool scalarFunc = false;
      if (qIsBuiltinFunction(t->z, t->n, &scalarFunc) < 0) {
217 218
        char* fname = strndup(t->z, t->n);
        taosArrayPush(pMetaInfo->pUdf, &fname);
219 220 221
      }
    }
  }
222 223

  return code;
224 225
}

226
void qParserCleanupMetaRequestInfo(SCatalogReq* pMetaReq) {
227 228 229 230 231 232 233
  if (pMetaReq == NULL) {
    return;
  }

  taosArrayDestroy(pMetaReq->pTableName);
  taosArrayDestroy(pMetaReq->pUdf);
}
X
Xiaoyu Wang 已提交
234

235 236 237 238 239 240
void qDestroyQuery(SQueryNode* pQueryNode) {
  if (nodeType(pQueryNode) == TSDB_SQL_INSERT || nodeType(pQueryNode) == TSDB_SQL_CREATE_TABLE) {
    SVnodeModifOpStmtInfo* pModifInfo = (SVnodeModifOpStmtInfo*)pQueryNode;
    taosArrayDestroy(pModifInfo->pDataBlocks);
  }
  tfree(pQueryNode);
X
Xiaoyu Wang 已提交
241
}