-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Grouping on complex columns aka unifying GroupBy strategies #16068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cryptoe
merged 50 commits into
apache:master
from
LakshSingla:unified-group-by-strategies-2
Apr 24, 2024
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
39d6a32
init
LakshSingla 731d273
draft work
LakshSingla b01344a
more
LakshSingla 6e16865
some stuff working
LakshSingla 718eef9
group by complex col working
LakshSingla cbb1181
fixup
LakshSingla 33de9a8
fixup
LakshSingla f1394f7
remove original strategies
LakshSingla bd82649
checkstyle
LakshSingla 3017e4d
comments
LakshSingla f159f67
add benchmarks
LakshSingla 638bf52
all dictionaries now use sorted map
LakshSingla b21faac
Merge branch 'master' into unified-group-by-strategies-2
LakshSingla ead1ddf
tests 1
LakshSingla 25fc42c
tests and comments
LakshSingla 58f8834
tests
LakshSingla 5939715
fixup big mistake
LakshSingla 0f620b0
Merge branch 'master' into unified-group-by-strategies-2
LakshSingla d8a250c
cleanup, refactor for diff strategies
LakshSingla 2479155
group by on nested arrays, disallow topN and vector engine
LakshSingla e49206f
hash stuff
LakshSingla 973fa88
some review, more tests
LakshSingla 8b62073
fixup benchmark
LakshSingla 53f1b54
Merge branch 'master' into unified-group-by-strategies-2
LakshSingla c07d77d
checkstyle
LakshSingla 2e0fe26
codeql
LakshSingla a580326
static check
LakshSingla 15fb3bb
static check
LakshSingla 3066942
static check
LakshSingla 8cd955e
delete
LakshSingla be500d5
tests fix
LakshSingla 6090d82
openrewrite
LakshSingla f9bbc21
Revert "openrewrite"
LakshSingla c28eb79
openrewrite test
LakshSingla b51248f
Revert "openrewrite test"
LakshSingla 031f586
Revert "Revert "openrewrite test""
LakshSingla 170d8d9
openrewrite test
LakshSingla 60c2b42
review comments
LakshSingla ad44b5f
test fix
LakshSingla f30c7c6
tests fix
LakshSingla 6e2db86
tests fix
LakshSingla a3a0145
tests fix
LakshSingla e95731c
review 1, delete bogus class
LakshSingla dfb5c9f
review 1, more comments
LakshSingla a412386
Merge branch 'master' into unified-group-by-strategies-2
LakshSingla 2098f90
cleanup todos, review comments
LakshSingla 3fbadd9
sorting and limiting fixup
LakshSingla 5d5c8ae
prevent round trip
LakshSingla ad2fb04
prevent round trip 2
LakshSingla 7fe36b1
tests, and fixup flake, preserve old incorrect behaviour
LakshSingla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
385 changes: 385 additions & 0 deletions
385
benchmarks/src/test/java/org/apache/druid/benchmark/query/SqlGroupByBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,385 @@ | ||
| /* | ||
| * 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.benchmark.query; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import org.apache.druid.common.config.NullHandling; | ||
| import org.apache.druid.data.input.impl.DimensionSchema; | ||
| import org.apache.druid.data.input.impl.DimensionsSpec; | ||
| import org.apache.druid.guice.NestedDataModule; | ||
| import org.apache.druid.java.util.common.StringUtils; | ||
| import org.apache.druid.java.util.common.granularity.Granularities; | ||
| import org.apache.druid.java.util.common.guava.Sequence; | ||
| import org.apache.druid.java.util.common.io.Closer; | ||
| import org.apache.druid.math.expr.ExpressionProcessing; | ||
| import org.apache.druid.query.DruidProcessingConfig; | ||
| import org.apache.druid.query.QueryRunnerFactoryConglomerate; | ||
| import org.apache.druid.query.expression.TestExprMacroTable; | ||
| import org.apache.druid.segment.AutoTypeColumnSchema; | ||
| import org.apache.druid.segment.IndexSpec; | ||
| import org.apache.druid.segment.QueryableIndex; | ||
| import org.apache.druid.segment.column.StringEncodingStrategy; | ||
| import org.apache.druid.segment.generator.GeneratorBasicSchemas; | ||
| import org.apache.druid.segment.generator.GeneratorSchemaInfo; | ||
| import org.apache.druid.segment.generator.SegmentGenerator; | ||
| import org.apache.druid.segment.transform.ExpressionTransform; | ||
| import org.apache.druid.segment.transform.TransformSpec; | ||
| import org.apache.druid.server.QueryStackTests; | ||
| import org.apache.druid.server.SpecificSegmentsQuerySegmentWalker; | ||
| import org.apache.druid.server.security.AuthConfig; | ||
| import org.apache.druid.server.security.AuthTestUtils; | ||
| import org.apache.druid.sql.calcite.SqlVectorizedExpressionSanityTest; | ||
| import org.apache.druid.sql.calcite.planner.CalciteRulesManager; | ||
| import org.apache.druid.sql.calcite.planner.CatalogResolver; | ||
| import org.apache.druid.sql.calcite.planner.DruidPlanner; | ||
| import org.apache.druid.sql.calcite.planner.PlannerConfig; | ||
| import org.apache.druid.sql.calcite.planner.PlannerFactory; | ||
| import org.apache.druid.sql.calcite.planner.PlannerResult; | ||
| import org.apache.druid.sql.calcite.run.SqlEngine; | ||
| import org.apache.druid.sql.calcite.schema.DruidSchemaCatalog; | ||
| import org.apache.druid.sql.calcite.util.CalciteTests; | ||
| import org.apache.druid.timeline.DataSegment; | ||
| import org.apache.druid.timeline.partition.LinearShardSpec; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @Fork(value = 1) | ||
| @Warmup(iterations = 3) | ||
| @Measurement(iterations = 5) | ||
| public class SqlGroupByBenchmark | ||
| { | ||
| static { | ||
| NullHandling.initializeForTests(); | ||
| ExpressionProcessing.initializeForTests(); | ||
| NestedDataModule.registerHandlersAndSerde(); | ||
| } | ||
|
|
||
| private static final DruidProcessingConfig PROCESSING_CONFIG = new DruidProcessingConfig() | ||
| { | ||
| @Override | ||
| public int intermediateComputeSizeBytes() | ||
| { | ||
| return 512 * 1024 * 1024; | ||
| } | ||
|
|
||
| @Override | ||
| public int getNumMergeBuffers() | ||
| { | ||
| return 3; | ||
| } | ||
|
|
||
| @Override | ||
| public int getNumThreads() | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| @Override | ||
| public String getFormatString() | ||
| { | ||
| return "benchmarks-processing-%s"; | ||
| } | ||
| }; | ||
|
|
||
| @Param({ | ||
| "string-Sequential-100_000", | ||
| "string-Sequential-10_000_000", | ||
| // "string-Sequential-1_000_000_000", | ||
| "string-ZipF-1_000_000", | ||
| "string-Uniform-1_000_000", | ||
|
|
||
| "multi-string-Sequential-100_000", | ||
| "multi-string-Sequential-10_000_000", | ||
| // "multi-string-Sequential-1_000_000_000", | ||
| "multi-string-ZipF-1_000_000", | ||
| "multi-string-Uniform-1_000_000", | ||
|
|
||
| "long-Sequential-100_000", | ||
| "long-Sequential-10_000_000", | ||
| // "long-Sequential-1_000_000_000", | ||
| "long-ZipF-1_000_000", | ||
| "long-Uniform-1_000_000", | ||
|
|
||
| "double-ZipF-1_000_000", | ||
| "double-Uniform-1_000_000", | ||
|
|
||
| "float-ZipF-1_000_000", | ||
| "float-Uniform-1_000_000", | ||
|
|
||
| "stringArray-Sequential-100_000", | ||
| "stringArray-Sequential-3_000_000", | ||
| // "stringArray-Sequential-1_000_000_000", | ||
| "stringArray-ZipF-1_000_000", | ||
| "stringArray-Uniform-1_000_000", | ||
|
|
||
| "longArray-Sequential-100_000", | ||
| "longArray-Sequential-3_000_000", | ||
| // "longArray-Sequential-1_000_000_000", | ||
| "longArray-ZipF-1_000_000", | ||
| "longArray-Uniform-1_000_000", | ||
|
|
||
| "nested-Sequential-100_000", | ||
| "nested-Sequential-3_000_000", | ||
| // "nested-Sequential-1_000_000_000", | ||
| "nested-ZipF-1_000_000", | ||
| "nested-Uniform-1_000_000", | ||
| }) | ||
| private String groupingDimension; | ||
|
|
||
| private SqlEngine engine; | ||
| @Nullable | ||
| private PlannerFactory plannerFactory; | ||
| private Closer closer = Closer.create(); | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() | ||
| { | ||
| final GeneratorSchemaInfo schemaInfo = GeneratorBasicSchemas.SCHEMA_MAP.get("groupBy-testbench"); | ||
|
|
||
| final DataSegment dataSegment = DataSegment.builder() | ||
| .dataSource("foo") | ||
| .interval(schemaInfo.getDataInterval()) | ||
| .version("1") | ||
| .shardSpec(new LinearShardSpec(0)) | ||
| .size(0) | ||
| .build(); | ||
| final DataSegment dataSegment2 = DataSegment.builder() | ||
| .dataSource("foo") | ||
| .interval(schemaInfo.getDataInterval()) | ||
| .version("1") | ||
| .shardSpec(new LinearShardSpec(1)) | ||
| .size(0) | ||
| .build(); | ||
|
|
||
|
|
||
| final PlannerConfig plannerConfig = new PlannerConfig(); | ||
|
|
||
| String columnCardinalityWithUnderscores = groupingDimension.substring(groupingDimension.lastIndexOf('-') + 1); | ||
| int rowsPerSegment = Integer.parseInt(StringUtils.replace(columnCardinalityWithUnderscores, "_", "")); | ||
|
|
||
| final SegmentGenerator segmentGenerator = closer.register(new SegmentGenerator()); | ||
|
|
||
| TransformSpec transformSpec = new TransformSpec( | ||
| null, | ||
| ImmutableList.of( | ||
| // string array dims | ||
| new ExpressionTransform( | ||
| "stringArray-Sequential-100_000", | ||
| "array(\"string-Sequential-100_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| new ExpressionTransform( | ||
| "stringArray-Sequential-3_000_000", | ||
| "array(\"string-Sequential-10_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| /* | ||
| new ExpressionTransform( | ||
| "stringArray-Sequential-1_000_000_000", | ||
| "array(\"string-Sequential-1_000_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ),*/ | ||
| new ExpressionTransform( | ||
| "stringArray-ZipF-1_000_000", | ||
| "array(\"string-ZipF-1_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| new ExpressionTransform( | ||
| "stringArray-Uniform-1_000_000", | ||
| "array(\"string-Uniform-1_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
|
|
||
| // long array dims | ||
| new ExpressionTransform( | ||
| "longArray-Sequential-100_000", | ||
| "array(\"long-Sequential-100_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| new ExpressionTransform( | ||
| "longArray-Sequential-3_000_000", | ||
| "array(\"long-Sequential-10_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| /* | ||
| new ExpressionTransform( | ||
| "longArray-Sequential-1_000_000_000", | ||
| "array(\"long-Sequential-1_000_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ),*/ | ||
| new ExpressionTransform( | ||
| "longArray-ZipF-1_000_000", | ||
| "array(\"long-ZipF-1_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| new ExpressionTransform( | ||
| "longArray-Uniform-1_000_000", | ||
| "array(\"long-Uniform-1_000_000\")", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
|
|
||
| // nested complex json dim | ||
| new ExpressionTransform( | ||
| "nested-Sequential-100_000", | ||
| "json_object('long1', \"long-Sequential-100_000\", 'nesteder', json_object('long1', \"long-Sequential-100_000\"))", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| new ExpressionTransform( | ||
| "nested-Sequential-3_000_000", | ||
| "json_object('long1', \"long-Sequential-10_000_000\", 'nesteder', json_object('long1', \"long-Sequential-10_000_000\"))", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| /*new ExpressionTransform( | ||
| "nested-Sequential-1_000_000_000", | ||
| "json_object('long1', \"long-Sequential-1_000_000_000\", 'nesteder', json_object('long1', \"long-Sequential-1_000_000_000\"))", | ||
| TestExprMacroTable.INSTANCE | ||
| ),*/ | ||
| new ExpressionTransform( | ||
| "nested-ZipF-1_000_000", | ||
| "json_object('long1', \"long-ZipF-1_000_000\", 'nesteder', json_object('long1', \"long-ZipF-1_000_000\"))", | ||
| TestExprMacroTable.INSTANCE | ||
| ), | ||
| new ExpressionTransform( | ||
| "nested-Uniform-1_000_000", | ||
| "json_object('long1', \"long-Uniform-1_000_000\", 'nesteder', json_object('long1', \"long-Uniform-1_000_000\"))", | ||
| TestExprMacroTable.INSTANCE | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| List<DimensionSchema> columnSchemas = schemaInfo.getDimensionsSpec() | ||
| .getDimensions() | ||
| .stream() | ||
| .map(x -> new AutoTypeColumnSchema(x.getName(), null)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| List<DimensionSchema> transformSchemas = transformSpec | ||
| .getTransforms() | ||
| .stream() | ||
| .map( | ||
| transform -> new AutoTypeColumnSchema(transform.getName(), null) | ||
| ) | ||
| .collect(Collectors.toList()); | ||
|
|
||
|
|
||
|
|
||
| final QueryableIndex index = segmentGenerator.generate( | ||
| dataSegment, | ||
| schemaInfo, | ||
| DimensionsSpec.builder() | ||
| .setDimensions(ImmutableList.<DimensionSchema>builder() | ||
| .addAll(columnSchemas) | ||
| .addAll(transformSchemas) | ||
| .build() | ||
| ) | ||
| .build(), | ||
| transformSpec, | ||
| IndexSpec.builder().withStringDictionaryEncoding(new StringEncodingStrategy.Utf8()).build(), | ||
| Granularities.NONE, | ||
| rowsPerSegment | ||
| ); | ||
|
|
||
| final QueryRunnerFactoryConglomerate conglomerate = QueryStackTests.createQueryRunnerFactoryConglomerate( | ||
| closer, | ||
| PROCESSING_CONFIG | ||
| ); | ||
|
|
||
| final SpecificSegmentsQuerySegmentWalker walker = SpecificSegmentsQuerySegmentWalker.createWalker(conglomerate) | ||
| .add(dataSegment, index) | ||
| .add(dataSegment2, index); | ||
| closer.register(walker); | ||
|
|
||
| // Hacky and pollutes global namespace, but it is fine since benchmarks are run in isolation. Wasn't able | ||
| // to work up a cleaner way of doing it by modifying the injector. | ||
| CalciteTests.getJsonMapper().registerModules(NestedDataModule.getJacksonModulesList()); | ||
|
|
||
| final DruidSchemaCatalog rootSchema = | ||
| CalciteTests.createMockRootSchema(conglomerate, walker, plannerConfig, AuthTestUtils.TEST_AUTHORIZER_MAPPER); | ||
| engine = CalciteTests.createMockSqlEngine(walker, conglomerate); | ||
| plannerFactory = new PlannerFactory( | ||
| rootSchema, | ||
| CalciteTests.createOperatorTable(), | ||
| CalciteTests.createExprMacroTable(), | ||
| plannerConfig, | ||
| AuthTestUtils.TEST_AUTHORIZER_MAPPER, | ||
| CalciteTests.getJsonMapper(), | ||
| CalciteTests.DRUID_SCHEMA_NAME, | ||
| new CalciteRulesManager(ImmutableSet.of()), | ||
| CalciteTests.createJoinableFactoryWrapper(), | ||
| CatalogResolver.NULL_RESOLVER, | ||
| new AuthConfig() | ||
| ); | ||
|
|
||
| try { | ||
| SqlVectorizedExpressionSanityTest.sanityTestVectorizedSqlQueries( | ||
| plannerFactory, | ||
| sqlQuery(groupingDimension) | ||
| ); | ||
| } | ||
| catch (Throwable ignored) { | ||
| // the show must go on | ||
| } | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void tearDown() throws Exception | ||
| { | ||
| closer.close(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| public void querySql(Blackhole blackhole) | ||
| { | ||
| final String sql = sqlQuery(groupingDimension); | ||
| try (final DruidPlanner planner = plannerFactory.createPlannerForTesting(engine, sql, Collections.emptyMap())) { | ||
| final PlannerResult plannerResult = planner.plan(); | ||
| final Sequence<Object[]> resultSequence = plannerResult.run().getResults(); | ||
| final Object[] lastRow = resultSequence.accumulate(null, (accumulated, in) -> in); | ||
| blackhole.consume(lastRow); | ||
| } | ||
| } | ||
|
|
||
| private static String sqlQuery(String groupingDimension) | ||
| { | ||
| return StringUtils.format("SELECT \"%s\", COUNT(*) FROM foo GROUP BY 1", groupingDimension); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Missing catch of NumberFormatException