-
Notifications
You must be signed in to change notification settings - Fork 94
feat(isthmus): support converting Substrait Plan.Root to Calcite RelRoot #339
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
isthmus/src/test/java/io/substrait/isthmus/SubstraitToCalciteTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package io.substrait.isthmus; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import io.substrait.plan.ImmutableRoot; | ||
| import io.substrait.plan.Plan.Root; | ||
| import io.substrait.type.Type; | ||
| import io.substrait.type.TypeCreator; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import org.apache.calcite.rel.RelRoot; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SubstraitToCalciteTest extends PlanTestBase { | ||
| final SubstraitToCalcite converter = new SubstraitToCalcite(extensions, typeFactory); | ||
|
|
||
| @Test | ||
| void testConvertRootSingleColumn() { | ||
| Iterable<Type> types = List.of(TypeCreator.REQUIRED.STRING); | ||
| Root root = | ||
| ImmutableRoot.builder() | ||
| .input(substraitBuilder.namedScan(List.of("stores"), List.of("s"), types)) | ||
| .addNames("store") | ||
| .build(); | ||
|
|
||
| RelRoot relRoot = converter.convert(root); | ||
|
|
||
| assertEquals(root.getNames(), relRoot.fields.rightList()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertRootMultipleColumns() { | ||
| Iterable<Type> types = List.of(TypeCreator.REQUIRED.I64, TypeCreator.REQUIRED.STRING); | ||
| Root root = | ||
| ImmutableRoot.builder() | ||
| .input(substraitBuilder.namedScan(List.of("stores"), List.of("s_store_id", "s"), types)) | ||
| .addNames("s_store_id", "store") | ||
| .build(); | ||
|
|
||
| RelRoot relRoot = converter.convert(root); | ||
|
|
||
| assertEquals(root.getNames(), relRoot.fields.rightList()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertRootStructField() { | ||
| final Type structType = | ||
| TypeCreator.REQUIRED.struct(TypeCreator.REQUIRED.I64, TypeCreator.REQUIRED.STRING); | ||
| Iterable<Type> types = List.of(structType); | ||
| Root root = | ||
| ImmutableRoot.builder() | ||
| .input( | ||
| substraitBuilder.namedScan( | ||
| List.of("stores"), List.of("s", "s_store_id", "s_store_name"), types)) | ||
| .addNames("store", "store_id", "store_name") | ||
| .build(); | ||
|
|
||
| assertEquals(List.of("store", "store_id", "store_name"), root.getNames()); | ||
|
|
||
| RelRoot relRoot = converter.convert(root); | ||
|
|
||
| // Apache Calcite's RelRoot.fields only contains the top level field names | ||
| assertEquals(List.of("store"), relRoot.fields.rightList()); | ||
|
|
||
| // the sub field names are stored within RelRoot.validatedRowType | ||
| assertEquals(List.of("store"), relRoot.validatedRowType.getFieldNames()); | ||
|
|
||
| RelDataType storeFieldDataType = relRoot.validatedRowType.getFieldList().get(0).getType(); | ||
| assertEquals(List.of("store_id", "store_name"), storeFieldDataType.getFieldNames()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertRootArrayWithStructField() { | ||
| final Type structType = | ||
| TypeCreator.REQUIRED.struct(TypeCreator.REQUIRED.I64, TypeCreator.REQUIRED.STRING); | ||
| final Type arrayType = TypeCreator.REQUIRED.list(structType); | ||
| Set<Type> types = Set.of(arrayType); | ||
| Root root = | ||
| ImmutableRoot.builder() | ||
| .input( | ||
| substraitBuilder.namedScan( | ||
| List.of("stores"), List.of("s", "s_store_id", "s_store_name"), types)) | ||
| .addNames("store", "store_id", "store_name") | ||
| .build(); | ||
|
|
||
| RelRoot relRoot = converter.convert(root); | ||
|
|
||
| // Apache Calcite's RelRoot.fields only contains the top level field names | ||
| assertEquals(List.of("store"), relRoot.fields.rightList()); | ||
|
|
||
| // the hierarchical structure is stored within RelRoot.validatedRowType | ||
| assertEquals(List.of("store"), relRoot.validatedRowType.getFieldNames()); | ||
|
|
||
| RelDataType storeFieldDataType = relRoot.validatedRowType.getFieldList().get(0).getType(); | ||
| assertEquals(SqlTypeName.ARRAY, storeFieldDataType.getSqlTypeName()); | ||
|
|
||
| final RelDataType arrayElementType = storeFieldDataType.getComponentType(); | ||
| assertEquals(SqlTypeName.ROW, arrayElementType.getSqlTypeName()); | ||
| assertEquals(List.of("store_id", "store_name"), arrayElementType.getFieldNames()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertRootMapWithStructValues() { | ||
| final Type structType = | ||
| TypeCreator.REQUIRED.struct(TypeCreator.REQUIRED.I64, TypeCreator.REQUIRED.STRING); | ||
| final Type mapValueType = TypeCreator.REQUIRED.map(TypeCreator.REQUIRED.I64, structType); | ||
| Set<Type> types = Set.of(mapValueType); | ||
| Root root = | ||
| ImmutableRoot.builder() | ||
| .input( | ||
| substraitBuilder.namedScan( | ||
| List.of("stores"), List.of("s", "s_store_id", "s_store_name"), types)) | ||
| .addNames("store", "store_id", "store_name") | ||
| .build(); | ||
|
|
||
| final RelRoot relRoot = converter.convert(root); | ||
|
|
||
| // Apache Calcite's RelRoot.fields only contains the top level field names | ||
| assertEquals(List.of("store"), relRoot.fields.rightList()); | ||
|
|
||
| // the hierarchical structure is stored within RelRoot.validatedRowType | ||
| assertEquals(List.of("store"), relRoot.validatedRowType.getFieldNames()); | ||
|
|
||
| final RelDataType storeFieldDataType = relRoot.validatedRowType.getFieldList().get(0).getType(); | ||
| assertEquals(SqlTypeName.MAP, storeFieldDataType.getSqlTypeName()); | ||
|
|
||
| final RelDataType mapValueDataType = storeFieldDataType.getValueType(); | ||
| assertEquals(SqlTypeName.ROW, mapValueDataType.getSqlTypeName()); | ||
| assertEquals(List.of("store_id", "store_name"), mapValueDataType.getFieldNames()); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertRootMapWithStructKeys() { | ||
| final Type structType = | ||
| TypeCreator.REQUIRED.struct(TypeCreator.REQUIRED.I64, TypeCreator.REQUIRED.STRING); | ||
| final Type mapKeyType = TypeCreator.REQUIRED.map(structType, TypeCreator.REQUIRED.I64); | ||
| Set<Type> types = Set.of(mapKeyType); | ||
| Root root = | ||
| ImmutableRoot.builder() | ||
| .input( | ||
| substraitBuilder.namedScan( | ||
| List.of("stores"), List.of("s", "s_store_id", "s_store_name"), types)) | ||
| .addNames("store", "store_id", "store_name") | ||
| .build(); | ||
|
|
||
| RelRoot relRoot = converter.convert(root); | ||
|
|
||
| // Apache Calcite's RelRoot.fields only contains the top level field names | ||
| assertEquals(List.of("store"), relRoot.fields.rightList()); | ||
|
|
||
| // the hierarchical structure is stored within RelRoot.validatedRowType | ||
| assertEquals(List.of("store"), relRoot.validatedRowType.getFieldNames()); | ||
|
|
||
| RelDataType storeFieldDataType = relRoot.validatedRowType.getFieldList().get(0).getType(); | ||
| assertEquals(SqlTypeName.MAP, storeFieldDataType.getSqlTypeName()); | ||
|
|
||
| final RelDataType mapKeyDataType = storeFieldDataType.getKeyType(); | ||
| assertEquals(SqlTypeName.ROW, mapKeyDataType.getSqlTypeName()); | ||
| assertEquals(List.of("store_id", "store_name"), mapKeyDataType.getFieldNames()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.