Skip to content

Commit af0995b

Browse files
committed
Adds parsing and ast modeling for CTEs
Adds parsing and ast modeling for WITH clause Fixes the expression tree to exclude SELECTs Adds a dedicated subquery node to allow for query expressions
1 parent 920a76f commit af0995b

File tree

12 files changed

+751
-134
lines changed

12 files changed

+751
-134
lines changed

partiql-ast/api/partiql-ast.api

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public final class org/partiql/ast/Ast {
7979
public static final fun exprPosition (Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;)Lorg/partiql/ast/expr/ExprPosition;
8080
public static final fun exprQuerySet (Lorg/partiql/ast/QueryBody;)Lorg/partiql/ast/expr/ExprQuerySet;
8181
public static final fun exprQuerySet (Lorg/partiql/ast/QueryBody;Lorg/partiql/ast/OrderBy;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;)Lorg/partiql/ast/expr/ExprQuerySet;
82+
public static final fun exprQuerySet (Lorg/partiql/ast/QueryBody;Lorg/partiql/ast/OrderBy;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/With;)Lorg/partiql/ast/expr/ExprQuerySet;
8283
public static synthetic fun exprQuerySet$default (Lorg/partiql/ast/QueryBody;Lorg/partiql/ast/OrderBy;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;ILjava/lang/Object;)Lorg/partiql/ast/expr/ExprQuerySet;
84+
public static synthetic fun exprQuerySet$default (Lorg/partiql/ast/QueryBody;Lorg/partiql/ast/OrderBy;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/With;ILjava/lang/Object;)Lorg/partiql/ast/expr/ExprQuerySet;
8385
public static final fun exprRowValue (Ljava/util/List;)Lorg/partiql/ast/expr/ExprRowValue;
8486
public static final fun exprRowValue (Ljava/util/List;Z)Lorg/partiql/ast/expr/ExprRowValue;
8587
public static final fun exprSessionAttribute (Lorg/partiql/ast/expr/SessionAttribute;)Lorg/partiql/ast/expr/ExprSessionAttribute;
@@ -572,6 +574,8 @@ public abstract class org/partiql/ast/AstVisitor {
572574
public fun visitUpdateTargetStepElement (Lorg/partiql/ast/dml/UpdateTargetStep$Element;Ljava/lang/Object;)Ljava/lang/Object;
573575
public fun visitUpdateTargetStepField (Lorg/partiql/ast/dml/UpdateTargetStep$Field;Ljava/lang/Object;)Ljava/lang/Object;
574576
public fun visitUpsert (Lorg/partiql/ast/dml/Upsert;Ljava/lang/Object;)Ljava/lang/Object;
577+
public fun visitWith (Lorg/partiql/ast/With;Ljava/lang/Object;)Ljava/lang/Object;
578+
public fun visitWithListElement (Lorg/partiql/ast/WithListElement;Ljava/lang/Object;)Ljava/lang/Object;
575579
}
576580

577581
public final class org/partiql/ast/DataType : org/partiql/ast/AstEnum {
@@ -1054,21 +1058,27 @@ public class org/partiql/ast/Identifier$Simple$Builder {
10541058
public final class org/partiql/ast/JoinType : org/partiql/ast/AstEnum {
10551059
public static final field CROSS I
10561060
public static final field FULL I
1061+
public static final field FULL_CROSS I
10571062
public static final field FULL_OUTER I
10581063
public static final field INNER I
1064+
public static final field INNER_CROSS I
10591065
public static final field LEFT I
10601066
public static final field LEFT_CROSS I
10611067
public static final field LEFT_OUTER I
10621068
public static final field RIGHT I
1069+
public static final field RIGHT_CROSS I
10631070
public static final field RIGHT_OUTER I
10641071
public static fun CROSS ()Lorg/partiql/ast/JoinType;
10651072
public static fun FULL ()Lorg/partiql/ast/JoinType;
1073+
public static fun FULL_CROSS ()Lorg/partiql/ast/JoinType;
10661074
public static fun FULL_OUTER ()Lorg/partiql/ast/JoinType;
10671075
public static fun INNER ()Lorg/partiql/ast/JoinType;
1076+
public static fun INNER_CROSS ()Lorg/partiql/ast/JoinType;
10681077
public static fun LEFT ()Lorg/partiql/ast/JoinType;
10691078
public static fun LEFT_CROSS ()Lorg/partiql/ast/JoinType;
10701079
public static fun LEFT_OUTER ()Lorg/partiql/ast/JoinType;
10711080
public static fun RIGHT ()Lorg/partiql/ast/JoinType;
1081+
public static fun RIGHT_CROSS ()Lorg/partiql/ast/JoinType;
10721082
public static fun RIGHT_OUTER ()Lorg/partiql/ast/JoinType;
10731083
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
10741084
public fun code ()I
@@ -1455,6 +1465,42 @@ public abstract class org/partiql/ast/Statement : org/partiql/ast/AstNode {
14551465
public fun <init> ()V
14561466
}
14571467

1468+
public final class org/partiql/ast/With : org/partiql/ast/AstNode {
1469+
public fun <init> (Ljava/util/List;)V
1470+
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
1471+
public static fun builder ()Lorg/partiql/ast/With$Builder;
1472+
public fun equals (Ljava/lang/Object;)Z
1473+
public fun getChildren ()Ljava/util/List;
1474+
public fun getElements ()Ljava/util/List;
1475+
public fun hashCode ()I
1476+
}
1477+
1478+
public class org/partiql/ast/With$Builder {
1479+
public fun build ()Lorg/partiql/ast/With;
1480+
public fun elements (Ljava/util/List;)Lorg/partiql/ast/With$Builder;
1481+
public fun toString ()Ljava/lang/String;
1482+
}
1483+
1484+
public final class org/partiql/ast/WithListElement : org/partiql/ast/AstNode {
1485+
public fun <init> (Lorg/partiql/ast/Identifier$Simple;Lorg/partiql/ast/expr/ExprQuerySet;Ljava/util/List;)V
1486+
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
1487+
public static fun builder ()Lorg/partiql/ast/WithListElement$Builder;
1488+
public fun equals (Ljava/lang/Object;)Z
1489+
public fun getAsQuery ()Lorg/partiql/ast/expr/ExprQuerySet;
1490+
public fun getChildren ()Ljava/util/List;
1491+
public fun getColumnList ()Ljava/util/List;
1492+
public fun getQueryName ()Lorg/partiql/ast/Identifier$Simple;
1493+
public fun hashCode ()I
1494+
}
1495+
1496+
public class org/partiql/ast/WithListElement$Builder {
1497+
public fun asQuery (Lorg/partiql/ast/expr/ExprQuerySet;)Lorg/partiql/ast/WithListElement$Builder;
1498+
public fun build ()Lorg/partiql/ast/WithListElement;
1499+
public fun queryName (Lorg/partiql/ast/Identifier$Simple;)Lorg/partiql/ast/WithListElement$Builder;
1500+
public fun toString ()Ljava/lang/String;
1501+
public fun withColumnList (Ljava/util/List;)Lorg/partiql/ast/WithListElement$Builder;
1502+
}
1503+
14581504
public abstract class org/partiql/ast/ddl/AttributeConstraint : org/partiql/ast/AstNode {
14591505
protected final field name Lorg/partiql/ast/Identifier;
14601506
protected fun <init> (Lorg/partiql/ast/Identifier;)V
@@ -2422,6 +2468,7 @@ public class org/partiql/ast/expr/ExprPosition$Builder {
24222468

24232469
public final class org/partiql/ast/expr/ExprQuerySet : org/partiql/ast/expr/Expr {
24242470
public fun <init> (Lorg/partiql/ast/QueryBody;Lorg/partiql/ast/OrderBy;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;)V
2471+
public fun <init> (Lorg/partiql/ast/QueryBody;Lorg/partiql/ast/OrderBy;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/With;)V
24252472
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
24262473
public static fun builder ()Lorg/partiql/ast/expr/ExprQuerySet$Builder;
24272474
public fun equals (Ljava/lang/Object;)Z
@@ -2430,6 +2477,7 @@ public final class org/partiql/ast/expr/ExprQuerySet : org/partiql/ast/expr/Expr
24302477
public fun getLimit ()Lorg/partiql/ast/expr/Expr;
24312478
public fun getOffset ()Lorg/partiql/ast/expr/Expr;
24322479
public fun getOrderBy ()Lorg/partiql/ast/OrderBy;
2480+
public fun getWith ()Lorg/partiql/ast/With;
24332481
public fun hashCode ()I
24342482
}
24352483

@@ -2440,6 +2488,7 @@ public class org/partiql/ast/expr/ExprQuerySet$Builder {
24402488
public fun offset (Lorg/partiql/ast/expr/Expr;)Lorg/partiql/ast/expr/ExprQuerySet$Builder;
24412489
public fun orderBy (Lorg/partiql/ast/OrderBy;)Lorg/partiql/ast/expr/ExprQuerySet$Builder;
24422490
public fun toString ()Ljava/lang/String;
2491+
public fun with (Lorg/partiql/ast/With;)Lorg/partiql/ast/expr/ExprQuerySet$Builder;
24432492
}
24442493

24452494
public final class org/partiql/ast/expr/ExprRowValue : org/partiql/ast/expr/Expr {

partiql-ast/src/main/java/org/partiql/ast/AstVisitor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,14 @@ public R visitLet(Let node, C ctx) {
515515
return defaultVisit(node, ctx);
516516
}
517517

518+
public R visitWith(With node, C ctx) {
519+
return defaultVisit(node, ctx);
520+
}
521+
522+
public R visitWithListElement(WithListElement node, C ctx) {
523+
return defaultVisit(node, ctx);
524+
}
525+
518526
public R visitLetBinding(Let.Binding node, C ctx) {
519527
return defaultVisit(node, ctx);
520528
}

partiql-ast/src/main/java/org/partiql/ast/JoinType.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,27 @@ public final class JoinType extends AstEnum {
4343
* Cross join variant.
4444
*/
4545
public static final int CROSS = 7;
46+
4647
/**
4748
* Left cross join variant.
4849
*/
4950
public static final int LEFT_CROSS = 8;
5051

52+
/**
53+
* Right cross join variant.
54+
*/
55+
public static final int RIGHT_CROSS = 9;
56+
57+
/**
58+
* Outer cross join variant.
59+
*/
60+
public static final int INNER_CROSS = 10;
61+
62+
/**
63+
* Full cross join variant.
64+
*/
65+
public static final int FULL_CROSS = 11;
66+
5167
public static JoinType INNER() {
5268
return new JoinType(INNER);
5369
}
@@ -84,6 +100,18 @@ public static JoinType LEFT_CROSS() {
84100
return new JoinType(LEFT_CROSS);
85101
}
86102

103+
public static JoinType RIGHT_CROSS() {
104+
return new JoinType(RIGHT_CROSS);
105+
}
106+
107+
public static JoinType INNER_CROSS() {
108+
return new JoinType(INNER_CROSS);
109+
}
110+
111+
public static JoinType FULL_CROSS() {
112+
return new JoinType(FULL_CROSS);
113+
}
114+
87115
private final int code;
88116

89117
private JoinType(int code) {
@@ -108,6 +136,9 @@ public String name() {
108136
case FULL_OUTER: return "FULL_OUTER";
109137
case CROSS: return "CROSS";
110138
case LEFT_CROSS: return "LEFT_CROSS";
139+
case RIGHT_CROSS: return "RIGHT_CROSS";
140+
case INNER_CROSS: return "INNER_CROSS";
141+
case FULL_CROSS: return "FULL_CROSS";
111142
default: throw new IllegalStateException("Invalid JoinType code: " + code);
112143
}
113144
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.partiql.ast;
2+
3+
import lombok.Builder;
4+
import lombok.EqualsAndHashCode;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* <p>
12+
* Represents a PartiQL WITH clause.
13+
* </p>
14+
* <p>{@code <with clause> ::= WITH [ RECURSIVE ] <with list>}</p>
15+
* <p>{@code <with list> ::= <with list element> [ { <comma> <with list element> }... ]}</p>
16+
* @see WithListElement
17+
* @see org.partiql.ast.expr.ExprQuerySet
18+
*/
19+
@Builder(builderClassName = "Builder")
20+
@EqualsAndHashCode(callSuper = false)
21+
public final class With extends AstNode {
22+
23+
@NotNull
24+
private final List<WithListElement> elements;
25+
26+
/**
27+
* TODO
28+
* @param elements TODO
29+
*/
30+
public With(@NotNull List<WithListElement> elements) {
31+
this.elements = elements;
32+
}
33+
34+
@NotNull
35+
@Override
36+
public List<AstNode> getChildren() {
37+
return new ArrayList<>(elements);
38+
}
39+
40+
@Override
41+
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
42+
return visitor.visitWith(this, ctx);
43+
}
44+
45+
/**
46+
* TODO
47+
* @return TODO
48+
*/
49+
@NotNull
50+
public List<WithListElement> getElements() {
51+
return this.elements;
52+
}
53+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.partiql.ast;
2+
3+
import lombok.Builder;
4+
import lombok.EqualsAndHashCode;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
import org.partiql.ast.expr.ExprQuerySet;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
/**
13+
* <p>
14+
* Represents a {@code <with list element>}.
15+
* </p>
16+
* <p>
17+
* {@code
18+
* <with list element> ::=
19+
* <query name>
20+
* [ <left paren> <with column list> <right paren> ]
21+
* AS <left paren> <query expression> <right paren>
22+
* [ <search or cycle clause> ]
23+
* }
24+
* </p>
25+
* <p>{@code <with column list> ::= <column name list>}</p>
26+
* <p>{@code <column name list> ::= <column name> [ { <comma> <column name> }... ]}</p>
27+
* <p>{@code <column name> ::= <identifier>}</p>
28+
* @see With
29+
* @see ExprQuerySet
30+
*/
31+
@Builder(builderClassName = "Builder")
32+
@EqualsAndHashCode(callSuper = false)
33+
public final class WithListElement extends AstNode {
34+
35+
@NotNull
36+
private final Identifier.Simple queryName;
37+
38+
@NotNull
39+
private final ExprQuerySet asQuery;
40+
41+
@Nullable
42+
private final List<Identifier.Simple> withColumnList;
43+
44+
/**
45+
* TODO
46+
* @param queryName TODO
47+
* @param asQuery TODO
48+
* @param columnList TODO
49+
*/
50+
public WithListElement(@NotNull Identifier.Simple queryName, @NotNull ExprQuerySet asQuery, @Nullable List<Identifier.Simple> columnList) {
51+
this.queryName = queryName;
52+
this.asQuery = asQuery;
53+
this.withColumnList = columnList;
54+
}
55+
56+
@NotNull
57+
@Override
58+
public List<AstNode> getChildren() {
59+
List<AstNode> children = new ArrayList<>();
60+
children.add(queryName);
61+
if (withColumnList != null) {
62+
children.addAll(withColumnList);
63+
}
64+
children.add(asQuery);
65+
return children;
66+
}
67+
68+
@Override
69+
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
70+
return visitor.visitWithListElement(this, ctx);
71+
}
72+
73+
/**
74+
* TODO
75+
* @return TODO
76+
*/
77+
@NotNull
78+
public Identifier.Simple getQueryName() {
79+
return this.queryName;
80+
}
81+
82+
/**
83+
* TODO
84+
* @return TODO
85+
*/
86+
@Nullable
87+
public List<Identifier.Simple> getColumnList() {
88+
return this.withColumnList;
89+
}
90+
91+
/**
92+
* TODO
93+
* @return TODO
94+
*/
95+
@NotNull
96+
public ExprQuerySet getAsQuery() {
97+
return this.asQuery;
98+
}
99+
}

partiql-ast/src/main/java/org/partiql/ast/expr/ExprQuerySet.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.partiql.ast.AstVisitor;
99
import org.partiql.ast.OrderBy;
1010
import org.partiql.ast.QueryBody;
11+
import org.partiql.ast.With;
1112

1213
import java.util.ArrayList;
1314
import java.util.List;
@@ -33,11 +34,23 @@ public final class ExprQuerySet extends Expr {
3334
@Nullable
3435
private final Expr offset;
3536

37+
@Nullable
38+
private final With with;
39+
3640
public ExprQuerySet(@NotNull QueryBody body, @Nullable OrderBy orderBy, @Nullable Expr limit, @Nullable Expr offset) {
3741
this.body = body;
3842
this.orderBy = orderBy;
3943
this.limit = limit;
4044
this.offset = offset;
45+
this.with = null;
46+
}
47+
48+
public ExprQuerySet(@NotNull QueryBody body, @Nullable OrderBy orderBy, @Nullable Expr limit, @Nullable Expr offset, @Nullable With with) {
49+
this.body = body;
50+
this.orderBy = orderBy;
51+
this.limit = limit;
52+
this.offset = offset;
53+
this.with = with;
4154
}
4255

4356
@Override
@@ -62,6 +75,11 @@ public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
6275
return visitor.visitExprQuerySet(this, ctx);
6376
}
6477

78+
@Nullable
79+
public With getWith() {
80+
return this.with;
81+
}
82+
6583
@NotNull
6684
public QueryBody getBody() {
6785
return this.body;

partiql-ast/src/main/kotlin/org/partiql/ast/Ast.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ public object Ast {
224224
return ExprQuerySet(body, orderBy, limit, offset)
225225
}
226226

227+
@JvmStatic
228+
public fun exprQuerySet(body: QueryBody, orderBy: OrderBy? = null, limit: Expr? = null, offset: Expr? = null, with: With? = null): ExprQuerySet {
229+
return ExprQuerySet(body, orderBy, limit, offset, with)
230+
}
231+
227232
@JvmStatic
228233
public fun exprSessionAttribute(sessionAttribute: SessionAttribute): ExprSessionAttribute {
229234
return ExprSessionAttribute(sessionAttribute)

0 commit comments

Comments
 (0)