Skip to content

Commit

Permalink
[BugFix] Fix wrong result when query cache work with select node
Browse files Browse the repository at this point in the history
The query cache does not normalize the select node during the normalize stage,
which can lead to incorrect results if different predicates use the same digest.

Signed-off-by: stdpain <[email protected]>
  • Loading branch information
stdpain committed Jan 21, 2025
1 parent 8216577 commit 9fd94b5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/planner/SelectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@
import com.starrocks.analysis.Analyzer;
import com.starrocks.analysis.Expr;
import com.starrocks.analysis.SlotId;
import com.starrocks.common.Pair;
import com.starrocks.common.StarRocksException;
import com.starrocks.thrift.TExplainLevel;
import com.starrocks.thrift.TNormalPlanNode;
import com.starrocks.thrift.TNormalSelectNode;
import com.starrocks.thrift.TPlanNode;
import com.starrocks.thrift.TPlanNodeType;
import com.starrocks.thrift.TSelectNode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -84,6 +88,16 @@ public void init(Analyzer analyzer) throws StarRocksException {
public void computeStats(Analyzer analyzer) {
}

@Override
protected void toNormalForm(TNormalPlanNode planNode, FragmentNormalizer normalizer) {
TNormalSelectNode selectNode = new TNormalSelectNode();
Pair<List<Integer>, List<ByteBuffer>> slotIdsAndExprs = normalizer.normalizeSlotIdsAndExprs(commonSlotMap);
selectNode.setCse_slot_ids(slotIdsAndExprs.first);
selectNode.setCse_exprs(slotIdsAndExprs.second);
planNode.setNode_type(TPlanNodeType.SELECT_NODE);
normalizeConjuncts(normalizer, planNode, conjuncts);
}

@Override
protected String getNodeExplainString(String prefix, TExplainLevel detailLevel) {
StringBuilder output = new StringBuilder();
Expand Down
6 changes: 6 additions & 0 deletions gensrc/thrift/Normalization.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ struct TNormalSetOperationNode {
4: optional i64 first_materialized_child_idx
}

struct TNormalSelectNode {
1: optional list<Types.TSlotId> cse_slot_ids;
2: optional list<binary> cse_exprs;
}

struct TNormalPlanNode {
1: optional Types.TPlanNodeId node_id
2: optional PlanNodes.TPlanNodeType node_type
Expand All @@ -166,4 +171,5 @@ struct TNormalPlanNode {
19: optional TNormalSortNode sort_node
20: optional TNormalSortAggregationNode sort_aggregation_node
22: optional TNormalSetOperationNode set_operation_node
23: optional TNormalSelectNode select_node
}
32 changes: 32 additions & 0 deletions test/sql/test_query_cache/R/test_query_cache_select_node
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- name: test_query_cache_select_node
CREATE TABLE `tarray` (
`id` int(4) NULL COMMENT "",
`val` array<int> NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES (
"compression" = "LZ4",
"fast_schema_evolution" = "false",
"replicated_storage" = "true",
"replication_num" = "1"
);
-- result:
-- !result
insert into tarray SELECT generate_series, [generate_series % 4, generate_series%3] FROM TABLE(generate_series(1, 4096));
-- result:
-- !result
set enable_query_cache = true;
-- result:
-- !result
set pipeline_dop=1;
-- result:
-- !result
SELECT rule_id,COUNT(*) AS cnt FROM ( SELECT id,unnest AS rule_id FROM tarray, unnest(val) ) er WHERE rule_id IN (28,128,127) GROUP BY rule_id;
-- result:
-- !result
SELECT rule_id,COUNT(*) AS cnt FROM ( SELECT id,unnest AS rule_id FROM tarray, unnest(val) ) er WHERE rule_id IN (1) GROUP BY rule_id;
-- result:
1 2390
-- !result
22 changes: 22 additions & 0 deletions test/sql/test_query_cache/T/test_query_cache_select_node
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- name: test_query_cache_select_node
CREATE TABLE `tarray` (
`id` int(4) NULL COMMENT "",
`val` array<int> NULL COMMENT ""
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 2
PROPERTIES (
"compression" = "LZ4",
"fast_schema_evolution" = "false",
"replicated_storage" = "true",
"replication_num" = "1"
);

insert into tarray SELECT generate_series, [generate_series % 4, generate_series%3] FROM TABLE(generate_series(1, 4096));

set enable_query_cache = true;
set pipeline_dop=1;

SELECT rule_id,COUNT(*) AS cnt FROM ( SELECT id,unnest AS rule_id FROM tarray, unnest(val) ) er WHERE rule_id IN (28,128,127) GROUP BY rule_id;
SELECT rule_id,COUNT(*) AS cnt FROM ( SELECT id,unnest AS rule_id FROM tarray, unnest(val) ) er WHERE rule_id IN (1) GROUP BY rule_id;

0 comments on commit 9fd94b5

Please sign in to comment.