parser.c 6.4 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 23

bool qIsInsertSql(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
}

34
int32_t qParseQuerySql(const char* pStr, size_t length, int64_t id, int32_t *type, void** pOutput, int32_t* outputLen, char* msg, int32_t msgLen) {
H
Haojun Liao 已提交
35
  SSqlInfo info = doGenerateAST(pStr);
36
  if (!info.valid) {
37
    strncpy(msg, info.msg, msgLen);
38
    terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
39
    return terrno;
40 41
  }

42 43
  if (isDclSqlStatement(&info)) {
    int32_t code = qParserValidateDclSqlNode(&info, id, pOutput, outputLen, type, msg, msgLen);
44 45 46 47
    if (code == TSDB_CODE_SUCCESS) {
      // do nothing
    }
  } else {
H
Haojun Liao 已提交
48 49 50 51 52 53
    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 已提交
54 55 56
    struct SCatalog* pCatalog = NULL;
    int32_t code = catalogGetHandle(NULL, &pCatalog);
    code = qParserValidateSqlNode(pCatalog, &info, pQueryInfo, id, msg, msgLen);
57 58 59
    if (code == TSDB_CODE_SUCCESS) {
      *pOutput = pQueryInfo;
    }
60 61
  }

H
Haojun Liao 已提交
62
  destroySqlInfo(&info);
63
  return TSDB_CODE_SUCCESS;
64 65
}

66 67
int32_t qParseInsertSql(SParseContext* pContext, SInsertStmtInfo** pInfo) {
  return parseInsertSql(pContext, pInfo);
68 69 70 71 72 73
}

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

74
static int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf);
75 76 77 78 79

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

H
Haojun Liao 已提交
80
  int32_t ret = pn1->acctId - pn2->acctId;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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;
      }
    }
  }
}

98
static int32_t getTableNameFromSubquery(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf) {
99 100 101
  int32_t numOfSub = (int32_t)taosArrayGetSize(pSqlNode->from->list);

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

104
    int32_t num = (int32_t)taosArrayGetSize(sub->pSubquery->node);
105
    for (int32_t i = 0; i < num; ++i) {
106 107
      SSqlNode* p = taosArrayGetP(sub->pSubquery->node, i);
      if (p->from->type == SQL_FROM_NODE_TABLES) {
108
        int32_t code = getTableNameFromSqlNode(p, tableNameList, pMsgBuf);
109 110 111 112
        if (code != TSDB_CODE_SUCCESS) {
          return code;
        }
      } else {
113
        getTableNameFromSubquery(p, tableNameList, pMsgBuf);
114 115 116 117 118 119 120
      }
    }
  }

  return TSDB_CODE_SUCCESS;
}

121
int32_t getTableNameFromSqlNode(SSqlNode* pSqlNode, SArray* tableNameList, SMsgBuf* pMsgBuf) {
122 123 124
  const char* msg1 = "invalid table name";

  int32_t numOfTables = (int32_t) taosArrayGetSize(pSqlNode->from->list);
125
  assert(pSqlNode->from->type == SQL_FROM_NODE_TABLES);
126 127

  for(int32_t j = 0; j < numOfTables; ++j) {
128
    SRelElement* item = taosArrayGet(pSqlNode->from->list, j);
129 130

    SToken* t = &item->tableName;
131
    if (t->type == TK_INTEGER || t->type == TK_FLOAT || t->type == TK_STRING) {
132
      return buildInvalidOperationMsg(pMsgBuf, msg1);
133 134 135
    }

    if (parserValidateIdToken(t) != TSDB_CODE_SUCCESS) {
136
      return buildInvalidOperationMsg(pMsgBuf, msg1);
137 138 139
    }

    SName name = {0};
140
    strndequote(name.tname, t->z, t->n);
141 142 143 144 145 146
    taosArrayPush(tableNameList, &name);
  }

  return TSDB_CODE_SUCCESS;
}

147 148 149
static void freePtrElem(void* p) {
  tfree(*(char**)p);
}
150

D
dapan1121 已提交
151
int32_t qParserExtractRequestedMetaInfo(const SSqlInfo* pSqlInfo, SCatalogReq* pMetaInfo, char* msg, int32_t msgBufLen) {
152
  int32_t code  = TSDB_CODE_SUCCESS;
153
  SMsgBuf msgBuf = {.buf = msg, .len = msgBufLen};
154

155 156
  pMetaInfo->pTableName = taosArrayInit(4, sizeof(SName));
  pMetaInfo->pUdf = taosArrayInit(4, POINTER_BYTES);
157

158
  size_t size = taosArrayGetSize(pSqlInfo->sub.node);
159
  for (int32_t i = 0; i < size; ++i) {
160
    SSqlNode* pSqlNode = taosArrayGetP(pSqlInfo->sub.node, i);
161
    if (pSqlNode->from == NULL) {
162
      return buildInvalidOperationMsg(&msgBuf, "invalid from clause");
163 164
    }

165
    // load the table meta in the FROM clause
166
    if (pSqlNode->from->type == SQL_FROM_NODE_TABLES) {
167
      code = getTableNameFromSqlNode(pSqlNode, pMetaInfo->pTableName, &msgBuf);
168
      if (code != TSDB_CODE_SUCCESS) {
169
        return code;
170 171
      }
    } else {
172
      code = getTableNameFromSubquery(pSqlNode, pMetaInfo->pTableName, &msgBuf);
173
      if (code != TSDB_CODE_SUCCESS) {
174
        return code;
175 176 177 178
      }
    }
  }

179 180
  taosArraySort(pMetaInfo->pTableName, tnameComparFn);
  taosArrayRemoveDuplicate(pMetaInfo->pTableName, tnameComparFn, NULL);
181 182

  size_t funcSize = 0;
183 184
  if (pSqlInfo->funcs) {
    funcSize = taosArrayGetSize(pSqlInfo->funcs);
185 186 187 188
  }

  if (funcSize > 0) {
    for (size_t i = 0; i < funcSize; ++i) {
189 190
      SToken* t = taosArrayGet(pSqlInfo->funcs, i);
      assert(t != NULL);
191 192

      if (t->n >= TSDB_FUNC_NAME_LEN) {
193
        return buildSyntaxErrMsg(&msgBuf, "too long function name", t->z);
194 195
      }

196
      // Let's assume that it is an UDF/UDAF, if it is not a built-in function.
197 198
      bool scalarFunc = false;
      if (qIsBuiltinFunction(t->z, t->n, &scalarFunc) < 0) {
199 200
        char* fname = strndup(t->z, t->n);
        taosArrayPush(pMetaInfo->pUdf, &fname);
201 202 203
      }
    }
  }
204 205

  return code;
206 207
}

D
dapan1121 已提交
208
void qParserClearupMetaRequestInfo(SCatalogReq* pMetaReq) {
209 210 211 212 213 214 215
  if (pMetaReq == NULL) {
    return;
  }

  taosArrayDestroy(pMetaReq->pTableName);
  taosArrayDestroy(pMetaReq->pUdf);
}