Skip to content

Commit

Permalink
Issue apache#12367
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya0811 committed Apr 29, 2024
1 parent 7b68aa3 commit e5ad786
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.testng.annotations.Test;

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


public class CaseTransformFunctionTest extends BaseTransformFunctionTest {
Expand Down Expand Up @@ -106,7 +107,8 @@ public void testCaseTransformFunctionWithIntResults() {
testCaseQueries(String.format("%s(%s, %s)", functionType.getName(), LONG_SV_COLUMN,
String.format("%d", _longSVValues[INDEX_TO_COMPARE])), getPredicateResults(LONG_SV_COLUMN, functionType));
testCaseQueries(String.format("%s(%s, %s)", functionType.getName(), FLOAT_SV_COLUMN,
String.format("%f", _floatSVValues[INDEX_TO_COMPARE])), getPredicateResults(FLOAT_SV_COLUMN, functionType));
"CAST(" + String.format("%f", _floatSVValues[INDEX_TO_COMPARE]) + " AS FLOAT)"),
getPredicateResults(FLOAT_SV_COLUMN, functionType));
testCaseQueries(String.format("%s(%s, %s)", functionType.getName(), DOUBLE_SV_COLUMN,
String.format("%.20f", _doubleSVValues[INDEX_TO_COMPARE])),
getPredicateResults(DOUBLE_SV_COLUMN, functionType));
Expand All @@ -116,6 +118,33 @@ public void testCaseTransformFunctionWithIntResults() {
}
}

@Test
public void testCaseTransformFunctionWithoutCastForFloatValues() {
boolean[] predicateResults = new boolean[1];
Arrays.fill(predicateResults, true);
int[] expectedValues = new int[1];
int index = -1;
for (int i = 0; i < NUM_ROWS; i++) {
if (Double.compare(_floatSVValues[i], Double.parseDouble(String.format("%f", _floatSVValues[i]))) != 0) {
index = i;
expectedValues[0] = predicateResults[0] ? _intSVValues[i] : 10;
break;
}
}

if (index != -1) {
String predicate = String.format("%s(%s, %s)", TransformFunctionType.EQUALS, FLOAT_SV_COLUMN,
String.format("%f", _floatSVValues[index]));
String expression = String.format("CASE WHEN %s THEN %s ELSE 10 END", predicate, INT_SV_COLUMN);
ExpressionContext expressionContext = RequestContextUtils.getExpression(expression);
TransformFunction transformFunction = TransformFunctionFactory.get(expressionContext, _dataSourceMap);
Assert.assertTrue(transformFunction instanceof CaseTransformFunction);
assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.INT);
int[] intValues = transformFunction.transformToIntValuesSV(_projectionBlock);
assertNotEquals(intValues[index], expectedValues[0]);
}
}

@DataProvider
public static String[] illegalExpressions() {
//@formatter:off
Expand Down

0 comments on commit e5ad786

Please sign in to comment.