[TD-4098]<feature> refactor IN filter
This commit is contained in:
parent
b7fded8b4f
commit
41c7b062bb
|
@ -65,7 +65,6 @@ static char* getAccountId(SSqlObj* pSql);
|
||||||
|
|
||||||
static int convertTimestampStrToInt64(tVariant *pVar, int32_t precision);
|
static int convertTimestampStrToInt64(tVariant *pVar, int32_t precision);
|
||||||
static bool serializeExprListToVariant(SArray* pList, tVariant **dest, int16_t colType, uint8_t precision);
|
static bool serializeExprListToVariant(SArray* pList, tVariant **dest, int16_t colType, uint8_t precision);
|
||||||
static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType);
|
|
||||||
|
|
||||||
static bool has(SArray* pFieldList, int32_t startIdx, const char* name);
|
static bool has(SArray* pFieldList, int32_t startIdx, const char* name);
|
||||||
static char* cloneCurrentDBName(SSqlObj* pSql);
|
static char* cloneCurrentDBName(SSqlObj* pSql);
|
||||||
|
@ -156,78 +155,60 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
tSqlExprItem* item = (tSqlExprItem *)taosArrayGet(pList, 0);
|
tSqlExpr* item = ((tSqlExprItem*)(taosArrayGet(pList, 0)))->pNode;
|
||||||
int32_t firstTokenType = item->pNode->token.type;
|
int32_t firstVarType = item->value.nType;
|
||||||
int32_t type = firstTokenType;
|
|
||||||
|
|
||||||
//nchar to binary and other xxint to bigint
|
|
||||||
toTSDBType(type);
|
|
||||||
if (colType != TSDB_DATA_TYPE_TIMESTAMP && !IS_UNSIGNED_NUMERIC_TYPE(colType)) {
|
|
||||||
if (type != colType && (type != TSDB_DATA_TYPE_BINARY || colType != TSDB_DATA_TYPE_NCHAR)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
type = colType;
|
|
||||||
|
|
||||||
SBufferWriter bw = tbufInitWriter( NULL, false);
|
SBufferWriter bw = tbufInitWriter( NULL, false);
|
||||||
|
|
||||||
tbufEnsureCapacity(&bw, 512);
|
tbufEnsureCapacity(&bw, 512);
|
||||||
|
if (colType == TSDB_DATA_TYPE_TIMESTAMP) {
|
||||||
|
tbufWriteUint32(&bw, TSDB_DATA_TYPE_BIGINT);
|
||||||
|
} else {
|
||||||
|
tbufWriteUint32(&bw, colType);
|
||||||
|
}
|
||||||
|
tbufWriteInt32(&bw, (int32_t)(pList->size));
|
||||||
|
|
||||||
int32_t size = (int32_t)(pList->size);
|
for (int32_t i = 0; i < (int32_t)pList->size; i++) {
|
||||||
tbufWriteUint32(&bw, type);
|
|
||||||
tbufWriteInt32(&bw, size);
|
|
||||||
|
|
||||||
for (int32_t i = 0; i < size; i++) {
|
|
||||||
tSqlExpr* pSub = ((tSqlExprItem*)(taosArrayGet(pList, i)))->pNode;
|
tSqlExpr* pSub = ((tSqlExprItem*)(taosArrayGet(pList, i)))->pNode;
|
||||||
|
tVariant* var = &pSub->value;
|
||||||
|
|
||||||
// check all the token type in expr list same or not
|
// check all the token type in expr list same or not
|
||||||
if (firstTokenType != pSub->token.type) {
|
if (firstVarType != var->nType) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
toTSDBType(pSub->token.type);
|
if ((colType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(colType))) {
|
||||||
|
tbufWriteInt64(&bw, var->i64);
|
||||||
tVariant var;
|
} else if (IS_UNSIGNED_NUMERIC_TYPE(colType)) {
|
||||||
tVariantCreate(&var, &pSub->token);
|
tbufWriteUint64(&bw, var->u64);
|
||||||
if (type == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(type)) {
|
} else if (colType == TSDB_DATA_TYPE_DOUBLE || colType == TSDB_DATA_TYPE_FLOAT) {
|
||||||
tbufWriteInt64(&bw, var.i64);
|
if (IS_SIGNED_NUMERIC_TYPE(var->nType) || IS_UNSIGNED_NUMERIC_TYPE(var->nType)) {
|
||||||
} else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
|
tbufWriteDouble(&bw, (double)(var->i64));
|
||||||
// ugly code, refactor later
|
|
||||||
if (IS_UNSIGNED_NUMERIC_TYPE(pSub->token.type) || IS_SIGNED_NUMERIC_TYPE(pSub->token.type)) {
|
|
||||||
tbufWriteUint64(&bw, var.i64);
|
|
||||||
} else {
|
} else {
|
||||||
tVariantDestroy(&var);
|
tbufWriteDouble(&bw, var->dKey);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
} else if (colType == TSDB_DATA_TYPE_BINARY) {
|
||||||
else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) {
|
tbufWriteBinary(&bw, var->pz, var->nLen);
|
||||||
tbufWriteDouble(&bw, var.dKey);
|
} else if (colType == TSDB_DATA_TYPE_NCHAR) {
|
||||||
} else if (type == TSDB_DATA_TYPE_BINARY){
|
char *buf = (char *)calloc(1, (var->nLen + 1)*TSDB_NCHAR_SIZE);
|
||||||
tbufWriteBinary(&bw, var.pz, var.nLen);
|
if (tVariantDump(var, buf, colType, false) != TSDB_CODE_SUCCESS) {
|
||||||
} else if (type == TSDB_DATA_TYPE_NCHAR) {
|
|
||||||
char *buf = (char *)calloc(1, (var.nLen + 1)*TSDB_NCHAR_SIZE);
|
|
||||||
if (tVariantDump(&var, buf, type, false) != TSDB_CODE_SUCCESS) {
|
|
||||||
free(buf);
|
free(buf);
|
||||||
tVariantDestroy(&var);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tbufWriteBinary(&bw, buf, twcslen((wchar_t *)buf) * TSDB_NCHAR_SIZE);
|
tbufWriteBinary(&bw, buf, twcslen((wchar_t *)buf) * TSDB_NCHAR_SIZE);
|
||||||
free(buf);
|
free(buf);
|
||||||
} else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
|
} else if (colType == TSDB_DATA_TYPE_TIMESTAMP) {
|
||||||
if (var.nType == TSDB_DATA_TYPE_BINARY) {
|
if (var->nType == TSDB_DATA_TYPE_BINARY) {
|
||||||
if (convertTimestampStrToInt64(&var, precision) < 0) {
|
if (convertTimestampStrToInt64(var, precision) < 0) {
|
||||||
tVariantDestroy(&var);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tbufWriteInt64(&bw, var.i64);
|
tbufWriteInt64(&bw, var->i64);
|
||||||
} else if (var.nType == TSDB_DATA_TYPE_BIGINT) {
|
} else if (var->nType == TSDB_DATA_TYPE_BIGINT) {
|
||||||
tbufWriteInt64(&bw, var.i64);
|
tbufWriteInt64(&bw, var->i64);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
tVariantDestroy(&var);
|
if (i == (int32_t)(pList->size - 1)) { ret = true;}
|
||||||
|
}
|
||||||
if (i == size - 1) { ret = true;}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret == true) {
|
if (ret == true) {
|
||||||
if ((*dst = calloc(1, sizeof(tVariant))) != NULL) {
|
if ((*dst = calloc(1, sizeof(tVariant))) != NULL) {
|
||||||
tVariantCreateFromBinary(*dst, tbufGetData(&bw, false), tbufTell(&bw), TSDB_DATA_TYPE_BINARY);
|
tVariantCreateFromBinary(*dst, tbufGetData(&bw, false), tbufTell(&bw), TSDB_DATA_TYPE_BINARY);
|
||||||
|
@ -239,13 +220,6 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType) {
|
|
||||||
if (pVar->nType != TSDB_DATA_TYPE_BINARY) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
SBufferReader br = tbufInitReader(pVar->pz, pVar->nLen, false);
|
|
||||||
return colType == TSDB_DATA_TYPE_NCHAR ? 0 : (tbufReadUint32(&br) == colType ? 0: -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t convertOptr(SStrToken *pToken) {
|
static uint8_t convertOptr(SStrToken *pToken) {
|
||||||
switch (pToken->type) {
|
switch (pToken->type) {
|
||||||
|
@ -3366,11 +3340,6 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo,
|
||||||
if (pRight->tokenId != TK_SET || !serializeExprListToVariant(pRight->pParam, &pVal, colType, timePrecision)) {
|
if (pRight->tokenId != TK_SET || !serializeExprListToVariant(pRight->pParam, &pVal, colType, timePrecision)) {
|
||||||
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg);
|
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg);
|
||||||
}
|
}
|
||||||
if (validateParamOfRelationIn(pVal, colType) != TSDB_CODE_SUCCESS) {
|
|
||||||
tVariantDestroy(pVal);
|
|
||||||
free(pVal);
|
|
||||||
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg);
|
|
||||||
}
|
|
||||||
pColumnFilter->pz = (int64_t)calloc(1, pVal->nLen + 1);
|
pColumnFilter->pz = (int64_t)calloc(1, pVal->nLen + 1);
|
||||||
pColumnFilter->len = pVal->nLen;
|
pColumnFilter->len = pVal->nLen;
|
||||||
pColumnFilter->filterstr = 1;
|
pColumnFilter->filterstr = 1;
|
||||||
|
@ -8151,7 +8120,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS
|
||||||
int32_t colType = -1;
|
int32_t colType = -1;
|
||||||
STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, 0)->pTableMeta;
|
STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, 0)->pTableMeta;
|
||||||
if (pCols != NULL && taosArrayGetSize(pCols) > 0) {
|
if (pCols != NULL && taosArrayGetSize(pCols) > 0) {
|
||||||
SColIndex* idx = taosArrayGet(pCols, 0);
|
SColIndex* idx = taosArrayGet(pCols, taosArrayGetSize(pCols) - 1);
|
||||||
SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, idx->colIndex);
|
SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, idx->colIndex);
|
||||||
if (pSchema != NULL) {
|
if (pSchema != NULL) {
|
||||||
colType = pSchema->type;
|
colType = pSchema->type;
|
||||||
|
|
|
@ -3364,7 +3364,7 @@ static bool tableFilterFp(const void* pNode, void* param) {
|
||||||
GET_TYPED_DATA(v, uint64_t, pInfo->sch.type, val);
|
GET_TYPED_DATA(v, uint64_t, pInfo->sch.type, val);
|
||||||
return NULL != taosHashGet((SHashObj *)pInfo->q, (char *)&v, sizeof(v));
|
return NULL != taosHashGet((SHashObj *)pInfo->q, (char *)&v, sizeof(v));
|
||||||
}
|
}
|
||||||
else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_DOUBLE) {
|
else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) {
|
||||||
double v;
|
double v;
|
||||||
GET_TYPED_DATA(v, double, pInfo->sch.type, val);
|
GET_TYPED_DATA(v, double, pInfo->sch.type, val);
|
||||||
return NULL != taosHashGet((SHashObj *)pInfo->q, (char *)&v, sizeof(v));
|
return NULL != taosHashGet((SHashObj *)pInfo->q, (char *)&v, sizeof(v));
|
||||||
|
|
Loading…
Reference in New Issue