walterddr commented on code in PR #11105:
URL: https://github.com/apache/pinot/pull/11105#discussion_r1266992920


##########
pinot-query-planner/src/main/java/org/apache/calcite/rel/hint/PinotHintStrategyTable.java:
##########
@@ -90,4 +111,33 @@ public static String getHintOption(List<RelHint> hintList, 
String hintName, Stri
     }
     return null;
   }
+
+  /**
+   * Replace the option value by option key in the {@link RelHint#kvOptions}. 
the option key is looked up from the
+   * specified hint name for a hint-able {@link 
org.apache.calcite.rel.RelNode}.
+   *
+   * @param oldHintList hint list from the {@link 
org.apache.calcite.rel.RelNode}.
+   * @param hintName the name of the {@link RelHint}.
+   * @param optionKey the option key to look for in the {@link 
RelHint#kvOptions}.
+   * @param optionValue the value to be set into {@link RelHint#kvOptions}.
+   */
+  public static List<RelHint> replaceHintOptions(List<RelHint> oldHintList, 
String hintName, String optionKey,
+      String optionValue) {

Review Comment:
   this is only needed b/c we are modifying the hint to allow RexLiterals to be 
propagated without creating an extra project. 
   
   since RelHint is not designed to be mutable. it is not an ideal solution, we 
should mark this function as "do not use it unless you absolutely know what you 
are doing"



##########
pinot-query-planner/src/main/java/org/apache/calcite/rel/rules/PinotAggregateLiteralAttachmentRule.java:
##########
@@ -0,0 +1,139 @@
+/**
+ * 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.calcite.rel.rules;
+
+import com.google.common.collect.ImmutableList;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.calcite.avatica.util.ByteString;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.hep.HepRelVertex;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.hint.PinotHintOptions;
+import org.apache.calcite.rel.hint.PinotHintStrategyTable;
+import org.apache.calcite.rel.hint.RelHint;
+import org.apache.calcite.rel.logical.LogicalAggregate;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.runtime.Geometries;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.DateString;
+import org.apache.calcite.util.NlsString;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Sarg;
+import org.apache.calcite.util.TimeString;
+import org.apache.calcite.util.TimestampString;
+import org.apache.calcite.util.Util;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.query.planner.logical.RexExpression;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+
+/**
+ * Special rule to attach Literal to Aggregate call.
+ */
+public class PinotAggregateLiteralAttachmentRule extends RelOptRule {
+  public static final PinotAggregateLiteralAttachmentRule INSTANCE =
+      new 
PinotAggregateLiteralAttachmentRule(PinotRuleUtils.PINOT_REL_FACTORY);
+
+  public PinotAggregateLiteralAttachmentRule(RelBuilderFactory factory) {
+    super(operand(LogicalAggregate.class, some(operand(LogicalProject.class, 
any()))), factory, null);
+  }
+
+  @Override
+  public boolean matches(RelOptRuleCall call) {
+    if (call.rels.length < 1) {
+      return false;
+    }
+    if (call.rel(0) instanceof Aggregate) {
+      Aggregate agg = call.rel(0);
+      ImmutableList<RelHint> hints = agg.getHints();
+      return !PinotHintStrategyTable.containsHintOption(hints,
+          PinotHintOptions.INTERNAL_AGG_OPTIONS, 
PinotHintOptions.InternalAggregateOptions.AGG_CALL_SIGNATURE);
+    }
+    return false;
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    Aggregate aggregate = call.rel(0);
+    Map<Pair<Integer, Integer>, RexLiteral> rexLiterals = 
extractRexLiterals(call);
+    List<RelHint> newHints = 
PinotHintStrategyTable.replaceHintOptions(aggregate.getHints(),
+        PinotHintOptions.INTERNAL_AGG_OPTIONS, 
PinotHintOptions.InternalAggregateOptions.AGG_CALL_SIGNATURE,
+        createRexLiteralHintsString(rexLiterals));
+    call.transformTo(new LogicalAggregate(aggregate.getCluster(), 
aggregate.getTraitSet(), newHints,
+        aggregate.getInput(), aggregate.getGroupSet(), 
aggregate.getGroupSets(), aggregate.getAggCallList()));
+  }
+
+  private static Map<Pair<Integer, Integer>, RexLiteral> 
extractRexLiterals(RelOptRuleCall call) {
+    Aggregate aggregate = call.rel(0);
+    Project project = call.rel(1);
+    List<RexNode> rexNodes = project.getProjects();
+    List<AggregateCall> aggCallList = aggregate.getAggCallList();
+    final Map<Pair<Integer, Integer>, RexLiteral> rexLiteralMap = new 
HashMap<>();
+    for (int aggIdx = 0; aggIdx < aggCallList.size(); aggIdx++) {
+      AggregateCall aggCall = aggCallList.get(aggIdx);
+      for (int argIdx = 0; argIdx < aggCall.getArgList().size(); argIdx++) {
+        RexNode field = rexNodes.get(aggCall.getArgList().get(argIdx));
+        if (field instanceof RexLiteral) {
+          rexLiteralMap.put(new Pair<>(aggIdx, argIdx), (RexLiteral) field);
+        }
+      }
+    }
+    return rexLiteralMap;
+  }
+
+  private static String createRexLiteralHintsString(Map<Pair<Integer, 
Integer>, RexLiteral> rexLiterals) {

Review Comment:
   factor these stringify-object conversion to a util



##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/AggregateOperator.java:
##########
@@ -234,18 +298,34 @@ private FunctionContext 
convertRexExpressionsToFunctionContext(RexExpression.Fun
     return functionContext;
   }
 
+  private void rewriteAggArgumentWithLiterals(List<ExpressionContext> 
aggArguments, int aggIdx,
+      RexExpression.FunctionCall aggFunctionCall) {
+    String functionName = aggFunctionCall.getFunctionName();
+    // This should be provided by AggregationFunctionType indicating which 
argIdx requires literal.
+    if (functionName.equals("FIRSTWITHTIME") || 
functionName.equals("LASTWITHTIME")) {
+      aggArguments.add(ExpressionContext.forIdentifier("__PLACEHOLDER__"));
+      
aggArguments.add(ExpressionContext.forLiteralContext(_aggCallLiteralArgsMap.get(aggIdx).get(2)));
+    }
+    if (functionName.equals("PERCENTILE")) {
+      
aggArguments.add(ExpressionContext.forLiteralContext(_aggCallLiteralArgsMap.get(aggIdx).get(1)));
+    }
+  }

Review Comment:
   this function needs to be replaced by a util in `AggregateFunctionType` that 
handles each registered function separately. 



##########
pinot-query-planner/src/main/java/org/apache/calcite/rel/rules/PinotAggregateExchangeNodeInsertRule.java:
##########
@@ -111,17 +112,18 @@ public void onMatch(RelOptRuleCall call) {
     ImmutableList<RelHint> oldHints = oldAggRel.getHints();
 
     Aggregate newAgg;

Review Comment:
   note to self:
   after this main `onMatch` conversion
   1. we should check if all the Aggregate functions have desired literals put 
in place
   2. we should make `AggregationFunctionType`s register optional literal 
checkers on specific operands



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to