-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds parsing and modeling of CTEs (#1736)
* 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 grammar node to allow for query expressions
- Loading branch information
1 parent
920a76f
commit 3f0e2e1
Showing
22 changed files
with
868 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.partiql.ast; | ||
|
||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* <p> | ||
* Represents a PartiQL WITH clause. | ||
* </p> | ||
* <p>{@code <with clause> ::= WITH [ RECURSIVE ] <with list>}</p> | ||
* <p>{@code <with list> ::= <with list element> [ { <comma> <with list element> }... ]}</p> | ||
* @see WithListElement | ||
* @see org.partiql.ast.expr.ExprQuerySet | ||
*/ | ||
@Builder(builderClassName = "Builder") | ||
@EqualsAndHashCode(callSuper = false) | ||
public final class With extends AstNode { | ||
|
||
@NotNull | ||
private final List<WithListElement> elements; | ||
|
||
private final boolean isRecursive; | ||
|
||
/** | ||
* 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, 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 | ||
@Override | ||
public List<AstNode> getChildren() { | ||
return new ArrayList<>(elements); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) { | ||
return visitor.visitWith(this, ctx); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
partiql-ast/src/main/java/org/partiql/ast/WithListElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package org.partiql.ast; | ||
|
||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.partiql.ast.expr.ExprQuerySet; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* <p> | ||
* Represents a {@code <with list element>}. | ||
* </p> | ||
* <p> | ||
* {@code | ||
* <with list element> ::= | ||
* <query name> | ||
* [ <left paren> <with column list> <right paren> ] | ||
* AS <left paren> <query expression> <right paren> | ||
* [ <search or cycle clause> ] | ||
* } | ||
* </p> | ||
* <p>{@code <with column list> ::= <column name list>}</p> | ||
* <p>{@code <column name list> ::= <column name> [ { <comma> <column name> }... ]}</p> | ||
* <p>{@code <column name> ::= <identifier>}</p> | ||
* @see With | ||
* @see ExprQuerySet | ||
*/ | ||
@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; | ||
|
||
@NotNull | ||
private final ExprQuerySet asQuery; | ||
|
||
@Nullable | ||
private final List<Identifier.Simple> withColumnList; | ||
|
||
/** | ||
* 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() { | ||
List<AstNode> children = new ArrayList<>(); | ||
children.add(queryName); | ||
if (withColumnList != null) { | ||
children.addAll(withColumnList); | ||
} | ||
children.add(asQuery); | ||
return children; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) { | ||
return visitor.visitWithListElement(this, ctx); | ||
} | ||
|
||
/** | ||
* Returns the query name. | ||
* @return the query name | ||
*/ | ||
@NotNull | ||
public Identifier.Simple getQueryName() { | ||
return this.queryName; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* Returns the query that defines the with list element. | ||
* @return the query that defines the with list element | ||
*/ | ||
@NotNull | ||
public ExprQuerySet getAsQuery() { | ||
return this.asQuery; | ||
} | ||
} |
Oops, something went wrong.