11package io .substrait .isthmus ;
22
3- import io .substrait .isthmus .sql .SubstraitSqlValidator ;
3+ import io .substrait .isthmus .sql .SubstraitSqlToCalcite ;
4+ import java .util .List ;
45import java .util .Map ;
56import org .apache .calcite .adapter .tpcds .TpcdsSchema ;
7+ import org .apache .calcite .config .CalciteConnectionConfig ;
8+ import org .apache .calcite .config .CalciteConnectionProperty ;
9+ import org .apache .calcite .jdbc .CalciteSchema ;
10+ import org .apache .calcite .jdbc .JavaTypeFactoryImpl ;
611import org .apache .calcite .prepare .CalciteCatalogReader ;
712import org .apache .calcite .rel .RelRoot ;
813import org .apache .calcite .rex .RexFieldAccess ;
914import org .apache .calcite .sql .parser .SqlParseException ;
10- import org .apache .calcite .sql .parser .SqlParser ;
11- import org .apache .calcite .sql2rel .SqlToRelConverter ;
1215import org .junit .jupiter .api .Assertions ;
1316import org .junit .jupiter .api .Test ;
1417
15- public class ApplyJoinPlanTest {
16-
17- private static RelRoot getCalcitePlan (SqlToSubstrait s , TpcdsSchema schema , String sql )
18- throws SqlParseException {
19- CalciteCatalogReader catalogReader = s .registerSchema ("tpcds" , schema );
20- SubstraitSqlValidator validator = new SubstraitSqlValidator (catalogReader );
21- SqlToRelConverter converter = s .createSqlToRelConverter (validator , catalogReader );
22- SqlParser parser = SqlParser .create (sql , s .parserConfig );
23- return s .getBestExpRelRoot (converter , parser .parseQuery ());
18+ public class ApplyJoinPlanTest extends PlanTestBase {
19+ static CalciteCatalogReader TPCDS_CATALOG ;
20+
21+ static {
22+ TpcdsSchema tpcdsSchema = new TpcdsSchema (1.0 );
23+ CalciteSchema rootSchema = CalciteSchema .createRootSchema (false );
24+ rootSchema .add ("tpcds" , tpcdsSchema );
25+
26+ TPCDS_CATALOG =
27+ new CalciteCatalogReader (
28+ rootSchema ,
29+ List .of ("tpcds" ),
30+ new JavaTypeFactoryImpl (SubstraitTypeSystem .TYPE_SYSTEM ),
31+ CalciteConnectionConfig .DEFAULT .set (
32+ CalciteConnectionProperty .CASE_SENSITIVE , Boolean .FALSE .toString ()));
2433 }
2534
2635 private static void validateOuterRef (
@@ -62,7 +71,7 @@ public void lateralJoinQuery() throws SqlParseException {
6271 */
6372
6473 // validate outer reference map
65- RelRoot root = getCalcitePlan ( new SqlToSubstrait (), schema , sql );
74+ RelRoot root = SubstraitSqlToCalcite . convertSelect ( sql , TPCDS_CATALOG );
6675 Map <RexFieldAccess , Integer > fieldAccessDepthMap = buildOuterFieldRefMap (root );
6776 Assertions .assertEquals (1 , fieldAccessDepthMap .size ());
6877 validateOuterRef (fieldAccessDepthMap , "$cor0" , "SS_ITEM_SK" , 1 );
@@ -77,26 +86,23 @@ public void lateralJoinQuery() throws SqlParseException {
7786
7887 @ Test
7988 public void outerApplyQuery () throws SqlParseException {
80- TpcdsSchema schema = new TpcdsSchema (1.0 );
8189 String sql ;
8290 sql =
8391 """
8492 SELECT ss_sold_date_sk, ss_item_sk, ss_customer_sk
8593 FROM store_sales OUTER APPLY
8694 (select i_item_sk from item where item.i_item_sk = store_sales.ss_item_sk)""" ;
87-
88- FeatureBoard featureBoard = ImmutableFeatureBoard .builder ().build ();
89- SqlToSubstrait s = new SqlToSubstrait (featureBoard );
90- RelRoot root = getCalcitePlan (s , schema , sql );
95+ RelRoot root = SubstraitSqlToCalcite .convertSelect (sql , TPCDS_CATALOG );
9196
9297 Map <RexFieldAccess , Integer > fieldAccessDepthMap = buildOuterFieldRefMap (root );
9398 Assertions .assertEquals (1 , fieldAccessDepthMap .size ());
9499 validateOuterRef (fieldAccessDepthMap , "$cor0" , "SS_ITEM_SK" , 1 );
95100
96101 // TODO validate end to end conversion
102+ SqlToSubstrait s = new SqlToSubstrait ();
97103 Assertions .assertThrows (
98104 UnsupportedOperationException .class ,
99- () -> s .execute (sql , "tpcds" , schema ),
105+ () -> s .execute (sql , TPCDS_CATALOG ),
100106 "APPLY is not supported" );
101107 }
102108
@@ -127,9 +133,7 @@ public void nestedApplyJoinQuery() throws SqlParseException {
127133 LogicalFilter(condition=[AND(=($4, $cor0.I_ITEM_SK), =($4, $cor2.SS_ITEM_SK))])
128134 LogicalTableScan(table=[[tpcds, PROMOTION]])
129135 */
130- FeatureBoard featureBoard = ImmutableFeatureBoard .builder ().build ();
131- SqlToSubstrait s = new SqlToSubstrait (featureBoard );
132- RelRoot root = getCalcitePlan (s , schema , sql );
136+ RelRoot root = SubstraitSqlToCalcite .convertSelect (sql , TPCDS_CATALOG );
133137
134138 Map <RexFieldAccess , Integer > fieldAccessDepthMap = buildOuterFieldRefMap (root );
135139 Assertions .assertEquals (3 , fieldAccessDepthMap .size ());
@@ -138,29 +142,28 @@ public void nestedApplyJoinQuery() throws SqlParseException {
138142 validateOuterRef (fieldAccessDepthMap , "$cor0" , "I_ITEM_SK" , 1 );
139143
140144 // TODO validate end to end conversion
145+ SqlToSubstrait s = new SqlToSubstrait ();
141146 Assertions .assertThrows (
142147 UnsupportedOperationException .class ,
143- () -> s .execute (sql , "tpcds" , schema ),
148+ () -> s .execute (sql , TPCDS_CATALOG ),
144149 "APPLY is not supported" );
145150 }
146151
147152 @ Test
148- public void crossApplyQuery () throws SqlParseException {
149- TpcdsSchema schema = new TpcdsSchema (1.0 );
153+ public void crossApplyQuery () {
150154 String sql ;
151155 sql =
152156 """
153157 SELECT ss_sold_date_sk, ss_item_sk, ss_customer_sk
154158 FROM store_sales CROSS APPLY
155159 (select i_item_sk from item where item.i_item_sk = store_sales.ss_item_sk)""" ;
156160
157- FeatureBoard featureBoard = ImmutableFeatureBoard .builder ().build ();
158- SqlToSubstrait s = new SqlToSubstrait (featureBoard );
161+ SqlToSubstrait s = new SqlToSubstrait ();
159162
160163 // TODO validate end to end conversion
161164 Assertions .assertThrows (
162165 UnsupportedOperationException .class ,
163- () -> s .execute (sql , "tpcds" , schema ),
166+ () -> s .execute (sql , TPCDS_CATALOG ),
164167 "APPLY is not supported" );
165168 }
166169}
0 commit comments