Skip to content
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 @@ -442,11 +442,15 @@ public ExprEval apply(LambdaExpr lambdaExpr, List<Expr> argsExpr, Expr.ObjectBin

Object[] array = arrayEval.asArray();
if (array == null) {
return ExprEval.of(null);
return ExprEval.ofArray(arrayEval.asArrayType(), null);
}

SettableLambdaBinding lambdaBinding = new SettableLambdaBinding(arrayEval.elementType(), lambdaExpr, bindings);
Object[] filtered = filter(arrayEval.asArray(), lambdaExpr, lambdaBinding).toArray();
// return null array expr if nothing is left in filtered
if (filtered.length == 0) {
return ExprEval.ofArray(arrayEval.asArrayType(), null);
}
return ExprEval.ofArray(arrayEval.asArrayType(), filtered);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public void testFilter()

assertExpr("filter((x) -> x > 2, [1, 2, 3, 4, 5])", new Long[] {3L, 4L, 5L});
assertExpr("filter((x) -> x > 2, b)", new Long[] {3L, 4L, 5L});

String dummyNull = null;
assertExpr("filter((x) -> array_contains([], x), ['a', 'b'])", dummyNull);
assertExpr("filter((x) -> array_contains([], x), null)", dummyNull);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: this variable seems more chars than just using null directly...

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,6 @@ public DruidExpression toDruidExpression(
return null;
}

Expr expr = plannerContext.parseExpression(druidExpressions.get(1).getExpression());
// the right expression must be a literal array for this to work, since we need the values of the column
if (!expr.isLiteral()) {
return null;
}
Object[] lit = expr.eval(InputBindings.nilBindings()).asArray();
if (lit == null || lit.length == 0) {
return null;
}
HashSet<String> literals = Sets.newHashSetWithExpectedSize(lit.length);
for (Object o : lit) {
literals.add(Evals.asString(o));
}

final DruidExpression.ExpressionGenerator builder = (args) -> {
final StringBuilder expressionBuilder;
if (isAllowList()) {
Expand All @@ -370,7 +356,17 @@ public DruidExpression toDruidExpression(
return expressionBuilder.toString();
};

if (druidExpressions.get(0).isSimpleExtraction()) {
Expr expr = plannerContext.parseExpression(druidExpressions.get(1).getExpression());
if (druidExpressions.get(0).isSimpleExtraction() && expr.isLiteral()) {
Object[] lit = expr.eval(InputBindings.nilBindings()).asArray();
if (lit == null || lit.length == 0) {
return null;
}
HashSet<String> literals = Sets.newHashSetWithExpectedSize(lit.length);
for (Object o : lit) {
literals.add(Evals.asString(o));
}

DruidExpression druidExpression = DruidExpression.ofVirtualColumn(
Calcites.getColumnTypeForRelDataType(rexNode.getType()),
builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,41 @@ public void testMultiValueListFilter()
);
}

@Test
public void testMultiValueListFilterNonLiteral()
{
// Cannot vectorize due to usage of expressions.
cannotVectorize();

testQuery(
"SELECT MV_FILTER_ONLY(dim3, ARRAY[dim2]) FROM druid.numfoo",
ImmutableList.of(
newScanQueryBuilder()
.dataSource(CalciteTests.DATASOURCE3)
.intervals(querySegmentSpec(Filtration.eternity()))
.virtualColumns(
new ExpressionVirtualColumn(
"v0",
"filter((x) -> array_contains(array(\"dim2\"), x), \"dim3\")",
ColumnType.STRING,
TestExprMacroTable.INSTANCE
)
)
.columns("v0")
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ImmutableList.of(
new Object[]{"a"},
new Object[]{NullHandling.defaultStringValue()},
new Object[]{NullHandling.defaultStringValue()},
new Object[]{NullHandling.defaultStringValue()},
new Object[]{NullHandling.defaultStringValue()},
new Object[]{NullHandling.defaultStringValue()}
)
);
}

@Test
public void testMultiValueListFilterDeny()
{
Expand Down Expand Up @@ -1388,6 +1423,41 @@ public void testMultiValueListFilterDeny()
);
}

@Test
public void testMultiValueListFilterDenyNonLiteral()
{
// Cannot vectorize due to usage of expressions.
cannotVectorize();

testQuery(
"SELECT MV_FILTER_NONE(dim3, ARRAY[dim2]) FROM druid.numfoo",
ImmutableList.of(
newScanQueryBuilder()
.dataSource(CalciteTests.DATASOURCE3)
.intervals(querySegmentSpec(Filtration.eternity()))
.virtualColumns(
new ExpressionVirtualColumn(
"v0",
"filter((x) -> !array_contains(array(\"dim2\"), x), \"dim3\")",
ColumnType.STRING,
TestExprMacroTable.INSTANCE
)
)
.columns("v0")
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ImmutableList.of(
new Object[]{"b"},
new Object[]{"[\"b\",\"c\"]"},
new Object[]{"d"},
new Object[]{""},
new Object[]{NullHandling.defaultStringValue()},
new Object[]{NullHandling.defaultStringValue()}
)
);
}

@Test
public void testMultiValueListFilterComposed()
{
Expand Down