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

use NullFilter for SQL rewrite of MV_CONTAINS and MV_OVERLAP for null array elements #15855

Merged
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 @@ -34,6 +34,7 @@
import org.apache.druid.query.filter.ArrayContainsElementFilter;
import org.apache.druid.query.filter.DimFilter;
import org.apache.druid.query.filter.EqualityFilter;
import org.apache.druid.query.filter.NullFilter;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.column.RowSignature;
import org.apache.druid.sql.calcite.expression.DruidExpression;
Expand Down Expand Up @@ -129,14 +130,18 @@ public DimFilter toDruidFilter(
leftExpr,
leftExpr.getDruidType()
);
filters.add(
new EqualityFilter(
column,
ExpressionType.toColumnType(ExpressionType.elementType(exprEval.type())),
val,
null
)
);
if (val == null) {
filters.add(NullFilter.forColumn(column));
} else {
filters.add(
new EqualityFilter(
column,
ExpressionType.toColumnType(ExpressionType.elementType(exprEval.type())),
val,
null
)
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.druid.query.filter.DimFilter;
import org.apache.druid.query.filter.EqualityFilter;
import org.apache.druid.query.filter.InDimFilter;
import org.apache.druid.query.filter.NullFilter;
import org.apache.druid.query.filter.OrDimFilter;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.column.RowSignature;
Expand Down Expand Up @@ -135,10 +136,14 @@ public DimFilter toDruidFilter(
simpleExtractionExpr,
simpleExtractionExpr.getDruidType()
);
final Object elementValue = arrayElements[0];
if (elementValue == null) {
return NullFilter.forColumn(column);
}
return new EqualityFilter(
column,
ExpressionType.toColumnType(exprEval.type()),
arrayElements[0],
elementValue,
null
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.druid.query.extraction.SubstringDimExtractionFn;
import org.apache.druid.query.filter.InDimFilter;
import org.apache.druid.query.filter.LikeDimFilter;
import org.apache.druid.query.filter.NullFilter;
import org.apache.druid.query.groupby.GroupByQuery;
import org.apache.druid.query.groupby.GroupByQueryConfig;
import org.apache.druid.query.groupby.orderby.DefaultLimitSpec;
Expand Down Expand Up @@ -285,6 +286,37 @@ public void testMultiValueStringOverlapFilter()
);
}

@Test
public void testMultiValueStringOverlapFilterNull()
{
testQuery(
"SELECT dim3 FROM druid.numfoo WHERE MV_OVERLAP(dim3, ARRAY[NULL]) LIMIT 5",
ImmutableList.of(
newScanQueryBuilder()
.dataSource(CalciteTests.DATASOURCE3)
.eternityInterval()
.filters(
NullHandling.sqlCompatible() ? NullFilter.forColumn("dim3") : selector("dim3", null)
)
.columns("dim3")
.resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
.limit(5)
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
NullHandling.sqlCompatible()
? ImmutableList.of(
new Object[]{null},
new Object[]{null}
)
: ImmutableList.of(
new Object[]{""},
new Object[]{""},
new Object[]{""}
)
);
}

@Test
public void testMultiValueStringOverlapFilterNonLiteral()
{
Expand Down Expand Up @@ -332,6 +364,37 @@ public void testMultiValueStringContainsFilter()
);
}

@Test
public void testMultiValueStringContainsFilterNull()
{
testQuery(
"SELECT dim3 FROM druid.numfoo WHERE MV_CONTAINS(dim3, ARRAY[NULL]) LIMIT 5",
ImmutableList.of(
newScanQueryBuilder()
.dataSource(CalciteTests.DATASOURCE3)
.eternityInterval()
.filters(
NullHandling.sqlCompatible() ? NullFilter.forColumn("dim3") : selector("dim3", null)
)
.columns("dim3")
.resultFormat(ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST)
.limit(5)
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
NullHandling.sqlCompatible()
? ImmutableList.of(
new Object[]{null},
new Object[]{null}
)
: ImmutableList.of(
new Object[]{""},
new Object[]{""},
new Object[]{""}
)
);
}

@Test
public void testMultiValueStringContainsArrayOfOneElement()
{
Expand Down
Loading