Skip to content
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

Case order bug #8748

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.pinot.core.operator.blocks.ProjectionBlock;
Expand Down Expand Up @@ -86,13 +87,13 @@ public void init(List<TransformFunction> arguments, Map<String, DataSource> data
for (int i = 0; i < numWhenStatements; i++) {
_whenStatements.add(arguments.get(i));
}
// Add ELSE Statement first
_elseThenStatements = new ArrayList<>(numWhenStatements + 1);
_elseThenStatements.add(arguments.get(numWhenStatements * 2));
for (int i = numWhenStatements; i < numWhenStatements * 2; i++) {
for (int i = numWhenStatements; i < numWhenStatements * 2 + 1; i++) {
_elseThenStatements.add(arguments.get(i));
}
_selections = new boolean[_elseThenStatements.size()];
Collections.reverse(_elseThenStatements);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here to indicate the reasoning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, I'll follow up with cosmetic enhancements but want to get this merged quickly given the nature of the bug

Collections.reverse(_whenStatements);
_resultMetadata = calculateResultMetadata();
}

Expand Down Expand Up @@ -212,7 +213,8 @@ public TransformResultMetadata getResultMetadata() {

/**
* Evaluate the ProjectionBlock for the WHEN statements, returns an array with the
* index(1 to N) of matched WHEN clause, 0 means nothing matched, so go to ELSE.
* index(1 to N) of matched WHEN clause ordered by match priority, 0 means nothing
* matched, so go to ELSE.
*/
private int[] getSelectedArray(ProjectionBlock projectionBlock) {
int numDocs = projectionBlock.getNumDocs();
Expand All @@ -228,9 +230,12 @@ private int[] getSelectedArray(ProjectionBlock projectionBlock) {
int[] conditions = whenStatement.transformToIntValuesSV(projectionBlock);
for (int j = 0; j < numDocs & j < conditions.length; j++) {
_selectedResults[j] = Math.max(conditions[j] * (i + 1), _selectedResults[j]);
_selections[_selectedResults[j]] = true;
}
}
// try to prune clauses now
for (int i = 0; i < numDocs; i++) {
_selections[_selectedResults[i]] = true;
}
int numSelections = 0;
for (boolean selection : _selections) {
if (selection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Stream;
import org.apache.pinot.common.function.TransformFunctionType;
import org.apache.pinot.common.request.context.ExpressionContext;
import org.apache.pinot.common.request.context.RequestContextUtils;
import org.apache.pinot.spi.data.FieldSpec.DataType;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;


public class CaseTransformFunctionTest extends BaseTransformFunctionTest {
private static final int INDEX_TO_COMPARE = new Random(System.currentTimeMillis()).nextInt(NUM_ROWS);
Expand All @@ -37,6 +41,54 @@ public class CaseTransformFunctionTest extends BaseTransformFunctionTest {
TransformFunctionType.LESS_THAN_OR_EQUAL
};

@DataProvider
public Object[][] params() {
return Stream.of(INT_SV_COLUMN, LONG_SV_COLUMN, FLOAT_SV_COLUMN, DOUBLE_SV_COLUMN)
.flatMap(col -> Stream.of(
new int[] {3, 2, 1},
new int[] {1, 2, 3},
new int[] {Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 4, 0},
new int[] {0, Integer.MAX_VALUE / 4, Integer.MAX_VALUE / 2},
new int[] {0, Integer.MIN_VALUE / 4, Integer.MIN_VALUE},
new int[] {Integer.MIN_VALUE, 0, 1},
new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE, 1},
new int[] {Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2}
).map(thresholds -> new Object[]{col, thresholds[0], thresholds[1], thresholds[2]}))
.toArray(Object[][]::new);
}

@Test(dataProvider = "params")
public void testCasePriorityObserved(String column, int threshold1, int threshold2, int threshold3) {
String statement = String.format("CASE WHEN %s > %d THEN 3 WHEN %s > %d THEN 2 WHEN %s > %d THEN 1 ELSE -1 END",
column, threshold1, column, threshold2, column, threshold3);
ExpressionContext expression = RequestContextUtils.getExpression(statement);
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap);
int[] expectedIntResults = new int[NUM_ROWS];
for (int i = 0; i < expectedIntResults.length; i++) {
switch (column) {
case INT_SV_COLUMN:
expectedIntResults[i] = _intSVValues[i] > threshold1 ? 3
: _intSVValues[i] > threshold2 ? 2 : _intSVValues[i] > threshold3 ? 1 : -1;
break;
case LONG_SV_COLUMN:
expectedIntResults[i] = _longSVValues[i] > threshold1 ? 3
: _longSVValues[i] > threshold2 ? 2 : _longSVValues[i] > threshold3 ? 1 : -1;
break;
case FLOAT_SV_COLUMN:
expectedIntResults[i] = _floatSVValues[i] > threshold1 ? 3
: _floatSVValues[i] > threshold2 ? 2 : _floatSVValues[i] > threshold3 ? 1 : -1;
break;
case DOUBLE_SV_COLUMN:
expectedIntResults[i] = _doubleSVValues[i] > threshold1 ? 3
: _doubleSVValues[i] > threshold2 ? 2 : _doubleSVValues[i] > threshold3 ? 1 : -1;
break;
default:
}
}
int[] intValues = transformFunction.transformToIntValuesSV(_projectionBlock);
assertEquals(expectedIntResults, intValues);
}

@Test
public void testCaseTransformFunctionWithIntResults() {
int[] expectedIntResults = new int[NUM_ROWS];
Expand Down Expand Up @@ -149,8 +201,8 @@ private void testCaseQueryWithIntResults(String predicate, int[] expectedValues)
RequestContextUtils.getExpression(String.format("CASE WHEN %s THEN 100 ELSE 10 END", predicate));
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap);
Assert.assertTrue(transformFunction instanceof CaseTransformFunction);
Assert.assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
Assert.assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.INT);
assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.INT);
testTransformFunction(transformFunction, expectedValues);
}

Expand All @@ -159,8 +211,8 @@ private void testCaseQueryWithFloatResults(String predicate, float[] expectedVal
RequestContextUtils.getExpression(String.format("CASE WHEN %s THEN 100.0 ELSE 10.0 END", predicate));
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap);
Assert.assertTrue(transformFunction instanceof CaseTransformFunction);
Assert.assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
Assert.assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.FLOAT);
assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.FLOAT);
testTransformFunction(transformFunction, expectedValues);
}

Expand All @@ -170,8 +222,8 @@ private void testCaseQueryWithBigDecimalResults(String predicate, BigDecimal[] e
String.format("CASE WHEN %s THEN '100.99887766554433221' ELSE '10.1122334455667788909' END", predicate));
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap);
Assert.assertTrue(transformFunction instanceof CaseTransformFunction);
Assert.assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
Assert.assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.BIG_DECIMAL);
assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.BIG_DECIMAL);
testTransformFunction(transformFunction, expectedValues);
}

Expand All @@ -180,8 +232,8 @@ private void testCaseQueryWithStringResults(String predicate, String[] expectedV
RequestContextUtils.getExpression(String.format("CASE WHEN %s THEN 'aaa' ELSE 'bbb' END", predicate));
TransformFunction transformFunction = TransformFunctionFactory.get(expression, _dataSourceMap);
Assert.assertTrue(transformFunction instanceof CaseTransformFunction);
Assert.assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
Assert.assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.STRING);
assertEquals(transformFunction.getName(), CaseTransformFunction.FUNCTION_NAME);
assertEquals(transformFunction.getResultMetadata().getDataType(), DataType.STRING);
testTransformFunction(transformFunction, expectedValues);
}

Expand Down