Skip to content
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

Configure a max limit for lgK values of HLL sketches #15516

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.druid.common.config.NullHandling;
import org.apache.druid.query.aggregation.AggregatorFactory;
import org.apache.druid.query.aggregation.BufferAggregator;
import org.apache.druid.query.aggregation.datasketches.SketchConfig;
import org.apache.druid.query.aggregation.datasketches.hll.HllSketchMergeAggregatorFactory;
import org.apache.druid.query.dimension.DimensionSpec;
import org.apache.druid.segment.ColumnSelectorFactory;
Expand Down Expand Up @@ -66,7 +67,8 @@ public class DataSketchesHllBenchmark
null,
null,
null,
false
false,
new SketchConfig()
);

private final ByteBuffer buf = ByteBuffer.allocateDirect(aggregatorFactory.getMaxIntermediateSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.query.QueryContexts;
import org.apache.druid.query.QueryRunnerFactoryConglomerate;
import org.apache.druid.query.aggregation.datasketches.SketchConfig;
import org.apache.druid.query.aggregation.datasketches.hll.sql.HllSketchApproxCountDistinctSqlAggregator;
import org.apache.druid.query.aggregation.datasketches.hll.sql.HllSketchApproxCountDistinctUtf8SqlAggregator;
import org.apache.druid.query.aggregation.datasketches.quantiles.sql.DoublesSketchApproxQuantileSqlAggregator;
Expand Down Expand Up @@ -546,16 +547,17 @@ private void addSegmentToWalker(
private static DruidOperatorTable createOperatorTable()
{
try {
final SketchConfig sketchConfig = new SketchConfig();
final Set<SqlOperatorConversion> extractionOperators = new HashSet<>();
extractionOperators.add(CalciteTests.INJECTOR.getInstance(QueryLookupOperatorConversion.class));
final ApproxCountDistinctSqlAggregator countDistinctSqlAggregator =
new ApproxCountDistinctSqlAggregator(new HllSketchApproxCountDistinctSqlAggregator());
new ApproxCountDistinctSqlAggregator(new HllSketchApproxCountDistinctSqlAggregator(sketchConfig));
final Set<SqlAggregator> aggregators = new HashSet<>(
ImmutableList.of(
new DoublesSketchApproxQuantileSqlAggregator(),
new DoublesSketchObjectSqlAggregator(),
new HllSketchApproxCountDistinctSqlAggregator(),
new HllSketchApproxCountDistinctUtf8SqlAggregator(),
new HllSketchApproxCountDistinctSqlAggregator(sketchConfig),
new HllSketchApproxCountDistinctUtf8SqlAggregator(sketchConfig),
new ThetaSketchApproxCountDistinctSqlAggregator(),
new CountSqlAggregator(countDistinctSqlAggregator),
countDistinctSqlAggregator
Expand Down
8 changes: 8 additions & 0 deletions docs/development/extensions-core/datasketches-hll.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ For additional sketch types supported in Druid, see [DataSketches extension](dat
The default `lgK` value has proven to be sufficient for most use cases; expect only very negligible improvements in accuracy with `lgK` values over `16` in normal circumstances.
:::

### Runtime properties

The following runtime properties apply:

|Property| Description| Default |
|--------|------------|------|
|`druid.sketch.config.hllMaxLgK`| The maximum possible value of lgK that HLL sketches can be created with. Useful to limit the maximum lgK in sketches, to avoid the significant usage of resources used by sketches at higher values of lgK. An exception will be thrown if a query configures a lgK value higher than this. This property needs to be set on the broker and middle-manager/indexer. | 20 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an ingestion time property only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lgK can be specified during query time as well, wouldn't it need to be limited to avoid running into similar issues?

Copy link
Contributor

@cryptoe cryptoe Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its more of a ingestion time check only since what we want to avoid is huge rows in the generated segments which increasing the segment sizes which in turn decrease query performance.


### HLLSketchBuild aggregator

```
Expand Down
2 changes: 2 additions & 0 deletions docs/release-info/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ This section contains detailed release notes separated by areas.

### Extensions

- Datasketches extension has a new runtime parameter `druid.sketch.config.hllMaxLgK`, which configures the maximum allowed value of lgK for HLL sketches.

### Documentation improvements

## Upgrade notes and incompatible changes
Expand Down
5 changes: 5 additions & 0 deletions extensions-core/datasketches/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
<artifactId>guice</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nullable;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
* Contains runtime configurations specific to datasketches extension.
*/
public class SketchConfig
{
public static final int DEFAULT_HLL_MAX_LG_K = 20;

@JsonProperty
@Min(1)
@Max(21)
private int hllMaxLgK = DEFAULT_HLL_MAX_LG_K;

public SketchConfig(@Nullable Integer maxLgK)
{
this.hllMaxLgK = maxLgK == null ? DEFAULT_HLL_MAX_LG_K : maxLgK;
}

public SketchConfig()
{
}

public int getHllMaxLgK()
{
return hllMaxLgK;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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;

import com.google.inject.Binder;
import org.apache.druid.guice.JsonConfigProvider;
import org.apache.druid.initialization.DruidModule;

public class SketchModule implements DruidModule
{
@Override
public void configure(Binder binder)
{
JsonConfigProvider.bind(binder, "druid.sketch.config", SketchConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.datasketches.hll.HllSketch;
import org.apache.datasketches.hll.TgtHllType;
import org.apache.datasketches.hll.Union;
import org.apache.druid.error.DruidException;
import org.apache.druid.jackson.DefaultTrueJsonIncludeFilter;
import org.apache.druid.java.util.common.StringEncoding;
import org.apache.druid.java.util.common.StringEncodingDefaultUTF16LEJsonIncludeFilter;
Expand Down Expand Up @@ -62,6 +63,31 @@ public abstract class HllSketchAggregatorFactory extends AggregatorFactory
private final boolean shouldFinalize;
private final boolean round;

HllSketchAggregatorFactory(
final String name,
final String fieldName,
@Nullable final Integer lgK,
@Nullable final String tgtHllType,
@Nullable final StringEncoding stringEncoding,
final Boolean shouldFinalize,
final boolean round,
final int maxLgK
)
{
this(name, fieldName, lgK, tgtHllType, stringEncoding, shouldFinalize, round);

if (this.lgK > maxLgK) {
throw DruidException.forPersona(DruidException.Persona.USER)
.ofCategory(DruidException.Category.INVALID_INPUT)
.build(
"LgK value [%s] for HLL sketch cannot be greater than [%s]. Reduce the lgK value"
adarshsanjeev marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mention the name as well.

+ " or increase the runtime property druid.sketch.config.hllMaxLgK",
this.lgK,
maxLgK
);
}
}

HllSketchAggregatorFactory(
final String name,
final String fieldName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.druid.query.aggregation.datasketches.hll;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.datasketches.hll.HllSketch;
Expand All @@ -31,6 +32,7 @@
import org.apache.druid.query.aggregation.AggregatorUtil;
import org.apache.druid.query.aggregation.BufferAggregator;
import org.apache.druid.query.aggregation.VectorAggregator;
import org.apache.druid.query.aggregation.datasketches.SketchConfig;
import org.apache.druid.query.dimension.DefaultDimensionSpec;
import org.apache.druid.segment.ColumnInspector;
import org.apache.druid.segment.ColumnSelectorFactory;
Expand Down Expand Up @@ -62,12 +64,25 @@ public HllSketchBuildAggregatorFactory(
@JsonProperty("tgtHllType") @Nullable final String tgtHllType,
@JsonProperty("stringEncoding") @Nullable final StringEncoding stringEncoding,
@JsonProperty("shouldFinalize") final Boolean shouldFinalize,
@JsonProperty("round") final boolean round
@JsonProperty("round") final boolean round,
@JacksonInject SketchConfig serverConfig
)
{
super(name, fieldName, lgK, tgtHllType, stringEncoding, shouldFinalize, round);
super(name, fieldName, lgK, tgtHllType, stringEncoding, shouldFinalize, round, serverConfig.getHllMaxLgK());
}

HllSketchBuildAggregatorFactory(
String name,
String fieldName,
Integer lgK,
String tgtHllType,
StringEncoding stringEncoding,
Boolean shouldFinalize,
boolean round
)
{
super(name, fieldName, lgK, tgtHllType, stringEncoding, shouldFinalize, round);
}

@Override
public ColumnType getIntermediateType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.druid.query.aggregation.datasketches.hll;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.datasketches.hll.HllSketch;
Expand All @@ -31,6 +32,7 @@
import org.apache.druid.query.aggregation.AggregatorUtil;
import org.apache.druid.query.aggregation.BufferAggregator;
import org.apache.druid.query.aggregation.VectorAggregator;
import org.apache.druid.query.aggregation.datasketches.SketchConfig;
import org.apache.druid.segment.ColumnInspector;
import org.apache.druid.segment.ColumnSelectorFactory;
import org.apache.druid.segment.ColumnValueSelector;
Expand Down Expand Up @@ -61,7 +63,21 @@ public HllSketchMergeAggregatorFactory(
@JsonProperty("tgtHllType") @Nullable final String tgtHllType,
@JsonProperty("stringEncoding") @Nullable final StringEncoding stringEncoding,
@JsonProperty("shouldFinalize") final Boolean shouldFinalize,
@JsonProperty("round") final boolean round
@JsonProperty("round") final boolean round,
@JacksonInject SketchConfig serverConfig
)
{
super(name, fieldName, lgK, tgtHllType, stringEncoding, shouldFinalize, round, serverConfig.getHllMaxLgK());
}

HllSketchMergeAggregatorFactory(
String name,
String fieldName,
Integer lgK,
String tgtHllType,
StringEncoding stringEncoding,
Boolean shouldFinalize,
boolean round
)
{
super(name, fieldName, lgK, tgtHllType, stringEncoding, shouldFinalize, round);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

package org.apache.druid.query.aggregation.datasketches.hll.sql;

import com.google.inject.Inject;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.SqlTypeFamily;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.druid.java.util.common.StringEncoding;
import org.apache.druid.query.aggregation.AggregatorFactory;
import org.apache.druid.query.aggregation.datasketches.SketchConfig;
import org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator;
import org.apache.druid.sql.calcite.aggregation.Aggregation;
import org.apache.druid.sql.calcite.aggregation.SqlAggregator;
Expand All @@ -47,9 +49,10 @@ public class HllSketchApproxCountDistinctSqlAggregator extends HllSketchBaseSqlA
.functionCategory(SqlFunctionCategory.NUMERIC)
.build();

public HllSketchApproxCountDistinctSqlAggregator()
@Inject
public HllSketchApproxCountDistinctSqlAggregator(SketchConfig sketchConfig)
{
super(true, StringEncoding.UTF16LE);
super(true, StringEncoding.UTF16LE, sketchConfig);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

package org.apache.druid.query.aggregation.datasketches.hll.sql;

import com.google.inject.Inject;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.type.InferTypes;
import org.apache.calcite.sql.type.SqlTypeFamily;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.druid.java.util.common.StringEncoding;
import org.apache.druid.query.aggregation.AggregatorFactory;
import org.apache.druid.query.aggregation.datasketches.SketchConfig;
import org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator;
import org.apache.druid.sql.calcite.aggregation.Aggregation;
import org.apache.druid.sql.calcite.aggregation.SqlAggregator;
Expand Down Expand Up @@ -58,9 +60,10 @@ public class HllSketchApproxCountDistinctUtf8SqlAggregator
.functionCategory(SqlFunctionCategory.NUMERIC)
.build();

public HllSketchApproxCountDistinctUtf8SqlAggregator()
@Inject
public HllSketchApproxCountDistinctUtf8SqlAggregator(SketchConfig sketchConfig)
{
super(true, StringEncoding.UTF8);
super(true, StringEncoding.UTF8, sketchConfig);
}

@Override
Expand Down
Loading
Loading