Merge pull request #15643 from taosdata/fix/TD-18040

enh(query): add selectivity support for indefinite rows functions
This commit is contained in:
Ganlin Zhao 2022-08-01 18:42:22 +08:00 committed by GitHub
commit 56ca1ac288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 375 additions and 291 deletions

View File

@ -672,7 +672,7 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc
numOfRows = pfCtx->fpSet.process(pfCtx); numOfRows = pfCtx->fpSet.process(pfCtx);
} else if (fmIsAggFunc(pfCtx->functionId)) { } else if (fmIsAggFunc(pfCtx->functionId)) {
// diff/derivative selective value should be set during function execution // selective value output should be set during corresponding function execution
if (fmIsSelectValueFunc(pfCtx->functionId)) { if (fmIsSelectValueFunc(pfCtx->functionId)) {
continue; continue;
} }

View File

@ -2465,7 +2465,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
{ {
.name = "statecount", .name = "statecount",
.type = FUNCTION_TYPE_STATE_COUNT, .type = FUNCTION_TYPE_STATE_COUNT,
.classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC, .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
.translateFunc = translateStateCount, .translateFunc = translateStateCount,
.getEnvFunc = getStateFuncEnv, .getEnvFunc = getStateFuncEnv,
.initFunc = functionSetup, .initFunc = functionSetup,
@ -2476,7 +2476,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
{ {
.name = "stateduration", .name = "stateduration",
.type = FUNCTION_TYPE_STATE_DURATION, .type = FUNCTION_TYPE_STATE_DURATION,
.classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_FORBID_STREAM_FUNC, .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
.translateFunc = translateStateDuration, .translateFunc = translateStateDuration,
.getEnvFunc = getStateFuncEnv, .getEnvFunc = getStateFuncEnv,
.initFunc = functionSetup, .initFunc = functionSetup,
@ -2487,7 +2487,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
{ {
.name = "csum", .name = "csum",
.type = FUNCTION_TYPE_CSUM, .type = FUNCTION_TYPE_CSUM,
.classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_CUMULATIVE_FUNC, .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_CUMULATIVE_FUNC,
.translateFunc = translateCsum, .translateFunc = translateCsum,
.getEnvFunc = getCsumFuncEnv, .getEnvFunc = getCsumFuncEnv,
.initFunc = functionSetup, .initFunc = functionSetup,
@ -2499,7 +2499,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
{ {
.name = "mavg", .name = "mavg",
.type = FUNCTION_TYPE_MAVG, .type = FUNCTION_TYPE_MAVG,
.classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC, .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
.translateFunc = translateMavg, .translateFunc = translateMavg,
.getEnvFunc = getMavgFuncEnv, .getEnvFunc = getMavgFuncEnv,
.initFunc = mavgFunctionSetup, .initFunc = mavgFunctionSetup,

View File

@ -4651,10 +4651,15 @@ int32_t stateCountFunction(SqlFunctionCtx* pCtx) {
numOfElems++; numOfElems++;
if (colDataIsNull_f(pInputCol->nullbitmap, i)) { if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
colDataAppendNULL(pOutput, i); colDataAppendNULL(pOutput, i);
// handle selectivity
if (pCtx->subsidiaries.num > 0) {
appendSelectivityValue(pCtx, i, i);
}
continue; continue;
} }
bool ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param); bool ret = checkStateOp(op, pInputCol, i, pCtx->param[2].param);
int64_t output = -1; int64_t output = -1;
if (ret) { if (ret) {
output = ++pInfo->count; output = ++pInfo->count;
@ -4662,6 +4667,11 @@ int32_t stateCountFunction(SqlFunctionCtx* pCtx) {
pInfo->count = 0; pInfo->count = 0;
} }
colDataAppend(pOutput, i, (char*)&output, false); colDataAppend(pOutput, i, (char*)&output, false);
// handle selectivity
if (pCtx->subsidiaries.num > 0) {
appendSelectivityValue(pCtx, i, i);
}
} }
return numOfElems; return numOfElems;
@ -4694,6 +4704,10 @@ int32_t stateDurationFunction(SqlFunctionCtx* pCtx) {
numOfElems++; numOfElems++;
if (colDataIsNull_f(pInputCol->nullbitmap, i)) { if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
colDataAppendNULL(pOutput, i); colDataAppendNULL(pOutput, i);
// handle selectivity
if (pCtx->subsidiaries.num > 0) {
appendSelectivityValue(pCtx, i, i);
}
continue; continue;
} }
@ -4710,6 +4724,11 @@ int32_t stateDurationFunction(SqlFunctionCtx* pCtx) {
pInfo->durationStart = 0; pInfo->durationStart = 0;
} }
colDataAppend(pOutput, i, (char*)&output, false); colDataAppend(pOutput, i, (char*)&output, false);
// handle selectivity
if (pCtx->subsidiaries.num > 0) {
appendSelectivityValue(pCtx, i, i);
}
} }
return numOfElems; return numOfElems;
@ -4762,6 +4781,11 @@ int32_t csumFunction(SqlFunctionCtx* pCtx) {
} }
} }
// handle selectivity
if (pCtx->subsidiaries.num > 0) {
appendSelectivityValue(pCtx, i, pos);
}
numOfElems++; numOfElems++;
} }
@ -4834,6 +4858,11 @@ int32_t mavgFunction(SqlFunctionCtx* pCtx) {
colDataAppend(pOutput, pos, (char*)&result, false); colDataAppend(pOutput, pos, (char*)&result, false);
} }
// handle selectivity
if (pCtx->subsidiaries.num > 0) {
appendSelectivityValue(pCtx, i, pos);
}
numOfElems++; numOfElems++;
} }

File diff suppressed because it is too large Load Diff

View File

@ -279,14 +279,14 @@ class TDTestCase:
tdSql.error(self.csum_query_form(alias=", diff(c1)")) # mix with calculation function 2 tdSql.error(self.csum_query_form(alias=", diff(c1)")) # mix with calculation function 2
# tdSql.error(self.csum_query_form(alias=" + 2")) # mix with arithmetic 1 # tdSql.error(self.csum_query_form(alias=" + 2")) # mix with arithmetic 1
tdSql.error(self.csum_query_form(alias=" + avg(c1)")) # mix with arithmetic 2 tdSql.error(self.csum_query_form(alias=" + avg(c1)")) # mix with arithmetic 2
tdSql.error(self.csum_query_form(alias=", c2")) # mix with other 1 # tdSql.error(self.csum_query_form(alias=", c2")) # mix with other 1
# tdSql.error(self.csum_query_form(table_expr="stb1")) # select stb directly # tdSql.error(self.csum_query_form(table_expr="stb1")) # select stb directly
stb_join = { #stb_join = {
"col": "stb1.c1", # "col": "stb1.c1",
"table_expr": "stb1, stb2", # "table_expr": "stb1, stb2",
"condition": "where stb1.ts=stb2.ts and stb1.st1=stb2.st2 order by stb1.ts" # "condition": "where stb1.ts=stb2.ts and stb1.st1=stb2.st2 order by stb1.ts"
} #}
tdSql.error(self.csum_query_form(**stb_join)) # stb join #tdSql.error(self.csum_query_form(**stb_join)) # stb join
interval_sql = { interval_sql = {
"condition": "where ts>0 and ts < now interval(1h) fill(next)" "condition": "where ts>0 and ts < now interval(1h) fill(next)"
} }
@ -421,6 +421,19 @@ class TDTestCase:
tdSql.query("select csum(abs(c1))+2 from db.t1 ") tdSql.query("select csum(abs(c1))+2 from db.t1 ")
tdSql.checkRows(4) tdSql.checkRows(4)
# support selectivity
tdSql.query("select ts, c1, csum(1) from db.t1")
tdSql.checkRows(7)
tdSql.query("select csum(1), ts, c1 from db.t1")
tdSql.checkRows(7)
tdSql.query("select ts, c1, c2, c3, csum(1), ts, c4, c5, c6 from db.t1")
tdSql.checkRows(7)
tdSql.query("select ts, c1, csum(1), c4, c5, csum(1), c6 from db.t1")
tdSql.checkRows(7)
def csum_support_stable(self): def csum_support_stable(self):
tdSql.query(" select csum(1) from db.stb1 ") tdSql.query(" select csum(1) from db.stb1 ")
tdSql.checkRows(70) tdSql.checkRows(70)
@ -474,6 +487,7 @@ class TDTestCase:
# tdSql.checkRows(4) # tdSql.checkRows(4)
def run(self): def run(self):
import traceback import traceback
try: try:

View File

@ -104,8 +104,6 @@ class TDTestCase:
"select stateduration(c1 ,'GT',1,1s) , min(c1) from t1", "select stateduration(c1 ,'GT',1,1s) , min(c1) from t1",
"select stateduration(c1 ,'GT',1,1s) , spread(c1) from t1", "select stateduration(c1 ,'GT',1,1s) , spread(c1) from t1",
"select stateduration(c1 ,'GT',1,1s) , diff(c1) from t1", "select stateduration(c1 ,'GT',1,1s) , diff(c1) from t1",
"select stateduration(c1 ,'GT',1,1s) , abs(c1) from t1",
"select stateduration(c1 ,'GT',1,1s) , c1 from t1",
] ]
for error_sql in error_sql_lists: for error_sql in error_sql_lists:
tdSql.error(error_sql) tdSql.error(error_sql)
@ -226,18 +224,24 @@ class TDTestCase:
tdSql.query("select stateduration(c6,'GT',1,1s) from ct4") tdSql.query("select stateduration(c6,'GT',1,1s) from ct4")
tdSql.checkRows(12) tdSql.checkRows(12)
tdSql.error("select stateduration(c6,'GT',1,1s),tbname from ct1") tdSql.query("select stateduration(c6,'GT',1,1s),tbname from ct1")
tdSql.error("select stateduration(c6,'GT',1,1s),t1 from ct1") tdSql.checkRows(13)
tdSql.query("select stateduration(c6,'GT',1,1s),t1 from ct1")
tdSql.checkRows(13)
# unique with common col # unique with common col
tdSql.error("select stateduration(c6,'GT',1,1s) ,ts from ct1") tdSql.query("select stateduration(c6,'GT',1,1s) ,ts from ct1")
tdSql.error("select stateduration(c6,'GT',1,1s) ,c1 from ct1") tdSql.checkRows(13)
tdSql.query("select stateduration(c6,'GT',1,1s) ,c1 from ct1")
tdSql.checkRows(13)
# unique with scalar function # unique with scalar function
tdSql.error("select stateduration(c6,'GT',1,1s) ,abs(c1) from ct1") tdSql.query("select stateduration(c6,'GT',1,1s) , abs(c1) from ct1")
tdSql.error("select stateduration(c6,'GT',1,1s) , unique(c2) from ct1") tdSql.checkRows(13)
tdSql.error("select stateduration(c6,'GT',1,1s) , abs(c2)+2 from ct1") tdSql.query("select stateduration(c6,'GT',1,1s) , abs(c2)+2 from ct1")
tdSql.checkRows(13)
tdSql.error("select stateduration(c6,'GT',1,1s) , unique(c2) from ct1")
# unique with aggregate function # unique with aggregate function
tdSql.error("select stateduration(c6,'GT',1,1s) ,sum(c1) from ct1") tdSql.error("select stateduration(c6,'GT',1,1s) ,sum(c1) from ct1")

View File

@ -105,8 +105,6 @@ class TDTestCase:
"select statecount(c1 ,'GT',1) , min(c1) from t1", "select statecount(c1 ,'GT',1) , min(c1) from t1",
"select statecount(c1 ,'GT',1) , spread(c1) from t1", "select statecount(c1 ,'GT',1) , spread(c1) from t1",
"select statecount(c1 ,'GT',1) , diff(c1) from t1", "select statecount(c1 ,'GT',1) , diff(c1) from t1",
"select statecount(c1 ,'GT',1) , abs(c1) from t1",
"select statecount(c1 ,'GT',1) , c1 from t1",
] ]
for error_sql in error_sql_lists: for error_sql in error_sql_lists:
tdSql.error(error_sql) tdSql.error(error_sql)
@ -227,17 +225,56 @@ class TDTestCase:
tdSql.query("select statecount(c6,'GT',1) from ct4") tdSql.query("select statecount(c6,'GT',1) from ct4")
tdSql.checkRows(12) tdSql.checkRows(12)
tdSql.error("select statecount(c6,'GT',1),tbname from ct1") tdSql.query("select statecount(c6,'GT',1),tbname from ct1")
tdSql.error("select statecount(c6,'GT',1),t1 from ct1") tdSql.checkRows(13)
tdSql.query("select statecount(c6,'GT',1),t1 from ct1")
tdSql.checkRows(13)
# unique with common col # unique with common col
tdSql.error("select statecount(c6,'GT',1) ,ts from ct1") tdSql.query("select statecount(c6,'GT',1) ,ts from ct1")
tdSql.error("select statecount(c6,'GT',1) ,c1 from ct1") tdSql.checkRows(13)
tdSql.query("select ts, statecount(c6,'GT',1) from ct1")
tdSql.checkRows(13)
tdSql.query("select statecount(c6,'GT',1) ,c1 from ct1")
tdSql.checkRows(13)
tdSql.query("select c1, statecount(c6,'GT',1) from ct1")
tdSql.checkRows(13)
tdSql.query("select ts, c1, c2, c3, statecount(c6,'GT',1) from ct1")
tdSql.checkRows(13)
tdSql.query("select statecount(c6,'GT',1), ts, c1, c2, c3 from ct1")
tdSql.checkRows(13)
tdSql.query("select ts, c1, c2, c3, statecount(c6,'GT',1), ts, c4, c5, c6 from ct1")
tdSql.checkRows(13)
tdSql.query("select stateduration(c6,'GT',1) ,ts from ct1")
tdSql.checkRows(13)
tdSql.query("select ts, stateduration(c6,'GT',1) from ct1")
tdSql.checkRows(13)
tdSql.query("select stateduration(c6,'GT',1) ,c1 from ct1")
tdSql.checkRows(13)
tdSql.query("select c1, stateduration(c6,'GT',1) from ct1")
tdSql.checkRows(13)
tdSql.query("select ts, c1, c2, c3, stateduration(c6,'GT',1) from ct1")
tdSql.checkRows(13)
tdSql.query("select stateduration(c6,'GT',1), ts, c1, c2, c3 from ct1")
tdSql.checkRows(13)
tdSql.query("select ts, c1, c2, c3, stateduration(c6,'GT',1), ts, c4, c5, c6 from ct1")
tdSql.checkRows(13)
# unique with scalar function # unique with scalar function
tdSql.error("select statecount(c6,'GT',1) ,abs(c1) from ct1") tdSql.query("select statecount(c6,'GT',1) , abs(c1) from ct1")
tdSql.checkRows(13)
tdSql.query("select statecount(c6,'GT',1) , abs(c2)+2 from ct1")
tdSql.checkRows(13)
tdSql.error("select statecount(c6,'GT',1) , unique(c2) from ct1") tdSql.error("select statecount(c6,'GT',1) , unique(c2) from ct1")
tdSql.error("select statecount(c6,'GT',1) , abs(c2)+2 from ct1")
tdSql.query("select stateduration(c6,'GT',1) , abs(c1) from ct1")
tdSql.checkRows(13)
tdSql.query("select stateduration(c6,'GT',1) , abs(c2)+2 from ct1")
tdSql.checkRows(13)
tdSql.error("select stateduration(c6,'GT',1) , unique(c2) from ct1")
# unique with aggregate function # unique with aggregate function

View File

@ -147,7 +147,7 @@ python3 ./test.py -f 2-query/query_cols_tags_and_or.py
python3 ./test.py -f 2-query/elapsed.py python3 ./test.py -f 2-query/elapsed.py
python3 ./test.py -f 2-query/csum.py python3 ./test.py -f 2-query/csum.py
python3 ./test.py -f 2-query/mavg.py #python3 ./test.py -f 2-query/mavg.py
python3 ./test.py -f 2-query/sample.py python3 ./test.py -f 2-query/sample.py
python3 ./test.py -f 2-query/function_diff.py python3 ./test.py -f 2-query/function_diff.py
python3 ./test.py -f 2-query/unique.py python3 ./test.py -f 2-query/unique.py
@ -341,7 +341,7 @@ python3 ./test.py -f 2-query/query_cols_tags_and_or.py -Q 2
python3 ./test.py -f 2-query/avg.py -Q 2 python3 ./test.py -f 2-query/avg.py -Q 2
# python3 ./test.py -f 2-query/elapsed.py -Q 2 # python3 ./test.py -f 2-query/elapsed.py -Q 2
python3 ./test.py -f 2-query/csum.py -Q 2 python3 ./test.py -f 2-query/csum.py -Q 2
python3 ./test.py -f 2-query/mavg.py -Q 2 #python3 ./test.py -f 2-query/mavg.py -Q 2
python3 ./test.py -f 2-query/sample.py -Q 2 python3 ./test.py -f 2-query/sample.py -Q 2
python3 ./test.py -f 2-query/function_diff.py -Q 2 python3 ./test.py -f 2-query/function_diff.py -Q 2
python3 ./test.py -f 2-query/unique.py -Q 2 python3 ./test.py -f 2-query/unique.py -Q 2
@ -428,7 +428,7 @@ python3 ./test.py -f 2-query/query_cols_tags_and_or.py -Q 3
# python3 ./test.py -f 2-query/avg.py -Q 3 # python3 ./test.py -f 2-query/avg.py -Q 3
# python3 ./test.py -f 2-query/elapsed.py -Q 3 # python3 ./test.py -f 2-query/elapsed.py -Q 3
python3 ./test.py -f 2-query/csum.py -Q 3 python3 ./test.py -f 2-query/csum.py -Q 3
python3 ./test.py -f 2-query/mavg.py -Q 3 #python3 ./test.py -f 2-query/mavg.py -Q 3
python3 ./test.py -f 2-query/sample.py -Q 3 python3 ./test.py -f 2-query/sample.py -Q 3
python3 ./test.py -f 2-query/function_diff.py -Q 3 python3 ./test.py -f 2-query/function_diff.py -Q 3
python3 ./test.py -f 2-query/unique.py -Q 3 python3 ./test.py -f 2-query/unique.py -Q 3
@ -452,4 +452,4 @@ python3 ./test.py -f 2-query/count_partition.py -Q 3
python3 ./test.py -f 2-query/max_partition.py -Q 3 python3 ./test.py -f 2-query/max_partition.py -Q 3
python3 ./test.py -f 2-query/last_row.py -Q 3 python3 ./test.py -f 2-query/last_row.py -Q 3
python3 ./test.py -f 2-query/tsbsQuery.py -Q 3 python3 ./test.py -f 2-query/tsbsQuery.py -Q 3
python3 ./test.py -f 2-query/sml.py -Q 3 python3 ./test.py -f 2-query/sml.py -Q 3