Skip to content
Open
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 @@ -308,14 +308,14 @@ static void attachDynamicFilter(PinotQuery pinotQuery, List<Integer> leftKeys, L
List<Expression> expressions = new ArrayList<>();
for (int i = 0; i < leftKeys.size(); i++) {
Expression leftExpr = pinotQuery.getSelectList().get(leftKeys.get(i));
if (dataContainer.isEmpty()) {
List<Expression> inOperands = computeInOperands(dataContainer, dataSchema, rightKeys.get(i));
if (inOperands.isEmpty()) {
// put a constant false expression
expressions.add(RequestUtils.getLiteralExpression(false));
} else {
int rightIdx = rightKeys.get(i);
List<Expression> operands = new ArrayList<>(dataContainer.size() + 1);
List<Expression> operands = new ArrayList<>(inOperands.size() + 1);
operands.add(leftExpr);
operands.addAll(computeInOperands(dataContainer, dataSchema, rightIdx));
operands.addAll(inOperands);
expressions.add(RequestUtils.getFunctionExpression(FilterKind.IN.name(), operands));
}
}
Expand All @@ -333,13 +333,24 @@ static void attachDynamicFilter(PinotQuery pinotQuery, List<Integer> leftKeys, L
private static List<Expression> computeInOperands(List<Object[]> dataContainer, DataSchema dataSchema, int colIdx) {
final DataSchema.ColumnDataType columnDataType = dataSchema.getColumnDataType(colIdx);
final FieldSpec.DataType storedType = columnDataType.getStoredType().toDataType();
final int numRows = dataContainer.size();
// A null key never matches in the semi join, so it is left out of the IN list
List<Object> values = new ArrayList<>(dataContainer.size());
for (Object[] row : dataContainer) {
Object value = row[colIdx];
if (value != null) {
values.add(value);
}
}
final int numRows = values.size();
if (numRows == 0) {
return List.of();
}
List<Expression> expressions = new ArrayList<>();
switch (storedType) {
case INT:
int[] arrInt = new int[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrInt[rowIdx] = (int) dataContainer.get(rowIdx)[colIdx];
arrInt[rowIdx] = (int) values.get(rowIdx);
}
Arrays.sort(arrInt);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand All @@ -349,7 +360,7 @@ private static List<Expression> computeInOperands(List<Object[]> dataContainer,
case LONG:
long[] arrLong = new long[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrLong[rowIdx] = (long) dataContainer.get(rowIdx)[colIdx];
arrLong[rowIdx] = (long) values.get(rowIdx);
}
Arrays.sort(arrLong);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand All @@ -359,7 +370,7 @@ private static List<Expression> computeInOperands(List<Object[]> dataContainer,
case FLOAT:
float[] arrFloat = new float[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrFloat[rowIdx] = (float) dataContainer.get(rowIdx)[colIdx];
arrFloat[rowIdx] = (float) values.get(rowIdx);
}
Arrays.sort(arrFloat);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand All @@ -369,7 +380,7 @@ private static List<Expression> computeInOperands(List<Object[]> dataContainer,
case DOUBLE:
double[] arrDouble = new double[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrDouble[rowIdx] = (double) dataContainer.get(rowIdx)[colIdx];
arrDouble[rowIdx] = (double) values.get(rowIdx);
}
Arrays.sort(arrDouble);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand All @@ -379,7 +390,7 @@ private static List<Expression> computeInOperands(List<Object[]> dataContainer,
case STRING:
String[] arrString = new String[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrString[rowIdx] = (String) dataContainer.get(rowIdx)[colIdx];
arrString[rowIdx] = (String) values.get(rowIdx);
}
Arrays.sort(arrString);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand All @@ -389,7 +400,7 @@ private static List<Expression> computeInOperands(List<Object[]> dataContainer,
case BIG_DECIMAL:
BigDecimal[] arrBigDecimal = new BigDecimal[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrBigDecimal[rowIdx] = (BigDecimal) dataContainer.get(rowIdx)[colIdx];
arrBigDecimal[rowIdx] = (BigDecimal) values.get(rowIdx);
}
Arrays.sort(arrBigDecimal);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand All @@ -399,7 +410,7 @@ private static List<Expression> computeInOperands(List<Object[]> dataContainer,
case BYTES:
ByteArray[] arrBytes = new ByteArray[numRows];
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
arrBytes[rowIdx] = (ByteArray) dataContainer.get(rowIdx)[colIdx];
arrBytes[rowIdx] = (ByteArray) values.get(rowIdx);
}
Arrays.sort(arrBytes);
for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
Expand Down
73 changes: 73 additions & 0 deletions pinot-query-runtime/src/test/resources/queries/SemiJoins.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,78 @@
]
}
]
},
"semi_join_null_keys": {
"extraProps": {
"enableColumnBasedNullHandling": true
},
"tables": {
"tbl": {
"schema": [
{"name": "int_col", "type": "INT"},
{"name": "long_col", "type": "LONG"},
{"name": "float_col", "type": "FLOAT"},
{"name": "double_col", "type": "DOUBLE"},
{"name": "string_col", "type": "STRING"},
{"name": "big_decimal_col", "type": "BIG_DECIMAL"},
{"name": "bytes_col", "type": "BYTES"},
{"name": "group_col", "type": "STRING"}
],
"inputs": [
[1, 1, 1.0, 1.0, "a", 1.0, "AA", "x"],
[2, 2, 2.0, 2.0, "b", 2.0, "BB", "y"],
[3, 3, 3.0, 3.0, "c", 3.0, "CC", "x"],
[null, null, null, null, null, null, null, "x"],
[null, null, null, null, null, null, null, "z"]
]
}
},
"queries": [
{
"description": "Semi join using IN clause with INT when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT int_col FROM {tbl} WHERE int_col IN (SELECT int_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [[1], [3]]
},
{
"description": "Semi join using IN clause with LONG when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT long_col FROM {tbl} WHERE long_col IN (SELECT long_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [[1], [3]]
},
{
"description": "Semi join using IN clause with FLOAT when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT float_col FROM {tbl} WHERE float_col IN (SELECT float_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [[1.0], [3.0]]
},
{
"description": "Semi join using IN clause with DOUBLE when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT double_col FROM {tbl} WHERE double_col IN (SELECT double_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [[1.0], [3.0]]
},
{
"description": "Semi join using IN clause with STRING when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT string_col FROM {tbl} WHERE string_col IN (SELECT string_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [["a"], ["c"]]
},
{
"description": "Semi join using IN clause with BIG_DECIMAL when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT big_decimal_col FROM {tbl} WHERE big_decimal_col IN (SELECT big_decimal_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [["1.0"], ["3.0"]]
},
{
"description": "Semi join using IN clause with BYTES when the subquery returns a null key",
"sql": "SET enableNullHandling=true; SELECT bytes_col FROM {tbl} WHERE bytes_col IN (SELECT bytes_col FROM {tbl} WHERE group_col = 'x')",
"outputs": [["aa"], ["cc"]]
},
{
"description": "Semi join using IN clause when the subquery returns only null keys",
"sql": "SET enableNullHandling=true; SELECT int_col FROM {tbl} WHERE int_col IN (SELECT int_col FROM {tbl} WHERE group_col = 'z')",
"outputs": []
},
{
"description": "Semi join using IN clause with STRING when the subquery returns only null keys",
"sql": "SET enableNullHandling=true; SELECT string_col FROM {tbl} WHERE string_col IN (SELECT string_col FROM {tbl} WHERE group_col = 'z')",
"outputs": []
}
]
}
}