From e6caf6fd43bf4b4cb1be7f40f075a238413846ba Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 22 Mar 2024 11:02:21 +0800 Subject: [PATCH] fix: binary copy and case issue --- source/common/src/tdatablock.c | 2 +- source/libs/command/inc/commandInt.h | 6 +++- source/libs/command/src/explain.c | 47 +++++++++++++++++++------ source/libs/planner/src/planOptimizer.c | 12 +++++++ tests/system-test/2-query/stbJoin.py | 4 ++- 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index bcfab8c59c..95bf1ed648 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -541,7 +541,7 @@ int32_t colDataAssignNRows(SColumnInfoData* pDst, int32_t dstIdx, const SColumnI pDst->varmeta.allocLen = pDst->varmeta.length + allLen; } - memcpy(pDst->pData + pDst->varmeta.length, pSrc->pData, allLen); + memcpy(pDst->pData + pDst->varmeta.length, colDataGetVarData(pSrc, srcIdx), allLen); pDst->varmeta.length = pDst->varmeta.length + allLen; } } else { diff --git a/source/libs/command/inc/commandInt.h b/source/libs/command/inc/commandInt.h index d7bac9416f..e580238d22 100644 --- a/source/libs/command/inc/commandInt.h +++ b/source/libs/command/inc/commandInt.h @@ -53,7 +53,9 @@ extern "C" { #define EXPLAIN_FILTER_FORMAT "Filter: " #define EXPLAIN_MERGEBLOCKS_FORMAT "Merge ResBlocks: %s" #define EXPLAIN_FILL_VALUE_FORMAT "Fill Values: " -#define EXPLAIN_ON_CONDITIONS_FORMAT "Join Cond: " +#define EXPLAIN_PRIM_CONDITIONS_FORMAT "Join Prim Cond: " +#define EXPLAIN_ON_CONDITIONS_FORMAT "Join Full Cond: " +#define EXPLAIN_COL_ON_CONDITIONS_FORMAT "Join Col Cond: " #define EXPLAIN_TIMERANGE_FORMAT "Time Range: [%" PRId64 ", %" PRId64 "]" #define EXPLAIN_OUTPUT_FORMAT "Output: " #define EXPLAIN_JOIN_PARAM_FORMAT "Join Param: " @@ -72,6 +74,8 @@ extern "C" { #define EXPLAIN_DYN_QRY_CTRL_FORMAT "Dynamic Query Control for %s" #define EXPLAIN_COUNT_FORMAT "Count" #define EXPLAIN_COUNT_INFO_FORMAT "Window Count Info" +#define EXPLAIN_JOIN_EQ_LEFT_FORMAT "Left Equal Cond: " +#define EXPLAIN_JOIN_EQ_RIGHT_FORMAT "Right Equal Cond: " #define EXPLAIN_COUNT_NUM_FORMAT "Window Count=%" PRId64 #define EXPLAIN_COUNT_SLIDING_FORMAT "Window Sliding=%" PRId64 diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index 51c8bcd605..2537931f05 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -684,18 +684,45 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); } - if (pJoinNode->pPrimKeyCond || pJoinNode->pFullOnCond) { + if (NULL != pJoinNode->pPrimKeyCond) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_PRIM_CONDITIONS_FORMAT); + QRY_ERR_RET(nodesNodeToSQL(pJoinNode->pPrimKeyCond, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + } + + if (NULL != pJoinNode->pEqLeft && pJoinNode->pEqLeft->length > 0) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_JOIN_EQ_LEFT_FORMAT); + SNode* pCol = NULL; + FOREACH(pCol, pJoinNode->pEqLeft) { + EXPLAIN_ROW_APPEND("`%s`.`%s` ", ((SColumnNode*)pCol)->tableAlias, ((SColumnNode*)pCol)->colName); + } + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + } + + if (NULL != pJoinNode->pEqRight && pJoinNode->pEqRight->length > 0) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_JOIN_EQ_RIGHT_FORMAT); + SNode* pCol = NULL; + FOREACH(pCol, pJoinNode->pEqRight) { + EXPLAIN_ROW_APPEND("`%s`.`%s` ", ((SColumnNode*)pCol)->tableAlias, ((SColumnNode*)pCol)->colName); + } + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + } + + if (NULL != pJoinNode->pFullOnCond) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_ON_CONDITIONS_FORMAT); - if (pJoinNode->pPrimKeyCond) { - QRY_ERR_RET(nodesNodeToSQL(pJoinNode->pPrimKeyCond, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); - } - if (pJoinNode->pFullOnCond) { - if (pJoinNode->pPrimKeyCond) { - EXPLAIN_ROW_APPEND(" AND "); - } - QRY_ERR_RET( + QRY_ERR_RET( nodesNodeToSQL(pJoinNode->pFullOnCond, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); - } + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + } + + if (NULL != pJoinNode->pColOnCond) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_COL_ON_CONDITIONS_FORMAT); + QRY_ERR_RET( + nodesNodeToSQL(pJoinNode->pColOnCond, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); } diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index 8ead27a01c..8c8b4a8550 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -4415,6 +4415,18 @@ static int32_t stbJoinOptCreateTagHashJoinNode(SLogicNode* pOrig, SNodeList* pCh pScan->node.pParent = (SLogicNode*)pJoin; } + SNodeList* pCols = NULL; + nodesCollectColumnsFromNode(pJoin->pFullOnCond, NULL, COLLECT_COL_TYPE_ALL, &pCols); + + FOREACH(pNode, pCols) { + code = createColumnByRewriteExpr(pNode, &pJoin->node.pTargets); + if (code) { + break; + } + } + + nodesDestroyList(pCols); + if (TSDB_CODE_SUCCESS == code) { *ppLogic = (SLogicNode*)pJoin; } else { diff --git a/tests/system-test/2-query/stbJoin.py b/tests/system-test/2-query/stbJoin.py index 6eb95349fe..9ab9412910 100644 --- a/tests/system-test/2-query/stbJoin.py +++ b/tests/system-test/2-query/stbJoin.py @@ -124,11 +124,13 @@ class TDTestCase: tdSql.query(f"select a.* from sta a ,stb b where a.ts=b.ts and a.ts is not null;") tdSql.checkRows(48) + tdSql.query(f"select a.* from sta a join stb b on a.ts=b.ts where a.tg1=b.tg1 or a.tg2=b.tg2;"); #!!!!it works now + tdSql.checkRows(18) + # tdSql.checkData(0,1,10) tdSql.error(f"select a.* from sta a join stb b on a.tg1=b.tg1 where a.ts=b.ts or a.tg2=b.tg2;") tdSql.error(f"select b.* from sta a, stb b where a.tg1=b.tg1 or a.ts=b.ts;") - tdSql.error(f"select a.* from sta a join stb b on a.ts=b.ts where a.tg1=b.tg1 or a.tg2=b.tg2;"); #!!!!make it work def stop(self): tdSql.close()