Skip to content

Commit

Permalink
Change lit to extend AstEnum; rename factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Dec 6, 2024
1 parent f9a25b6 commit 55ec171
Show file tree
Hide file tree
Showing 29 changed files with 442 additions and 644 deletions.
97 changes: 42 additions & 55 deletions partiql-ast/api/partiql-ast.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class org/partiql/ast/Ast {
public static final fun exprInCollection (Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;Z)Lorg/partiql/ast/expr/ExprInCollection;
public static final fun exprIsType (Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/DataType;Z)Lorg/partiql/ast/expr/ExprIsType;
public static final fun exprLike (Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;Z)Lorg/partiql/ast/expr/ExprLike;
public static final fun exprLit (Lorg/partiql/ast/literal/Literal;)Lorg/partiql/ast/expr/ExprLit;
public static final fun exprLit (Lorg/partiql/ast/Literal;)Lorg/partiql/ast/expr/ExprLit;
public static final fun exprMatch (Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/graph/GraphMatch;)Lorg/partiql/ast/expr/ExprMatch;
public static final fun exprNot (Lorg/partiql/ast/expr/Expr;)Lorg/partiql/ast/expr/ExprNot;
public static final fun exprNullIf (Lorg/partiql/ast/expr/Expr;Lorg/partiql/ast/expr/Expr;)Lorg/partiql/ast/expr/ExprNullIf;
Expand Down Expand Up @@ -233,8 +233,8 @@ public abstract class org/partiql/ast/AstRewriter : org/partiql/ast/AstVisitor {
public fun visitLet (Lorg/partiql/ast/Let;Ljava/lang/Object;)Lorg/partiql/ast/AstNode;
public synthetic fun visitLetBinding (Lorg/partiql/ast/Let$Binding;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLetBinding (Lorg/partiql/ast/Let$Binding;Ljava/lang/Object;)Lorg/partiql/ast/AstNode;
public synthetic fun visitLiteral (Lorg/partiql/ast/literal/Literal;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLiteral (Lorg/partiql/ast/literal/Literal;Ljava/lang/Object;)Lorg/partiql/ast/AstNode;
public synthetic fun visitLiteral (Lorg/partiql/ast/Literal;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLiteral (Lorg/partiql/ast/Literal;Ljava/lang/Object;)Lorg/partiql/ast/AstNode;
public synthetic fun visitPathStepAllElements (Lorg/partiql/ast/expr/PathStep$AllElements;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitPathStepAllElements (Lorg/partiql/ast/expr/PathStep$AllElements;Ljava/lang/Object;)Lorg/partiql/ast/AstNode;
public synthetic fun visitPathStepAllFields (Lorg/partiql/ast/expr/PathStep$AllFields;Ljava/lang/Object;)Ljava/lang/Object;
Expand Down Expand Up @@ -350,7 +350,7 @@ public abstract class org/partiql/ast/AstVisitor {
public fun visitKeyValue (Lorg/partiql/ast/ddl/KeyValue;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLet (Lorg/partiql/ast/Let;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLetBinding (Lorg/partiql/ast/Let$Binding;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLiteral (Lorg/partiql/ast/literal/Literal;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitLiteral (Lorg/partiql/ast/Literal;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitNullable (Lorg/partiql/ast/ddl/AttributeConstraint$Null;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitOrderBy (Lorg/partiql/ast/OrderBy;Ljava/lang/Object;)Ljava/lang/Object;
public fun visitPartitionBy (Lorg/partiql/ast/ddl/PartitionBy;Ljava/lang/Object;)Ljava/lang/Object;
Expand Down Expand Up @@ -932,6 +932,42 @@ public class org/partiql/ast/Let$Builder {
public fun toString ()Ljava/lang/String;
}

public class org/partiql/ast/Literal : org/partiql/ast/AstEnum {
public static final field APPROX_NUM I
public static final field BOOL I
public static final field EXACT_NUM I
public static final field INT_NUM I
public static final field MISSING I
public static final field NULL I
public static final field STRING I
public static final field TYPED_STRING I
public static final field UNKNOWN I
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public static fun approxNum (Ljava/lang/String;)Lorg/partiql/ast/Literal;
public fun bigDecimalValue ()Ljava/math/BigDecimal;
public static fun bool (Z)Lorg/partiql/ast/Literal;
public fun booleanValue ()Z
protected fun canEqual (Ljava/lang/Object;)Z
public fun children ()Ljava/util/Collection;
public fun code ()I
public fun dataType ()Lorg/partiql/ast/DataType;
public fun equals (Ljava/lang/Object;)Z
public static fun exactNum (Ljava/lang/String;)Lorg/partiql/ast/Literal;
public static fun exactNum (Ljava/math/BigDecimal;)Lorg/partiql/ast/Literal;
public fun hashCode ()I
public static fun intNum (I)Lorg/partiql/ast/Literal;
public static fun intNum (J)Lorg/partiql/ast/Literal;
public static fun intNum (Ljava/lang/String;)Lorg/partiql/ast/Literal;
public static fun intNum (Ljava/math/BigInteger;)Lorg/partiql/ast/Literal;
public static fun missing ()Lorg/partiql/ast/Literal;
public fun name ()Ljava/lang/String;
public static fun nul ()Lorg/partiql/ast/Literal;
public fun numberValue ()Ljava/lang/String;
public static fun string (Ljava/lang/String;)Lorg/partiql/ast/Literal;
public fun stringValue ()Ljava/lang/String;
public static fun typedString (Lorg/partiql/ast/DataType;Ljava/lang/String;)Lorg/partiql/ast/Literal;
}

public class org/partiql/ast/Nulls : org/partiql/ast/AstEnum {
public static final field FIRST I
public static final field LAST I
Expand Down Expand Up @@ -1660,8 +1696,8 @@ public class org/partiql/ast/expr/ExprLike$Builder {
}

public class org/partiql/ast/expr/ExprLit : org/partiql/ast/expr/Expr {
public field lit Lorg/partiql/ast/literal/Literal;
public fun <init> (Lorg/partiql/ast/literal/Literal;)V
public field lit Lorg/partiql/ast/Literal;
public fun <init> (Lorg/partiql/ast/Literal;)V
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
protected fun canEqual (Ljava/lang/Object;)Z
public fun children ()Ljava/util/Collection;
Expand Down Expand Up @@ -2565,55 +2601,6 @@ public class org/partiql/ast/graph/GraphSelector$ShortestKGroup$Builder {
public fun toString ()Ljava/lang/String;
}

public abstract class org/partiql/ast/literal/Literal : org/partiql/ast/AstNode {
public fun <init> ()V
public fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public fun bigDecimalValue ()Ljava/math/BigDecimal;
public fun booleanValue ()Z
public fun children ()Ljava/util/Collection;
public fun dataType ()Lorg/partiql/ast/DataType;
public abstract fun kind ()Lorg/partiql/ast/literal/LiteralKind;
public static fun litApprox (Ljava/lang/String;)Lorg/partiql/ast/literal/Literal;
public static fun litBool (Z)Lorg/partiql/ast/literal/Literal;
public static fun litExact (Ljava/lang/String;)Lorg/partiql/ast/literal/Literal;
public static fun litExact (Ljava/math/BigDecimal;)Lorg/partiql/ast/literal/Literal;
public static fun litInt (I)Lorg/partiql/ast/literal/Literal;
public static fun litInt (J)Lorg/partiql/ast/literal/Literal;
public static fun litInt (Ljava/lang/String;)Lorg/partiql/ast/literal/Literal;
public static fun litInt (Ljava/math/BigInteger;)Lorg/partiql/ast/literal/Literal;
public static fun litMissing ()Lorg/partiql/ast/literal/Literal;
public static fun litNull ()Lorg/partiql/ast/literal/Literal;
public static fun litString (Ljava/lang/String;)Lorg/partiql/ast/literal/Literal;
public static fun litTypedString (Lorg/partiql/ast/DataType;Ljava/lang/String;)Lorg/partiql/ast/literal/Literal;
public fun numberValue ()Ljava/lang/String;
public fun stringValue ()Ljava/lang/String;
}

public class org/partiql/ast/literal/LiteralKind {
public static final field BOOLEAN I
public static final field MISSING I
public static final field NULL I
public static final field NUM_APPROX I
public static final field NUM_EXACT I
public static final field NUM_INT I
public static final field STRING I
public static final field TYPED_STRING I
public static final field UNKNOWN I
public static fun BOOLEAN ()Lorg/partiql/ast/literal/LiteralKind;
public static fun MISSING ()Lorg/partiql/ast/literal/LiteralKind;
public static fun NULL ()Lorg/partiql/ast/literal/LiteralKind;
public static fun NUM_APPROX ()Lorg/partiql/ast/literal/LiteralKind;
public static fun NUM_EXACT ()Lorg/partiql/ast/literal/LiteralKind;
public static fun NUM_INT ()Lorg/partiql/ast/literal/LiteralKind;
public static fun STRING ()Lorg/partiql/ast/literal/LiteralKind;
public static fun TYPED_STRING ()Lorg/partiql/ast/literal/LiteralKind;
public static fun UNKNOWN ()Lorg/partiql/ast/literal/LiteralKind;
protected fun canEqual (Ljava/lang/Object;)Z
public fun code ()I
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
}

public abstract class org/partiql/ast/sql/SqlBlock {
public static final field Companion Lorg/partiql/ast/sql/SqlBlock$Companion;
public field next Lorg/partiql/ast/sql/SqlBlock;
Expand Down
1 change: 0 additions & 1 deletion partiql-ast/src/main/java/org/partiql/ast/AstVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.partiql.ast.graph.GraphPattern;
import org.partiql.ast.graph.GraphQuantifier;
import org.partiql.ast.graph.GraphSelector;
import org.partiql.ast.literal.Literal;

// TODO docs
// Also include docs on how a library user could create a new variant for sum types and which methods to override
Expand Down
1 change: 0 additions & 1 deletion partiql-ast/src/main/java/org/partiql/ast/Explain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.partiql.ast.literal.Literal;

import java.util.ArrayList;
import java.util.Collection;
Expand Down
242 changes: 242 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/Literal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package org.partiql.ast;

import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;

import static java.util.Objects.requireNonNull;

/**
* TODO docs
*/
@EqualsAndHashCode(callSuper = false)
public class Literal extends AstEnum {
public static final int UNKNOWN = 0;
// absent literals
public static final int NULL = 1;
public static final int MISSING = 2;
// boolean literal
public static final int BOOL = 3;
// numeric literals
public static final int APPROX_NUM = 4;
public static final int EXACT_NUM = 5;
public static final int INT_NUM = 6;
// string literal
public static final int STRING = 7;
// typed string literal
public static final int TYPED_STRING = 8;

// Literal fields
private final int code;
private final Boolean boolValue;
private final String stringValue;
private final DataType dataType;

/////// Constructors
private Literal(int code, Boolean value, String stringValue, DataType dataType) {
this.code = code;
this.boolValue = value;
this.stringValue = stringValue;
this.dataType = dataType;
}

// Private constructor for absent literals
private Literal(int code) {
this.code = code;
// Rest set to null
this.boolValue = null;
this.stringValue = null;
this.dataType = null;
}

// Private constructor for boolean literal
private Literal(boolean value) {
this.code = BOOL;
this.boolValue = value;
// Rest set to null
this.stringValue = null;
this.dataType = null;
}

// Private constructor for literals stored w/ just a string (e.g. numerics, single-quoted strings)
private Literal(int code, String value) {
this.code = code;
this.stringValue = value;
// Rest set to null
this.boolValue = null;
this.dataType = null;
}

// Private constructor for typed string literal
private Literal(DataType dataType, String value) {
this.code = TYPED_STRING;
this.stringValue = value;
this.dataType = dataType;
// Rest set to null
this.boolValue = null;
}

@Override
public int code() {
return code;
}

@NotNull
@Override
public String name() {
switch (code) {
case NULL: return "NULL";
case MISSING: return "MISSING";
case BOOL: return "BOOL";
case APPROX_NUM: return "APPROX_NUM";
case EXACT_NUM: return "EXACT_NUM";
case INT_NUM: return "INT_NUM";
case STRING: return "STRING";
case TYPED_STRING: return "TYPED_STRING";
default: return "UNKNOWN";
}
}

@Override
@NotNull
public Collection<AstNode> children() {
return Collections.emptyList();
}

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

// Factory methods
public static Literal approxNum(@NotNull String value) {
return new Literal(APPROX_NUM, value);
}

public static Literal bool(boolean value) {
return new Literal(value);
}

public static Literal exactNum(@NotNull BigDecimal value) {
if (value.scale() == 0) {
return new Literal(EXACT_NUM, value + ".");
} else {
return new Literal(EXACT_NUM, value.toString());
}
}

public static Literal exactNum(@NotNull String value) {
return new Literal(EXACT_NUM, value);
}

public static Literal intNum(int value) {
return new Literal(INT_NUM, Integer.toString(value));
}

public static Literal intNum(long value) {
return new Literal(INT_NUM, Long.toString(value));
}

public static Literal intNum(@NotNull BigInteger value) {
return new Literal(INT_NUM, value.toString());
}

public static Literal intNum(@NotNull String value) {
return new Literal(INT_NUM, value);
}

public static Literal nul() {
return new Literal(NULL);
}

public static Literal missing() {
return new Literal(MISSING);
}

public static Literal string(@NotNull String value) {
return new Literal(STRING, value);
}

public static Literal typedString(@NotNull DataType type, @NotNull String value) {
return new Literal(type, value);
}

// Value extraction
/**
* TODO docs
* Valid for just BOOL
*/
public boolean booleanValue() {
if (code == BOOL) {
requireNonNull(boolValue, "bool value");
return boolValue;
}
throw new UnsupportedOperationException();
}

/**
* TODO docs
* Valid for just APPROX_NUM, EXACT_NUM, and INT_NUM.
*/
@NotNull
public String numberValue() {
switch (code) {
case APPROX_NUM:
case EXACT_NUM:
case INT_NUM:
requireNonNull(stringValue, "string value for numerics");
return stringValue;
default:
throw new UnsupportedOperationException();
}
}

/**
* TODO docs
* Valid for just EXACT_NUM and INT_NUM
*/
@NotNull
public BigDecimal bigDecimalValue() {
switch (code) {
case EXACT_NUM:
case INT_NUM:
requireNonNull(stringValue, "string value for exact and int numerics");
return new BigDecimal(stringValue);
default:
throw new UnsupportedOperationException();
}
}

/**
* TODO docs
* Valid for just STRING and TYPED_STRING
*/
@NotNull
public String stringValue() {
switch (code) {
case STRING:
case TYPED_STRING:
requireNonNull(stringValue, "string value");
return stringValue;
default:
throw new UnsupportedOperationException();
}
}

/**
* TODO docs
* Valid for just TYPED_STRING
*/
@NotNull
public DataType dataType() {
if (code == TYPED_STRING) {
requireNonNull(dataType, "data type");
return dataType;
}
throw new UnsupportedOperationException();
}
}
Loading

0 comments on commit 55ec171

Please sign in to comment.