Skip to content

Commit 83095ec

Browse files
committed
refine code and fix tests
1 parent edb1a92 commit 83095ec

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

src/query/service/src/sql/planner/binder/join.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use common_exception::Result;
2727
use crate::sessions::TableContext;
2828
use crate::sql::binder::scalar_common::split_conjunctions;
2929
use crate::sql::binder::scalar_common::split_equivalent_predicate;
30-
use crate::sql::binder::scalar_common::wrap_cast_if_needed;
30+
use crate::sql::binder::wrap_cast;
3131
use crate::sql::binder::Visibility;
3232
use crate::sql::normalize_identifier;
3333
use crate::sql::optimizer::ColumnSet;
@@ -450,8 +450,10 @@ impl<'a> JoinConditionResolver<'a> {
450450
let right_type = right.data_type();
451451
if left_type.ne(&right_type) {
452452
let least_super_type = compare_coercion(&left_type, &right_type)?;
453-
left = wrap_cast_if_needed(left, &least_super_type);
454-
right = wrap_cast_if_needed(right, &least_super_type);
453+
// Wrap cast for both left and right, `cast` can change the physical type of the data block
454+
// Related issue: https://github.com/datafuselabs/databend/issues/7650
455+
left = wrap_cast(left, &least_super_type);
456+
right = wrap_cast(right, &least_super_type);
455457
}
456458

457459
if left_used_columns.is_subset(&left_columns)

src/query/service/src/sql/planner/binder/scalar_common.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ pub fn split_equivalent_predicate(scalar: &Scalar) -> Option<(Scalar, Scalar)> {
8686
}
8787

8888
pub fn wrap_cast_if_needed(scalar: Scalar, target_type: &DataTypeImpl) -> Scalar {
89+
if scalar.data_type() != *target_type {
90+
let cast = CastExpr {
91+
from_type: Box::new(scalar.data_type()),
92+
argument: Box::new(scalar),
93+
target_type: Box::new(target_type.clone()),
94+
};
95+
cast.into()
96+
} else {
97+
scalar
98+
}
99+
}
100+
101+
pub fn wrap_cast(scalar: Scalar, target_type: &DataTypeImpl) -> Scalar {
89102
CastExpr {
90103
from_type: Box::new(scalar.data_type()),
91104
argument: Box::new(scalar),

src/query/service/src/sql/planner/optimizer/heuristic/decorrelate.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use common_datavalues::NullableType;
2121
use common_exception::ErrorCode;
2222
use common_exception::Result;
2323

24-
use crate::sql::binder::wrap_cast_if_needed;
24+
use crate::sql::binder::wrap_cast;
2525
use crate::sql::binder::JoinPredicate;
2626
use crate::sql::binder::Visibility;
2727
use crate::sql::optimizer::heuristic::subquery_rewriter::FlattenInfo;
@@ -172,9 +172,14 @@ impl SubqueryRewriter {
172172
}
173173

174174
JoinPredicate::Both { left, right } => {
175+
if left.data_type().eq(&right.data_type()) {
176+
left_conditions.push(left.clone());
177+
right_conditions.push(right.clone());
178+
continue;
179+
}
175180
let join_type = compare_coercion(&left.data_type(), &right.data_type())?;
176-
let left = wrap_cast_if_needed(left.clone(), &join_type);
177-
let right = wrap_cast_if_needed(right.clone(), &join_type);
181+
let left = wrap_cast(left.clone(), &join_type);
182+
let right = wrap_cast(right.clone(), &join_type);
178183
left_conditions.push(left);
179184
right_conditions.push(right);
180185
}

src/query/service/src/sql/planner/optimizer/rule/rewrite/rule_push_down_filter_join.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use common_datavalues::type_coercion::compare_coercion;
1616
use common_exception::Result;
1717

18-
use crate::sql::binder::wrap_cast_if_needed;
18+
use crate::sql::binder::wrap_cast;
1919
use crate::sql::binder::JoinPredicate;
2020
use crate::sql::optimizer::rule::Rule;
2121
use crate::sql::optimizer::rule::TransformState;
@@ -117,10 +117,15 @@ impl Rule for RulePushDownFilterJoin {
117117
if join.join_type == JoinType::Cross {
118118
join.join_type = JoinType::Inner;
119119
}
120-
let left = wrap_cast_if_needed(left.clone(), &join_key_type);
121-
let right = wrap_cast_if_needed(right.clone(), &join_key_type);
122-
join.left_conditions.push(left);
123-
join.right_conditions.push(right);
120+
if left.data_type().ne(&right.data_type()) {
121+
let left = wrap_cast(left.clone(), &join_key_type);
122+
let right = wrap_cast(right.clone(), &join_key_type);
123+
join.left_conditions.push(left);
124+
join.right_conditions.push(right);
125+
} else {
126+
join.left_conditions.push(left.clone());
127+
join.right_conditions.push(right.clone());
128+
}
124129
need_push = true;
125130
} else {
126131
original_predicates.push(predicate);

tests/logictest/suites/mode/cluster/exchange.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Exchange
129129
└── HashJoin
130130
├── join type: INNER
131131
├── build keys: [CAST(numbers.number (#4) AS BIGINT UNSIGNED NULL)]
132-
├── probe keys: [number (#1)]
132+
├── probe keys: [CAST(number (#1) AS BIGINT UNSIGNED NULL)]
133133
├── filters: []
134134
├── Exchange(Build)
135135
│ ├── exchange type: Hash(CAST(numbers.number (#4) AS BIGINT UNSIGNED NULL))

tests/logictest/suites/mode/standalone/explain/limit.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Limit
148148
├── sort keys: [c1 ASC NULLS LAST]
149149
└── HashJoin
150150
├── join type: LEFT OUTER
151-
├── build keys: [c (#5)]
151+
├── build keys: [CAST(c (#5) AS BIGINT UNSIGNED NULL)]
152152
├── probe keys: [CAST(c1 (#1) AS BIGINT UNSIGNED NULL)]
153153
├── filters: []
154154
├── Project(Build)

tests/logictest/suites/mode/standalone/explain/select_limit_offset.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Limit
141141
├── offset: 1
142142
└── HashJoin
143143
├── join type: LEFT OUTER
144-
├── build keys: [t1.a (#1)]
144+
├── build keys: [CAST(t1.a (#1) AS INT NULL)]
145145
├── probe keys: [CAST(t.a (#0) AS INT NULL)]
146146
├── filters: []
147147
├── Limit(Build)

0 commit comments

Comments
 (0)