1
1
package io .substrait .isthmus ;
2
2
3
- import io .substrait .isthmus .sql .SubstraitSqlValidator ;
3
+ import io .substrait .isthmus .sql .SubstraitSqlToCalcite ;
4
+ import java .util .List ;
4
5
import java .util .Map ;
5
6
import 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 ;
6
11
import org .apache .calcite .prepare .CalciteCatalogReader ;
7
12
import org .apache .calcite .rel .RelRoot ;
8
13
import org .apache .calcite .rex .RexFieldAccess ;
9
14
import org .apache .calcite .sql .parser .SqlParseException ;
10
- import org .apache .calcite .sql .parser .SqlParser ;
11
- import org .apache .calcite .sql2rel .SqlToRelConverter ;
12
15
import org .junit .jupiter .api .Assertions ;
13
16
import org .junit .jupiter .api .Test ;
14
17
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 ()));
24
33
}
25
34
26
35
private static void validateOuterRef (
@@ -62,7 +71,7 @@ public void lateralJoinQuery() throws SqlParseException {
62
71
*/
63
72
64
73
// validate outer reference map
65
- RelRoot root = getCalcitePlan ( new SqlToSubstrait (), schema , sql );
74
+ RelRoot root = SubstraitSqlToCalcite . convertSelect ( sql , TPCDS_CATALOG );
66
75
Map <RexFieldAccess , Integer > fieldAccessDepthMap = buildOuterFieldRefMap (root );
67
76
Assertions .assertEquals (1 , fieldAccessDepthMap .size ());
68
77
validateOuterRef (fieldAccessDepthMap , "$cor0" , "SS_ITEM_SK" , 1 );
@@ -77,26 +86,23 @@ public void lateralJoinQuery() throws SqlParseException {
77
86
78
87
@ Test
79
88
public void outerApplyQuery () throws SqlParseException {
80
- TpcdsSchema schema = new TpcdsSchema (1.0 );
81
89
String sql ;
82
90
sql =
83
91
"""
84
92
SELECT ss_sold_date_sk, ss_item_sk, ss_customer_sk
85
93
FROM store_sales OUTER APPLY
86
94
(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 );
91
96
92
97
Map <RexFieldAccess , Integer > fieldAccessDepthMap = buildOuterFieldRefMap (root );
93
98
Assertions .assertEquals (1 , fieldAccessDepthMap .size ());
94
99
validateOuterRef (fieldAccessDepthMap , "$cor0" , "SS_ITEM_SK" , 1 );
95
100
96
101
// TODO validate end to end conversion
102
+ SqlToSubstrait s = new SqlToSubstrait ();
97
103
Assertions .assertThrows (
98
104
UnsupportedOperationException .class ,
99
- () -> s .execute (sql , "tpcds" , schema ),
105
+ () -> s .execute (sql , TPCDS_CATALOG ),
100
106
"APPLY is not supported" );
101
107
}
102
108
@@ -127,9 +133,7 @@ public void nestedApplyJoinQuery() throws SqlParseException {
127
133
LogicalFilter(condition=[AND(=($4, $cor0.I_ITEM_SK), =($4, $cor2.SS_ITEM_SK))])
128
134
LogicalTableScan(table=[[tpcds, PROMOTION]])
129
135
*/
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 );
133
137
134
138
Map <RexFieldAccess , Integer > fieldAccessDepthMap = buildOuterFieldRefMap (root );
135
139
Assertions .assertEquals (3 , fieldAccessDepthMap .size ());
@@ -138,29 +142,28 @@ public void nestedApplyJoinQuery() throws SqlParseException {
138
142
validateOuterRef (fieldAccessDepthMap , "$cor0" , "I_ITEM_SK" , 1 );
139
143
140
144
// TODO validate end to end conversion
145
+ SqlToSubstrait s = new SqlToSubstrait ();
141
146
Assertions .assertThrows (
142
147
UnsupportedOperationException .class ,
143
- () -> s .execute (sql , "tpcds" , schema ),
148
+ () -> s .execute (sql , TPCDS_CATALOG ),
144
149
"APPLY is not supported" );
145
150
}
146
151
147
152
@ Test
148
- public void crossApplyQuery () throws SqlParseException {
149
- TpcdsSchema schema = new TpcdsSchema (1.0 );
153
+ public void crossApplyQuery () {
150
154
String sql ;
151
155
sql =
152
156
"""
153
157
SELECT ss_sold_date_sk, ss_item_sk, ss_customer_sk
154
158
FROM store_sales CROSS APPLY
155
159
(select i_item_sk from item where item.i_item_sk = store_sales.ss_item_sk)""" ;
156
160
157
- FeatureBoard featureBoard = ImmutableFeatureBoard .builder ().build ();
158
- SqlToSubstrait s = new SqlToSubstrait (featureBoard );
161
+ SqlToSubstrait s = new SqlToSubstrait ();
159
162
160
163
// TODO validate end to end conversion
161
164
Assertions .assertThrows (
162
165
UnsupportedOperationException .class ,
163
- () -> s .execute (sql , "tpcds" , schema ),
166
+ () -> s .execute (sql , TPCDS_CATALOG ),
164
167
"APPLY is not supported" );
165
168
}
166
169
}
0 commit comments