Skip to content

Commit

Permalink
[v1] Add .valueOf and .values methods for enums (#1628)
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 authored Oct 25, 2024
1 parent 03905db commit 7f382db
Show file tree
Hide file tree
Showing 18 changed files with 788 additions and 50 deletions.
100 changes: 81 additions & 19 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/v1/AstEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.partiql.ast.v1;

/**
* TODO docs, equals, hashcode
*/
public abstract class AstEnum extends AstNode {
public abstract int code();
}
136 changes: 134 additions & 2 deletions partiql-ast/src/main/java/org/partiql/ast/v1/DataType.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.partiql.ast.v1;

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 AstEnum {
public static final int UNKNOWN = 0;
// <absent types>
public static final int NULL = 1;
Expand Down Expand Up @@ -349,7 +354,11 @@ public static DataType INTERVAL() {
return new DataType(INTERVAL);
}

public static DataType USER_DEFINED(IdentifierChain name) {
public static DataType USER_DEFINED() {
return new DataType(USER_DEFINED);
}

public static DataType USER_DEFINED(@NotNull IdentifierChain name) {
return new DataType(USER_DEFINED, name);
}

Expand Down Expand Up @@ -391,6 +400,114 @@ public int code() {
return code;
}

@NotNull
private static final int[] codes = {
NULL,
MISSING,
BOOL,
BOOLEAN,
TINYINT,
SMALLINT,
INTEGER2,
INT2,
INTEGER,
INT,
INTEGER4,
INT4,
INTEGER8,
INT8,
BIGINT,
REAL,
DOUBLE_PRECISION,
FLOAT,
DECIMAL,
DEC,
NUMERIC,
BIT,
BIT_VARYING,
CHAR,
CHARACTER,
VARCHAR,
CHARACTER_LARGE_OBJECT,
CHAR_LARGE_OBJECT,
CHAR_VARYING,
STRING,
SYMBOL,
BLOB,
BINARY_LARGE_OBJECT,
CLOB,
DATE,
STRUCT,
TUPLE,
LIST,
SEXP,
BAG,
TIME,
TIME_WITH_TIME_ZONE,
TIMESTAMP,
TIMESTAMP_WITH_TIME_ZONE,
INTERVAL,
USER_DEFINED
};

@NotNull
public static DataType parse(@NotNull String value) {
switch (value) {
case "NULL": return NULL();
case "MISSING": return MISSING();
case "BOOL": return BOOL();
case "BOOLEAN": return BOOLEAN();
case "TINYINT": return TINYINT();
case "SMALLINT": return SMALLINT();
case "INTEGER2": return INTEGER2();
case "INT2": return INT2();
case "INTEGER": return INTEGER();
case "INT": return INT();
case "INTEGER4": return INTEGER4();
case "INT4": return INT4();
case "INTEGER8": return INTEGER8();
case "INT8": return INT8();
case "BIGINT": return BIGINT();
case "REAL": return REAL();
case "DOUBLE_PRECISION": return DOUBLE_PRECISION();
case "FLOAT": return FLOAT();
case "DECIMAL": return DECIMAL();
case "DEC": return DEC();
case "NUMERIC": return NUMERIC();
case "BIT": return BIT();
case "BIT_VARYING": return BIT_VARYING();
case "CHAR": return CHAR();
case "CHARACTER": return CHARACTER();
case "VARCHAR": return VARCHAR();
case "CHARACTER_LARGE_OBJECT": return CHARACTER_LARGE_OBJECT();
case "CHAR_LARGE_OBJECT": return CHAR_LARGE_OBJECT();
case "CHAR_VARYING": return CHAR_VARYING();
case "STRING": return STRING();
case "SYMBOL": return SYMBOL();
case "BLOB": return BLOB();
case "BINARY_LARGE_OBJECT": return BINARY_LARGE_OBJECT();
case "CLOB": return CLOB();
case "DATE": return DATE();
case "STRUCT": return STRUCT();
case "TUPLE": return TUPLE();
case "LIST": return LIST();
case "SEXP": return SEXP();
case "BAG": return BAG();
case "TIME": return TIME();
case "TIME_WITH_TIME_ZONE": return TIME_WITH_TIME_ZONE();
case "TIMESTAMP": return TIMESTAMP();
case "TIMESTAMP_WITH_TIME_ZONE": return TIMESTAMP_WITH_TIME_ZONE();
case "INTERVAL": return INTERVAL();
case "USER_DEFINED": return USER_DEFINED();
default: return UNKNOWN();
}
}

@NotNull
public static int[] codes() {
return codes;
}

/**
* TODO docs
* @return
Expand Down Expand Up @@ -422,4 +539,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;
}
}
49 changes: 48 additions & 1 deletion partiql-ast/src/main/java/org/partiql/ast/v1/DatetimeField.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.partiql.ast.v1;

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

import java.util.Collection;
import java.util.Collections;

/**
* TODO docs, equals, hashcode
*/
@EqualsAndHashCode(callSuper = false)
public class DatetimeField implements Enum {
public class DatetimeField extends AstEnum {
public static final int UNKNOWN = 0;
public static final int YEAR = 1;
public static final int MONTH = 2;
Expand Down Expand Up @@ -63,4 +67,47 @@ private DatetimeField(int code) {
public int code() {
return code;
}

@NotNull
private static final int[] codes = {
YEAR,
MONTH,
DAY,
HOUR,
MINUTE,
SECOND,
TIMEZONE_HOUR,
TIMEZONE_MINUTE
};

@NotNull
public static DatetimeField parse(@NotNull String value) {
switch (value) {
case "YEAR": return YEAR();
case "MONTH": return MONTH();
case "DAY": return DAY();
case "HOUR": return HOUR();
case "MINUTE": return MINUTE();
case "SECOND": return SECOND();
case "TIMEZONE_HOUR": return TIMEZONE_HOUR();
case "TIMEZONE_MINUTE": return TIMEZONE_MINUTE();
default: return UNKNOWN();
}
}

@NotNull
public static int[] codes() {
return codes;
}

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

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return null;
}
}
8 changes: 0 additions & 8 deletions partiql-ast/src/main/java/org/partiql/ast/v1/Enum.java

This file was deleted.

37 changes: 36 additions & 1 deletion partiql-ast/src/main/java/org/partiql/ast/v1/FromType.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.partiql.ast.v1;

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

import java.util.Collection;
import java.util.Collections;

/**
* TODO docs, equals, hashcode
*/
@EqualsAndHashCode(callSuper = false)
public class FromType implements Enum {
public class FromType extends AstEnum {
public static final int UNKNOWN = 0;
public static final int SCAN = 1;
public static final int UNPIVOT = 2;
Expand All @@ -33,4 +37,35 @@ private FromType(int code) {
public int code() {
return code;
}

@NotNull
private static final int[] codes = {
SCAN,
UNPIVOT
};

@NotNull
public static FromType parse(@NotNull String value) {
switch (value) {
case "SCAN": return SCAN();
case "UNPIVOT": return UNPIVOT();
default: return UNKNOWN();
}
}

@NotNull
public static int[] codes() {
return codes;
}

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

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

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

import java.util.Collection;
import java.util.Collections;

/**
* TODO docs, equals, hashcode
*/
@EqualsAndHashCode(callSuper = false)
public class GroupByStrategy implements Enum {
public class GroupByStrategy extends AstEnum {
public static final int UNKNOWN = 0;
public static final int FULL = 1;
public static final int PARTIAL = 2;
Expand All @@ -25,6 +29,12 @@ public static GroupByStrategy PARTIAL() {

private final int code;

@NotNull
private static final int[] codes = {
FULL,
PARTIAL
};

private GroupByStrategy(int code) {
this.code = code;
}
Expand All @@ -33,4 +43,29 @@ private GroupByStrategy(int code) {
public int code() {
return code;
}

@NotNull
public static GroupByStrategy parse(@NotNull String value) {
switch (value) {
case "FULL": return FULL();
case "PARTIAL": return PARTIAL();
default: return UNKNOWN();
}
}

@NotNull
public static int[] codes() {
return codes;
}

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

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return null;
}
}
Loading

0 comments on commit 7f382db

Please sign in to comment.