Skip to content

Commit

Permalink
feat: Add support for FLOAT32 column type
Browse files Browse the repository at this point in the history
  • Loading branch information
nielm committed May 2, 2024
1 parent 19be745 commit e5ee7c1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,18 @@ public static String tokensToString(SimpleNode node) {
public static String tokensToString(SimpleNode node, boolean upperCaseReserved) {
return tokensToString(node.jjtGetFirstToken(), node.jjtGetLastToken(), upperCaseReserved);
}

/** Verifies that each child is one of the specified classes. */
public static void validateChildrenClasses(
Node[] children, Set<Class<? extends SimpleNode>> validChildClasses) {
for (Node child : children) {
if (!validChildClasses.contains(child.getClass())) {
throw new IllegalArgumentException(
"Unexpected child node "
+ child.getClass().getSimpleName()
+ " in parent "
+ child.jjtGetParent().getClass().getSimpleName());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public String toString() {

String typeName = getTypeName();
switch (typeName) {
case "FLOAT32":
case "FLOAT64":
case "INT64":
case "BOOL":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -94,21 +95,17 @@ public String getDefinitionWithoutStoring() {
}

private void validateChildren() {
for (Node child : children) {
switch (child.getId()) {
case DdlParserTreeConstants.JJTUNIQUE_INDEX:
case DdlParserTreeConstants.JJTNULL_FILTERED:
case DdlParserTreeConstants.JJTCOLUMNS:
case DdlParserTreeConstants.JJTSTORED_COLUMN_LIST:
case DdlParserTreeConstants.JJTIF_NOT_EXISTS:
case DdlParserTreeConstants.JJTNAME:
case DdlParserTreeConstants.JJTTABLE:
case DdlParserTreeConstants.JJTINDEX_INTERLEAVE_CLAUSE:
break;
default:
throw new IllegalArgumentException("Unknown child node " + child.getClass());
}
}
AstTreeUtils.validateChildrenClasses(
children,
ImmutableSet.of(
ASTunique_index.class,
ASTnull_filtered.class,
ASTcolumns.class,
ASTstored_column_list.class,
ASTif_not_exists.class,
ASTname.class,
ASTtable.class,
ASTindex_interleave_clause.class));
}

public List<String> getStoredColumnNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -97,7 +98,7 @@ public String toString() {

/** Create string version, optionally including the IF NOT EXISTS clause */
public String toStringOptionalExistClause(boolean includeExists) {
verifyTableElements();
validateChildren();

List<String> tabledef = new ArrayList<>();
tabledef.addAll(
Expand Down Expand Up @@ -128,20 +129,18 @@ public String toStringOptionalExistClause(boolean includeExists) {
: null)));
}

private void verifyTableElements() {
for (Node child : children) {
if (!(child instanceof ASTcolumn_def)
&& !(child instanceof ASTforeign_key)
&& !(child instanceof ASTname)
&& !(child instanceof ASTif_not_exists)
&& !(child instanceof ASTcheck_constraint)
&& !(child instanceof ASTprimary_key)
&& !(child instanceof ASTtable_interleave_clause)
&& !(child instanceof ASTrow_deletion_policy_clause)) {
throw new IllegalArgumentException(
"Unknown child type " + child.getClass().getSimpleName() + " - " + child);
}
}
private void validateChildren() {
AstTreeUtils.validateChildrenClasses(
children,
ImmutableSet.of(
ASTcolumn_def.class,
ASTforeign_key.class,
ASTname.class,
ASTif_not_exists.class,
ASTcheck_constraint.class,
ASTprimary_key.class,
ASTtable_interleave_clause.class,
ASTrow_deletion_policy_clause.class));
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/jjtree-sources/ddl_keywords.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ TOKEN:
| <DATABASE: "database">
| <DATE: "date">
| <DAY: "day">
| <DEFINER: "definer">
| <DELETE: "delete">
| <DELETION: "deletion">
| <DROP: "drop">
| <EXECUTE: "execute">
| <FIRST: "first">
| <FLOAT32: "float32">
| <FLOAT64: "float64">
| <FOREIGN: "foreign">
| <FUNCTION: "function">
Expand Down Expand Up @@ -204,11 +206,13 @@ void pseudoReservedWord() #void :
| <DATABASE>
| <DATE>
| <DAY>
| <DEFINER> // Not supported by emulator
| <DELETE>
| <DELETION>
| <DROP>
| <EXECUTE>
| <FIRST>
| <FLOAT32> // Not supported by emulator
| <FLOAT64>
| <FOREIGN>
| <FUNCTION>
Expand Down
4 changes: 3 additions & 1 deletion src/main/jjtree-sources/ddl_parser.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ void column_def_alter() :
void column_type() :
{}
{
<FLOAT64>
<FLOAT32> // not supported by emulator
| <FLOAT64>
| <INT64>
| <BOOL>
| <STRING> "(" column_length() #length ")"
Expand Down Expand Up @@ -562,6 +563,7 @@ void sql_security() :
{}
{
<INVOKER>
| <DEFINER> // Not supported by emulator
}

void create_view_statement() :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void parseCreateTable() throws ParseException {
ASTcreate_table_statement statement =
(ASTcreate_table_statement)
parse(
"create table test.test (boolcol bool, intcol int64 not null, floatcol float64,"
"create table test.test (boolcol bool, intcol int64 not null, "
+ " float32col float32, floatcol float64,"
+ " `sizedstring` string(55), maxstring string(max) NOT NULL DEFAULT"
+ " (\"prefix\" || sizedstring || \"suffix\"), sizedbytes bytes(55),"
+ " maxbytes bytes(max), datecol date, timestampcol timestamp options"
Expand All @@ -55,6 +56,7 @@ public void parseCreateTable() throws ParseException {
+ "(\n"
+ " boolcol BOOL, \n"
+ " intcol INT64 NOT NULL, \n"
+ " float32col FLOAT32, \n"
+ " floatcol FLOAT64, \n"
+ " `sizedstring` STRING(55), \n"
+ " maxstring STRING(MAX) NOT NULL DEFAULT (\"prefix\" || sizedstring ||\n"
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/ddlParserValidation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ CREATE UNIQUE NULL_FILTERED INDEX testindex ON testtable (
CREATE TABLE test.test (
boolcol BOOL,
intcol INT64 NOT NULL,
float32col FLOAT32,
floatcol FLOAT64,
`sizedstring` STRING(55),
maxstring STRING(MAX) NOT NULL DEFAULT ("prefix" | | sizedstring | | "suffix"),
Expand Down

0 comments on commit e5ee7c1

Please sign in to comment.