Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query)[TS-5443]. Fix invalid key in ORDER BY within subqueries #27998

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/libs/nodes/querynodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ typedef struct SExprNode {
char aliasName[TSDB_COL_NAME_LEN];
char userAlias[TSDB_COL_NAME_LEN];
SArray* pAssociation;
bool orderAlias;
bool asAlias;
bool asParam;
int32_t projIdx;
Expand Down
1 change: 0 additions & 1 deletion source/libs/nodes/src/nodesCloneFuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ static int32_t exprNodeCopy(const SExprNode* pSrc, SExprNode* pDst) {
COPY_OBJECT_FIELD(resType, sizeof(SDataType));
COPY_CHAR_ARRAY_FIELD(aliasName);
COPY_CHAR_ARRAY_FIELD(userAlias);
COPY_SCALAR_FIELD(orderAlias);
COPY_SCALAR_FIELD(projIdx);
return TSDB_CODE_SUCCESS;
}
Expand Down
3 changes: 1 addition & 2 deletions source/libs/nodes/src/nodesUtilFuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,8 +2380,7 @@ typedef struct SCollectFuncsCxt {

static EDealRes collectFuncs(SNode* pNode, void* pContext) {
SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext;
if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId) &&
!(((SExprNode*)pNode)->orderAlias)) {
if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId)) {
SFunctionNode* pFunc = (SFunctionNode*)pNode;
if (FUNCTION_TYPE_TBNAME == pFunc->funcType && pCxt->tableAlias) {
SValueNode* pVal = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 0);
Expand Down
3 changes: 0 additions & 3 deletions source/libs/parser/src/parTranslater.c
Original file line number Diff line number Diff line change
Expand Up @@ -6173,7 +6173,6 @@ static EDealRes replaceOrderByAliasImpl(SNode** pNode, void* pContext) {
pCxt->pTranslateCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
return DEAL_RES_ERROR;
}
((SExprNode*)pNew)->orderAlias = true;
nodesDestroyNode(*pNode);
*pNode = pNew;
return DEAL_RES_CONTINUE;
Expand All @@ -6196,7 +6195,6 @@ static EDealRes replaceOrderByAliasImpl(SNode** pNode, void* pContext) {
pCxt->pTranslateCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
return DEAL_RES_ERROR;
}
((SExprNode*)pNew)->orderAlias = true;
((SOrderByExprNode*)*pNode)->pExpr = pNew;
nodesDestroyNode(pExpr);
return DEAL_RES_CONTINUE;
Expand Down Expand Up @@ -6566,7 +6564,6 @@ static int32_t addOrderByPrimaryKeyToQueryImpl(STranslateContext* pCxt, SNode* p
nodesDestroyNode((SNode*)pOrderByExpr);
return TSDB_CODE_OUT_OF_MEMORY;
}
((SExprNode*)pOrderByExpr->pExpr)->orderAlias = true;
// NODES_DESTORY_LIST(*pOrderByList);
return nodesListMakeStrictAppend(pOrderByList, (SNode*)pOrderByExpr);
}
Expand Down
68 changes: 68 additions & 0 deletions tests/army/query/subquery/subqueryBugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,81 @@ def ts_30189(self):
rows = [row1, row2, row3, row4]
tdSql.checkDataMem(sql1, rows)

def ts_5443(self):
tdLog.info("create database ts_5443")
tdSql.execute("create database ts_5443")
tdSql.execute("use ts_5443")
sqls = [
"CREATE STABLE demo (ts TIMESTAMP, site NCHAR(8), expected BIGINT) TAGS (group_id BIGINT UNSIGNED)",
"CREATE TABLE demo_1 USING demo (group_id) TAGS (1)",
"INSERT INTO demo_1 VALUES ('2022-10-25 16:05:00.000', 'MN-01', 1)",
"CREATE TABLE demo_2 USING demo (group_id) TAGS (2)",
"INSERT INTO demo_2 VALUES ('2022-10-25 16:10:00.000', 'MN-02', 2)",
"CREATE TABLE demo_3 USING demo (group_id) TAGS (3)",
"INSERT INTO demo_3 VALUES ('2022-10-25 16:15:00.000', 'MN-03', 3)",
]
tdSql.executes(sqls)
# test result of order by in plain query
query = '''
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
ORDER BY 1 DESC, 2, 3
'''
tdSql.query(query)
tdSql.checkRows(3)
rows = [
['2022-10-25 16:20:00.000', 'MN-03', 3],
['2022-10-25 16:15:00.000', 'MN-02', 2],
['2022-10-25 16:10:00.000', 'MN-01', 1],
]
tdSql.checkDataMem(query, rows)
# test order by position alias within subquery
query = '''
SELECT COUNT(*) FROM (
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
ORDER BY 1 DESC, 2, 3
) WHERE check <> 0
'''
tdSql.query(query)
tdSql.checkRows(1)
tdSql.checkData(0, 0, 3)
# test order by target name within subquery
query = '''
SELECT COUNT(*) FROM (
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
ORDER BY _wend DESC, site, check
) WHERE check <> 0
'''
tdSql.query(query)
tdSql.checkRows(1)
tdSql.checkData(0, 0, 3)
# test having clause within subquery
query = '''
SELECT COUNT(*) FROM (
SELECT _wend, site, SUM(expected) AS check
FROM ts_5443.demo
PARTITION BY site INTERVAL(5m) SLIDING (5m)
HAVING _wend > '2022-10-25 16:13:00.000'
) WHERE check <> 0
'''
tdSql.query(query)
tdSql.checkRows(1)
tdSql.checkData(0, 0, 2)

# run
def run(self):
tdLog.debug(f"start to excute {__file__}")

# TS-30189
self.ts_30189()

# TS-5443
self.ts_5443()

tdLog.success(f"{__file__} successfully executed")

Expand Down