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

fix(core): if-then expression should be nullable if any 'then' is nullable #287

Merged
merged 1 commit into from
Aug 16, 2024
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
8 changes: 7 additions & 1 deletion core/src/main/java/io/substrait/expression/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,13 @@ abstract static class IfThen implements Expression {
public abstract Expression elseClause();

public Type getType() {
return elseClause().getType();
Type elseType = elseClause().getType();

// If any of the clauses are nullable, the whole expression is also nullable.
if (ifClauses().stream().anyMatch(clause -> clause.then().getType().nullable())) {
return TypeCreator.asNullable(elseType);
}
return elseType;
}

public static ImmutableExpression.IfThen.Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.substrait.type.proto;

import static io.substrait.expression.proto.ProtoExpressionConverter.EMPTY_TYPE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.substrait.TestBase;
import io.substrait.expression.Expression;
import io.substrait.expression.ExpressionCreator;
import io.substrait.expression.proto.ExpressionProtoConverter;
import io.substrait.expression.proto.ProtoExpressionConverter;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

public class IfThenRoundtripTest extends TestBase {

@Test
void ifThenNotNullable() {
final Expression.IfThen ifRel =
b.ifThen(
Arrays.asList(
b.ifClause(ExpressionCreator.bool(false, false), ExpressionCreator.i64(false, 1))),
ExpressionCreator.i64(false, 2));
assertFalse(ifRel.getType().nullable());

var to = new ExpressionProtoConverter(null, null);
var from = new ProtoExpressionConverter(null, null, EMPTY_TYPE, protoRelConverter);
assertEquals(ifRel, from.from(ifRel.accept(to)));
}

@Test
void ifThenNullable() {
final Expression.IfThen ifRel =
b.ifThen(
Arrays.asList(
b.ifClause(ExpressionCreator.bool(true, false), ExpressionCreator.i64(true, 1))),
ExpressionCreator.i64(false, 2));
assertTrue(ifRel.getType().nullable());

var to = new ExpressionProtoConverter(null, null);
var from = new ProtoExpressionConverter(null, null, EMPTY_TYPE, protoRelConverter);
assertEquals(ifRel, from.from(ifRel.accept(to)));
}
}
Loading