Skip to content

Commit

Permalink
Added parsing support for Create Table Statement (#1652)
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Cai <[email protected]>
  • Loading branch information
yliuuuu and alancai98 authored Dec 4, 2024
1 parent 443c3eb commit 48a9ee3
Show file tree
Hide file tree
Showing 17 changed files with 1,985 additions and 117 deletions.
182 changes: 182 additions & 0 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/AstVisitor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.partiql.ast;

import org.partiql.ast.ddl.AttributeConstraint;
import org.partiql.ast.ddl.ColumnDefinition;
import org.partiql.ast.ddl.CreateTable;
import org.partiql.ast.ddl.Ddl;
import org.partiql.ast.ddl.KeyValue;
import org.partiql.ast.ddl.PartitionBy;
import org.partiql.ast.ddl.TableConstraint;
import org.partiql.ast.expr.Expr;
import org.partiql.ast.expr.ExprAnd;
import org.partiql.ast.expr.ExprArray;
Expand Down Expand Up @@ -61,6 +68,50 @@ public R visitStatement(Statement node, C ctx) {
return node.accept(this, ctx);
}

//
// DDL
//
public R visitDdl(Ddl node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitCreateTable(CreateTable node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitColumnDefinition(ColumnDefinition node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUnique(TableConstraint.Unique node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitNullable(AttributeConstraint.Null node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitKeyValue(KeyValue node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitPartitionBy(PartitionBy node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUnique(AttributeConstraint.Unique node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitCheck(AttributeConstraint.Check node, C ctx) {
return defaultVisit(node, ctx);
}


//
// END OF DDL
//

public R visitQuery(Query node, C ctx) {
return defaultVisit(node, ctx);
}
Expand Down Expand Up @@ -440,4 +491,8 @@ public R visitGraphLabelDisj(GraphLabel.Disj node, C ctx) {
public R visitDataType(DataType node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitStructField(DataType.StructField node, C ctx) {
return defaultVisit(node, ctx);
}
}
129 changes: 129 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,74 @@

import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.partiql.ast.ddl.AttributeConstraint;

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

@EqualsAndHashCode(callSuper = false)
public class DataType extends AstEnum {

/**
* A field definition with in a Struct Type Definition
*/
// At the moment, this is identical to column definition;
// But we split those into two classes for the following reason:
// 1. potentially feature addition to the columnDefinition node: See SQL-99 Grammar
// <column definition> ::=
// <column name>
// { <data type> | <domain name> }
// [ <reference scope check> ]
// [ <default clause> ]
// [ <column constraint definition> ... ]
// [ <collate clause> ]
// 2. the semantics of parameterized struct type has not been finalized,
// and the fact that parameterized struct type being an extension to SQL-99.
@EqualsAndHashCode(callSuper = false)
public static class StructField extends AstNode {
@NotNull
public final Identifier name;
@NotNull
public final DataType type;

public final boolean isOptional;

@Nullable
public final List<AttributeConstraint> constraints;
@Nullable
public final String comment;

public StructField(
@NotNull Identifier name,
@NotNull DataType type,
boolean isOptional,
@Nullable List<AttributeConstraint> constraints,
@Nullable String comment) {
this.name = name;
this.type = type;
this.isOptional = isOptional;
this.constraints = constraints;
this.comment = comment;
}

@NotNull
@Override
public Collection<AstNode> children() {
ArrayList<AstNode> kids = new ArrayList<>();
kids.add(name);
kids.add(type);
kids.addAll(constraints);
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitStructField(this, ctx);
}
}

public static final int UNKNOWN = 0;
// TODO remove `NULL` and `MISSING` variants from DataType
// <absent types>
Expand Down Expand Up @@ -66,6 +127,7 @@ public class DataType extends AstEnum {
public static final int TUPLE = 43;
// <collection type>
public static final int LIST = 44;
public static final int ARRAY = 48; // TODO: Fix the numbering
public static final int BAG = 45;
public static final int SEXP = 46;
// <user defined type>
Expand Down Expand Up @@ -311,6 +373,10 @@ public static DataType LIST() {
return new DataType(LIST);
}

public static DataType ARRAY() {
return new DataType(ARRAY);
}

public static DataType SEXP() {
return new DataType(SEXP);
}
Expand Down Expand Up @@ -363,10 +429,21 @@ public static DataType USER_DEFINED(@NotNull IdentifierChain name) {
return new DataType(USER_DEFINED, name);
}

// Parameterized Complex Data Type
public static DataType ARRAY(DataType elementType) {
return new DataType(ARRAY, elementType);
}

public static DataType STRUCT(List<StructField> fields) {
return new DataType(STRUCT, fields);
}

private final int code;
private final Integer precision;
private final Integer scale;
private final Integer length;
private final DataType elementType;
private final List<StructField> fields;
private final IdentifierChain name;

// Private constructor for no parameter DataTypes
Expand All @@ -375,6 +452,8 @@ private DataType(int code) {
this.precision = null;
this.scale = null;
this.length = null;
this.elementType = null;
this.fields = null;
this.name = null;
}

Expand All @@ -384,6 +463,29 @@ private DataType(int code, Integer precision, Integer scale, Integer length) {
this.precision = precision;
this.scale = scale;
this.length = length;
this.elementType = null;
this.fields = null;
this.name = null;
}

// Private constructor for DataTypes with elementType parameter; set `name` to null
private DataType(int code, DataType elementType) {
this.code = code;
this.precision = null;
this.scale = null;
this.length = null;
this.elementType = elementType;
this.fields = null;
this.name = null;
}

private DataType(int code, List<StructField> fields) {
this.code = code;
this.precision = null;
this.scale = null;
this.length = null;
this.elementType = null;
this.fields = fields;
this.name = null;
}

Expand All @@ -394,6 +496,8 @@ private DataType(int code, IdentifierChain name) {
this.precision = null;
this.scale = null;
this.length = null;
this.elementType = null;
this.fields = null;
}

@Override
Expand Down Expand Up @@ -449,6 +553,7 @@ public String name() {
case STRUCT: return "STRUCT";
case TUPLE: return "TUPLE";
case LIST: return "LIST";
case ARRAY: return "ARRAY";
case BAG: return "BAG";
case SEXP: return "SEXP";
case USER_DEFINED: return "USER_DEFINED";
Expand Down Expand Up @@ -502,6 +607,7 @@ public String name() {
STRUCT,
TUPLE,
LIST,
ARRAY,
BAG,
SEXP,
USER_DEFINED
Expand Down Expand Up @@ -548,6 +654,7 @@ public static DataType parse(@NotNull String value) {
case "STRUCT": return STRUCT();
case "TUPLE": return TUPLE();
case "LIST": return LIST();
case "ARRAY": return ARRAY();
case "SEXP": return SEXP();
case "BAG": return BAG();
case "TIME": return TIME();
Expand Down Expand Up @@ -597,13 +704,35 @@ public IdentifierChain getName() {
return name;
}

/**
* TODO docs
* @return
*/
public DataType getElementType() {
return elementType;
}

/**
* TODO docs
* @return
*/
public List<StructField> getFields() {
return fields;
}

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

Expand Down
Loading

0 comments on commit 48a9ee3

Please sign in to comment.