indexexcutorTests.cpp 8.2 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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/>.
 */

#include <gtest/gtest.h>
#include <tglobal.h>
#include <tsort.h>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

#include "executor.h"
#include "executorimpl.h"
dengyihao's avatar
dengyihao 已提交
29 30 31
#include "indexoperator.h"
#include "os.h"

dengyihao's avatar
dengyihao 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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
#include "stub.h"
#include "taos.h"
#include "tcompare.h"
#include "tdatablock.h"
#include "tdef.h"
#include "trpc.h"
#include "tvariant.h"

namespace {
SColumnInfo createColumnInfo(int32_t colId, int32_t type, int32_t bytes) {
  SColumnInfo info = {0};
  info.colId = colId;
  info.type = type;
  info.bytes = bytes;
  return info;
}

int64_t sifLeftV = 21, sifRightV = 10;
double  sifLeftVd = 21.0, sifRightVd = 10.0;

void sifFreeDataBlock(void *block) { blockDataDestroy(*(SSDataBlock **)block); }

void sifInitLogFile() {
  const char *  defaultLogFileNamePrefix = "taoslog";
  const int32_t maxLogFileNum = 10;

  tsAsyncLog = 0;
  qDebugFlag = 159;
  strcpy(tsLogDir, "/tmp/sif");
  taosRemoveDir(tsLogDir);
  taosMkDir(tsLogDir);

  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }
}

void sifAppendReservedSlot(SArray *pBlockList, int16_t *dataBlockId, int16_t *slotId, bool newBlock, int32_t rows,
                           SColumnInfo *colInfo) {
  if (newBlock) {
    SSDataBlock *res = (SSDataBlock *)taosMemoryCalloc(1, sizeof(SSDataBlock));
    res->info.numOfCols = 1;
    res->info.rows = rows;
    res->pDataBlock = taosArrayInit(1, sizeof(SColumnInfoData));
    SColumnInfoData idata = {0};
    idata.info = *colInfo;

    taosArrayPush(res->pDataBlock, &idata);
    taosArrayPush(pBlockList, &res);

    blockDataEnsureCapacity(res, rows);

    *dataBlockId = taosArrayGetSize(pBlockList) - 1;
    res->info.blockId = *dataBlockId;
    *slotId = 0;
  } else {
    SSDataBlock *res = *(SSDataBlock **)taosArrayGetLast(pBlockList);
    res->info.numOfCols++;
    SColumnInfoData idata = {0};
    idata.info = *colInfo;

    colInfoDataEnsureCapacity(&idata, 0, rows);

    taosArrayPush(res->pDataBlock, &idata);

    *dataBlockId = taosArrayGetSize(pBlockList) - 1;
    *slotId = taosArrayGetSize(res->pDataBlock) - 1;
  }
}

void sifMakeValueNode(SNode **pNode, int32_t dataType, void *value) {
  SNode *     node = (SNode *)nodesMakeNode(QUERY_NODE_VALUE);
  SValueNode *vnode = (SValueNode *)node;
  vnode->node.resType.type = dataType;

  if (IS_VAR_DATA_TYPE(dataType)) {
    vnode->datum.p = (char *)taosMemoryMalloc(varDataTLen(value));
    varDataCopy(vnode->datum.p, value);
    vnode->node.resType.bytes = varDataTLen(value);
  } else {
    vnode->node.resType.bytes = tDataTypes[dataType].bytes;
    assignVal((char *)nodesGetValueFromNode(vnode), (const char *)value, 0, dataType);
  }

  *pNode = (SNode *)vnode;
}

void sifMakeColumnNode(SNode **pNode, const char *db, const char *colName, EColumnType colType, uint8_t colValType) {
  SNode *      node = (SNode *)nodesMakeNode(QUERY_NODE_COLUMN);
  SColumnNode *rnode = (SColumnNode *)node;
  memcpy(rnode->dbName, db, strlen(db));
  memcpy(rnode->colName, colName, strlen(colName));

  rnode->colType = colType;
  rnode->node.resType.type = colValType;

  *pNode = (SNode *)rnode;
}

void sifMakeOpNode(SNode **pNode, EOperatorType opType, int32_t resType, SNode *pLeft, SNode *pRight) {
  SNode *        node = (SNode *)nodesMakeNode(QUERY_NODE_OPERATOR);
  SOperatorNode *onode = (SOperatorNode *)node;
  onode->node.resType.type = resType;
  onode->node.resType.bytes = tDataTypes[resType].bytes;

  onode->opType = opType;
  onode->pLeft = pLeft;
  onode->pRight = pRight;

  *pNode = (SNode *)onode;
}

void sifMakeListNode(SNode **pNode, SNodeList *list, int32_t resType) {
  SNode *        node = (SNode *)nodesMakeNode(QUERY_NODE_NODE_LIST);
  SNodeListNode *lnode = (SNodeListNode *)node;
  lnode->dataType.type = resType;
  lnode->pNodeList = list;

  *pNode = (SNode *)lnode;
}

void sifMakeLogicNode(SNode **pNode, ELogicConditionType opType, SNode **nodeList, int32_t nodeNum) {
  SNode *              node = (SNode *)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  SLogicConditionNode *onode = (SLogicConditionNode *)node;
  onode->condType = opType;
  onode->node.resType.type = TSDB_DATA_TYPE_BOOL;
  onode->node.resType.bytes = sizeof(bool);

  onode->pParameterList = nodesMakeList();
  for (int32_t i = 0; i < nodeNum; ++i) {
    nodesListAppend(onode->pParameterList, nodeList[i]);
  }

  *pNode = (SNode *)onode;
}

void sifMakeTargetNode(SNode **pNode, int16_t dataBlockId, int16_t slotId, SNode *snode) {
  SNode *      node = (SNode *)nodesMakeNode(QUERY_NODE_TARGET);
  STargetNode *onode = (STargetNode *)node;
  onode->pExpr = snode;
  onode->dataBlockId = dataBlockId;
  onode->slotId = slotId;

  *pNode = (SNode *)onode;
}

}  // namespace

#if 1
TEST(testCase, index_filter) {
dengyihao's avatar
dengyihao 已提交
182 183 184 185
  {
    SNode *pLeft = NULL, *pRight = NULL, *opNode = NULL, *res = NULL;
    sifMakeColumnNode(&pLeft, "test", "col", COLUMN_TYPE_TAG, TSDB_DATA_TYPE_INT);
    sifMakeValueNode(&pRight, TSDB_DATA_TYPE_INT, &sifRightV);
dengyihao's avatar
dengyihao 已提交
186 187 188 189 190
    sifMakeOpNode(&opNode, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_INT, pLeft, pRight);
    SArray *result = taosArrayInit(4, sizeof(uint64_t));
    doFilterTag(opNode, result);
    EXPECT_EQ(1, taosArrayGetSize(result));
    taosArrayDestroy(result);
dengyihao's avatar
dengyihao 已提交
191 192 193 194 195 196
    nodesDestroyNode(res);
  }
  {
    SNode *pLeft = NULL, *pRight = NULL, *opNode = NULL, *res = NULL;
    sifMakeColumnNode(&pLeft, "test", "col", COLUMN_TYPE_TAG, TSDB_DATA_TYPE_INT);
    sifMakeValueNode(&pRight, TSDB_DATA_TYPE_INT, &sifRightV);
dengyihao's avatar
dengyihao 已提交
197
    sifMakeOpNode(&opNode, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_DOUBLE, pLeft, pRight);
dengyihao's avatar
dengyihao 已提交
198

dengyihao's avatar
dengyihao 已提交
199 200 201 202 203
    SArray *result = taosArrayInit(4, sizeof(uint64_t));
    doFilterTag(opNode, result);
    EXPECT_EQ(1, taosArrayGetSize(result));

    taosArrayDestroy(result);
dengyihao's avatar
dengyihao 已提交
204 205
    nodesDestroyNode(res);
  }
dengyihao's avatar
dengyihao 已提交
206 207
}

dengyihao's avatar
dengyihao 已提交
208 209 210 211 212
TEST(testCase, index_filter_varify) {
  {
    SNode *pLeft = NULL, *pRight = NULL, *opNode = NULL, *res = NULL;
    sifMakeColumnNode(&pLeft, "test", "col", COLUMN_TYPE_TAG, TSDB_DATA_TYPE_INT);
    sifMakeValueNode(&pRight, TSDB_DATA_TYPE_INT, &sifRightV);
dengyihao's avatar
dengyihao 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    sifMakeOpNode(&opNode, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_INT, pLeft, pRight);
    nodesDestroyNode(res);

    SIdxFltStatus st = idxGetFltStatus(opNode);
    EXPECT_EQ(st, SFLT_ACCURATE_INDEX);
  }
  {
    SNode *pLeft = NULL, *pRight = NULL, *opNode = NULL, *res = NULL;
    sifMakeColumnNode(&pLeft, "test", "col", COLUMN_TYPE_TAG, TSDB_DATA_TYPE_INT);
    sifMakeValueNode(&pRight, TSDB_DATA_TYPE_INT, &sifRightV);
    sifMakeOpNode(&opNode, OP_TYPE_LOWER_THAN, TSDB_DATA_TYPE_DOUBLE, pLeft, pRight);

    SIdxFltStatus st = idxGetFltStatus(opNode);
    EXPECT_EQ(st, SFLT_COARSE_INDEX);
    nodesDestroyNode(res);
  }
  {
    SNode *pLeft = NULL, *pRight = NULL, *opNode = NULL, *res = NULL;
    sifMakeColumnNode(&pLeft, "test", "col", COLUMN_TYPE_TAG, TSDB_DATA_TYPE_INT);
    sifMakeValueNode(&pRight, TSDB_DATA_TYPE_INT, &sifRightV);
    sifMakeOpNode(&opNode, OP_TYPE_GREATER_THAN, TSDB_DATA_TYPE_INT, pLeft, pRight);
dengyihao's avatar
dengyihao 已提交
234 235 236 237 238 239 240 241 242
    nodesDestroyNode(res);

    SIdxFltStatus st = idxGetFltStatus(opNode);
    EXPECT_EQ(st, SFLT_ACCURATE_INDEX);
  }
  {
    SNode *pLeft = NULL, *pRight = NULL, *opNode = NULL, *res = NULL;
    sifMakeColumnNode(&pLeft, "test", "col", COLUMN_TYPE_TAG, TSDB_DATA_TYPE_INT);
    sifMakeValueNode(&pRight, TSDB_DATA_TYPE_INT, &sifRightV);
dengyihao's avatar
dengyihao 已提交
243
    sifMakeOpNode(&opNode, OP_TYPE_GREATER_THAN, TSDB_DATA_TYPE_DOUBLE, pLeft, pRight);
dengyihao's avatar
dengyihao 已提交
244 245 246 247 248 249

    SIdxFltStatus st = idxGetFltStatus(opNode);
    EXPECT_EQ(st, SFLT_COARSE_INDEX);
    nodesDestroyNode(res);
  }
}
dengyihao's avatar
dengyihao 已提交
250 251 252 253

#endif

#pragma GCC diagnostic pop