Skip to content

Commit

Permalink
[v1] Port SqlDialect to v1 AST (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 authored Nov 4, 2024
1 parent d561ece commit ff87a2a
Show file tree
Hide file tree
Showing 15 changed files with 3,373 additions and 2,976 deletions.
513 changes: 217 additions & 296 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

17 changes: 10 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,8 @@ 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.
// Tracking issue for defaults -- https://github.com/partiql/partiql-lang-kotlin/issues/1640.
public object Ast {
// Expr
@JvmStatic
Expand Down Expand Up @@ -113,8 +116,8 @@ public object Ast {
}

@JvmStatic
public fun exprLike(value: Expr, Pattern: Expr, escape: Expr?, not: Boolean): ExprLike {
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 +198,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 {
@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);
}
}
31 changes: 4 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,16 @@

/**
* TODO docs, equals, hashcode
* Also may not be an [Expr]?
* Tracking issue for VALUES and subqueries -- https://github.com/partiql/partiql-lang-kotlin/issues/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 +35,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

0 comments on commit ff87a2a

Please sign in to comment.