-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement ARRAYLENGTH UDF for multi-valued columns #5301
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* 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; | ||
|
||
|
||
public class LengthTransformFunction extends BaseTransformFunction { | ||
public static final String FUNCTION_NAME = "length"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this applies to multi-valued column, It might be better to name this array_length or cardinality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion. Changed this to arrayLength so people do not confuse it with the length function for strings. |
||
|
||
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 LENGTH 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 LENGTH 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* 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 LengthTransformFunctionTest extends BaseTransformFunctionTest { | ||
|
||
@Test | ||
public void testLengthTransformFunction() { | ||
TransformExpressionTree expression = | ||
TransformExpressionTree.compileToExpressionTree(String.format("length(%s)", INT_MV_COLUMN)); | ||
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap); | ||
Assert.assertTrue(transformFunction instanceof LengthTransformFunction); | ||
Assert.assertEquals(transformFunction.getName(), LengthTransformFunction.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("length(%s,1)", | ||
INT_MV_COLUMN)}, new Object[]{"length(2)"}, new Object[]{String.format("length(%s)", LONG_SV_COLUMN)}}; | ||
} | ||
} |
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.
Add java docs with a sample query.
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.
Done.