asolimando commented on code in PR #2876:
URL: https://github.com/apache/calcite/pull/2876#discussion_r1020889694


##########
core/src/main/java/org/apache/calcite/rel/rules/materialize/MaterializedViewAggregateRule.java:
##########
@@ -910,6 +942,53 @@ protected SqlFunction getFloorSqlFunction(TimeUnitRange 
flag) {
     return Pair.of(resultTopViewProject, requireNonNull(resultViewNode, 
"resultViewNode"));
   }
 
+  /**
+   * If the view contains a FLOOR(col) and the query contains a range 
predicate on the col then
+   * rewrite the view to include a predicate based on the query predicate so 
that the view
+   * can be used.
+   * @return pair of view and added view predicate, or null if the rewrite 
can't be done
+   */
+  @Override public @Nullable  Pair<RelNode, RexNode> 
rewriteInputView(RelOptRuleCall call,
+      RelNode view, RelNode viewNode, RexBuilder rexBuilder, Pair<RexNode, 
RexNode> queryPreds,
+      RexNode viewPred) {
+    if (!queryPreds.right.isAlwaysTrue() && viewPred.isAlwaysTrue()) {
+      // If there is no view predicate add one based on the query predicate if 
the
+      // view is a rollup ( contains an aggregation that groups by FLOOR )
+      if (viewNode instanceof Aggregate) {
+        Aggregate aggregate = (Aggregate) viewNode;
+        RelNode input = aggregate.getInput();
+        if (input instanceof Project) {
+          Project project = (Project) input;
+          int viewColumnIndex = 0;
+          for (RexNode rexNode : project.getProjects()) {
+            if (rexNode instanceof RexCall) {
+              RexCall rexCall = (RexCall) rexNode;
+              if (rexCall.getKind() == SqlKind.FLOOR) {
+                RexInputRef rexInputRef = RexInputRef.of(viewColumnIndex, 
view.getRowType());
+                ImplicitViewPredicateShuttle viewPredicateShuttle =
+                    new ImplicitViewPredicateShuttle(rexBuilder, rexCall, 
rexInputRef, false);
+                ImplicitViewPredicateShuttle viewPredicateShuttle2 =
+                    new ImplicitViewPredicateShuttle(rexBuilder, rexCall, 
rexInputRef, true);
+                RexNode viewPredicate = 
queryPreds.right.accept(viewPredicateShuttle);
+                if (viewPredicateShuttle.isRangeMatched()) {
+                  RexNode viewFilter = 
queryPreds.right.accept(viewPredicateShuttle2);

Review Comment:
   We can move the init of the second shuttle inside the `if`, the only place 
where it's used.
   ```suggestion
                   RexNode viewPredicate = 
queryPreds.right.accept(viewPredicateShuttle);
                   if (viewPredicateShuttle.isRangeMatched()) {
                     ImplicitViewPredicateShuttle viewPredicateShuttle2 = 
                         new ImplicitViewPredicateShuttle(rexBuilder, rexCall, 
rexInputRef, true);
                     RexNode viewFilter = 
queryPreds.right.accept(viewPredicateShuttle2);
   ```
   
   Additionally, let's find a more descriptive name than 
`viewPredicateShuttle`, something like`viewPredicateShuttleFilterGenerator`?
   
   Possibly let's improve also `viewPredicateShuttle`, with something like 
`viewPredicateShuttleRangeMatcher`?



-- 
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]

Reply via email to