-
Notifications
You must be signed in to change notification settings - Fork 93
feat: support Nested Lists #627
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
95c27c9
4b42b10
9e99091
5b277c9
3ae2b62
0e4c6fd
5ad5fa1
925c9ef
b0a787d
041a625
9f2ed4d
e000913
f6a7459
2c61543
b489367
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
gord02 marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package io.substrait.type.proto; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import io.substrait.TestBase; | ||
| import io.substrait.expression.Expression; | ||
| import io.substrait.expression.ImmutableExpression; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class NestedListExpressionTest extends TestBase { | ||
| io.substrait.expression.Expression literalExpression = | ||
| Expression.BoolLiteral.builder().value(true).build(); | ||
| Expression.ScalarFunctionInvocation nonLiteralExpression = b.add(b.i32(7), b.i32(42)); | ||
|
|
||
| @Test | ||
| void rejectNestedListWithElementsOfDifferentTypes() { | ||
| ImmutableExpression.NestedList.Builder builder = | ||
| Expression.NestedList.builder().addValues(literalExpression).addValues(b.i32(12)); | ||
| assertThrows(AssertionError.class, builder::build); | ||
| } | ||
|
|
||
| @Test | ||
| void acceptNestedListWithElementsOfSameType() { | ||
| ImmutableExpression.NestedList.Builder builder = | ||
| Expression.NestedList.builder().addValues(nonLiteralExpression).addValues(b.i32(12)); | ||
| assertDoesNotThrow(builder::build); | ||
|
|
||
| io.substrait.relation.Project project = | ||
| io.substrait.relation.Project.builder() | ||
| .addExpressions(builder.build()) | ||
| .input(b.emptyScan()) | ||
| .build(); | ||
| verifyRoundTrip(project); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectEmptyNestedListTest() { | ||
| ImmutableExpression.NestedList.Builder builder = Expression.NestedList.builder(); | ||
| assertThrows(AssertionError.class, builder::build); | ||
| } | ||
gord02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| void literalNestedListTest() { | ||
| Expression.NestedList literalNestedList = | ||
| Expression.NestedList.builder() | ||
| .addValues(literalExpression) | ||
| .addValues(literalExpression) | ||
| .build(); | ||
|
|
||
| io.substrait.relation.Project project = | ||
| io.substrait.relation.Project.builder() | ||
| .addExpressions(literalNestedList) | ||
| .input(b.emptyScan()) | ||
| .build(); | ||
|
|
||
| verifyRoundTrip(project); | ||
gord02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Test | ||
| void literalNullableNestedListTest() { | ||
| Expression.NestedList literalNestedList = | ||
| Expression.NestedList.builder() | ||
| .addValues(literalExpression) | ||
| .addValues(literalExpression) | ||
| .nullable(true) | ||
| .build(); | ||
|
|
||
| io.substrait.relation.Project project = | ||
| io.substrait.relation.Project.builder() | ||
| .addExpressions(literalNestedList) | ||
| .input(b.emptyScan()) | ||
| .build(); | ||
|
|
||
| verifyRoundTrip(project); | ||
| } | ||
|
|
||
| @Test | ||
| void nonLiteralNestedListTest() { | ||
| Expression.NestedList nonLiteralNestedList = | ||
| Expression.NestedList.builder() | ||
| .addValues(nonLiteralExpression) | ||
| .addValues(nonLiteralExpression) | ||
| .build(); | ||
|
|
||
| io.substrait.relation.Project project = | ||
| io.substrait.relation.Project.builder() | ||
| .addExpressions(nonLiteralNestedList) | ||
| .input(b.emptyScan()) | ||
| .build(); | ||
|
|
||
| verifyRoundTrip(project); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package io.substrait.isthmus; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.sql.SqlKind; | ||
| import org.apache.calcite.sql.SqlOperatorBinding; | ||
| import org.apache.calcite.sql.fun.SqlMultisetValueConstructor; | ||
| import org.apache.calcite.sql.type.SqlTypeUtil; | ||
| import org.apache.calcite.sql.validate.SqlValidatorUtil; | ||
|
|
||
| /** | ||
| * Substrait-specific constructor to map back to the Expression NestedList type in Substrait. This | ||
| * constructor creates a special type of SqlKind.ARRAY_VALUE_CONSTRUCTOR for lists that can contain | ||
| * both literal and non-literal expressions. | ||
| */ | ||
| public class NestedListConstructor extends SqlMultisetValueConstructor { | ||
|
||
|
|
||
| public NestedListConstructor() { | ||
| super("NESTEDLIST", SqlKind.ARRAY_VALUE_CONSTRUCTOR); | ||
| } | ||
|
|
||
| @Override | ||
| public RelDataType inferReturnType(SqlOperatorBinding opBinding) { | ||
| RelDataType type = | ||
| getComponentType(opBinding.getTypeFactory(), opBinding.collectOperandTypes()); | ||
| requireNonNull(type, "inferred array element type"); | ||
|
|
||
| // explicit cast elements to component type if they are not same | ||
| SqlValidatorUtil.adjustTypeForArrayConstructor(type, opBinding); | ||
|
|
||
| return SqlTypeUtil.createArrayType(opBinding.getTypeFactory(), type, false); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.