gianm commented on code in PR #15626: URL: https://github.com/apache/druid/pull/15626#discussion_r1450821364
########## sql/src/main/java/org/apache/druid/sql/calcite/rule/AggregatePullUpLookupRule.java: ########## @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.rule; + +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.ImmutableBitSet; +import org.apache.druid.query.lookup.LookupExtractor; +import org.apache.druid.sql.calcite.expression.builtin.QueryLookupOperatorConversion; +import org.apache.druid.sql.calcite.planner.PlannerContext; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * Rule that pulls {@link QueryLookupOperatorConversion#SQL_FUNCTION} up through an {@link Aggregate}. + */ +public class AggregatePullUpLookupRule extends RelOptRule +{ + private final PlannerContext plannerContext; + + public AggregatePullUpLookupRule(final PlannerContext plannerContext) + { + super(operand(Aggregate.class, operand(Project.class, any()))); + this.plannerContext = plannerContext; + } + + @Override + public void onMatch(RelOptRuleCall call) + { + final Aggregate aggregate = call.rel(0); + final Project project = call.rel(1); + final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder(); + final Set<Integer> aggCallInputs = RelOptUtil.getAllFields2(ImmutableBitSet.of(), aggregate.getAggCallList()); + + RexNode[] topProjects = null; // Projects pulled up on top of the Aggregate + RexNode[] bottomProjects = null; // Projects that stay on the bottom of the Aggregate + boolean matched = false; // Whether we found a LOOKUP call to pull up + + int dimensionIndex = 0; + for (Iterator<Integer> iterator = aggregate.getGroupSet().iterator(); iterator.hasNext(); dimensionIndex++) { + int projectIndex = iterator.next(); + final RexNode projectExpr = project.getProjects().get(projectIndex); + + if (ReverseLookupRule.isLookupCall(projectExpr) && !aggCallInputs.contains(projectIndex)) { + final RexCall lookupCall = (RexCall) projectExpr; + final String lookupName = RexLiteral.stringValue(lookupCall.getOperands().get(1)); + final LookupExtractor lookup = plannerContext.getLookup(lookupName); + + if (lookup != null && lookup.isOneToOne()) { Review Comment: > I've wrote some simple test which shows some odd behaviour - however it does retain it even without this rule enabled - which is I guess is another issue This could be because a similar rewrite has been applied at the native query layer for some time. This patch disables the native rewrite when the SQL-level one is enabled; but if you disable the rule using `sqlPullUpLookups: false` then the native one is reactivated. The code for this is in `QueryLookupOperatorConversion` (it sets `optimize: true` on the native `extractionFn` if the SQL-level pull-up rule is disabled). -- 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]
