jihoonson commented on a change in pull request #6951: Add more sketch 
aggregator support in Druid SQL
URL: https://github.com/apache/incubator-druid/pull/6951#discussion_r253233599
 
 

 ##########
 File path: 
extensions-core/datasketches/src/test/java/org/apache/druid/query/aggregation/datasketches/hll/sql/HllSketchSqlAggregatorTest.java
 ##########
 @@ -0,0 +1,402 @@
+/*
+ * 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.query.aggregation.datasketches.hll.sql;
+
+import com.fasterxml.jackson.databind.Module;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.java.util.common.Pair;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.Query;
+import org.apache.druid.query.QueryDataSource;
+import org.apache.druid.query.QueryRunnerFactoryConglomerate;
+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.aggregation.LongSumAggregatorFactory;
+import 
org.apache.druid.query.aggregation.datasketches.hll.HllSketchBuildAggregatorFactory;
+import 
org.apache.druid.query.aggregation.datasketches.hll.HllSketchMergeAggregatorFactory;
+import org.apache.druid.query.aggregation.datasketches.hll.HllSketchModule;
+import org.apache.druid.query.aggregation.post.ArithmeticPostAggregator;
+import org.apache.druid.query.aggregation.post.FieldAccessPostAggregator;
+import 
org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.expression.TestExprMacroTable;
+import org.apache.druid.query.groupby.GroupByQuery;
+import org.apache.druid.query.spec.MultipleIntervalSegmentSpec;
+import org.apache.druid.segment.IndexBuilder;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.column.ValueType;
+import org.apache.druid.segment.incremental.IncrementalIndexSchema;
+import org.apache.druid.segment.virtual.ExpressionVirtualColumn;
+import 
org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.server.security.AuthTestUtils;
+import org.apache.druid.server.security.AuthenticationResult;
+import org.apache.druid.sql.SqlLifecycle;
+import org.apache.druid.sql.SqlLifecycleFactory;
+import org.apache.druid.sql.calcite.BaseCalciteQueryTest;
+import org.apache.druid.sql.calcite.filtration.Filtration;
+import org.apache.druid.sql.calcite.planner.DruidOperatorTable;
+import org.apache.druid.sql.calcite.planner.PlannerConfig;
+import org.apache.druid.sql.calcite.planner.PlannerContext;
+import org.apache.druid.sql.calcite.planner.PlannerFactory;
+import org.apache.druid.sql.calcite.schema.DruidSchema;
+import org.apache.druid.sql.calcite.schema.SystemSchema;
+import org.apache.druid.sql.calcite.util.CalciteTestBase;
+import org.apache.druid.sql.calcite.util.CalciteTests;
+import org.apache.druid.sql.calcite.util.QueryLogHook;
+import org.apache.druid.sql.calcite.util.SpecificSegmentsQuerySegmentWalker;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.partition.LinearShardSpec;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class HllSketchSqlAggregatorTest extends CalciteTestBase
+{
+  private static final String DATA_SOURCE = "foo";
+
+  private static QueryRunnerFactoryConglomerate conglomerate;
+  private static Closer resourceCloser;
+  private static AuthenticationResult authenticationResult = 
CalciteTests.REGULAR_USER_AUTH_RESULT;
+  private static final Map<String, Object> QUERY_CONTEXT_DEFAULT = 
ImmutableMap.of(
+      PlannerContext.CTX_SQL_QUERY_ID, "dummy"
+  );
+
+  @BeforeClass
+  public static void setUpClass()
+  {
+    final Pair<QueryRunnerFactoryConglomerate, Closer> conglomerateCloserPair 
= CalciteTests
+        .createQueryRunnerFactoryConglomerate();
+    conglomerate = conglomerateCloserPair.lhs;
+    resourceCloser = conglomerateCloserPair.rhs;
+  }
+
+  @AfterClass
+  public static void tearDownClass() throws IOException
+  {
+    resourceCloser.close();
+  }
+
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Rule
+  public QueryLogHook queryLogHook = QueryLogHook.create();
+
+  private SpecificSegmentsQuerySegmentWalker walker;
+  private SqlLifecycleFactory sqlLifecycleFactory;
+
+  @Before
+  public void setUp() throws Exception
+  {
+    HllSketchModule.registerSerde();
+    for (Module mod : new HllSketchModule().getJacksonModules()) {
+      CalciteTests.getJsonMapper().registerModule(mod);
+    }
+
+    final QueryableIndex index = IndexBuilder.create()
+                                             
.tmpDir(temporaryFolder.newFolder())
+                                             
.segmentWriteOutMediumFactory(OffHeapMemorySegmentWriteOutMediumFactory.instance())
+                                             .schema(
+                                                 new 
IncrementalIndexSchema.Builder()
+                                                     .withMetrics(
+                                                         new 
CountAggregatorFactory("cnt"),
+                                                         new 
DoubleSumAggregatorFactory("m1", "m1"),
+                                                         new 
HllSketchBuildAggregatorFactory(
+                                                             "hllsketch_dim1",
+                                                             "dim1",
+                                                             null,
+                                                             null
+                                                         )
+                                                     )
+                                                     .withRollup(false)
+                                                     .build()
+                                             )
+                                             .rows(CalciteTests.ROWS1)
+                                             .buildMMappedIndex();
+
+    walker = new SpecificSegmentsQuerySegmentWalker(conglomerate).add(
+        DataSegment.builder()
+                   .dataSource(DATA_SOURCE)
+                   .interval(index.getDataInterval())
+                   .version("1")
+                   .shardSpec(new LinearShardSpec(0))
+                   .build(),
+        index
+    );
+
+    final PlannerConfig plannerConfig = new PlannerConfig();
+    final DruidSchema druidSchema = 
CalciteTests.createMockSchema(conglomerate, walker, plannerConfig);
+    final SystemSchema systemSchema = 
CalciteTests.createMockSystemSchema(druidSchema, walker);
+    final DruidOperatorTable operatorTable = new DruidOperatorTable(
+        ImmutableSet.of(new HllSketchSqlAggregator()),
+        ImmutableSet.of()
+    );
+
+    sqlLifecycleFactory = CalciteTests.createSqlLifecycleFactory(
+        new PlannerFactory(
+            druidSchema,
+            systemSchema,
+            CalciteTests.createMockQueryLifecycleFactory(walker, conglomerate),
+            operatorTable,
+            CalciteTests.createExprMacroTable(),
+            plannerConfig,
+            AuthTestUtils.TEST_AUTHORIZER_MAPPER,
+            CalciteTests.getJsonMapper()
+        )
+    );
+  }
+
+  @After
+  public void tearDown() throws Exception
+  {
+    walker.close();
+    walker = null;
+  }
+
+  @Test
+  public void testApproxCountDistinctHllSketch() throws Exception
+  {
+    SqlLifecycle sqlLifecycle = sqlLifecycleFactory.factorize();
+    try {
+      final String sql = "SELECT\n"
+                         + "  SUM(cnt),\n"
+                         + "  APPROX_COUNT_DISTINCT_DS_HLL(dim2),\n" // 
uppercase
+                         + "  APPROX_COUNT_DISTINCT_DS_HLL(dim2) FILTER(WHERE 
dim2 <> ''),\n" // lowercase; also, filtered
+                         + "  APPROX_COUNT_DISTINCT_DS_HLL(SUBSTRING(dim2, 1, 
1)),\n" // on extractionFn
+                         + "  APPROX_COUNT_DISTINCT_DS_HLL(SUBSTRING(dim2, 1, 
1) || 'x'),\n" // on expression
+                         + "  APPROX_COUNT_DISTINCT_DS_HLL(hllsketch_dim1, 21, 
'HLL_8'),\n" // on native HllSketch column
+                         + "  APPROX_COUNT_DISTINCT_DS_HLL(hllsketch_dim1)\n" 
// on native HllSketch column
+                         + "FROM druid.foo";
+
+      // Verify results
+      final List<Object[]> results = sqlLifecycle.runSimple(sql, 
QUERY_CONTEXT_DEFAULT, authenticationResult).toList();
+      final List<Object[]> expectedResults;
+
+      if (NullHandling.replaceWithDefault()) {
+        expectedResults = ImmutableList.of(
+            new Object[]{
+                6L,
+                2L,
+                2L,
+                1L,
+                2L,
+                5L,
+                5L
+            }
+        );
+      } else {
+        expectedResults = ImmutableList.of(
+            new Object[]{
+                6L,
+                2L,
+                2L,
+                1L,
+                1L,
+                5L,
+                5L
+            }
+        );
+      }
+
+      Assert.assertEquals(expectedResults.size(), results.size());
+      for (int i = 0; i < expectedResults.size(); i++) {
+        Assert.assertArrayEquals(expectedResults.get(i), results.get(i));
+      }
+
+      // Verify query
+      Assert.assertEquals(
+          Druids.newTimeseriesQueryBuilder()
+                .dataSource(CalciteTests.DATASOURCE1)
+                .intervals(new 
MultipleIntervalSegmentSpec(ImmutableList.of(Filtration.eternity())))
+                .granularity(Granularities.ALL)
+                .virtualColumns(
+                    new ExpressionVirtualColumn(
+                        "a3:v",
+                        "substring(\"dim2\", 0, 1)",
+                        ValueType.STRING,
+                        TestExprMacroTable.INSTANCE
+                    ),
+                    new ExpressionVirtualColumn(
+                        "a4:v",
+                        "concat(substring(\"dim2\", 0, 1),'x')",
+                        ValueType.STRING,
+                        TestExprMacroTable.INSTANCE
+                    )
+                )
+                .aggregators(
+                    ImmutableList.of(
+                        new LongSumAggregatorFactory("a0", "cnt"),
+                        new HllSketchBuildAggregatorFactory(
+                            "a1",
+                            "dim2",
+                            null,
+                            null
+                        ),
+                        new FilteredAggregatorFactory(
+                            new HllSketchBuildAggregatorFactory(
+                                "a2",
+                                "dim2",
+                                null,
+                                null
+                            ),
+                            
BaseCalciteQueryTest.NOT(BaseCalciteQueryTest.SELECTOR("dim2", "", null))
+                        ),
+                        new HllSketchBuildAggregatorFactory(
+                            "a3",
+                            "a3:v",
+                            null,
+                            null
+                        ),
+                        new HllSketchBuildAggregatorFactory(
+                            "a4",
+                            "a4:v",
+                            null,
+                            null
+                        ),
+                        new HllSketchMergeAggregatorFactory("a5", 
"hllsketch_dim1", 21, "HLL_8"),
+                        new HllSketchMergeAggregatorFactory("a6", 
"hllsketch_dim1", null, null)
+                    )
+                )
+                .context(ImmutableMap.of("skipEmptyBuckets", true, 
PlannerContext.CTX_SQL_QUERY_ID, "dummy"))
+                .build(),
+          Iterables.getOnlyElement(queryLogHook.getRecordedQueries())
+      );
+    }
+    catch (Exception e) {
 
 Review comment:
   This catch looks not needed?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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