-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Feature] Support Coalesce for Column Names #9327
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
959d32c
test
814c237
coalesce
407c29f
use bitmap from projection and check argument types
0aa8d30
Handle different types of Coalesce
015d94f
add double float support
43ac34c
address comments
69589be
Merge branch 'master' into coalesce
walterddr 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
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
369 changes: 369 additions & 0 deletions
369
...ain/java/org/apache/pinot/core/operator/transform/function/CoalesceTransformFunction.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,369 @@ | ||
| /** | ||
| * 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.pinot.core.operator.transform.function; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.pinot.common.function.TransformFunctionType; | ||
| import org.apache.pinot.core.operator.blocks.ProjectionBlock; | ||
| import org.apache.pinot.core.operator.transform.TransformResultMetadata; | ||
| import org.apache.pinot.segment.spi.datasource.DataSource; | ||
| import org.apache.pinot.spi.data.FieldSpec; | ||
| import org.apache.pinot.spi.utils.NullValueUtils; | ||
| import org.roaringbitmap.RoaringBitmap; | ||
|
|
||
|
|
||
| /** | ||
| * The <code>CoalesceTransformFunction</code> implements the Coalesce operator. | ||
| * | ||
| * The results are in String format for first non-null value in the argument list. | ||
| * If all arguments are null, return a 'null' string. | ||
| * Note: arguments have to be column names and single type. The type can be either numeric or string. | ||
| * Number of arguments has to be greater than 0. | ||
| * | ||
| * Expected result: | ||
| * Coalesce(nullColumn, columnA): columnA | ||
| * Coalesce(columnA, nullColumn): nullColumn | ||
| * Coalesce(nullColumnA, nullColumnB): "null" | ||
| * | ||
| * Note this operator only takes column names for now. | ||
| * SQL Syntax: | ||
| * Coalesce(columnA, columnB) | ||
| */ | ||
| public class CoalesceTransformFunction extends BaseTransformFunction { | ||
| private TransformFunction[] _transformFunctions; | ||
| private FieldSpec.DataType _storedType; | ||
| private TransformResultMetadata _resultMetadata; | ||
|
|
||
| /** | ||
| * Returns a bit map of corresponding column. | ||
| * Returns an empty bitmap by default if null option is disabled. | ||
| */ | ||
| private static RoaringBitmap[] getNullBitMaps(ProjectionBlock projectionBlock, | ||
| TransformFunction[] transformFunctions) { | ||
| RoaringBitmap[] roaringBitmaps = new RoaringBitmap[transformFunctions.length]; | ||
| for (int i = 0; i < roaringBitmaps.length; i++) { | ||
| TransformFunction func = transformFunctions[i]; | ||
| String columnName = ((IdentifierTransformFunction) func).getColumnName(); | ||
| RoaringBitmap nullBitmap = projectionBlock.getBlockValueSet(columnName).getNullBitmap(); | ||
| roaringBitmaps[i] = nullBitmap; | ||
| } | ||
| return roaringBitmaps; | ||
| } | ||
|
|
||
| /** | ||
| * Get transform int results based on store type. | ||
| * @param projectionBlock | ||
| */ | ||
| private int[] getIntTransformResults(ProjectionBlock projectionBlock) { | ||
| int length = projectionBlock.getNumDocs(); | ||
| int[] results = new int[length]; | ||
| int width = _transformFunctions.length; | ||
| RoaringBitmap[] nullBitMaps = getNullBitMaps(projectionBlock, _transformFunctions); | ||
| int[][] data = new int[width][length]; | ||
| RoaringBitmap filledData = new RoaringBitmap(); | ||
| for (int i = 0; i < length; i++) { | ||
| boolean hasNonNullValue = false; | ||
| for (int j = 0; j < width; j++) { | ||
| // Consider value as null only when null option is enabled. | ||
| if (nullBitMaps[j] != null && nullBitMaps[j].contains(i)) { | ||
| continue; | ||
| } | ||
| if (!filledData.contains(j)) { | ||
| filledData.add(j); | ||
| data[j] = _transformFunctions[j].transformToIntValuesSV(projectionBlock); | ||
| } | ||
| hasNonNullValue = true; | ||
| results[i] = data[j][i]; | ||
| break; | ||
| } | ||
| if (!hasNonNullValue) { | ||
| results[i] = (int) NullValueUtils.getDefaultNullValue(_storedType); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Get transform long results based on store type. | ||
| * @param projectionBlock | ||
| */ | ||
| private long[] getLongTransformResults(ProjectionBlock projectionBlock) { | ||
| int length = projectionBlock.getNumDocs(); | ||
| long[] results = new long[length]; | ||
| int width = _transformFunctions.length; | ||
| RoaringBitmap[] nullBitMaps = getNullBitMaps(projectionBlock, _transformFunctions); | ||
| long[][] data = new long[width][length]; | ||
| RoaringBitmap filledData = new RoaringBitmap(); // indicates whether certain column has be filled in data. | ||
| for (int i = 0; i < length; i++) { | ||
| boolean hasNonNullValue = false; | ||
| for (int j = 0; j < width; j++) { | ||
| // Consider value as null only when null option is enabled. | ||
| if (nullBitMaps[j] != null && nullBitMaps[j].contains(i)) { | ||
| continue; | ||
| } | ||
| if (!filledData.contains(j)) { | ||
| filledData.add(j); | ||
| data[j] = _transformFunctions[j].transformToLongValuesSV(projectionBlock); | ||
| } | ||
| hasNonNullValue = true; | ||
| results[i] = data[j][i]; | ||
| break; | ||
| } | ||
| if (!hasNonNullValue) { | ||
| results[i] = (long) NullValueUtils.getDefaultNullValue(_storedType); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Get transform float results based on store type. | ||
| * @param projectionBlock | ||
| */ | ||
| private float[] getFloatTransformResults(ProjectionBlock projectionBlock) { | ||
| int length = projectionBlock.getNumDocs(); | ||
| float[] results = new float[length]; | ||
| int width = _transformFunctions.length; | ||
| RoaringBitmap[] nullBitMaps = getNullBitMaps(projectionBlock, _transformFunctions); | ||
| float[][] data = new float[width][length]; | ||
| RoaringBitmap filledData = new RoaringBitmap(); // indicates whether certain column has be filled in data. | ||
| for (int i = 0; i < length; i++) { | ||
| boolean hasNonNullValue = false; | ||
| for (int j = 0; j < width; j++) { | ||
| // Consider value as null only when null option is enabled. | ||
| if (nullBitMaps[j] != null && nullBitMaps[j].contains(i)) { | ||
| continue; | ||
| } | ||
| if (!filledData.contains(j)) { | ||
| filledData.add(j); | ||
| data[j] = _transformFunctions[j].transformToFloatValuesSV(projectionBlock); | ||
| } | ||
| hasNonNullValue = true; | ||
| results[i] = data[j][i]; | ||
| break; | ||
| } | ||
| if (!hasNonNullValue) { | ||
| results[i] = (float) NullValueUtils.getDefaultNullValue(_storedType); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Get transform double results based on store type. | ||
| * @param projectionBlock | ||
| */ | ||
| private double[] getDoublelTransformResults(ProjectionBlock projectionBlock) { | ||
| int length = projectionBlock.getNumDocs(); | ||
| double[] results = new double[length]; | ||
| int width = _transformFunctions.length; | ||
| RoaringBitmap[] nullBitMaps = getNullBitMaps(projectionBlock, _transformFunctions); | ||
| double[][] data = new double[width][length]; | ||
| RoaringBitmap filledData = new RoaringBitmap(); // indicates whether certain column has be filled in data. | ||
| for (int i = 0; i < length; i++) { | ||
| boolean hasNonNullValue = false; | ||
| for (int j = 0; j < width; j++) { | ||
| // Consider value as null only when null option is enabled. | ||
| if (nullBitMaps[j] != null && nullBitMaps[j].contains(i)) { | ||
| continue; | ||
| } | ||
| if (!filledData.contains(j)) { | ||
| filledData.add(j); | ||
| data[j] = _transformFunctions[j].transformToDoubleValuesSV(projectionBlock); | ||
| } | ||
| hasNonNullValue = true; | ||
| results[i] = data[j][i]; | ||
| break; | ||
| } | ||
| if (!hasNonNullValue) { | ||
| results[i] = (double) NullValueUtils.getDefaultNullValue(_storedType); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Get transform BigDecimal results based on store type. | ||
| * @param projectionBlock | ||
| */ | ||
| private BigDecimal[] getBigDecimalTransformResults(ProjectionBlock projectionBlock) { | ||
| int length = projectionBlock.getNumDocs(); | ||
| BigDecimal[] results = new BigDecimal[length]; | ||
| int width = _transformFunctions.length; | ||
| RoaringBitmap[] nullBitMaps = getNullBitMaps(projectionBlock, _transformFunctions); | ||
| BigDecimal[][] data = new BigDecimal[width][length]; | ||
| RoaringBitmap filledData = new RoaringBitmap(); // indicates whether certain column has be filled in data. | ||
| for (int i = 0; i < length; i++) { | ||
| boolean hasNonNullValue = false; | ||
| for (int j = 0; j < width; j++) { | ||
| // Consider value as null only when null option is enabled. | ||
| if (nullBitMaps[j] != null && nullBitMaps[j].contains(i)) { | ||
| continue; | ||
| } | ||
| if (!filledData.contains(j)) { | ||
| filledData.add(j); | ||
| data[j] = _transformFunctions[j].transformToBigDecimalValuesSV(projectionBlock); | ||
| } | ||
| hasNonNullValue = true; | ||
| results[i] = data[j][i]; | ||
| break; | ||
| } | ||
| if (!hasNonNullValue) { | ||
| results[i] = (BigDecimal) NullValueUtils.getDefaultNullValue(_storedType); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Get transform String results based on store type. | ||
| * @param projectionBlock | ||
| */ | ||
| private String[] getStringTransformResults(ProjectionBlock projectionBlock) { | ||
| int length = projectionBlock.getNumDocs(); | ||
| String[] results = new String[length]; | ||
| int width = _transformFunctions.length; | ||
| RoaringBitmap[] nullBitMaps = getNullBitMaps(projectionBlock, _transformFunctions); | ||
| String[][] data = new String[width][length]; | ||
| RoaringBitmap filledData = new RoaringBitmap(); // indicates whether certain column has be filled in data. | ||
| for (int i = 0; i < length; i++) { | ||
| boolean hasNonNullValue = false; | ||
| for (int j = 0; j < width; j++) { | ||
| // Consider value as null only when null option is enabled. | ||
| if (nullBitMaps[j] != null && nullBitMaps[j].contains(i)) { | ||
| continue; | ||
| } | ||
| if (!filledData.contains(j)) { | ||
| filledData.add(j); | ||
| data[j] = _transformFunctions[j].transformToStringValuesSV(projectionBlock); | ||
| } | ||
| hasNonNullValue = true; | ||
| results[i] = data[j][i]; | ||
| break; | ||
| } | ||
| if (!hasNonNullValue) { | ||
| results[i] = (String) NullValueUtils.getDefaultNullValue(_storedType); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return TransformFunctionType.COALESCE.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) { | ||
| int argSize = arguments.size(); | ||
| Preconditions.checkArgument(argSize > 0, "COALESCE needs to have at least one argument."); | ||
| _transformFunctions = new TransformFunction[argSize]; | ||
| for (int i = 0; i < argSize; i++) { | ||
| TransformFunction func = arguments.get(i); | ||
| Preconditions.checkArgument(func instanceof IdentifierTransformFunction, | ||
| "Only column names are supported in COALESCE."); | ||
| FieldSpec.DataType storedType = func.getResultMetadata().getDataType().getStoredType(); | ||
| if (_storedType == null) { | ||
| _storedType = storedType; | ||
| } else { | ||
| Preconditions.checkArgument(storedType.equals(_storedType), "Argument types have to be the same."); | ||
| } | ||
| _transformFunctions[i] = func; | ||
| } | ||
| switch (_storedType) { | ||
| case INT: | ||
| _resultMetadata = INT_SV_NO_DICTIONARY_METADATA; | ||
| break; | ||
| case LONG: | ||
| _resultMetadata = LONG_SV_NO_DICTIONARY_METADATA; | ||
| break; | ||
| case FLOAT: | ||
| _resultMetadata = FLOAT_SV_NO_DICTIONARY_METADATA; | ||
| break; | ||
| case DOUBLE: | ||
| _resultMetadata = DOUBLE_SV_NO_DICTIONARY_METADATA; | ||
| break; | ||
| case BIG_DECIMAL: | ||
| _resultMetadata = BIG_DECIMAL_SV_NO_DICTIONARY_METADATA; | ||
| break; | ||
| case STRING: | ||
| _resultMetadata = STRING_SV_NO_DICTIONARY_METADATA; | ||
| break; | ||
| default: | ||
| throw new UnsupportedOperationException("Coalesce only supports numerical and string data type"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public TransformResultMetadata getResultMetadata() { | ||
| return _resultMetadata; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] transformToStringValuesSV(ProjectionBlock projectionBlock) { | ||
|
||
| if (_storedType != FieldSpec.DataType.STRING) { | ||
| return super.transformToStringValuesSV(projectionBlock); | ||
| } | ||
| return getStringTransformResults(projectionBlock); | ||
| } | ||
|
|
||
| @Override | ||
| public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) { | ||
| if (_storedType != FieldSpec.DataType.INT) { | ||
| return super.transformToIntValuesSV(projectionBlock); | ||
| } | ||
| return getIntTransformResults(projectionBlock); | ||
| } | ||
|
|
||
| @Override | ||
| public long[] transformToLongValuesSV(ProjectionBlock projectionBlock) { | ||
| if (_storedType != FieldSpec.DataType.LONG) { | ||
| return super.transformToLongValuesSV(projectionBlock); | ||
| } | ||
| return getLongTransformResults(projectionBlock); | ||
| } | ||
|
|
||
| @Override | ||
| public BigDecimal[] transformToBigDecimalValuesSV(ProjectionBlock projectionBlock) { | ||
| if (_storedType != FieldSpec.DataType.BIG_DECIMAL) { | ||
| return super.transformToBigDecimalValuesSV(projectionBlock); | ||
| } | ||
| return getBigDecimalTransformResults(projectionBlock); | ||
| } | ||
|
|
||
| @Override | ||
| public double[] transformToDoubleValuesSV(ProjectionBlock projectionBlock) { | ||
| if (_storedType != FieldSpec.DataType.DOUBLE) { | ||
| return super.transformToDoubleValuesSV(projectionBlock); | ||
| } | ||
| return getDoublelTransformResults(projectionBlock); | ||
| } | ||
|
|
||
| @Override | ||
| public float[] transformToFloatValuesSV(ProjectionBlock projectionBlock) { | ||
| if (_storedType != FieldSpec.DataType.FLOAT) { | ||
| return super.transformToFloatValuesSV(projectionBlock); | ||
| } | ||
| return getFloatTransformResults(projectionBlock); | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think implicitly treating everything as STRING will give confusing semantics imo. If we don't want to support this function on few data types, then throwing exception is better.
Reference - https://docs.snowflake.com/en/sql-reference/functions/coalesce.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. For the first version, we can check if all the input are numbers or strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a check for argument type. but the return value has to be a string, otherwise we are not able to represent null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Currently we don't support
nullvalues for transform function yet. You may readTransformBlockValSet.getNullBitmap(), which count the result asnullwhen any argument isnull. This won't work forcoalesce.For now, we may use
NullValueUtils.getDefaultNullValue()for each data type to present the null. Then we should figure out a way to support the realnullin transform function.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(edited) represented using default nullvalue probably is not a good idea since all numeric data type uses zero as default null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walterddr Not really. We use min value as the default for numeric types except for big decimal, which doesn't have a min value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use NullValueUtils.getDefaultNullValue() and only supports numeric and string types. Also it requires the type to be same for all arguments.