Skip to content

Commit

Permalink
Bugfix. Maintain literal data type during function evaluation (#12607)
Browse files Browse the repository at this point in the history
  • Loading branch information
shounakmk219 authored Mar 8, 2024
1 parent 71cd29f commit 499b661
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public InbuiltFunctionEvaluator(String functionExpression) {
private ExecutableNode planExecution(ExpressionContext expression) {
switch (expression.getType()) {
case LITERAL:
// TODO: pass literal with type into ConstantExecutionNode.
return new ConstantExecutionNode(expression.getLiteral().getStringValue());
return new ConstantExecutionNode(expression.getLiteral().getValue());
case IDENTIFIER:
String columnName = expression.getIdentifier();
ColumnExecutionNode columnExecutionNode = new ColumnExecutionNode(columnName, _arguments.size());
Expand Down Expand Up @@ -276,14 +275,14 @@ public String toString() {
}

private static class ConstantExecutionNode implements ExecutableNode {
final String _value;
final Object _value;

ConstantExecutionNode(String value) {
ConstantExecutionNode(Object value) {
_value = value;
}

@Override
public String execute(GenericRow row) {
public Object execute(GenericRow row) {
return _value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.segment.local.function;

import org.apache.pinot.common.function.FunctionUtils;
import org.apache.pinot.common.utils.PinotDataType;
import org.apache.pinot.spi.data.readers.GenericRow;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;


public class InbuiltFunctionEvaluatorTest {

@Test
public void booleanLiteralTest() {
checkBooleanLiteralExpression("true", 1);
checkBooleanLiteralExpression("false", 0);
checkBooleanLiteralExpression("True", 1);
checkBooleanLiteralExpression("False", 0);
checkBooleanLiteralExpression("1", 1);
checkBooleanLiteralExpression("0", 0);
}

private void checkBooleanLiteralExpression(String expression, int value) {
InbuiltFunctionEvaluator evaluator = new InbuiltFunctionEvaluator(expression);
Object output = evaluator.evaluate(new GenericRow());
Class<?> outputValueClass = output.getClass();
PinotDataType outputType = FunctionUtils.getArgumentType(outputValueClass);
assertNotNull(outputType);
// as INT is the stored type for BOOLEAN
assertEquals(outputType.toInt(output), value);
}
}

0 comments on commit 499b661

Please sign in to comment.