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 @@ -1066,7 +1066,18 @@ protected Object visitLambdaExpression(LambdaExpression node, Object context)
if (optimize) {
// TODO: enable optimization related to lambda expression
// A mechanism to convert function type back into lambda expression need to exist to enable optimization
return node;
Object value = processWithExceptionHandling(node.getBody(), context);
Expression optimizedBody;

// value may be null, converted to an expression by toExpression(value, type)
if (value instanceof Expression) {
optimizedBody = (Expression) value;
}
else {
Type type = type(node.getBody());
optimizedBody = toExpression(value, type);
}
return new LambdaExpression(node.getArguments(), optimizedBody);
}

Expression body = node.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ public void testIsNotNull()
assertOptimizedEquals("bound_decimal_long IS NOT NULL", "true");
}

@Test
public void testLambdaBody()
{
assertOptimizedEquals("transform(ARRAY[bound_long], n -> CAST(n as BIGINT))",
"transform(ARRAY[bound_long], n -> n)");
assertOptimizedEquals("transform(ARRAY[bound_long], n -> CAST(n as VARCHAR(5)))",
"transform(ARRAY[bound_long], n -> CAST(n as VARCHAR(5)))");
assertOptimizedEquals("transform(ARRAY[bound_long], n -> IF(false, 1, 0 / 0))",
"transform(ARRAY[bound_long], n -> 0 / 0)");
assertOptimizedEquals("transform(ARRAY[bound_long], n -> 5 / 0)",
"transform(ARRAY[bound_long], n -> 5 / 0)");
assertOptimizedEquals("transform(ARRAY[bound_long], n -> nullif(true, true))",
"transform(ARRAY[bound_long], n -> CAST(null AS Boolean))");
assertOptimizedEquals("transform(ARRAY[bound_long], n -> n + 10 * 10)",
"transform(ARRAY[bound_long], n -> n + 100)");
assertOptimizedEquals("reduce_agg(bound_long, 0, (a, b) -> IF(false, a, b), (a, b) -> IF(true, a, b))",
"reduce_agg(bound_long, 0, (a, b) -> b, (a, b) -> a)");
}

@Test
public void testNullIf()
{
Expand Down