22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44
5+ import io .substrait .isthmus .operation .CalciteOperation ;
56import io .substrait .isthmus .sql .SubstraitCreateStatementParser ;
67import io .substrait .plan .Plan ;
78import io .substrait .plan .PlanProtoConverter ;
89import io .substrait .plan .ProtoPlanConverter ;
10+ import io .substrait .relation .Rel ;
911import org .apache .calcite .prepare .Prepare ;
1012import org .apache .calcite .sql .parser .SqlParseException ;
1113import org .junit .jupiter .api .Test ;
@@ -28,23 +30,55 @@ void testSqlToSubstrait(String sqlStatement) throws SqlParseException {
2830 assertEquals (protoPlan , protoPlan1 );
2931 }
3032
31- void testPlanRoundTrip (String sqlStatement ) throws SqlParseException {
33+ protected void assertCalciteOperationSubstraitRelRoundTrip (
34+ String query , Prepare .CatalogReader catalogReader ) throws Exception {
35+ // sql <--> substrait round trip test.
36+ // Assert (sql -> calcite -> substrait) and (sql -> substrait -> calcite -> substrait) are same.
37+ // Return list of sql -> Substrait rel -> Calcite rel.
38+ SqlToSubstrait s = new SqlToSubstrait ();
39+
40+ // 1. SQL -> Substrait Plan
41+ Plan plan1 = s .convert (query , catalogReader );
42+
43+ // 2. Substrait Plan -> Substrait Rel
44+ Plan .Root pojo1 = plan1 .getRoots ().get (0 );
45+
46+ // 3. Substrait Rel -> CalciteOperation
47+
48+ SubstraitToCalciteOperation substraitToCalciteOperation =
49+ new SubstraitToCalciteOperation (extensions , typeFactory , catalogReader , s .parserConfig );
50+ Rel rel = plan1 .getRoots ().get (0 ).getInput ();
51+ CalciteOperation calciteOperation =
52+ rel .accept (substraitToCalciteOperation , SubstraitRelNodeConverter .Context .newContext ());
53+
54+ // 4. CalciteOperation -> Substrait Rel
55+ CalciteOperationToSubstrait calciteOperationToSubstrait =
56+ new CalciteOperationToSubstrait (extensions , s .featureBoard );
57+ Plan .Root pojo2 = calciteOperation .accept (calciteOperationToSubstrait );
58+
59+ assertEquals (pojo1 , pojo2 );
60+ }
61+
62+ void testPlanRoundTrip (String sqlStatement ) throws Exception {
3263 SqlToSubstrait sql2subst = new SqlToSubstrait ();
3364 final Plan plan = sql2subst .convert (sqlStatement , catalogReader );
3465
3566 assertPlanRoundtrip (plan );
67+ assertCalciteOperationSubstraitRelRoundTrip (sqlStatement , catalogReader );
68+
69+ assertFullRoundTrip (sqlStatement , catalogReader );
3670 }
3771
3872 @ Test
39- void testCreateTable () throws SqlParseException {
73+ void testCreateTable () throws Exception {
4074 String sql = "create table dst1 as select * from src1" ;
4175 testSqlToSubstrait (sql );
4276 // TBD: full roundtrip is not possible because there is no relational algebra for DDL
4377 testPlanRoundTrip (sql );
4478 }
4579
4680 @ Test
47- void testCreateView () throws SqlParseException {
81+ void testCreateView () throws Exception {
4882 String sql = "create view dst1 as select * from src1" ;
4983 testSqlToSubstrait (sql );
5084 testPlanRoundTrip (sql );
0 commit comments