parUtil.c 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15

X
Xiaoyu Wang 已提交
16
#include "parUtil.h"
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static char* getSyntaxErrFormat(int32_t errCode) {
  switch (errCode) {
    case TSDB_CODE_PAR_SYNTAX_ERROR:
      return "syntax error near \"%s\"";
    case TSDB_CODE_PAR_INCOMPLETE_SQL:
      return "Incomplete SQL statement";
    case TSDB_CODE_PAR_INVALID_COLUMN:
      return "Invalid column name : %s";
    case TSDB_CODE_PAR_TABLE_NOT_EXIST:
      return "Table does not exist : %s";
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
      return "Column ambiguously defined : %s";
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
      return "Invalid value type : %s";
    case TSDB_CODE_PAR_INVALID_FUNTION:
      return "Invalid function name : %s";
    case TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION:
      return "There mustn't be aggregation";
    case TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT:
      return "ORDER BY item must be the number of a SELECT-list expression";
    case TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION:
      return "Not a GROUP BY expression";
    case TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION:
      return "Not SELECTed expression";
    case TSDB_CODE_PAR_NOT_SINGLE_GROUP:
      return "Not a single-group group function";
    case TSDB_CODE_PAR_TAGS_NOT_MATCHED:
45
      return "Tags number not matched";
46
    case TSDB_CODE_PAR_INVALID_TAG_NAME:
47
      return "Invalid tag name : %s";
48
    case TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG:
49
      return "Name or password too long";
50
    case TSDB_CODE_PAR_PASSWD_EMPTY:
51
      return "Password can not be empty";
52
    case TSDB_CODE_PAR_INVALID_PORT:
53
      return "Port should be an integer that is less than 65535 and greater than 0";
54
    case TSDB_CODE_PAR_INVALID_ENDPOINT:
55 56 57
      return "Endpoint should be in the format of 'fqdn:port'";
    case TSDB_CODE_PAR_EXPRIE_STATEMENT:
      return "This statement is no longer supported";
X
Xiaoyu Wang 已提交
58 59
    case TSDB_CODE_PAR_INTER_VALUE_TOO_SMALL:
      return "Interval cannot be less than %d us";
X
Xiaoyu Wang 已提交
60
    case TSDB_CODE_PAR_DB_NOT_SPECIFIED:
61
      return "Database not specified";
X
Xiaoyu Wang 已提交
62 63
    case TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME:
      return "Invalid identifier name : %s";
X
Xiaoyu Wang 已提交
64
    case TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR:
65
      return "Corresponding super table not in this db";
X
Xiaoyu Wang 已提交
66
    case TSDB_CODE_PAR_INVALID_RANGE_OPTION:
67
      return "Invalid option %s: %"PRId64" valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
68
    case TSDB_CODE_PAR_INVALID_STR_OPTION:
69
      return "Invalid option %s: %s";
X
Xiaoyu Wang 已提交
70
    case TSDB_CODE_PAR_INVALID_ENUM_OPTION:
71
      return "Invalid option %s: %"PRId64", only %d, %d allowed";
X
Xiaoyu Wang 已提交
72
    case TSDB_CODE_PAR_INVALID_TTL_OPTION:
73
      return "Invalid option ttl: %"PRId64", should be greater than or equal to %d";
X
Xiaoyu Wang 已提交
74
    case TSDB_CODE_PAR_INVALID_KEEP_NUM:
75
      return "Invalid number of keep options";
X
Xiaoyu Wang 已提交
76
    case TSDB_CODE_PAR_INVALID_KEEP_ORDER:
77
      return "Invalid keep value, should be keep0 <= keep1 <= keep2";
X
Xiaoyu Wang 已提交
78
    case TSDB_CODE_PAR_INVALID_KEEP_VALUE:
79
      return "Invalid option keep: %d, %d, %d valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
80
    case TSDB_CODE_PAR_INVALID_COMMENT_OPTION:
81
      return "Invalid option comment, length cannot exceed %d";
X
Xiaoyu Wang 已提交
82
    case TSDB_CODE_PAR_INVALID_F_RANGE_OPTION:
83
      return "Invalid option %s: %f valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
84
    case TSDB_CODE_PAR_INVALID_ROLLUP_OPTION:
85
      return "Invalid option rollup: only one function is allowed";
X
Xiaoyu Wang 已提交
86
    case TSDB_CODE_PAR_INVALID_RETENTIONS_OPTION:
87
      return "Invalid option retentions";
X
Xiaoyu Wang 已提交
88 89
    case TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST:
      return "GROUP BY and WINDOW-clause can't be used together";
90 91 92 93
    case TSDB_CODE_PAR_INVALID_OPTION_UNIT:
      return "Invalid option %s unit: %c, only m, h, d allowed";
    case TSDB_CODE_PAR_INVALID_KEEP_UNIT:
      return "Invalid option keep unit: %c, %c, %c, only m, h, d allowed";
X
Xiaoyu Wang 已提交
94 95
    case TSDB_CODE_PAR_AGG_FUNC_NESTING:
      return "Aggregate functions do not support nesting";
X
Xiaoyu Wang 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE:
      return "Only support STATE_WINDOW on integer column";
    case TSDB_CODE_PAR_INVALID_STATE_WIN_COL:
      return "Not support STATE_WINDOW on tag column";
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TABLE:
      return "STATE_WINDOW not support for super table query";
    case TSDB_CODE_PAR_INTER_SESSION_GAP:
      return "SESSION gap should be fixed time window, and greater than 0";
    case TSDB_CODE_PAR_INTER_SESSION_COL:
      return "Only support SESSION on primary timestamp column";
    case TSDB_CODE_PAR_INTER_OFFSET_NEGATIVE:
      return "Interval offset cannot be negative";
    case TSDB_CODE_PAR_INTER_OFFSET_UNIT:
      return "Cannot use 'year' as offset when interval is 'month'";
    case TSDB_CODE_PAR_INTER_OFFSET_TOO_BIG:
      return "Interval offset should be shorter than interval";
    case TSDB_CODE_PAR_INTER_SLIDING_UNIT:
      return "Does not support sliding when interval is natural month/year";
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_BIG:
      return "sliding value no larger than the interval value";
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_SMALL:
      return "sliding value can not less than 1% of interval value";
118 119 120 121 122 123 124 125 126 127 128 129
    case TSDB_CODE_OUT_OF_MEMORY:
      return "Out of memory";
    default:
      return "Unknown error";
  }
}

int32_t generateSyntaxErrMsg(SMsgBuf* pBuf, int32_t errCode, ...) {
  va_list vArgList;
  va_start(vArgList, errCode);
  vsnprintf(pBuf->buf, pBuf->len, getSyntaxErrFormat(errCode), vArgList);
  va_end(vArgList);
X
Xiaoyu Wang 已提交
130
  terrno = errCode;
131 132 133
  return errCode;
}

X
Xiaoyu Wang 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) {
  strncpy(pBuf->buf, msg, pBuf->len);
  return TSDB_CODE_TSC_INVALID_OPERATION;
}

int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr) {
  const char* msgFormat1 = "syntax error near \'%s\'";
  const char* msgFormat2 = "syntax error near \'%s\' (%s)";
  const char* msgFormat3 = "%s";

  const char* prefix = "syntax error";
  if (sourceStr == NULL) {
    assert(additionalInfo != NULL);
    snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo);
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
  }
150

X
Xiaoyu Wang 已提交
151 152 153 154 155 156 157 158 159 160 161 162
  char buf[64] = {0};  // only extract part of sql string
  strncpy(buf, sourceStr, tListLen(buf) - 1);

  if (additionalInfo != NULL) {
    snprintf(pBuf->buf, pBuf->len, msgFormat2, buf, additionalInfo);
  } else {
    const char* msgFormat = (0 == strncmp(sourceStr, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1;
    snprintf(pBuf->buf, pBuf->len, msgFormat, buf);
  }

  return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
}
163

X
Xiaoyu Wang 已提交
164 165
static uint32_t getTableMetaSize(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
166

X
Xiaoyu Wang 已提交
167 168 169
  int32_t totalCols = 0;
  if (pTableMeta->tableInfo.numOfColumns >= 0) {
    totalCols = pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags;
170 171
  }

X
Xiaoyu Wang 已提交
172
  return sizeof(STableMeta) + totalCols * sizeof(SSchema);
173 174
}

X
Xiaoyu Wang 已提交
175 176 177
STableMeta* tableMetaDup(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  size_t size = getTableMetaSize(pTableMeta);
178

wafwerar's avatar
wafwerar 已提交
179
  STableMeta* p = taosMemoryMalloc(size);
X
Xiaoyu Wang 已提交
180 181
  memcpy(p, pTableMeta, size);
  return p;
182 183
}

X
Xiaoyu Wang 已提交
184 185 186
SSchema *getTableColumnSchema(const STableMeta *pTableMeta) {
  assert(pTableMeta != NULL);
  return (SSchema*) pTableMeta->schema;
187 188
}

X
Xiaoyu Wang 已提交
189 190
static SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) {
  assert(pTableMeta != NULL && pTableMeta->schema != NULL && colIndex >= 0 && colIndex < (getNumOfColumns(pTableMeta) + getNumOfTags(pTableMeta)));
191

X
Xiaoyu Wang 已提交
192 193
  SSchema* pSchema = (SSchema*) pTableMeta->schema;
  return &pSchema[colIndex];
194
}
195

X
Xiaoyu Wang 已提交
196 197 198
SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL && (pTableMeta->tableType == TSDB_SUPER_TABLE || pTableMeta->tableType == TSDB_CHILD_TABLE));
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
199 200
}

X
Xiaoyu Wang 已提交
201 202 203 204
int32_t getNumOfColumns(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  // table created according to super table, use data from super table
  return getTableInfo(pTableMeta).numOfColumns;
205 206
}

X
Xiaoyu Wang 已提交
207 208 209
int32_t getNumOfTags(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  return getTableInfo(pTableMeta).numOfTags;
210 211
}

X
Xiaoyu Wang 已提交
212
STableComInfo getTableInfo(const STableMeta* pTableMeta) {
213
  assert(pTableMeta != NULL);
X
Xiaoyu Wang 已提交
214
  return pTableMeta->tableInfo;
H
Haojun Liao 已提交
215
}
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen) {
  // delete escape character: \\, \', \"
  char delim = src[0];
  int32_t cnt = 0;
  int32_t j = 0;
  for (uint32_t k = 1; k < len - 1; ++k) {
    if (j >= dlen) {
      break;
    }
    if (src[k] == '\\' || (src[k] == delim && src[k + 1] == delim)) {
      dst[j] = src[k + 1];
      cnt++;
      j++;
      k++;
      continue;
    }
    dst[j] = src[k];
    j++;
  }
  dst[j] = '\0';
  return j;
}