englefly commented on code in PR #16927:
URL: https://github.com/apache/doris/pull/16927#discussion_r1121344769
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/InApplyToJoin.java:
##########
@@ -45,22 +52,52 @@ public class InApplyToJoin extends OneRewriteRuleFactory {
@Override
public Rule build() {
return logicalApply().when(LogicalApply::isIn).then(apply -> {
- Expression predicate;
+ if (needBitmapUnoin(apply)) {
+ /*
+ select t1.k1 from bigtable t1 where t1.k1 in (select t2.k2
from bitmap_table t2);
+ =>
+ select t1.k1 from bigtable t1 where t1.k1 in (select
bitmap_union(k2) from bitmap_table t2);
+ =>
+ select t1.k1 from bigtable t1 left semi join (select
bitmap_union(k2) x from bitmap_table ) t2
+ on bitmap_contains(x, t1.k1);
+ */
+ List<Expression> groupExpressions = Lists.newArrayList();
+ Expression bitmapCol = apply.right().getOutput().get(0);
+ BitmapUnion union = new BitmapUnion(bitmapCol);
+ Alias alias = new Alias(union, union.toSql());
+ List<NamedExpression> outputExpressions =
Lists.newArrayList(alias);
+
+ LogicalAggregate agg = new LogicalAggregate(groupExpressions,
outputExpressions, apply.right());
+ Expression compareExpr = ((InSubquery)
apply.getSubqueryExpr()).getCompareExpr();
+ if (!compareExpr.getDataType().isBigIntType()) {
+ //this rule is after type coercion, we need to add cast by
hand
+ compareExpr = new Cast(compareExpr, BigIntType.INSTANCE);
+ }
+ Expression expr = new BitmapContains(agg.getOutput().get(0),
compareExpr);
+ if (((InSubquery) apply.getSubqueryExpr()).isNot()) {
+ expr = new Not(expr);
+ }
+ return new LogicalJoin<>(JoinType.LEFT_SEMI_JOIN,
Lists.newArrayList(),
+ Lists.newArrayList(expr),
+ JoinHint.NONE,
+ apply.left(), agg);
+ }
+
+ //in-predicate to equal
+ Expression predicate = new EqualTo(((InSubquery)
apply.getSubqueryExpr()).getCompareExpr(),
+ apply.right().getOutput().get(0));
if (apply.isCorrelated()) {
predicate = ExpressionUtils.and(
new EqualTo(((InSubquery)
apply.getSubqueryExpr()).getCompareExpr(),
apply.right().getOutput().get(0)),
apply.getCorrelationFilter().get());
- } else {
- predicate = new EqualTo(((InSubquery)
apply.getSubqueryExpr()).getCompareExpr(),
- apply.right().getOutput().get(0));
}
-
- //TODO nereids should support bitmap runtime filter in future
List<Expression> conjuncts =
ExpressionUtils.extractConjunction(predicate);
- if (conjuncts.stream().anyMatch(expression ->
expression.children().stream()
- .anyMatch(expr -> expr.getDataType() ==
BitmapType.INSTANCE))) {
- throw new AnalysisException("nereids don't support bitmap
runtime filter");
+ if (predicate instanceof BitmapContains) {
Review Comment:
BitmapContains is generated in function: convertInPredicateToJoinConjunct()
line 129
this "if" cannot be removed. we transform " k in (select A ...)" to a join.
there are two types of in
1. A is comparable type to k, for example, A is int, and k is int.
in-predicate becomes to "k=A"
2. A is bitmap, k is not. for example, k is int, A is bitmap. in-predicate
becomes "bitmap_contains(A, k)"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]