提交 9db7c027 编写于 作者: X Xiaoyu Wang

[TD-10986]<feature>: Add elapsed function.

上级 43c94bd3
...@@ -380,7 +380,7 @@ bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo) { ...@@ -380,7 +380,7 @@ bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo) {
} }
bool tscNeedTableSeqScan(SQueryInfo* pQueryInfo) { bool tscNeedTableSeqScan(SQueryInfo* pQueryInfo) {
return tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_TWA) || tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_ELAPSED); return pQueryInfo->stableQuery && (tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_TWA) || tscQueryContainsFunction(pQueryInfo, TSDB_FUNC_ELAPSED));
} }
bool tsIsArithmeticQueryOnAggResult(SQueryInfo* pQueryInfo) { bool tsIsArithmeticQueryOnAggResult(SQueryInfo* pQueryInfo) {
......
...@@ -5090,23 +5090,54 @@ static void elapsedFunction(SQLFunctionCtx *pCtx) { ...@@ -5090,23 +5090,54 @@ static void elapsedFunction(SQLFunctionCtx *pCtx) {
pInfo->min = pCtx->preAggVals.statis.min; pInfo->min = pCtx->preAggVals.statis.min;
pInfo->max = pCtx->preAggVals.statis.max; pInfo->max = pCtx->preAggVals.statis.max;
} else { } else {
pInfo->max = pCtx->preAggVals.statis.max; if (pCtx->order == TSDB_ORDER_ASC) {
pInfo->max = pCtx->preAggVals.statis.max;
} else {
pInfo->min = pCtx->preAggVals.statis.min;
}
} }
} else { } else {
// 0 == pCtx->size mean this is end interpolation.
if (0 == pCtx->size) { if (0 == pCtx->size) {
if (pCtx->order == TSDB_ORDER_DESC) {
if (pCtx->end.key != INT64_MIN) {
pInfo->min = pCtx->end.key;
}
} else {
if (pCtx->end.key != INT64_MIN) {
pInfo->max = pCtx->end.key + 1;
}
}
goto elapsedOver; goto elapsedOver;
} }
if (pCtx->start.key == INT64_MIN) { int64_t *ptsList = (int64_t *)GET_INPUT_DATA_LIST(pCtx);
pInfo->min = pCtx->ptsList[0]; // pCtx->start.key == INT64_MIN mean this is first window or there is actual start point of current window.
} else { // pCtx->end.key == INT64_MIN mean current window does not end in current data block or there is actual end point of current window.
pInfo->min = pCtx->start.key; if (pCtx->order == TSDB_ORDER_DESC) {
} if (pCtx->start.key == INT64_MIN) {
pInfo->max = (pInfo->max < ptsList[pCtx->size - 1]) ? ptsList[pCtx->size - 1] : pInfo->max;
} else {
pInfo->max = pCtx->start.key + 1;
}
if (pCtx->end.key != INT64_MIN) { if (pCtx->end.key != INT64_MIN) {
pInfo->max = pCtx->end.key + 1; pInfo->min = pCtx->end.key;
} else {
pInfo->min = ptsList[0];
}
} else { } else {
pInfo->max = pCtx->ptsList[pCtx->size - 1]; if (pCtx->start.key == INT64_MIN) {
pInfo->min = (pInfo->min > ptsList[0]) ? ptsList[0] : pInfo->min;
} else {
pInfo->min = pCtx->start.key;
}
if (pCtx->end.key != INT64_MIN) {
pInfo->max = pCtx->end.key + 1;
} else {
pInfo->max = ptsList[pCtx->size - 1];
}
} }
} }
......
...@@ -1531,7 +1531,6 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul ...@@ -1531,7 +1531,6 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul
STimeWindow win = getActiveTimeWindow(pResultRowInfo, ts, pQueryAttr); STimeWindow win = getActiveTimeWindow(pResultRowInfo, ts, pQueryAttr);
bool masterScan = IS_MASTER_SCAN(pRuntimeEnv); bool masterScan = IS_MASTER_SCAN(pRuntimeEnv);
SResultRow* pResult = NULL; SResultRow* pResult = NULL;
int32_t ret = setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.tid, &win, masterScan, &pResult, tableGroupId, pInfo->pCtx, int32_t ret = setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.tid, &win, masterScan, &pResult, tableGroupId, pInfo->pCtx,
numOfOutput, pInfo->rowCellInfoOffset); numOfOutput, pInfo->rowCellInfoOffset);
...@@ -1554,23 +1553,22 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul ...@@ -1554,23 +1553,22 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul
continue; continue;
} }
STimeWindow w = pRes->win; STimeWindow w = pRes->win;
ret = setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.tid, &w, masterScan, &pResult, ret = setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.tid, &w, masterScan, &pResult,
tableGroupId, pInfo->pCtx, numOfOutput, pInfo->rowCellInfoOffset); tableGroupId, pInfo->pCtx, numOfOutput, pInfo->rowCellInfoOffset);
if (ret != TSDB_CODE_SUCCESS) { if (ret != TSDB_CODE_SUCCESS) {
longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
} }
assert(!resultRowInterpolated(pResult, RESULT_ROW_END_INTERP));
doTimeWindowInterpolation(pOperatorInfo, pInfo, pSDataBlock->pDataBlock, *(TSKEY*)pRuntimeEnv->prevRow[0], -1, assert(!resultRowInterpolated(pResult, RESULT_ROW_END_INTERP));
tsCols[startPos], startPos, w.ekey, RESULT_ROW_END_INTERP);
setResultRowInterpo(pResult, RESULT_ROW_END_INTERP); doTimeWindowInterpolation(pOperatorInfo, pInfo, pSDataBlock->pDataBlock, *(TSKEY*)pRuntimeEnv->prevRow[0], -1,
setNotInterpoWindowKey(pInfo->pCtx, pQueryAttr->numOfOutput, RESULT_ROW_START_INTERP); tsCols[startPos], startPos, QUERY_IS_ASC_QUERY(pQueryAttr) ? w.ekey : w.skey, RESULT_ROW_END_INTERP);
doApplyFunctions(pRuntimeEnv, pInfo->pCtx, &w, startPos, 0, tsCols, pSDataBlock->info.rows, numOfOutput); setResultRowInterpo(pResult, RESULT_ROW_END_INTERP);
} setNotInterpoWindowKey(pInfo->pCtx, pQueryAttr->numOfOutput, RESULT_ROW_START_INTERP);
doApplyFunctions(pRuntimeEnv, pInfo->pCtx, &w, startPos, 0, tsCols, pSDataBlock->info.rows, numOfOutput);
}
// restore current time window // restore current time window
ret = setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.tid, &win, masterScan, &pResult, tableGroupId, pInfo->pCtx, ret = setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.tid, &win, masterScan, &pResult, tableGroupId, pInfo->pCtx,
......
...@@ -22,58 +22,401 @@ class TDTestCase: ...@@ -22,58 +22,401 @@ class TDTestCase:
tdLog.debug("start to execute %s" % __file__) tdLog.debug("start to execute %s" % __file__)
tdSql.init(conn.cursor()) tdSql.init(conn.cursor())
def genTime(self, no):
h = int(no / 60)
hs = "%d" %h
if h < 10:
hs = "0%d" %h
m = int(no % 60)
ms = "%d" %m
if m < 10:
ms = "0%d" %m
return hs, ms
def general(self): def general(self):
# normal table # normal table
tdSql.execute("create database ms_test") tdSql.execute("create database wxy_db minrows 10 maxrows 200")
tdSql.execute("use ms_test") tdSql.execute("use wxy_db")
tdSql.execute("create table t1 (ts timestamp, f float)") tdSql.execute("create table t1(ts timestamp, i int, b bigint, f float, d double, bin binary(10), s smallint, t tinyint, bl bool, n nchar(10), ts1 timestamp)")
tdSql.execute("insert into t1 values('2021-11-18 00:00:10', 1)" for i in range(1, 1001):
"('2021-11-18 00:00:30', 2)" hs, ms = self.genTime(i)
"('2021-11-18 00:00:40', 3)" if i < 500:
"('2021-11-18 00:01:00', 4)") ret = tdSql.execute("insert into t1(ts, i, b) values (\"2021-11-22 %s:%s:00\", %d, 1)" % (hs, ms, i))
else:
tdSql.execute("create database ns_test precision \"ns\"") ret = tdSql.execute("insert into t1(ts, i, b) values (\"2021-11-22 %s:%s:00\", %d, 0)" % (hs, ms, i))
tdSql.execute("use ns_test") tdSql.query("select count(*) from t1")
tdSql.checkEqual(int(tdSql.getData(0, 0)), 1000)
tdSql.execute("create database wxy_db_ns precision \"ns\"")
tdSql.execute("use wxy_db_ns")
tdSql.execute("create table t1 (ts timestamp, f float)") tdSql.execute("create table t1 (ts timestamp, f float)")
tdSql.execute("insert into t1 values('2021-11-18 00:00:00.000000100', 1)" tdSql.execute("insert into t1 values('2021-11-18 00:00:00.000000100', 1)"
"('2021-11-18 00:00:00' + 200b, 2)" "('2021-11-18 00:00:00.000000200', 2)"
"('2021-11-18 00:00:00' + 300b, 3)" "('2021-11-18 00:00:00.000000300', 3)"
"('2021-11-18 00:00:00' + 500b, 4)") "('2021-11-18 00:00:00.000000500', 4)")
# super table # super table
tdSql.execute("create stable st1(ts timestamp, f float) tags(id int)") tdSql.execute("use wxy_db")
tdSql.execute("create table subt1 using st1 tags(1)") tdSql.execute("create stable st1(ts timestamp, i int, b bigint, f float, d double, bin binary(10), s smallint, t tinyint, bl bool, n nchar(10), ts1 timestamp) tags(id int)")
tdSql.execute("create table subt2 using st1 tags(2)") tdSql.execute("create table st1s1 using st1 tags(1)")
tdSql.execute("insert into subt1 values('2021-11-18 00:00:00', 1)('2021-11-18 00:06:00', 2)('2021-11-18 00:12:00', 3)('2021-11-18 00:24:00', 4)") tdSql.execute("create table st1s2 using st1 tags(2)")
for i in range(1, 1001):
hs, ms = self.genTime(i)
if 0 == i % 2:
ret = tdSql.execute("insert into st1s1(ts, i) values (\"2021-11-22 %s:%s:00\", %d)" % (hs, ms, i))
else:
ret = tdSql.execute("insert into st1s2(ts, i) values (\"2021-11-22 %s:%s:00\", %d)" % (hs, ms, i))
tdSql.query("select count(*) from st1s1")
tdSql.checkEqual(int(tdSql.getData(0, 0)), 500)
tdSql.query("select count(*) from st1s2")
tdSql.checkEqual(int(tdSql.getData(0, 0)), 500)
def selectTest(self):
tdSql.execute("use wxy_db")
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.checkRows(1)
tdSql.checkCols(1)
tdSql.query("select elapsed(ts, 1m) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.checkEqual(int(tdSql.getData(0, 0)), 999)
tdSql.query("select elapsed(ts), elapsed(ts, 1m), elapsed(ts, 10m) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.checkEqual(int(tdSql.getData(0, 1)), 999)
tdSql.checkEqual(int(tdSql.getData(0, 2)), 99)
tdSql.query("select elapsed(ts), count(*), avg(f), twa(f), irate(f), sum(f), stddev(f), leastsquares(f, 1, 1), "
"min(f), max(f), first(f), last(f), percentile(i, 20), apercentile(i, 30), last_row(i), spread(i) "
"from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.checkRows(1)
tdSql.checkCols(16)
tdSql.checkEqual(int(tdSql.getData(0, 1)), 1000)
tdSql.query("select elapsed(ts) + 10, elapsed(ts) - 20, elapsed(ts) * 0, elapsed(ts) / 10, elapsed(ts) / elapsed(ts, 1m) from t1 "
"where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.checkRows(1)
tdSql.checkCols(5)
tdSql.checkEqual(int(tdSql.getData(0, 2)), 0)
tdSql.query("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
tdSql.checkRows(2)
tdSql.checkCols(2) # append tbname
tdSql.query("select elapsed(ts, 10m) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
tdSql.checkEqual(int(tdSql.getData(0, 0)), 99)
tdSql.checkEqual(int(tdSql.getData(1, 0)), 99)
tdSql.query("select elapsed(ts), elapsed(ts, 10m), elapsed(ts, 100m) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
tdSql.checkEqual(int(tdSql.getData(0, 1)), 99)
tdSql.checkEqual(int(tdSql.getData(0, 2)), 9)
# stddev(f),
tdSql.query("select elapsed(ts), count(*), avg(f), twa(f), irate(f), sum(f), min(f), max(f), first(f), last(f), apercentile(i, 30), last_row(i), spread(i) "
"from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
tdSql.checkRows(2)
tdSql.checkCols(14) # append tbname
tdSql.checkEqual(int(tdSql.getData(0, 1)), 500)
tdSql.query("select elapsed(ts) + 10, elapsed(ts) - 20, elapsed(ts) * 0, elapsed(ts) / 10, elapsed(ts) / elapsed(ts, 1m) "
"from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
tdSql.checkRows(2)
tdSql.checkCols(6) # append tbname
tdSql.checkEqual(int(tdSql.getData(0, 2)), 0)
tdSql.execute("use wxy_db_ns")
tdSql.query("select elapsed(ts, 1b), elapsed(ts, 1u) from t1")
tdSql.checkRows(1)
tdSql.checkCols(2)
self.selectIllegalTest()
# It has little to do with the elapsed function, so just simple test.
def whereTest(self):
tdSql.execute("use wxy_db")
tdSql.query("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' and id = 1 group by tbname")
tdSql.checkRows(1)
tdSql.checkCols(2) # append tbname
# It has little to do with the elapsed function, so just simple test.
def sessionTest(self):
tdSql.execute("use wxy_db")
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' session(ts, 10s)")
tdSql.checkRows(1000)
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' session(ts, 70s)")
tdSql.checkRows(1)
# It has little to do with the elapsed function, so just simple test.
def stateWindowTest(self):
tdSql.execute("use wxy_db")
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' state_window(i)")
tdSql.checkRows(1000)
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' state_window(b)")
tdSql.checkRows(2)
def intervalTest(self):
tdSql.execute("use wxy_db")
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(1m)")
tdSql.checkRows(1000)
# The first window has 9 records, and the last window has 1 record.
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(10m)")
tdSql.checkRows(101)
tdSql.checkEqual(int(tdSql.getData(0, 1)), 9 * 60 * 1000)
tdSql.checkEqual(int(tdSql.getData(100, 1)), 0)
# Skip windows without data.
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(35s)")
tdSql.checkRows(1000)
tdSql.query("select elapsed(ts), count(*), avg(f), twa(f), irate(f), sum(f), stddev(f), leastsquares(f, 1, 1), "
"min(f), max(f), first(f), last(f), percentile(i, 20), apercentile(i, 30), last_row(i), spread(i) "
"from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(20m)")
tdSql.checkRows(51) # ceil(1000/50) + 1(last point), window is half-open interval.
tdSql.checkCols(17) # front push timestamp
def normalTable(self): tdSql.query("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s) group by tbname")
tdSql.query("select elapsed(ts) from t1") tdSql.checkRows(1000)
tdSql.checkData(0, 0, 50000)
tdSql.query("select elapsed(ts, 20s) from t1") tdSql.query("select elapsed(ts) + 10, elapsed(ts) - 20, elapsed(ts) * 0, elapsed(ts) / 10, elapsed(ts) / elapsed(ts, 1m) "
tdSql.checkData(0, 0, 2.5) "from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(30m) group by tbname")
tdSql.checkRows(68) # ceil(1000/30)
tdSql.checkCols(7) # front push timestamp and append tbname
tdSql.query("select elapsed(ts) from t1 interval(1s)") # It has little to do with the elapsed function, so just simple test.
def fillTest(self):
tdSql.execute("use wxy_db")
def superTable(self): tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(30s) fill(value, 1000)")
tdSql.query("select elapsed(ts) from st1 group by tbname") tdSql.checkRows(2880) # The range of window conditions is 24 hours.
tdSql.checkEqual(int(tdSql.getData(0, 1)), 1000)
tdSql.query("select elapsed(ts) from st1 interval(1s) group by tbname") tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(30s) fill(prev)")
tdSql.checkRows(2880) # The range of window conditions is 24 hours.
tdSql.checkData(0, 1, None)
tdSql.query("select elapsed(ts, 1s), twa(f), elapsed(ts, 1s) * twa(f) as integral from st1 interval(1s) group by tbname") tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(30s) fill(null)")
tdSql.checkRows(2880) # The range of window conditions is 24 hours.
tdSql.checkData(0, 1, None)
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(30s) fill(linear)")
tdSql.checkRows(2880) # The range of window conditions is 24 hours.
tdSql.query("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(30s) fill(next)")
tdSql.checkRows(2880) # The range of window conditions is 24 hours.
def groupbyTest(self):
tdSql.execute("use wxy_db")
# Elapsed only support group by tbname. Supported tests have been done in selectTest().
tdSql.error("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by i")
tdSql.error("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by i")
def orderbyCheck(self, sql, elapsedCol):
resultAsc = tdSql.getResult(sql)
resultdesc = tdSql.getResult(sql + " order by ts desc")
resultRows = len(resultAsc)
for i in range(resultRows):
tdSql.checkEqual(resultAsc[i][elapsedCol], resultdesc[resultRows - i - 1][elapsedCol])
def splitStableResult(self, sql, elapsedCol, tbnameCol):
subtable = {}
result = tdSql.getResult(sql)
for i in range(len(result)):
if None == subtable.get(result[i][tbnameCol]):
subtable[result[i][tbnameCol]] = [result[i][elapsedCol]]
else:
subtable[result[i][tbnameCol]].append(result[i][elapsedCol])
return subtable
def doCheck(self, resultAsc, resultdesc):
resultRows = len(resultAsc)
for i in range(resultRows):
tdSql.checkEqual(resultAsc[i], resultdesc[resultRows - i - 1])
def orderbyForStableCheck(self, sql, elapsedCol, tbnameCol):
subtableAsc = self.splitStableResult(sql, elapsedCol, tbnameCol)
subtableDesc = self.splitStableResult(sql + " order by ts desc", elapsedCol, tbnameCol)
for kv in subtableAsc.items():
descValue = subtableDesc.get(kv[0])
if None == descValue:
tdLog.exit("%s failed: subtable %s not exists" % (sql))
else:
self.doCheck(kv[1], descValue)
# Orderby clause only changes the output order and has no effect on the calculation results.
def orderbyTest(self):
tdSql.execute("use wxy_db")
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'", 0)
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s)", 1)
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(1m)", 1)
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(10m)", 1)
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(150m)", 1)
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(222m)", 1)
self.orderbyCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(1000m)", 1)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname", 0, 1)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s) group by tbname", 1, 2)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(1m) group by tbname", 1, 2)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(10m) group by tbname", 1, 2)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(150m) group by tbname", 1, 2)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(222m) group by tbname", 1, 2)
self.orderbyForStableCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(1000m) group by tbname", 1, 2)
def slimitCheck(self, sql):
tdSql.checkEqual(tdSql.query(sql + " slimit 0"), 0)
tdSql.checkEqual(tdSql.query(sql + " slimit 1 soffset 0"), tdSql.query(sql + " slimit 0, 1"))
tdSql.checkEqual(tdSql.query(sql + " slimit 1, 1"), tdSql.query(sql) / 2)
tdSql.checkEqual(tdSql.query(sql + " slimit 10"), tdSql.query(sql))
# It has little to do with the elapsed function, so just simple test.
def slimitTest(self):
tdSql.execute("use wxy_db")
self.slimitCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
self.slimitCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s) group by tbname")
def limitCheck(self, sql, groupby = 0):
rows = tdSql.query(sql)
if rows > 0:
tdSql.checkEqual(tdSql.query(sql + " limit 0"), 0)
if 1 == groupby:
tdSql.checkEqual(tdSql.query(sql + " limit 1"), 2)
tdSql.checkEqual(tdSql.query(sql + " limit %d offset %d" % (rows / 2, rows / 3)), tdSql.query(sql + " limit %d, %d" % (rows / 3, rows / 2)))
tdSql.checkEqual(tdSql.query(sql + " limit %d" % (rows / 2)), rows)
else:
tdSql.checkEqual(tdSql.query(sql + " limit 1"), 1)
tdSql.checkEqual(tdSql.query(sql + " limit %d offset %d" % (rows / 2, rows / 3)), tdSql.query(sql + " limit %d, %d" % (rows / 3, rows / 2)))
tdSql.checkEqual(tdSql.query(sql + " limit %d" % (rows + 1)), rows)
# It has little to do with the elapsed function, so just simple test.
def limitTest(self):
tdSql.execute("use wxy_db")
self.limitCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
self.limitCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s)")
self.limitCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname", 1)
self.limitCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s) group by tbname", 1)
def fromCheck(self, sqlTemplate, table):
tdSql.checkEqual(tdSql.getResult(sqlTemplate % table), tdSql.getResult(sqlTemplate % ("(select * from %s)" % table)))
tdSql.query(sqlTemplate % ("(select last(ts) from %s interval(10s))" % table))
# It has little to do with the elapsed function, so just simple test.
def fromTest(self):
tdSql.execute("use wxy_db")
self.fromCheck("select elapsed(ts) from %s where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'", "t1")
self.fromCheck("select elapsed(ts) from %s where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s)", "t1")
self.fromCheck("select elapsed(ts) from %s where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'", "st1s1")
self.fromCheck("select elapsed(ts) from %s where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s)", "st1s1")
# Tags not allowed for table query, so there is no need to test super table.
def joinCheck(self, sqlTemplate, rtable):
tdSql.checkEqual(tdSql.getResult(sqlTemplate % ("t1, %s t2" % rtable, "t1.ts = t2.ts and ")), tdSql.getResult(sqlTemplate % (rtable, "")))
# It has little to do with the elapsed function, so just simple test.
def joinTest(self):
tdSql.execute("use wxy_db")
# st1s1 is a subset of t1.
self.joinCheck("select elapsed(ts) from %s where %s ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'", "st1s1")
self.joinCheck("select elapsed(ts) from %s where %s ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(150m)", "st1s1")
# join query does not support group by, so there is no need to test super table.
def unionAllCheck(self, sql1, sql2):
rows1 = tdSql.query(sql1)
rows2 = tdSql.query(sql2)
tdSql.checkEqual(tdSql.query(sql1 + " union all " + sql2), rows1 + rows2)
# It has little to do with the elapsed function, so just simple test.
def unionAllTest(self):
tdSql.execute("use wxy_db")
self.unionAllCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'",
"select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-22 01:00:00'")
self.unionAllCheck("select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(40s)",
"select elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(150m)")
self.unionAllCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname",
"select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-22 02:00:00' group by tbname")
self.unionAllCheck("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(1m) group by tbname",
"select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' interval(222m) group by tbname")
def continuousQueryTest(self):
tdSql.execute("use wxy_db")
#tdSql.execute("create table elapsed_t as select elapsed(ts) from t1 interval(1m) sliding(30s)")
#tdSql.execute("create table elapsed_st as select elapsed(ts) from st1 interval(1m) sliding(30s) group by tbname")
#tdSql.execute("create table spread_st as select spread(ts) from st1 interval(1m) sliding(30s) group by tbname")
def selectIllegalTest(self):
tdSql.execute("use wxy_db")
tdSql.error("select elapsed(1) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed('2021-11-18 00:00:10') from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(now) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(b) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(f) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(d) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(bin) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(s) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(t) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(bl) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(n) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts1) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(*) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts, '1s') from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts, i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
#tdSql.error("select elapsed(ts, now) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts, ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts + 1) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts, 1b) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts, 1u) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(max(ts)) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select distinct elapsed(ts) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select distinct elapsed(ts) from st1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00' group by tbname")
tdSql.error("select elapsed(ts), i from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), ts from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), _c0 from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), top(i, 1) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), bottom(i, 1) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), inerp(i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), diff(i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), derivative(i, 1s, 0) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), ceil(i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), floor(i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
tdSql.error("select elapsed(ts), round(i) from t1 where ts > '2021-11-22 00:00:00' and ts < '2021-11-23 00:00:00'")
def run(self): def run(self):
tdSql.prepare() tdSql.prepare()
self.general() self.general()
self.normalTable() self.selectTest()
#self.superTable() self.whereTest()
self.sessionTest()
self.stateWindowTest()
self.intervalTest()
self.fillTest()
self.groupbyTest()
self.orderbyTest()
self.slimitTest()
self.limitTest()
self.fromTest()
self.joinTest()
self.unionAllTest()
self.continuousQueryTest()
def stop(self): def stop(self):
tdSql.close() tdSql.close()
tdLog.success("%s successfully executed" % __file__) tdLog.success("%s successfully executed" % __file__)
tdCases.addWindows(__file__, TDTestCase()) tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase()) tdCases.addLinux(__file__, TDTestCase())
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册