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


##########
core/src/test/java/org/apache/calcite/test/MaterializedViewRelOptRulesTest.java:
##########
@@ -338,6 +341,104 @@ protected final MaterializedViewFixture sql(String 
materialize,
         .ok();
   }
 
+  @Test void testAggregateMaterializationAggregateFuncsRange1() {
+    // if range predicate aligns on the rollup column boundary verify that 
only view is used
+    sql("select \"eventid\", floor(\"ts\" to minute), count(*) "
+            + "from \"events\""
+            + " group by \"eventid\", floor(\"ts\" to minute)",
+        "select \"eventid\", floor(\"ts\" to minute), count(*) \n"
+            + "from \"events\""
+            + " where \"ts\" >= TIMESTAMP'2018-01-01 00:02:00' "
+            + "AND \"ts\" < TIMESTAMP'2018-01-01 00:05:00'"
+            + "group by \"eventid\", floor(\"ts\" to minute)")
+        .checkingThatResultContains("EnumerableCalc(expr#0..2=[{inputs}], "
+            + "expr#3=[Sarg[[2018-01-01 00:02:00..2018-01-01 00:05:00)]], 
expr#4=[SEARCH($t1, $t3)"
+            + "], proj#0..2=[{exprs}], $condition=[$t4])\n"
+            + "  EnumerableTableScan(table=[[hr, MV0]])")
+        .withMetadataProvider(
+            ChainedRelMetadataProvider.of(
+                ImmutableList.of(TestMetadataHandlers.TestRelMdRowCount.SOURCE,
+                    TestMetadataHandlers.TestRelMdSelectivity.SOURCE,
+                    DefaultRelMetadataProvider.INSTANCE))
+        )
+        .ok();
+  }
+
+  @Test void testAggregateMaterializationAggregateFuncsRange2() {
+    // if range predicate doesn't align on the rollup column boundary verify 
that a union on both
+    // the table and view is used
+    sql("select \"eventid\", floor(\"ts\" to minute), count(*) "
+            + "from \"events\""
+            + " group by \"eventid\", floor(\"ts\" to minute)",
+        "select \"eventid\", floor(\"ts\" to minute), count(*) \n"
+            + "from \"events\""
+            + " where \"ts\" > TIMESTAMP'2018-01-01 00:02:30.123' "
+            + "AND \"ts\" <= TIMESTAMP'2018-01-01 00:05:30.456'"
+            + "group by \"eventid\", floor(\"ts\" to minute)")
+        .checkingThatResultContains("EnumerableAggregate(group=[{0, 1}], 
EXPR$2=[$SUM0($2)])\n"
+            + "  EnumerableUnion(all=[true])\n"
+            + "    EnumerableAggregate(group=[{0, 1}], EXPR$2=[COUNT()])\n"
+            + "      EnumerableCalc(expr#0..1=[{inputs}], 
expr#2=[FLAG(MINUTE)], expr#3=[FLOOR($t1,"
+            + " $t2)], expr#4=[Sarg[(2018-01-01 
00:02:30.123:TIMESTAMP(3)..2018-01-01 "
+            + "00:03:00:TIMESTAMP(3)), [2018-01-01 
00:05:00:TIMESTAMP(3)..2018-01-01 00:05:30"
+            + ".456:TIMESTAMP(3)]]:TIMESTAMP(3)], expr#5=[SEARCH($t1, $t4)], 
eventid=[$t0], "
+            + "$f1=[$t3], $condition=[$t5])\n"
+            + "        EnumerableTableScan(table=[[hr, events]])\n"
+            + "    EnumerableCalc(expr#0..2=[{inputs}], 
expr#3=[Sarg[[2018-01-01 00:03:00."
+            + ".2018-01-01 00:05:00)]], expr#4=[SEARCH($t1, $t3)], 
proj#0..2=[{exprs}], "
+            + "$condition=[$t4])\n"
+            + "      EnumerableTableScan(table=[[hr, MV0]])")
+        .withMetadataProvider(
+            ChainedRelMetadataProvider.of(
+                ImmutableList.of(TestMetadataHandlers.TestRelMdRowCount.SOURCE,
+                    TestMetadataHandlers.TestRelMdSelectivity.SOURCE,
+                    DefaultRelMetadataProvider.INSTANCE))
+        )
+        .ok();
+  }
+
+  @Test void testAggregateMaterializationAggregateFuncsRange31() {
+    // test using multiple views
+    String mv0 = "select \"eventid\", floor(\"ts\" to hour), "
+        + "count(*)\n"
+        + "from \"events\""
+        + " group by \"eventid\", floor(\"ts\" to hour)";
+    String mv1 = "select \"eventid\", floor(\"ts\" to minute), "
+        + "count(*)\n"
+        + "from \"events\""
+        + " group by \"eventid\", floor(\"ts\" to minute)";
+
+    String query = "select floor(\"ts\" to hour), count(*)\n"
+        + "from \"events\" where \"ts\" >= TIMESTAMP'2018-01-01 00:30:30' "
+        + "AND \"ts\" < TIMESTAMP'2018-01-01 02:30:30'"
+        + "group by floor(\"ts\" to hour)";
+
+    List<Pair<String, String>> mvs = Lists.newArrayList(
+        Pair.of(mv0, "mv0"), Pair.of(mv1, "mv1"));
+    fixture(query)
+        .withMaterializations(mvs)
+        .checkingThatResultContains("EnumerableAggregate(group=[{0}], 
EXPR$1=[$SUM0($1)])\n"
+            + "  EnumerableUnion(all=[true])\n"
+            + "    EnumerableAggregate(group=[{0}], EXPR$1=[COUNT()])\n"
+            + "      EnumerableCalc(expr#0..1=[{inputs}], expr#2=[FLAG(HOUR)], 
expr#3=[FLOOR($t1, "
+            + "$t2)], expr#4=[Sarg[[2018-01-01 00:30:30..2018-01-01 00:31:00), 
[2018-01-01 02:30:00"
+            + "..2018-01-01 02:30:30)]], expr#5=[SEARCH($t1, $t4)], $f0=[$t3], 
$condition=[$t5])\n"
+            + "        EnumerableTableScan(table=[[hr, events]])\n"
+            + "    EnumerableAggregate(group=[{1}], EXPR$1=[$SUM0($0)])\n"
+            + "      EnumerableCalc(expr#0..2=[{inputs}], expr#3=[FLAG(HOUR)], 
expr#4=[FLOOR($t1, "
+            + "$t3)], expr#5=[Sarg[[2018-01-01 00:31:00..2018-01-01 
02:30:00)]], expr#6=[SEARCH"
+            + "($t1, $t5)], EXPR$2=[$t2], $f3=[$t4], $condition=[$t6])\n"
+            + "        EnumerableTableScan(table=[[hr, mv1]])")
+        .withMetadataProvider(
+            ChainedRelMetadataProvider.of(
+                ImmutableList.of(TestMetadataHandlers.TestRelMdRowCount.SOURCE,
+                    TestMetadataHandlers.TestRelMdSelectivity.SOURCE,
+                    DefaultRelMetadataProvider.INSTANCE))
+        )
+        .ok();
+  }
+
+

Review Comment:
   done



-- 
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...@calcite.apache.org

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

Reply via email to