Skip to content

Commit

Permalink
Addresses PR feedback
Browse files Browse the repository at this point in the history
Adds support for parsing RECURSIVE CTEs

Adds modeling of RECURSIVE in AST

Updates Javadoc TODOs

Adds Ast#with and Ast#withListElement

Adds a TODO for search/cycle
  • Loading branch information
johnedquinn committed Feb 7, 2025
1 parent 3eeb520 commit 2edcd74
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
6 changes: 6 additions & 0 deletions partiql-ast/api/partiql-ast.api
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public final class org/partiql/ast/Ast {
public static final fun upsert (Lorg/partiql/ast/Identifier;Lorg/partiql/ast/Identifier$Simple;Lorg/partiql/ast/dml/InsertSource;)Lorg/partiql/ast/dml/Upsert;
public static final fun upsert (Lorg/partiql/ast/Identifier;Lorg/partiql/ast/dml/InsertSource;)Lorg/partiql/ast/dml/Upsert;
public static synthetic fun upsert$default (Lorg/partiql/ast/Identifier;Lorg/partiql/ast/Identifier$Simple;Lorg/partiql/ast/dml/InsertSource;ILjava/lang/Object;)Lorg/partiql/ast/dml/Upsert;
public static final fun with (Ljava/util/List;Z)Lorg/partiql/ast/With;
public static final fun withListElement (Lorg/partiql/ast/Identifier$Simple;Lorg/partiql/ast/expr/ExprQuerySet;Ljava/util/List;)Lorg/partiql/ast/WithListElement;
}

public abstract class org/partiql/ast/AstEnum : org/partiql/ast/AstNode {
Expand Down Expand Up @@ -1471,21 +1473,25 @@ public abstract class org/partiql/ast/Statement : org/partiql/ast/AstNode {

public final class org/partiql/ast/With : org/partiql/ast/AstNode {
public fun <init> (Ljava/util/List;)V
public fun <init> (Ljava/util/List;Z)V
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public static fun builder ()Lorg/partiql/ast/With$Builder;
public fun equals (Ljava/lang/Object;)Z
public fun getChildren ()Ljava/util/List;
public fun getElements ()Ljava/util/List;
public fun hashCode ()I
public fun isRecursive ()Z
}

public class org/partiql/ast/With$Builder {
public fun build ()Lorg/partiql/ast/With;
public fun elements (Ljava/util/List;)Lorg/partiql/ast/With$Builder;
public fun isRecursive (Z)Lorg/partiql/ast/With$Builder;
public fun toString ()Ljava/lang/String;
}

public final class org/partiql/ast/WithListElement : org/partiql/ast/AstNode {
public fun <init> (Lorg/partiql/ast/Identifier$Simple;Lorg/partiql/ast/expr/ExprQuerySet;)V
public fun <init> (Lorg/partiql/ast/Identifier$Simple;Lorg/partiql/ast/expr/ExprQuerySet;Ljava/util/List;)V
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public static fun builder ()Lorg/partiql/ast/WithListElement$Builder;
Expand Down
30 changes: 25 additions & 5 deletions partiql-ast/src/main/java/org/partiql/ast/With.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ public final class With extends AstNode {
@NotNull
private final List<WithListElement> elements;

private final boolean isRecursive;

/**
* TODO
* @param elements TODO
* Creates a new WITH clause with the specified elements and RECURSIVE set to the specified value.
* @param elements the list of WITH list elements
* @param isRecursive true if this WITH clause specified RECURSIVE;
*/
public With(@NotNull List<WithListElement> elements) {
public With(@NotNull List<WithListElement> elements, boolean isRecursive) {
this.elements = elements;
this.isRecursive = isRecursive;
}

/**
* Creates a new WITH clause with the specified elements and RECURSIVE set to false.
* @param elements the list of WITH list elements
*/
public With(@NotNull List<WithListElement> elements) {
this(elements, false);
}

@NotNull
Expand All @@ -43,11 +55,19 @@ public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
}

/**
* TODO
* @return TODO
* Returns the list of WITH list elements.
* @return the list of WITH list elements
*/
@NotNull
public List<WithListElement> getElements() {
return this.elements;
}

/**
* Returns whether this WITH clause specified RECURSIVE.
* @return whether this WITH clause specified RECURSIVE.
*/
public boolean isRecursive() {
return this.isRecursive;
}
}
32 changes: 21 additions & 11 deletions partiql-ast/src/main/java/org/partiql/ast/WithListElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* <query name>
* [ <left paren> <with column list> <right paren> ]
* AS <left paren> <query expression> <right paren>
* [ <search or cycle clause> ]
* [ <search or cycle clause> ]
* }
* </p>
* <p>{@code <with column list> ::= <column name list>}</p>
Expand All @@ -31,6 +31,7 @@
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public final class WithListElement extends AstNode {
// TODO: Add support for the search/cycle clause.

@NotNull
private final Identifier.Simple queryName;
Expand All @@ -42,17 +43,26 @@ public final class WithListElement extends AstNode {
private final List<Identifier.Simple> withColumnList;

/**
* TODO
* @param queryName TODO
* @param asQuery TODO
* @param columnList TODO
* Creates a new instance of {@link WithListElement}.
* @param queryName the name to bind
* @param asQuery the query that defines the with list element
* @param columnList the list of column names to be output from the query
*/
public WithListElement(@NotNull Identifier.Simple queryName, @NotNull ExprQuerySet asQuery, @Nullable List<Identifier.Simple> columnList) {
this.queryName = queryName;
this.asQuery = asQuery;
this.withColumnList = columnList;
}

/**
* Creates a new instance of {@link WithListElement}.
* @param queryName the name to bind
* @param asQuery the query that defines the with list element
*/
public WithListElement(@NotNull Identifier.Simple queryName, @NotNull ExprQuerySet asQuery) {
this(queryName, asQuery, null);
}

@NotNull
@Override
public List<AstNode> getChildren() {
Expand All @@ -71,26 +81,26 @@ public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
}

/**
* TODO
* @return TODO
* Returns the query name.
* @return the query name
*/
@NotNull
public Identifier.Simple getQueryName() {
return this.queryName;
}

/**
* TODO
* @return TODO
* Returns the list of column names to be output from the query.
* @return the list of column names to be output from the query. This may return null.
*/
@Nullable
public List<Identifier.Simple> getColumnList() {
return this.withColumnList;
}

/**
* TODO
* @return TODO
* Returns the query that defines the with list element.
* @return the query that defines the with list element
*/
@NotNull
public ExprQuerySet getAsQuery() {
Expand Down
10 changes: 10 additions & 0 deletions partiql-ast/src/main/kotlin/org/partiql/ast/Ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,16 @@ public object Ast {
return Identifier(qualifier, identifier)
}

@JvmStatic
public fun with(elements: List<WithListElement>, isRecursive: Boolean): With {
return With(elements, isRecursive)
}

@JvmStatic
public fun withListElement(queryName: Identifier.Simple, asQuery: ExprQuerySet, columnList: List<Identifier.Simple>?): WithListElement {
return WithListElement(queryName, asQuery, columnList)
}

@JvmStatic
public fun let(bindings: List<Let.Binding>): Let {
return Let(bindings)
Expand Down
2 changes: 1 addition & 1 deletion partiql-parser/src/main/antlr/PartiQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ letBinding
* <with list> ::= <with list element> [ { <comma> <with list element> }... ]
*/
withClause
: WITH elements+=withListElement ( COMMA elements+=withListElement)*
: WITH RECURSIVE? elements+=withListElement ( COMMA elements+=withListElement)*
;

/**
Expand Down
1 change: 1 addition & 0 deletions partiql-parser/src/main/antlr/PartiQLTokens.g4
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ PROCEDURE: 'PROCEDURE';
PUBLIC: 'PUBLIC';
READ: 'READ';
REAL: 'REAL';
RECURSIVE: 'RECURSIVE';
REFERENCES: 'REFERENCES';
RELATIVE: 'RELATIVE';
REPLACE: 'REPLACE';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,8 @@ internal class PartiQLParserDefault : PartiQLParser {

override fun visitWithClause(ctx: GeneratedParser.WithClauseContext) = translate(ctx) {
val elements = ctx.elements.map { elt -> visitWithListElement(elt) }
With(elements)
val isRecursive = ctx.RECURSIVE() != null
With(elements, isRecursive)
}

override fun visitWithListElement(ctx: GeneratedParser.WithListElementContext) = translate(ctx) {
Expand Down

0 comments on commit 2edcd74

Please sign in to comment.