Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1] Port SqlDialect to v1 AST #1638

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
513 changes: 217 additions & 296 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions partiql-ast/src/main/java/org/partiql/ast/v1/Ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.partiql.ast.v1.expr.ExprParameter
import org.partiql.ast.v1.expr.ExprPath
import org.partiql.ast.v1.expr.ExprPosition
import org.partiql.ast.v1.expr.ExprQuerySet
import org.partiql.ast.v1.expr.ExprRowValue
import org.partiql.ast.v1.expr.ExprSessionAttribute
import org.partiql.ast.v1.expr.ExprStruct
import org.partiql.ast.v1.expr.ExprSubstring
Expand All @@ -50,6 +51,7 @@ import org.partiql.value.PartiQLValue
import org.partiql.value.PartiQLValueExperimental

// TODO docs for all factory methods and move to Kotlin sources
// Also consider defaults for nullable. Need to look more into backwards compatibility.
RCHowell marked this conversation as resolved.
Show resolved Hide resolved
public object Ast {
// Expr
@JvmStatic
Expand Down Expand Up @@ -113,8 +115,8 @@ public object Ast {
}

@JvmStatic
public fun exprLike(value: Expr, Pattern: Expr, escape: Expr?, not: Boolean): ExprLike {
RCHowell marked this conversation as resolved.
Show resolved Hide resolved
return ExprLike(value, Pattern, escape, not)
public fun exprLike(value: Expr, pattern: Expr, escape: Expr?, not: Boolean): ExprLike {
return ExprLike(value, pattern, escape, not)
}

// This representation will be changed in https://github.com/partiql/partiql-lang-kotlin/issues/1589
Expand Down Expand Up @@ -195,18 +197,18 @@ public object Ast {
}

@JvmStatic
public fun exprTrim(Value: Expr, chars: Expr?, trimSpec: TrimSpec?): ExprTrim {
return ExprTrim(Value, chars, trimSpec)
public fun exprTrim(value: Expr, chars: Expr?, trimSpec: TrimSpec?): ExprTrim {
return ExprTrim(value, chars, trimSpec)
}

@JvmStatic
public fun exprValues(rows: List<ExprValues.Row>): ExprValues {
public fun exprValues(rows: List<ExprRowValue>): ExprValues {
return ExprValues(rows)
}

@JvmStatic
public fun exprValuesRow(values: List<Expr>): ExprValues.Row {
return ExprValues.Row(values)
public fun exprRowValue(values: List<Expr>): ExprRowValue {
return ExprRowValue(values)
}

@JvmStatic
Expand Down
7 changes: 4 additions & 3 deletions partiql-ast/src/main/java/org/partiql/ast/v1/AstRewriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.partiql.ast.v1.expr.ExprParameter
import org.partiql.ast.v1.expr.ExprPath
import org.partiql.ast.v1.expr.ExprPosition
import org.partiql.ast.v1.expr.ExprQuerySet
import org.partiql.ast.v1.expr.ExprRowValue
import org.partiql.ast.v1.expr.ExprSessionAttribute
import org.partiql.ast.v1.expr.ExprStruct
import org.partiql.ast.v1.expr.ExprSubstring
Expand Down Expand Up @@ -353,18 +354,18 @@ public abstract class AstRewriter<C> : AstVisitor<AstNode, C>() {
}

override fun visitExprValues(node: ExprValues, ctx: C): AstNode {
val values = _visitList(node.rows, ctx, ::visitExprValuesRow)
val values = _visitList(node.rows, ctx, ::visitExprRowValue)
return if (values !== node.rows) {
ExprValues(values)
} else {
node
}
}

override fun visitExprValuesRow(node: ExprValues.Row, ctx: C): AstNode {
override fun visitExprRowValue(node: ExprRowValue, ctx: C): AstNode {
val values = _visitList(node.values, ctx, ::visitExpr)
return if (values !== node.values) {
ExprValues.Row(values)
ExprRowValue(values)
} else {
node
}
Expand Down
7 changes: 6 additions & 1 deletion partiql-ast/src/main/java/org/partiql/ast/v1/AstVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.partiql.ast.v1.expr.ExprPath;
import org.partiql.ast.v1.expr.ExprPosition;
import org.partiql.ast.v1.expr.ExprQuerySet;
import org.partiql.ast.v1.expr.ExprRowValue;
import org.partiql.ast.v1.expr.ExprSessionAttribute;
import org.partiql.ast.v1.expr.ExprStruct;
import org.partiql.ast.v1.expr.ExprSubstring;
Expand Down Expand Up @@ -148,7 +149,7 @@ public R visitExprValues(ExprValues node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitExprValuesRow(ExprValues.Row node, C ctx) {
public R visitExprRowValue(ExprRowValue node, C ctx) {
return defaultVisit(node, ctx);
}

Expand Down Expand Up @@ -435,4 +436,8 @@ public R visitGraphLabelConj(GraphLabel.Conj node, C ctx) {
public R visitGraphLabelDisj(GraphLabel.Disj node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitDataType(DataType node, C ctx) {
return defaultVisit(node, ctx);
}
}
2 changes: 1 addition & 1 deletion partiql-ast/src/main/java/org/partiql/ast/v1/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,6 @@ public Collection<AstNode> children() {

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return null;
return visitor.visitDataType(this, ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ExprCast(@NotNull Expr value, @NotNull DataType asType) {
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(value);
kids.add(asType);
return kids;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public ExprIsType(@NotNull Expr value, @NotNull DataType type, boolean not) {
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(value);
kids.add(type);
return kids;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.partiql.ast.v1.expr;

import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.partiql.ast.v1.AstNode;
import org.partiql.ast.v1.AstVisitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* TODO docs, equals, hashcode
* Also add optional [ROW] keyword property. https://ronsavage.github.io/SQL/sql-99.bnf.html#row%20value%20constructor
*/
@lombok.Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public class ExprRowValue extends Expr {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(self-review): slight adjustment to RowValue modeling to be more consistent with what SQL99 defines -- https://ronsavage.github.io/SQL/sql-99.bnf.html#row%20value%20constructor. In the future, can consider also adding the optional ROW keyword.

@NotNull
public final List<Expr> values;

public ExprRowValue(@NotNull List<Expr> values) {
this.values = values;
}

@Override
@NotNull
public Collection<AstNode> children() {
return new ArrayList<>(values);
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitExprRowValue(this, ctx);
}
}
30 changes: 3 additions & 27 deletions partiql-ast/src/main/java/org/partiql/ast/v1/expr/ExprValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

/**
* TODO docs, equals, hashcode
* Also may not be an [Expr]?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(self-review): at some point before v1, need to take a closer look at the VALUES modeling. I'm not sure yet if it should be an Expr or something different.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subqueries generally need to be properly supported. #1641

*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public class ExprValues extends Expr {
@NotNull
public final List<Row> rows;
public final List<ExprRowValue> rows;

public ExprValues(@NotNull List<Row> rows) {
public ExprValues(@NotNull List<ExprRowValue> rows) {
this.rows = rows;
}

Expand All @@ -33,29 +34,4 @@ public Collection<AstNode> children() {
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitExprValues(this, ctx);
}

/**
* TODO docs, equals, hashcode
*/
@lombok.Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static class Row extends AstNode {
@NotNull
public final List<Expr> values;

public Row(@NotNull List<Expr> values) {
this.values = values;
}

@Override
@NotNull
public Collection<AstNode> children() {
return new ArrayList<>(values);
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitExprValuesRow(this, ctx);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.partiql.ast.sql
package org.partiql.ast.v1.sql

import org.partiql.ast.AstNode
import org.partiql.ast.v1.AstNode

/**
* Pretty-print this [AstNode] as SQL text with the given (or standard) [SqlLayout] and [SqlDialect].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* language governing permissions and limitations under the License.
*/

package org.partiql.ast.sql
package org.partiql.ast.v1.sql

/**
* Representation of some textual elements as a token (singly-linked) list.
Expand Down
Loading
Loading