-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ARRAYLENGTH UDF for multi-valued columns (#5301)
* Implement LENGTH UDF for multi-valued columns * Add integration test for LENGTH UDF * Fix a typo * Rename Length UDF to ArrayLength
- Loading branch information
1 parent
90c9060
commit 1fe22b5
Showing
7 changed files
with
202 additions
and
0 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
112 changes: 112 additions & 0 deletions
112
.../java/org/apache/pinot/core/operator/transform/function/ArrayLengthTransformFunction.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,112 @@ | ||
/** | ||
* 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 java.util.List; | ||
import java.util.Map; | ||
import org.apache.pinot.core.common.DataSource; | ||
import org.apache.pinot.core.operator.blocks.ProjectionBlock; | ||
import org.apache.pinot.core.operator.transform.TransformResultMetadata; | ||
import org.apache.pinot.core.plan.DocIdSetPlanNode; | ||
|
||
|
||
/** | ||
* The ArrayLengthTransformFunction class implements arrayLength function for multi-valued columns | ||
* | ||
* Sample queries: | ||
* SELECT COUNT(*) FROM table WHERE arrayLength(mvColumn) > 2 | ||
* SELECT COUNT(*) FROM table GROUP BY arrayLength(mvColumn) | ||
* SELECT MAX(arrayLength(mvColumn)) FROM table | ||
*/ | ||
public class ArrayLengthTransformFunction extends BaseTransformFunction { | ||
public static final String FUNCTION_NAME = "arrayLength"; | ||
|
||
private int[] _results; | ||
private TransformFunction _argument; | ||
|
||
@Override | ||
public String getName() { | ||
return FUNCTION_NAME; | ||
} | ||
|
||
@Override | ||
public void init(List<TransformFunction> arguments, Map<String, DataSource> dataSourceMap) { | ||
// Check that there is only 1 argument | ||
if (arguments.size() != 1) { | ||
throw new IllegalArgumentException("Exactly 1 argument is required for ARRAYLENGTH transform function"); | ||
} | ||
|
||
// Check that the argument is a multi-valued column or transform function | ||
TransformFunction firstArgument = arguments.get(0); | ||
if (firstArgument instanceof LiteralTransformFunction || firstArgument.getResultMetadata().isSingleValue()) { | ||
throw new IllegalArgumentException( | ||
"The argument of ARRAYLENGTH transform function must be a multi-valued column or a transform function"); | ||
} | ||
_argument = firstArgument; | ||
} | ||
|
||
@Override | ||
public TransformResultMetadata getResultMetadata() { | ||
return INT_SV_NO_DICTIONARY_METADATA; | ||
} | ||
|
||
@Override | ||
public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) { | ||
if (_results == null) { | ||
_results = new int[DocIdSetPlanNode.MAX_DOC_PER_CALL]; | ||
} | ||
|
||
int numDocs = projectionBlock.getNumDocs(); | ||
switch (_argument.getResultMetadata().getDataType()) { | ||
case INT: | ||
int[][] intValuesMV = _argument.transformToIntValuesMV(projectionBlock); | ||
for (int i = 0; i < numDocs; i++) { | ||
_results[i] = intValuesMV[i].length; | ||
} | ||
break; | ||
case LONG: | ||
long[][] longValuesMV = _argument.transformToLongValuesMV(projectionBlock); | ||
for (int i = 0; i < numDocs; i++) { | ||
_results[i] = longValuesMV[i].length; | ||
} | ||
break; | ||
case FLOAT: | ||
float[][] floatValuesMV = _argument.transformToFloatValuesMV(projectionBlock); | ||
for (int i = 0; i < numDocs; i++) { | ||
_results[i] = floatValuesMV[i].length; | ||
} | ||
break; | ||
case DOUBLE: | ||
double[][] doubleValuesMV = _argument.transformToDoubleValuesMV(projectionBlock); | ||
for (int i = 0; i < numDocs; i++) { | ||
_results[i] = doubleValuesMV[i].length; | ||
} | ||
break; | ||
case STRING: | ||
String[][] stringValuesMV = _argument.transformToStringValuesMV(projectionBlock); | ||
for (int i = 0; i < numDocs; i++) { | ||
_results[i] = stringValuesMV[i].length; | ||
} | ||
break; | ||
default: | ||
throw new IllegalStateException(); | ||
} | ||
return _results; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...a/org/apache/pinot/core/operator/transform/function/ArrayLengthTransformFunctionTest.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,61 @@ | ||
/** | ||
* 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 org.apache.pinot.common.request.transform.TransformExpressionTree; | ||
import org.apache.pinot.core.query.exception.BadQueryRequestException; | ||
import org.apache.pinot.spi.data.FieldSpec; | ||
import org.testng.Assert; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
public class ArrayLengthTransformFunctionTest extends BaseTransformFunctionTest { | ||
|
||
@Test | ||
public void testLengthTransformFunction() { | ||
TransformExpressionTree expression = | ||
TransformExpressionTree.compileToExpressionTree(String.format("arrayLength(%s)", INT_MV_COLUMN)); | ||
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap); | ||
Assert.assertTrue(transformFunction instanceof ArrayLengthTransformFunction); | ||
Assert.assertEquals(transformFunction.getName(), ArrayLengthTransformFunction.FUNCTION_NAME); | ||
Assert.assertEquals(transformFunction.getResultMetadata().getDataType(), FieldSpec.DataType.INT); | ||
Assert.assertTrue(transformFunction.getResultMetadata().isSingleValue()); | ||
Assert.assertFalse(transformFunction.getResultMetadata().hasDictionary()); | ||
|
||
int[] results = transformFunction.transformToIntValuesSV(_projectionBlock); | ||
for (int i = 0; i < NUM_ROWS; i++) { | ||
Assert.assertEquals(results[i], _intMVValues[i].length); | ||
} | ||
} | ||
|
||
@Test(dataProvider = "testIllegalArguments", expectedExceptions = {BadQueryRequestException.class}) | ||
public void testIllegalArguments(String expressionStr) { | ||
TransformExpressionTree expression = TransformExpressionTree.compileToExpressionTree(expressionStr); | ||
TransformFunctionFactory.get(expression, _dataSourceMap); | ||
} | ||
|
||
@DataProvider(name = "testIllegalArguments") | ||
public Object[][] testIllegalArguments() { | ||
return new Object[][]{ | ||
new Object[]{String.format("arrayLength(%s,1)", INT_MV_COLUMN)}, | ||
new Object[]{"arrayLength(2)"}, | ||
new Object[]{String.format("arrayLength(%s)", LONG_SV_COLUMN)}}; | ||
} | ||
} |
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