2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
4
import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
5
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5
6
6
7
import io .substrait .dsl .SubstraitBuilder ;
7
8
import io .substrait .expression .Expression ;
@@ -53,16 +54,7 @@ public void switchExpression() {
53
54
54
55
@ Test
55
56
public void scalarSubQuery () {
56
- Rel subQueryRel =
57
- b .project (
58
- input -> List .of (b .fieldReference (input , 0 )),
59
- Remap .of (List .of (3 )),
60
- b .filter (
61
- input ->
62
- b .equal (
63
- b .fieldReference (input , 2 ),
64
- Expression .StrLiteral .builder ().nullable (false ).value ("EUROPE" ).build ()),
65
- commonTable ));
57
+ Rel subQueryRel = createSubQueryRel ();
66
58
67
59
Expression .ScalarSubquery expr =
68
60
Expression .ScalarSubquery .builder ()
@@ -81,6 +73,92 @@ public void scalarSubQuery() {
81
73
assertEquals (SqlKind .SCALAR_QUERY , calciteProjectExpr .get (0 ).getKind ());
82
74
}
83
75
76
+ @ Test
77
+ public void existsSetPredicate () {
78
+ Rel subQueryRel = createSubQueryRel ();
79
+
80
+ Expression .SetPredicate expr =
81
+ Expression .SetPredicate .builder ()
82
+ .predicateOp (Expression .PredicateOp .PREDICATE_OP_EXISTS )
83
+ .tuples (subQueryRel )
84
+ .build ();
85
+
86
+ Project query = b .project (input -> List .of (expr ), b .emptyScan ());
87
+
88
+ SubstraitToCalcite substraitToCalcite = new SubstraitToCalcite (extensions , typeFactory );
89
+ RelNode calciteRel = substraitToCalcite .convert (query );
90
+
91
+ assertInstanceOf (LogicalProject .class , calciteRel );
92
+ List <RexNode > calciteProjectExpr = ((LogicalProject ) calciteRel ).getProjects ();
93
+ assertEquals (1 , calciteProjectExpr .size ());
94
+ assertEquals (SqlKind .EXISTS , calciteProjectExpr .get (0 ).getKind ());
95
+ }
96
+
97
+ @ Test
98
+ public void uniqueSetPredicate () {
99
+ Rel subQueryRel = createSubQueryRel ();
100
+
101
+ Expression .SetPredicate expr =
102
+ Expression .SetPredicate .builder ()
103
+ .predicateOp (Expression .PredicateOp .PREDICATE_OP_UNIQUE )
104
+ .tuples (subQueryRel )
105
+ .build ();
106
+
107
+ Project query = b .project (input -> List .of (expr ), b .emptyScan ());
108
+
109
+ SubstraitToCalcite substraitToCalcite = new SubstraitToCalcite (extensions , typeFactory );
110
+ RelNode calciteRel = substraitToCalcite .convert (query );
111
+
112
+ assertInstanceOf (LogicalProject .class , calciteRel );
113
+ List <RexNode > calciteProjectExpr = ((LogicalProject ) calciteRel ).getProjects ();
114
+ assertEquals (1 , calciteProjectExpr .size ());
115
+ assertEquals (SqlKind .UNIQUE , calciteProjectExpr .get (0 ).getKind ());
116
+ }
117
+
118
+ @ Test
119
+ public void unspecifiedSetPredicate () {
120
+ Rel subQueryRel = createSubQueryRel ();
121
+
122
+ Expression .SetPredicate expr =
123
+ Expression .SetPredicate .builder ()
124
+ .predicateOp (Expression .PredicateOp .PREDICATE_OP_UNSPECIFIED )
125
+ .tuples (subQueryRel )
126
+ .build ();
127
+
128
+ Project query = b .project (input -> List .of (expr ), b .emptyScan ());
129
+
130
+ SubstraitToCalcite substraitToCalcite = new SubstraitToCalcite (extensions , typeFactory );
131
+ Exception exception =
132
+ assertThrows (
133
+ UnsupportedOperationException .class ,
134
+ () -> {
135
+ substraitToCalcite .convert (query );
136
+ });
137
+
138
+ assertEquals (
139
+ "Cannot handle SetPredicate when PredicateOp is PREDICATE_OP_UNSPECIFIED." ,
140
+ exception .getMessage ());
141
+ }
142
+
143
+ /**
144
+ * Creates a Substrait {@link Rel} equivalent to the following SQL query:
145
+ *
146
+ * <p>select a from example where c = 'EUROPE'
147
+ *
148
+ * @return the Substrait {@link Rel} equivalent of the above SQL query
149
+ */
150
+ Rel createSubQueryRel () {
151
+ return b .project (
152
+ input -> List .of (b .fieldReference (input , 0 )),
153
+ Remap .of (List .of (3 )),
154
+ b .filter (
155
+ input ->
156
+ b .equal (
157
+ b .fieldReference (input , 2 ),
158
+ Expression .StrLiteral .builder ().nullable (false ).value ("EUROPE" ).build ()),
159
+ commonTable ));
160
+ }
161
+
84
162
void assertTypeMatch (RelDataType actual , Type expected ) {
85
163
Type type = TypeConverter .DEFAULT .toSubstrait (actual );
86
164
assertEquals (expected , type );
0 commit comments