tfunction.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "os.h"
#include "tarray.h"
#include "function.h"
#include "thash.h"
#include "taggfunction.h"
#include "tscalarfunction.h"

static SHashObj* functionHashTable = NULL;

static void doInitFunctionHashTable() {
  int numOfEntries = tListLen(aggFunc);
  functionHashTable = taosHashInit(numOfEntries, MurmurHash3_32, false, false);
  for (int32_t i = 0; i < numOfEntries; i++) {
    int32_t len = (uint32_t)strlen(aggFunc[i].name);
15 16 17

    SAggFunctionInfo* ptr = &aggFunc[i];
    taosHashPut(functionHashTable, aggFunc[i].name, len, (void*)&ptr, POINTER_BYTES);
18 19 20 21 22
  }

  numOfEntries = tListLen(scalarFunc);
  for(int32_t i = 0; i < numOfEntries; ++i) {
    int32_t len = (int32_t) strlen(scalarFunc[i].name);
23 24
    SScalarFunctionInfo* ptr = &scalarFunc[i];
    taosHashPut(functionHashTable, scalarFunc[i].name, len, (void*)&ptr, POINTER_BYTES);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  }
}

static pthread_once_t functionHashTableInit = PTHREAD_ONCE_INIT;

int32_t qIsBuiltinFunction(const char* name, int32_t len) {
  pthread_once(&functionHashTableInit, doInitFunctionHashTable);

  SAggFunctionInfo** pInfo = taosHashGet(functionHashTable, name, len);
  if (pInfo != NULL) {
    return (*pInfo)->functionId;
  } else {
    return -1;
  }
}

41 42 43 44 45 46 47 48
bool qIsValidUdf(SArray* pUdfInfo, const char* name, int32_t len, int32_t* functionId) {
  return true;
}

const char* qGetFunctionName(int32_t functionId) {

}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
bool isTagsQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int16_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);

    // "select count(tbname)" query
//    if (functId == FUNCTION_COUNT && pExpr->base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) {
//      continue;
//    }

    if (f != FUNCTION_TAGPRJ && f != FUNCTION_TID_TAG) {
      return false;
    }
  }

  return true;
}

//bool tscMultiRoundQuery(SArray* pFunctionIdList, int32_t index) {
//  if (!UTIL_TABLE_IS_SUPER_TABLE(pQueryInfo->pTableMetaInfo[index])) {
//    return false;
//  }
//
//  size_t numOfExprs = (int32_t) getNumOfExprs(pQueryInfo);
//  for(int32_t i = 0; i < numOfExprs; ++i) {
//    SExprInfo* pExpr = getExprInfo(pQueryInfo, i);
//    if (pExpr->base.functionId == FUNCTION_STDDEV_DST) {
//      return true;
//    }
//  }
//
//  return false;
//}

bool isBlockInfoQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);

    if (f == FUNCTION_BLKINFO) {
      return true;
    }
  }

  return false;
}

bool isProjectionQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TS_DUMMY) {
      continue;
    }

    if (f != FUNCTION_PRJ && f != FUNCTION_TAGPRJ && f != FUNCTION_TAG &&
        f != FUNCTION_TS && f != FUNCTION_ARITHM && f != FUNCTION_DIFF &&
        f != FUNCTION_DERIVATIVE) {
      return false;
    }
  }

  return true;
}

bool isDiffDerivQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TS_DUMMY) {
      continue;
    }

    if (f == FUNCTION_DIFF || f == FUNCTION_DERIVATIVE) {
      return true;
    }
  }

  return false;
}

bool isPointInterpQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TAG || f == FUNCTION_TS) {
      continue;
    }

    if (f != FUNCTION_INTERP) {
      return false;
    }
  }

  return true;
}

bool isArithmeticQueryOnAggResult(SArray* pFunctionIdList) {
  if (isProjectionQuery(pFunctionIdList)) {
    return false;
  }

  assert(0);

//  size_t numOfOutput = getNumOfFields(pQueryInfo);
//  for(int32_t i = 0; i < numOfOutput; ++i) {
//    SExprInfo* pExprInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, i)->pExpr;
//    if (pExprInfo->pExpr != NULL) {
//      return true;
//    }
//  }

  return false;
}

bool isGroupbyColumn(SArray* pFunctionIdList) {
//  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0);
//  int32_t         numOfCols = getNumOfColumns(pTableMetaInfo->pTableMeta);
//
//  SGroupbyExpr* pGroupbyExpr = &pQueryInfo->groupbyExpr;
//  for (int32_t k = 0; k < pGroupbyExpr->numOfGroupCols; ++k) {
//    SColIndex* pIndex = taosArrayGet(pGroupbyExpr->columnInfo, k);
//    if (!TSDB_COL_IS_TAG(pIndex->flag) && pIndex->colIndex < numOfCols) {  // group by normal columns
//      return true;
//    }
//  }

  return false;
}

bool isTopBotQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TS) {
      continue;
    }

    if (f == FUNCTION_TOP || f == FUNCTION_BOTTOM) {
      return true;
    }
  }

  return false;
}

bool isTsCompQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  if (num != 1) {
    return false;
  }

  int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, 0);
  return f == FUNCTION_TS_COMP;
}

bool isTWAQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TWA) {
      return true;
    }
  }

  return false;
}

bool isIrateQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_IRATE) {
      return true;
    }
  }

  return false;
}

bool isStabledev(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_STDDEV_DST) {
      return true;
    }
  }

  return false;
}

bool needReverseScan(SArray* pFunctionIdList) {
  assert(0);
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TS || f == FUNCTION_TS_DUMMY || f == FUNCTION_TAG) {
      continue;
    }

//    if ((f == FUNCTION_FIRST || f == FUNCTION_FIRST_DST) && pQueryInfo->order.order == TSDB_ORDER_DESC) {
//      return true;
//    }

    if (f == FUNCTION_LAST || f == FUNCTION_LAST_DST) {
      // the scan order to acquire the last result of the specified column
//      int32_t order = (int32_t)pExpr->base.param[0].i64;
//      if (order != pQueryInfo->order.order) {
//        return true;
//      }
    }
  }

  return false;
}

bool isSimpleAggregateRv(SArray* pFunctionIdList) {
  assert(0);

//  if (pQueryInfo->interval.interval > 0 || pQueryInfo->sessionWindow.gap > 0) {
//    return false;
//  }
//
//  if (tscIsDiffDerivQuery(pQueryInfo)) {
//    return false;
//  }
//
//  size_t numOfExprs = getNumOfExprs(pQueryInfo);
//  for (int32_t i = 0; i < numOfExprs; ++i) {
//    SExprInfo* pExpr = getExprInfo(pQueryInfo, i);
//    if (pExpr == NULL) {
//      continue;
//    }
//
//    int32_t functionId = pExpr->base.functionId;
//    if (functionId < 0) {
//      SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * functionId - 1);
//      if (pUdfInfo->funcType == TSDB_UDF_TYPE_AGGREGATE) {
//        return true;
//      }
//
//      continue;
//    }
//
//    if (functionId == FUNCTION_TS || functionId == FUNCTION_TS_DUMMY) {
//      continue;
//    }
//
//    if ((!IS_MULTIOUTPUT(aAggs[functionId].status)) ||
//        (functionId == FUNCTION_TOP || functionId == FUNCTION_BOTTOM || functionId == FUNCTION_TS_COMP)) {
//      return true;
//    }
//  }

  return false;
}

bool isBlockDistQuery(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, 0);
  return (num == 1 && f == FUNCTION_BLKINFO);
}

bool isTwoStageSTableQuery(SArray* pFunctionIdList, int32_t tableIndex) {
//  if (pQueryInfo == NULL) {
//    return false;
//  }
//
//  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, tableIndex);
//  if (pTableMetaInfo == NULL) {
//    return false;
//  }
//
//  if ((pQueryInfo->type & TSDB_QUERY_TYPE_FREE_RESOURCE) == TSDB_QUERY_TYPE_FREE_RESOURCE) {
//    return false;
//  }
//
//  // for ordered projection query, iterate all qualified vnodes sequentially
//  if (tscNonOrderedProjectionQueryOnSTable(pQueryInfo, tableIndex)) {
//    return false;
//  }
//
//  if (!TSDB_QUERY_HAS_TYPE(pQueryInfo->type, TSDB_QUERY_TYPE_STABLE_SUBQUERY) && pQueryInfo->command == TSDB_SQL_SELECT) {
//    return UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo);
//  }

  return false;
}

bool isProjectionQueryOnSTable(SArray* pFunctionIdList, int32_t tableIndex) {
//  STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, tableIndex);
//
//  /*
//   * In following cases, return false for non ordered project query on super table
//   * 1. failed to get tableMeta from server; 2. not a super table; 3. limitation is 0;
//   * 4. show queries, instead of a select query
//   */
//  size_t numOfExprs = getNumOfExprs(pQueryInfo);
//  if (pTableMetaInfo == NULL || !UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo) ||
//      pQueryInfo->command == TSDB_SQL_RETRIEVE_EMPTY_RESULT || numOfExprs == 0) {
//    return false;
//  }
//
//  for (int32_t i = 0; i < numOfExprs; ++i) {
//    int32_t functionId = getExprInfo(pQueryInfo, i)->base.functionId;
//
//    if (functionId < 0) {
//      SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * functionId - 1);
//      if (pUdfInfo->funcType == TSDB_UDF_TYPE_AGGREGATE) {
//        return false;
//      }
//
//      continue;
//    }
//
//    if (functionId != FUNCTION_PRJ &&
//        functionId != FUNCTION_TAGPRJ &&
//        functionId != FUNCTION_TAG &&
//        functionId != FUNCTION_TS &&
//        functionId != FUNCTION_ARITHM &&
//        functionId != FUNCTION_TS_COMP &&
//        functionId != FUNCTION_DIFF &&
//        functionId != FUNCTION_DERIVATIVE &&
//        functionId != FUNCTION_TS_DUMMY &&
//        functionId != FUNCTION_TID_TAG) {
//      return false;
//    }
//  }

  return true;
}

bool hasTagValOutput(SArray* pFunctionIdList) {
//  size_t    numOfExprs = getNumOfExprs(pQueryInfo);
//  SExprInfo* pExpr1 = getExprInfo(pQueryInfo, 0);
//
//  if (numOfExprs == 1 && pExpr1->base.functionId == FUNCTION_TS_COMP) {
//    return true;
//  }
//
//  for (int32_t i = 0; i < numOfExprs; ++i) {
//    SExprInfo* pExpr = getExprInfo(pQueryInfo, i);
//    if (pExpr == NULL) {
//      continue;
//    }
//
//    // ts_comp column required the tag value for join filter
//    if (TSDB_COL_IS_TAG(pExpr->base.colInfo.flag)) {
//      return true;
//    }
//  }

  return false;
}

bool timeWindowInterpoRequired(SArray* pFunctionIdList) {
  int32_t num = (int32_t) taosArrayGetSize(pFunctionIdList);
  for (int32_t i = 0; i < num; ++i) {
    int32_t f = *(int16_t*) taosArrayGet(pFunctionIdList, i);
    if (f == FUNCTION_TWA || f == FUNCTION_INTERP) {
      return true;
    }
  }

  return false;
}

//SQueryType setQueryType(SArray* pFunctionIdList) {
//  assert(pFunctionIdList != NULL);
//
//
//}