Skip to content

Commit

Permalink
[v1] Replicate PartiQLParserDefault with v1 AST
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Oct 23, 2024
1 parent bb30367 commit 7a68858
Show file tree
Hide file tree
Showing 15 changed files with 3,020 additions and 584 deletions.
16 changes: 9 additions & 7 deletions partiql-ast/api/partiql-ast.api
Original file line number Diff line number Diff line change
Expand Up @@ -5593,7 +5593,7 @@ public final class org/partiql/ast/v1/Ast {
public static final fun exprWindowOver (Ljava/util/List;Ljava/util/List;)Lorg/partiql/ast/v1/expr/ExprWindow$Over;
public static final fun from (Ljava/util/List;)Lorg/partiql/ast/v1/From;
public static final fun fromExpr (Lorg/partiql/ast/v1/expr/Expr;Lorg/partiql/ast/v1/FromType;Lorg/partiql/ast/v1/Identifier;Lorg/partiql/ast/v1/Identifier;)Lorg/partiql/ast/v1/FromExpr;
public static final fun fromJoin (Lorg/partiql/ast/v1/From;Lorg/partiql/ast/v1/From;Lorg/partiql/ast/v1/JoinType;Lorg/partiql/ast/v1/expr/Expr;)Lorg/partiql/ast/v1/FromJoin;
public static final fun fromJoin (Lorg/partiql/ast/v1/FromTableRef;Lorg/partiql/ast/v1/FromTableRef;Lorg/partiql/ast/v1/JoinType;Lorg/partiql/ast/v1/expr/Expr;)Lorg/partiql/ast/v1/FromJoin;
public static final fun graphLabelConj (Lorg/partiql/ast/v1/graph/GraphLabel;Lorg/partiql/ast/v1/graph/GraphLabel;)Lorg/partiql/ast/v1/graph/GraphLabel$Conj;
public static final fun graphLabelDisj (Lorg/partiql/ast/v1/graph/GraphLabel;Lorg/partiql/ast/v1/graph/GraphLabel;)Lorg/partiql/ast/v1/graph/GraphLabel$Disj;
public static final fun graphLabelName (Ljava/lang/String;)Lorg/partiql/ast/v1/graph/GraphLabel$Name;
Expand Down Expand Up @@ -5737,7 +5737,7 @@ public abstract interface class org/partiql/ast/v1/AstVisitor {
public abstract fun visitTableRef (Lorg/partiql/ast/v1/FromTableRef;Ljava/lang/Object;)Ljava/lang/Object;
}

public class org/partiql/ast/v1/DataType : org/partiql/ast/v1/Enum {
public class org/partiql/ast/v1/DataType : org/partiql/ast/v1/AstNode, org/partiql/ast/v1/Enum {
public static final field BAG I
public static final field BIGINT I
public static final field BINARY_LARGE_OBJECT I
Expand Down Expand Up @@ -5859,7 +5859,9 @@ public class org/partiql/ast/v1/DataType : org/partiql/ast/v1/Enum {
public static fun USER_DEFINED (Lorg/partiql/ast/v1/IdentifierChain;)Lorg/partiql/ast/v1/DataType;
public static fun VARCHAR ()Lorg/partiql/ast/v1/DataType;
public static fun VARCHAR (I)Lorg/partiql/ast/v1/DataType;
public fun accept (Lorg/partiql/ast/v1/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
protected fun canEqual (Ljava/lang/Object;)Z
public fun children ()Ljava/util/Collection;
public fun code ()I
public fun equals (Ljava/lang/Object;)Z
public fun getLength ()Ljava/lang/Integer;
Expand Down Expand Up @@ -6054,9 +6056,9 @@ public class org/partiql/ast/v1/FromExpr$Builder {
public class org/partiql/ast/v1/FromJoin : org/partiql/ast/v1/FromTableRef {
public final field condition Lorg/partiql/ast/v1/expr/Expr;
public final field joinType Lorg/partiql/ast/v1/JoinType;
public final field lhs Lorg/partiql/ast/v1/From;
public final field rhs Lorg/partiql/ast/v1/From;
public fun <init> (Lorg/partiql/ast/v1/From;Lorg/partiql/ast/v1/From;Lorg/partiql/ast/v1/JoinType;Lorg/partiql/ast/v1/expr/Expr;)V
public final field lhs Lorg/partiql/ast/v1/FromTableRef;
public final field rhs Lorg/partiql/ast/v1/FromTableRef;
public fun <init> (Lorg/partiql/ast/v1/FromTableRef;Lorg/partiql/ast/v1/FromTableRef;Lorg/partiql/ast/v1/JoinType;Lorg/partiql/ast/v1/expr/Expr;)V
public fun accept (Lorg/partiql/ast/v1/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public static fun builder ()Lorg/partiql/ast/v1/FromJoin$Builder;
protected fun canEqual (Ljava/lang/Object;)Z
Expand All @@ -6069,8 +6071,8 @@ public class org/partiql/ast/v1/FromJoin$Builder {
public fun build ()Lorg/partiql/ast/v1/FromJoin;
public fun condition (Lorg/partiql/ast/v1/expr/Expr;)Lorg/partiql/ast/v1/FromJoin$Builder;
public fun joinType (Lorg/partiql/ast/v1/JoinType;)Lorg/partiql/ast/v1/FromJoin$Builder;
public fun lhs (Lorg/partiql/ast/v1/From;)Lorg/partiql/ast/v1/FromJoin$Builder;
public fun rhs (Lorg/partiql/ast/v1/From;)Lorg/partiql/ast/v1/FromJoin$Builder;
public fun lhs (Lorg/partiql/ast/v1/FromTableRef;)Lorg/partiql/ast/v1/FromJoin$Builder;
public fun rhs (Lorg/partiql/ast/v1/FromTableRef;)Lorg/partiql/ast/v1/FromJoin$Builder;
public fun toString ()Ljava/lang/String;
}

Expand Down
2 changes: 1 addition & 1 deletion partiql-ast/src/main/java/org/partiql/ast/v1/Ast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public object Ast {
}

@JvmStatic
public fun fromJoin(lhs: From, rhs: From, joinType: JoinType?, condition: Expr?): FromJoin {
public fun fromJoin(lhs: FromTableRef, rhs: FromTableRef, joinType: JoinType?, condition: Expr?): FromJoin {
return FromJoin(lhs, rhs, joinType, condition)
}

Expand Down
21 changes: 20 additions & 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 @@ -3,8 +3,12 @@
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;

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

@EqualsAndHashCode(callSuper = false)
public class DataType implements Enum {
public class DataType extends AstNode implements Enum {
public static final int UNKNOWN = 0;
// <absent types>
public static final int NULL = 1;
Expand Down Expand Up @@ -530,4 +534,19 @@ public Integer getLength() {
public IdentifierChain getName() {
return name;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
if (name != null) {
kids.add(name);
}
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return null;
}
}
6 changes: 3 additions & 3 deletions partiql-ast/src/main/java/org/partiql/ast/v1/FromJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
@EqualsAndHashCode(callSuper = false)
public class FromJoin extends FromTableRef {
@NotNull
public final From lhs;
public final FromTableRef lhs;

@NotNull
public final From rhs;
public final FromTableRef rhs;

@Nullable
public final JoinType joinType;

@Nullable
public final Expr condition;

public FromJoin(@NotNull From lhs, @NotNull From rhs, @Nullable JoinType joinType, @Nullable Expr condition) {
public FromJoin(@NotNull FromTableRef lhs, @NotNull FromTableRef rhs, @Nullable JoinType joinType, @Nullable Expr condition) {
this.lhs = lhs;
this.rhs = rhs;
this.joinType = joinType;
Expand Down
32 changes: 32 additions & 0 deletions partiql-parser/api/partiql-parser.api
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,35 @@ public final class org/partiql/parser/SourceLocations : java/util/Map, kotlin/jv
public final fun values ()Ljava/util/Collection;
}

public abstract interface class org/partiql/parser/V1PartiQLParser {
public static final field Companion Lorg/partiql/parser/V1PartiQLParser$Companion;
public static fun builder ()Lorg/partiql/parser/V1PartiQLParserBuilder;
public abstract fun parse (Ljava/lang/String;)Lorg/partiql/parser/V1PartiQLParser$Result;
public static fun standard ()Lorg/partiql/parser/V1PartiQLParser;
}

public final class org/partiql/parser/V1PartiQLParser$Companion {
public final fun builder ()Lorg/partiql/parser/V1PartiQLParserBuilder;
public final fun standard ()Lorg/partiql/parser/V1PartiQLParser;
}

public final class org/partiql/parser/V1PartiQLParser$Result {
public fun <init> (Ljava/lang/String;Lorg/partiql/ast/v1/Statement;Lorg/partiql/parser/SourceLocations;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Lorg/partiql/ast/v1/Statement;
public final fun component3 ()Lorg/partiql/parser/SourceLocations;
public final fun copy (Ljava/lang/String;Lorg/partiql/ast/v1/Statement;Lorg/partiql/parser/SourceLocations;)Lorg/partiql/parser/V1PartiQLParser$Result;
public static synthetic fun copy$default (Lorg/partiql/parser/V1PartiQLParser$Result;Ljava/lang/String;Lorg/partiql/ast/v1/Statement;Lorg/partiql/parser/SourceLocations;ILjava/lang/Object;)Lorg/partiql/parser/V1PartiQLParser$Result;
public fun equals (Ljava/lang/Object;)Z
public final fun getLocations ()Lorg/partiql/parser/SourceLocations;
public final fun getRoot ()Lorg/partiql/ast/v1/Statement;
public final fun getSource ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class org/partiql/parser/V1PartiQLParserBuilder {
public fun <init> ()V
public final fun build ()Lorg/partiql/parser/V1PartiQLParser;
}

26 changes: 12 additions & 14 deletions partiql-parser/src/main/antlr/PartiQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ excludeExprSteps
;

fromClause
: FROM tableReference;
: FROM ( tableReference ( COMMA tableReference)* );

whereClauseSelect
: WHERE arg=exprSelect;
Expand Down Expand Up @@ -468,16 +468,15 @@ edgeAbbrev
*/

tableReference
: lhs=tableReference joinType? CROSS JOIN rhs=joinRhs # TableCrossJoin
| lhs=tableReference COMMA rhs=joinRhs # TableCrossJoin
| lhs=tableReference joinType? JOIN rhs=joinRhs joinSpec # TableQualifiedJoin
| tableNonJoin # TableRefBase
| PAREN_LEFT tableReference PAREN_RIGHT # TableWrapped
: tablePrimary # TableRefPrimary
| lhs=tableReference CROSS JOIN rhs=tablePrimary # TableCrossJoin
| lhs=tableReference joinType? JOIN rhs=tableReference joinSpec # TableQualifiedJoin
;

tableNonJoin
tablePrimary
: tableBaseReference
| tableUnpivot
| tableWrapped
;

tableBaseReference
Expand All @@ -487,15 +486,11 @@ tableBaseReference
;

tableUnpivot
: UNPIVOT expr asIdent? atIdent? byIdent?;

joinRhs
: tableNonJoin # JoinRhsBase
| PAREN_LEFT tableReference PAREN_RIGHT # JoinRhsTableJoined
: UNPIVOT expr asIdent? atIdent? byIdent?
;

joinSpec
: ON expr;
tableWrapped
: PAREN_LEFT tableReference PAREN_RIGHT;

joinType
: mod=INNER
Expand All @@ -505,6 +500,9 @@ joinType
| mod=OUTER
;

joinSpec
: ON expr;

/**
*
* EXPRESSIONS & PRECEDENCE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at:
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/

package org.partiql.parser

import org.partiql.ast.v1.Statement
import org.partiql.parser.internal.V1PartiQLParserDefault

public interface V1PartiQLParser {

@Throws(PartiQLSyntaxException::class, InterruptedException::class)
public fun parse(source: String): Result

public data class Result(
val source: String,
val root: Statement,
val locations: SourceLocations,
)

public companion object {

@JvmStatic
public fun builder(): V1PartiQLParserBuilder = V1PartiQLParserBuilder()

@JvmStatic
public fun standard(): V1PartiQLParser = V1PartiQLParserDefault()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at:
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/

package org.partiql.parser

import org.partiql.parser.internal.V1PartiQLParserDefault

/**
* A builder class to instantiate a [V1PartiQLParser].
*/
public class V1PartiQLParserBuilder {

public fun build(): V1PartiQLParser {
return V1PartiQLParserDefault()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,12 @@ internal class PartiQLParserDefault : PartiQLParser {
*
*/

override fun visitFromClause(ctx: GeneratedParser.FromClauseContext) = visitAs<From>(ctx.tableReference())
override fun visitFromClause(ctx: GeneratedParser.FromClauseContext) = translate(ctx) {
val tableRefs = visitOrEmpty<From>(ctx.tableReference())
tableRefs.drop(1).fold(tableRefs.first()) { acc, tableRef ->
fromJoin(acc, tableRef, From.Join.Type.CROSS, null)
}
}

override fun visitTableBaseRefClauses(ctx: GeneratedParser.TableBaseRefClausesContext) = translate(ctx) {
val expr = visitAs<Expr>(ctx.source)
Expand Down Expand Up @@ -1276,8 +1281,7 @@ internal class PartiQLParserDefault : PartiQLParser {
override fun visitTableCrossJoin(ctx: GeneratedParser.TableCrossJoinContext) = translate(ctx) {
val lhs = visitAs<From>(ctx.lhs)
val rhs = visitAs<From>(ctx.rhs)
val type = convertJoinType(ctx.joinType())
fromJoin(lhs, rhs, type, null)
fromJoin(lhs, rhs, null, null)
}

private fun convertJoinType(ctx: GeneratedParser.JoinTypeContext?): From.Join.Type? {
Expand Down Expand Up @@ -1323,9 +1327,6 @@ internal class PartiQLParserDefault : PartiQLParser {

override fun visitJoinSpec(ctx: GeneratedParser.JoinSpecContext) = visitExpr(ctx.expr())

override fun visitJoinRhsTableJoined(ctx: GeneratedParser.JoinRhsTableJoinedContext) =
visitAs<From>(ctx.tableReference())

/**
* SIMPLE EXPRESSIONS
*/
Expand Down
Loading

0 comments on commit 7a68858

Please sign in to comment.