ccaominh commented on a change in pull request #6974: sql support for dynamic 
parameters
URL: https://github.com/apache/incubator-druid/pull/6974#discussion_r311331198
 
 

 ##########
 File path: 
sql/src/test/java/org/apache/druid/sql/calcite/CalciteParameterQueryTest.java
 ##########
 @@ -0,0 +1,540 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.avatica.SqlType;
+import org.apache.calcite.util.TimestampString;
+import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.aggregation.CountAggregatorFactory;
+import org.apache.druid.query.aggregation.DoubleSumAggregatorFactory;
+import org.apache.druid.query.aggregation.FilteredAggregatorFactory;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.ordering.StringComparators;
+import org.apache.druid.query.scan.ScanQuery;
+import org.apache.druid.segment.column.ValueType;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.sql.http.SqlParameter;
+import org.junit.Test;
+
+public class CalciteParameterQueryTest extends BaseCalciteQueryTest
+{
+  @Test
+  public void testSelectConstantExpression() throws Exception
+  {
+    testQuery(
+        "SELECT 1 + ?",
+        ImmutableList.of(),
+        ImmutableList.of(
+            new Object[]{2}
+        ),
+        ImmutableList.of(new SqlParameter(1, SqlType.INTEGER, 1))
+    );
+  }
+
+  @Test
+  public void testSelectConstantExpressionFromTable() throws Exception
+  {
+    testQuery(
+        "SELECT 1 + ?, dim1 FROM foo LIMIT ?",
+        ImmutableList.of(
+            newScanQueryBuilder()
+                .dataSource(CalciteTests.DATASOURCE1)
+                .intervals(querySegmentSpec(Filtration.eternity()))
+                .virtualColumns(expressionVirtualColumn("v0", "2", 
ValueType.LONG))
+                .columns("dim1", "v0")
+                
.resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
+                .limit(1)
+                .context(QUERY_CONTEXT_DEFAULT)
+                .build()
+        ),
+        ImmutableList.of(
+            new Object[]{2, ""}
+        ),
+        ImmutableList.of(
+            new SqlParameter(1, SqlType.INTEGER, 1),
+            new SqlParameter(2, SqlType.INTEGER, 1)
+        )
+    );
+  }
+
+  @Test
+  public void testSelectCountStart() throws Exception
+  {
+    testQuery(
+        PLANNER_CONFIG_DEFAULT,
+        QUERY_CONTEXT_DONT_SKIP_EMPTY_BUCKETS,
+        ImmutableList.of(
+            new SqlParameter(1, SqlType.INTEGER, 10),
+            new SqlParameter(2, SqlType.INTEGER, 0)
+        ),
+        "SELECT exp(count(*)) + ?, sum(m2)  FROM druid.foo WHERE  dim2 = ?",
+        CalciteTests.REGULAR_USER_AUTH_RESULT,
+        ImmutableList.of(Druids.newTimeseriesQueryBuilder()
+                               .dataSource(CalciteTests.DATASOURCE1)
+                               
.intervals(querySegmentSpec(Filtration.eternity()))
+                               .filters(selector("dim2", "0", null))
+                               .granularity(Granularities.ALL)
+                               .aggregators(aggregators(
+                                   new CountAggregatorFactory("a0"),
+                                   new DoubleSumAggregatorFactory("a1", "m2")
+                               ))
+                               .postAggregators(
+                                   expressionPostAgg("p0", "(exp(\"a0\") + 
10)")
+                               )
+                               .context(QUERY_CONTEXT_DONT_SKIP_EMPTY_BUCKETS)
+                               .build()),
+        ImmutableList.of(
+            new Object[]{11.0, NullHandling.defaultDoubleValue()}
+        )
+    );
+  }
+
+  @Test
+  public void testTimestamp() throws Exception
+  {
+    long val = new TimestampString("2000-01-01 
00:00:00").getMillisSinceEpoch();
+    // with millis
+    testQuery(
+        PLANNER_CONFIG_DEFAULT,
+        QUERY_CONTEXT_DONT_SKIP_EMPTY_BUCKETS,
+        ImmutableList.of(
+            new SqlParameter(1, SqlType.INTEGER, 10),
+            new SqlParameter(
+                2,
+                SqlType.TIMESTAMP,
+                DateTimes.of("2999-01-01T00:00:00Z").getMillis()
+            )
+        ),
+        "SELECT exp(count(*)) + ?, sum(m2)  FROM druid.foo WHERE  __time >= ?",
+        CalciteTests.REGULAR_USER_AUTH_RESULT,
+        ImmutableList.of(Druids.newTimeseriesQueryBuilder()
+                               .dataSource(CalciteTests.DATASOURCE1)
+                               
.intervals(querySegmentSpec(Intervals.of("2999-01-01T00:00:00.000Z/146140482-04-24T15:36:27.903Z")))
+                               .granularity(Granularities.ALL)
+                               .aggregators(aggregators(
+                                   new CountAggregatorFactory("a0"),
+                                   new DoubleSumAggregatorFactory("a1", "m2")
+                               ))
+                               .postAggregators(
+                                   expressionPostAgg("p0", "(exp(\"a0\") + 
10)")
+                               )
+                               .context(QUERY_CONTEXT_DONT_SKIP_EMPTY_BUCKETS)
+                               .build()),
+        ImmutableList.of(
+            new Object[]{11.0, NullHandling.defaultDoubleValue()}
+        )
+    );
+
+
+    // with timestampstring
+    testQuery(
+        PLANNER_CONFIG_DEFAULT,
+        QUERY_CONTEXT_DONT_SKIP_EMPTY_BUCKETS,
+        ImmutableList.of(
+            new SqlParameter(1, SqlType.INTEGER, 10),
+            new SqlParameter(
+                2,
+                SqlType.TIMESTAMP,
+                "2999-01-01 00:00:00"
+            )
+        ),
+        "SELECT exp(count(*)) + ?, sum(m2)  FROM druid.foo WHERE  __time >= ?",
+        CalciteTests.REGULAR_USER_AUTH_RESULT,
+        ImmutableList.of(Druids.newTimeseriesQueryBuilder()
+                               .dataSource(CalciteTests.DATASOURCE1)
+                               
.intervals(querySegmentSpec(Intervals.of("2999-01-01T00:00:00.000Z/146140482-04-24T15:36:27.903Z")))
+                               .granularity(Granularities.ALL)
+                               .aggregators(aggregators(
+                                   new CountAggregatorFactory("a0"),
+                                   new DoubleSumAggregatorFactory("a1", "m2")
+                               ))
+                               .postAggregators(
+                                   expressionPostAgg("p0", "(exp(\"a0\") + 
10)")
+                               )
+                               .context(QUERY_CONTEXT_DONT_SKIP_EMPTY_BUCKETS)
+                               .build()),
+        ImmutableList.of(
+            new Object[]{11.0, NullHandling.defaultDoubleValue()}
+        )
+    );
+  }
+
+
+  @Test
+  public void testSelectTrimFamily() throws Exception
+  {
+    // TRIM has some whacky parsing. Make sure the different forms work.
+
+    testQuery(
+        "SELECT\n"
+        + "TRIM(BOTH 'x' FROM ?),\n"
+        + "TRIM(TRAILING 'x' FROM 'xfoox'),\n"
 
 Review comment:
   What do the `TRIM` methods that don't have a dynamic parameter as an 
argument test? If they don't test any of the dynamic parameter code, then they 
can be removed to make this test shorter.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to