diff --git a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/server/ServerPlanRequestUtils.java b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/server/ServerPlanRequestUtils.java index 3fd454b8a474..18c15bbf000a 100644 --- a/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/server/ServerPlanRequestUtils.java +++ b/pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/server/ServerPlanRequestUtils.java @@ -308,14 +308,14 @@ static void attachDynamicFilter(PinotQuery pinotQuery, List leftKeys, L List expressions = new ArrayList<>(); for (int i = 0; i < leftKeys.size(); i++) { Expression leftExpr = pinotQuery.getSelectList().get(leftKeys.get(i)); - if (dataContainer.isEmpty()) { + List 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 operands = new ArrayList<>(dataContainer.size() + 1); + List 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)); } } @@ -333,13 +333,24 @@ static void attachDynamicFilter(PinotQuery pinotQuery, List leftKeys, L private static List computeInOperands(List 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 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 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++) { @@ -349,7 +360,7 @@ private static List computeInOperands(List 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++) { @@ -359,7 +370,7 @@ private static List computeInOperands(List 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++) { @@ -369,7 +380,7 @@ private static List computeInOperands(List 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++) { @@ -379,7 +390,7 @@ private static List computeInOperands(List 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++) { @@ -389,7 +400,7 @@ private static List computeInOperands(List 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++) { @@ -399,7 +410,7 @@ private static List computeInOperands(List 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++) { diff --git a/pinot-query-runtime/src/test/resources/queries/SemiJoins.json b/pinot-query-runtime/src/test/resources/queries/SemiJoins.json index 4dc851aca989..b9a7a3d8dc09 100644 --- a/pinot-query-runtime/src/test/resources/queries/SemiJoins.json +++ b/pinot-query-runtime/src/test/resources/queries/SemiJoins.json @@ -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": [] + } + ] } }