forked from apache/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer more expressions in vectorized groupBy. (apache#16338)
* Defer more expressions in vectorized groupBy. This patch adds a way for columns to provide GroupByVectorColumnSelectors, which controls how the groupBy engine operates on them. This mechanism is used by ExpressionVirtualColumn to provide an ExpressionDeferredGroupByVectorColumnSelector that uses the inputs of an expression as the grouping key. The actual expression evaluation is deferred until the grouped ResultRow is created. A new context parameter "deferExpressionDimensions" allows users to control when this deferred selector is used. The default is "fixedWidthNonNumeric", which is a behavioral change from the prior behavior. Users can get the prior behavior by setting this to "singleString". * Fix style. * Add deferExpressionDimensions to SqlExpressionBenchmark. * Fix style. * Fix inspections. * Add more testing. * Use valueOrDefault. * Compute exprKeyBytes a bit lighter-weight.
- Loading branch information
Showing
14 changed files
with
593 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
197 changes: 197 additions & 0 deletions
197
processing/src/main/java/org/apache/druid/query/groupby/DeferExpressionDimensions.java
This file contains 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,197 @@ | ||
/* | ||
* 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.groupby; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import org.apache.druid.java.util.common.IAE; | ||
import org.apache.druid.math.expr.ExprType; | ||
import org.apache.druid.query.dimension.DimensionSpec; | ||
import org.apache.druid.segment.ColumnInspector; | ||
import org.apache.druid.segment.column.ColumnCapabilities; | ||
import org.apache.druid.segment.column.ValueType; | ||
import org.apache.druid.segment.vector.VectorColumnSelectorFactory; | ||
import org.apache.druid.segment.virtual.ExpressionPlan; | ||
import org.apache.druid.segment.virtual.ExpressionVirtualColumn; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Controls deferral of {@link ExpressionVirtualColumn} in {@link GroupByQuery}. | ||
*/ | ||
public enum DeferExpressionDimensions | ||
{ | ||
SINGLE_STRING("singleString") { | ||
@Override | ||
public boolean useDeferredGroupBySelector( | ||
ExpressionPlan plan, | ||
List<String> requiredBindingsList, | ||
ColumnInspector inspector | ||
) | ||
{ | ||
return false; | ||
} | ||
}, | ||
|
||
/** | ||
* Defer expressions when their input variables are all fixed-width types (primitive numbers, or dictionary encoded). | ||
*/ | ||
FIXED_WIDTH("fixedWidth") { | ||
@Override | ||
public boolean useDeferredGroupBySelector( | ||
ExpressionPlan plan, | ||
List<String> requiredBindingsList, | ||
ColumnInspector inspector | ||
) | ||
{ | ||
if (isInnatelyDeferrable(plan, requiredBindingsList, inspector)) { | ||
return false; | ||
} | ||
|
||
for (final String requiredBinding : requiredBindingsList) { | ||
final ColumnCapabilities capabilities = inspector.getColumnCapabilities(requiredBinding); | ||
if (capabilities == null) { | ||
return false; | ||
} | ||
|
||
if (!capabilities.isNumeric() && !capabilities.isDictionaryEncoded().isTrue()) { | ||
// Not fixed-width. | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}, | ||
|
||
/** | ||
* Defer expressions when their input variables are all fixed-width types (primitive numbers, or dictionary encoded). | ||
*/ | ||
FIXED_WIDTH_NON_NUMERIC("fixedWidthNonNumeric") { | ||
@Override | ||
public boolean useDeferredGroupBySelector( | ||
ExpressionPlan plan, | ||
List<String> requiredBindingsList, | ||
ColumnInspector inspector | ||
) | ||
{ | ||
if (isInnatelyDeferrable(plan, requiredBindingsList, inspector)) { | ||
return false; | ||
} | ||
|
||
boolean allNumericInputs = true; | ||
|
||
for (final String requiredBinding : requiredBindingsList) { | ||
final ColumnCapabilities capabilities = inspector.getColumnCapabilities(requiredBinding); | ||
if (capabilities == null) { | ||
return false; | ||
} | ||
|
||
allNumericInputs = allNumericInputs && capabilities.isNumeric(); | ||
|
||
if (!capabilities.isNumeric() && !capabilities.isDictionaryEncoded().isTrue()) { | ||
// Not fixed-width. | ||
return false; | ||
} | ||
} | ||
|
||
return !allNumericInputs || (plan.getOutputType() != null && !plan.getOutputType().isNumeric()); | ||
} | ||
}, | ||
|
||
ALWAYS("always") { | ||
@Override | ||
public boolean useDeferredGroupBySelector( | ||
ExpressionPlan plan, | ||
List<String> requiredBindingsList, | ||
ColumnInspector inspector | ||
) | ||
{ | ||
return !isInnatelyDeferrable(plan, requiredBindingsList, inspector); | ||
} | ||
}; | ||
|
||
public static final String JSON_KEY = "deferExpressionDimensions"; | ||
|
||
private final String jsonName; | ||
|
||
DeferExpressionDimensions(String jsonName) | ||
{ | ||
this.jsonName = jsonName; | ||
} | ||
|
||
@JsonCreator | ||
public static DeferExpressionDimensions fromString(final String jsonName) | ||
{ | ||
for (final DeferExpressionDimensions value : values()) { | ||
if (value.jsonName.equals(jsonName)) { | ||
return value; | ||
} | ||
} | ||
|
||
throw new IAE("Invalid value[%s] for[%s]", jsonName, JSON_KEY); | ||
} | ||
|
||
public abstract boolean useDeferredGroupBySelector( | ||
ExpressionPlan plan, | ||
List<String> requiredBindingsList, | ||
ColumnInspector inspector | ||
); | ||
|
||
@Override | ||
@JsonValue | ||
public String toString() | ||
{ | ||
return jsonName; | ||
} | ||
|
||
/** | ||
* Whether the given expression can be deferred innately by the selector created by | ||
* {@link ExpressionVirtualColumn#makeSingleValueVectorDimensionSelector(DimensionSpec, VectorColumnSelectorFactory)}. | ||
* | ||
* In this case, all options for this enum return false from | ||
* {@link #useDeferredGroupBySelector(ExpressionPlan, List, ColumnInspector)}, because there is no need to defer | ||
* redundantly. | ||
*/ | ||
private static boolean isInnatelyDeferrable( | ||
ExpressionPlan plan, | ||
List<String> requiredBindingsList, | ||
ColumnInspector inspector | ||
) | ||
{ | ||
if (plan.getOutputType() != null | ||
&& plan.getOutputType().is(ExprType.STRING) | ||
&& requiredBindingsList.size() <= 1) { | ||
for (final String requiredBinding : requiredBindingsList) { | ||
final ColumnCapabilities requiredBindingCapabilities = inspector.getColumnCapabilities(requiredBinding); | ||
|
||
if (requiredBindingCapabilities == null | ||
|| !requiredBindingCapabilities.is(ValueType.STRING) | ||
|| !requiredBindingCapabilities.isDictionaryEncoded().isTrue()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.