parser.c 7.5 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
    SQueryStmtInfo* pQueryInfo = createQueryInfo();
H
Haojun Liao 已提交
65
    if (pQueryInfo == NULL) {
66
      terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; // set correct error code.
H
Haojun Liao 已提交
67 68 69
      return terrno;
    }

H
Haojun Liao 已提交
70
    int32_t code = qParserValidateSqlNode(&pCxt->ctx, &info, pQueryInfo, pCxt->pMsg, pCxt->msgLen);
71
    if (code == TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
72
      *pQuery = (SQueryNode*)pQueryInfo;
73 74 75
    } else {
      terrno = code;
      return code;
76
    }
77 78
  }

H
Haojun Liao 已提交
79
  destroySqlInfo(&info);
80
  return TSDB_CODE_SUCCESS;
81 82
}

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

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

H
Haojun Liao 已提交
95
static int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SParseBasicCtx *pCtx, SMsgBuf* pMsgBuf);
96 97 98 99 100

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

H
Haojun Liao 已提交
101
  int32_t ret = pn1->acctId - pn2->acctId;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  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;
      }
    }
  }
}

H
Haojun Liao 已提交
119
static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameList, SParseBasicCtx *pCtx, SMsgBuf* pMsgBuf) {
120 121 122
  int32_t numOfSub = (int32_t)taosArrayGetSize(pSqlNode->from->list);

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

125
    int32_t num = (int32_t)taosArrayGetSize(sub->pSubquery->node);
126
    for (int32_t i = 0; i < num; ++i) {
127 128
      SSqlNode* p = taosArrayGetP(sub->pSubquery->node, i);
      if (p->from->type == SQL_FROM_NODE_TABLES) {
H
Haojun Liao 已提交
129
        int32_t code = getTableNameFromSqlNode(p, tableNameList, pCtx, pMsgBuf);
130 131 132 133
        if (code != TSDB_CODE_SUCCESS) {
          return code;
        }
      } else {
H
Haojun Liao 已提交
134
        getTableNameFromSubquery(p, tableNameList, pCtx, pMsgBuf);
135 136 137 138 139 140 141
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
142
int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SParseBasicCtx *pParseCtx, SMsgBuf* pMsgBuf) {
143 144 145
  const char* msg1 = "invalid table name";

  int32_t numOfTables = (int32_t) taosArrayGetSize(pSqlNode->from->list);
146
  assert(pSqlNode->from->type == SQL_FROM_NODE_TABLES);
147 148

  for(int32_t j = 0; j < numOfTables; ++j) {
149
    SRelElement* item = taosArrayGet(pSqlNode->from->list, j);
150 151

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

    if (parserValidateIdToken(t) != TSDB_CODE_SUCCESS) {
157
      return buildInvalidOperationMsg(pMsgBuf, msg1);
158 159 160
    }

    SName name = {0};
H
Haojun Liao 已提交
161 162 163 164 165
    int32_t code = createSName(&name, t, pParseCtx, pMsgBuf);
    if (code != TSDB_CODE_SUCCESS) {
      return buildInvalidOperationMsg(pMsgBuf, msg1);
    }

166 167 168 169 170 171
    taosArrayPush(tableNameList, &name);
  }

  return TSDB_CODE_SUCCESS;
}

172 173 174
static void freePtrElem(void* p) {
  tfree(*(char**)p);
}
175

H
Haojun Liao 已提交
176
int32_t qParserExtractRequestedMetaInfo(const SSqlInfo* pSqlInfo, SCatalogReq* pMetaInfo, SParseBasicCtx *pCtx, char* msg, int32_t msgBufLen) {
177
  int32_t code  = TSDB_CODE_SUCCESS;
178
  SMsgBuf msgBuf = {.buf = msg, .len = msgBufLen};
179

180 181
  pMetaInfo->pTableName = taosArrayInit(4, sizeof(SName));
  pMetaInfo->pUdf = taosArrayInit(4, POINTER_BYTES);
182

183
  size_t size = taosArrayGetSize(pSqlInfo->sub.node);
184
  for (int32_t i = 0; i < size; ++i) {
185
    SSqlNode* pSqlNode = taosArrayGetP(pSqlInfo->sub.node, i);
186
    if (pSqlNode->from == NULL) {
187
      return buildInvalidOperationMsg(&msgBuf, "invalid from clause");
188 189
    }

190
    // load the table meta in the FROM clause
191
    if (pSqlNode->from->type == SQL_FROM_NODE_TABLES) {
H
Haojun Liao 已提交
192
      code = getTableNameFromSqlNode(pSqlNode, pMetaInfo->pTableName, pCtx, &msgBuf);
193
      if (code != TSDB_CODE_SUCCESS) {
194
        return code;
195 196
      }
    } else {
H
Haojun Liao 已提交
197
      code = getTableNameFromSubquery(pSqlNode, pMetaInfo->pTableName, pCtx, &msgBuf);
198
      if (code != TSDB_CODE_SUCCESS) {
199
        return code;
200 201 202 203
      }
    }
  }

204 205
  taosArraySort(pMetaInfo->pTableName, tnameComparFn);
  taosArrayRemoveDuplicate(pMetaInfo->pTableName, tnameComparFn, NULL);
206 207

  size_t funcSize = 0;
208 209
  if (pSqlInfo->funcs) {
    funcSize = taosArrayGetSize(pSqlInfo->funcs);
210 211 212 213
  }

  if (funcSize > 0) {
    for (size_t i = 0; i < funcSize; ++i) {
214 215
      SToken* t = taosArrayGet(pSqlInfo->funcs, i);
      assert(t != NULL);
216 217

      if (t->n >= TSDB_FUNC_NAME_LEN) {
218
        return buildSyntaxErrMsg(&msgBuf, "too long function name", t->z);
219 220
      }

221
      // Let's assume that it is an UDF/UDAF, if it is not a built-in function.
222 223
      bool scalarFunc = false;
      if (qIsBuiltinFunction(t->z, t->n, &scalarFunc) < 0) {
224 225
        char* fname = strndup(t->z, t->n);
        taosArrayPush(pMetaInfo->pUdf, &fname);
226 227 228
      }
    }
  }
229 230

  return code;
231 232
}

233
void qParserCleanupMetaRequestInfo(SCatalogReq* pMetaReq) {
234 235 236 237 238 239 240
  if (pMetaReq == NULL) {
    return;
  }

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

242
void qDestroyQuery(SQueryNode* pQueryNode) {
X
Xiaoyu Wang 已提交
243 244 245
  if (NULL == pQueryNode) {
    return;
  }
246 247 248 249 250
  if (nodeType(pQueryNode) == TSDB_SQL_INSERT || nodeType(pQueryNode) == TSDB_SQL_CREATE_TABLE) {
    SVnodeModifOpStmtInfo* pModifInfo = (SVnodeModifOpStmtInfo*)pQueryNode;
    taosArrayDestroy(pModifInfo->pDataBlocks);
  }
  tfree(pQueryNode);
X
Xiaoyu Wang 已提交
251
}