parser.c 6.9 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
}

X
Xiaoyu Wang 已提交
34
bool qIsDdlQuery(const SQueryNode* pQuery) {
X
Xiaoyu Wang 已提交
35
  return TSDB_SQL_INSERT != pQuery->type && TSDB_SQL_SELECT != pQuery->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
    SDclStmtInfo* pDcl = calloc(1, sizeof(SDclStmtInfo));
X
Xiaoyu Wang 已提交
48 49 50 51
    if (NULL == pDcl) {
      terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; // set correct error code.
      return terrno;
    }
52

X
Xiaoyu Wang 已提交
53
    pDcl->nodeType = info.type;
X
Xiaoyu Wang 已提交
54
    int32_t code = qParserValidateDclSqlNode(&info, &pCxt->ctx, pDcl, pCxt->pMsg, pCxt->msgLen);
55
    if (code == TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
56
      *pQuery = (SQueryNode*)pDcl;
57 58
    }
  } else {
H
Haojun Liao 已提交
59 60 61 62 63 64
    SQueryStmtInfo* pQueryInfo = calloc(1, sizeof(SQueryStmtInfo));
    if (pQueryInfo == NULL) {
      terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; // set correct error code.
      return terrno;
    }

H
Haojun Liao 已提交
65 66
    struct SCatalog* pCatalog = NULL;
    int32_t code = catalogGetHandle(NULL, &pCatalog);
X
Xiaoyu Wang 已提交
67
    code = qParserValidateSqlNode(pCatalog, &info, pQueryInfo, pCxt->ctx.requestId, pCxt->pMsg, pCxt->msgLen);
68
    if (code == TSDB_CODE_SUCCESS) {
X
Xiaoyu Wang 已提交
69
      *pQuery = (SQueryNode*)pQueryInfo;
70
    }
71 72
  }

H
Haojun Liao 已提交
73
  destroySqlInfo(&info);
74
  return TSDB_CODE_SUCCESS;
75 76
}

X
Xiaoyu Wang 已提交
77 78 79 80 81 82
int32_t qParseQuerySql(SParseContext* pCxt, SQueryNode** pQuery) {
  if (isInsertSql(pCxt->pSql, pCxt->sqlLen)) {
    return parseInsertSql(pCxt, (SInsertStmtInfo**)pQuery);
  } else {
    return parseQuerySql(pCxt, pQuery);
  }
83 84 85 86 87 88
}

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

89
static int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf);
90 91 92 93 94

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

H
Haojun Liao 已提交
95
  int32_t ret = pn1->acctId - pn2->acctId;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  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;
      }
    }
  }
}

113
static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf) {
114 115 116
  int32_t numOfSub = (int32_t)taosArrayGetSize(pSqlNode->from->list);

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

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

  return TSDB_CODE_SUCCESS;
}

136
int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf) {
137 138 139
  const char* msg1 = "invalid table name";

  int32_t numOfTables = (int32_t) taosArrayGetSize(pSqlNode->from->list);
140
  assert(pSqlNode->from->type == SQL_FROM_NODE_TABLES);
141 142

  for(int32_t j = 0; j < numOfTables; ++j) {
143
    SRelElement* item = taosArrayGet(pSqlNode->from->list, j);
144 145

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

    if (parserValidateIdToken(t) != TSDB_CODE_SUCCESS) {
151
      return buildInvalidOperationMsg(pMsgBuf, msg1);
152 153 154
    }

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

  return TSDB_CODE_SUCCESS;
}

162 163 164
static void freePtrElem(void* p) {
  tfree(*(char**)p);
}
165

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

170 171
  pMetaInfo->pTableName = taosArrayInit(4, sizeof(SName));
  pMetaInfo->pUdf = taosArrayInit(4, POINTER_BYTES);
172

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

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

194 195
  taosArraySort(pMetaInfo->pTableName, tnameComparFn);
  taosArrayRemoveDuplicate(pMetaInfo->pTableName, tnameComparFn, NULL);
196 197

  size_t funcSize = 0;
198 199
  if (pSqlInfo->funcs) {
    funcSize = taosArrayGetSize(pSqlInfo->funcs);
200 201 202 203
  }

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

      if (t->n >= TSDB_FUNC_NAME_LEN) {
208
        return buildSyntaxErrMsg(&msgBuf, "too long function name", t->z);
209 210
      }

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

  return code;
221 222
}

D
dapan1121 已提交
223
void qParserClearupMetaRequestInfo(SCatalogReq* pMetaReq) {
224 225 226 227 228 229 230
  if (pMetaReq == NULL) {
    return;
  }

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

L
Liu Jicong 已提交
232
void qDestroyQuery(SQueryNode* pQuery) {
X
Xiaoyu Wang 已提交
233 234
  // todo
}