Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ASToptions_clause getOptionsClause() {
}

public String getDbName() {
return AstTreeUtils.tokensToString((ASTdatabase_name) jjtGetChild(0));
return AstTreeUtils.tokensToString((ASTdatabase_name) jjtGetChild(0), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ASTannotation(DdlParser p, int id) {
}

public String getName() {
return AstTreeUtils.tokensToString(getChildByType(children, ASTname.class));
return AstTreeUtils.tokensToString(getChildByType(children, ASTname.class), false);
}

public List<ASTannotation_param> getParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public ASTannotation_param(DdlParser p, int id) {

public String getKey() {
ASTparam_key key = AstTreeUtils.getChildByType(this, ASTparam_key.class);
return AstTreeUtils.tokensToString(key);
return AstTreeUtils.tokensToString(key, false);
}

public String getValue() {
ASTparam_val val = AstTreeUtils.getOptionalChildByType(this, ASTparam_val.class);
if (val != null) {
return AstTreeUtils.tokensToString(val);
return AstTreeUtils.tokensToString(val, false);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ASTcheck_constraint(DdlParser p, int id) {
public String getName() {
// name is optional
if (children[0] instanceof ASTconstraint_name) {
return AstTreeUtils.tokensToString((ASTconstraint_name) children[0]);
return AstTreeUtils.tokensToString((ASTconstraint_name) children[0], false);
} else {
return ASTcreate_table_statement.ANONYMOUS_NAME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.google.cloud.solutions.spannerddl.parser;

import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils;
import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/** Abstract Syntax Tree parser object for "column_type" token */
Expand Down Expand Up @@ -68,7 +71,16 @@ public String toString() {
return AstTreeUtils.tokensToString(this).toUpperCase(Locale.ROOT);
case "STRUCT":
if (jjtGetNumChildren() > 0) {
return AstTreeUtils.tokensToString(this);
ASTstruct_fields fields = AstTreeUtils.getChildByType(this, ASTstruct_fields.class);
List<String> renderedFields = new ArrayList<>();
for (ASTstruct_field field :
AstTreeUtils.getChildrenAssertType(fields, ASTstruct_field.class)) {
ASTname fieldName = AstTreeUtils.getOptionalChildByType(field, ASTname.class);
ASTcolumn_type fieldType = AstTreeUtils.getChildByType(field, ASTcolumn_type.class);
renderedFields.add(
fieldName != null ? fieldName + " " + fieldType : fieldType.toString());
}
return "STRUCT < " + Joiner.on(", ").join(renderedFields) + " >";
} else {
return "STRUCT <>";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ASTforeign_key(DdlParser p, int id) {
public String getName() {
// name is optional
if (children[0] instanceof ASTconstraint_name) {
return AstTreeUtils.tokensToString((ASTconstraint_name) children[0]);
return AstTreeUtils.tokensToString((ASTconstraint_name) children[0], false);
} else {
return ASTcreate_table_statement.ANONYMOUS_NAME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public ASTindex_interleave_clause(DdlParser p, int id) {

@Override
public String toString() {
return "INTERLEAVE IN " + AstTreeUtils.tokensToString((ASTinterleave_in) children[0]);
return "INTERLEAVE IN " + AstTreeUtils.tokensToString((ASTinterleave_in) children[0], false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public ASTrow_deletion_policy_clause(DdlParser p, int id) {
public String toString() {
ASTrow_deletion_policy_expression expression =
AstTreeUtils.getChildByType(children, ASTrow_deletion_policy_expression.class);
return "ROW DELETION POLICY (" + AstTreeUtils.tokensToString(expression) + ")";
ASTrow_deletion_policy_function function =
AstTreeUtils.getChildByType(expression, ASTrow_deletion_policy_function.class);
ASTrow_deletion_policy_column column =
AstTreeUtils.getChildByType(expression, ASTrow_deletion_policy_column.class);
ASTinterval_expression interval =
AstTreeUtils.getChildByType(expression, ASTinterval_expression.class);
return "ROW DELETION POLICY ("
+ AstTreeUtils.tokensToString(function, false)
+ " ( "
+ AstTreeUtils.tokensToString(column, false)
+ ", "
+ AstTreeUtils.tokensToString(interval, true)
+ " ))";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ASTtable(DdlParser p, int id) {

@Override
public String toString() {
return AstTreeUtils.tokensToString(this);
return AstTreeUtils.tokensToString(this, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String getInterleaveTargetClause() {

public String getInterleaveTableName() {
return AstTreeUtils.tokensToString(
AstTreeUtils.getChildByType(children, ASTinterleave_in.class));
AstTreeUtils.getChildByType(children, ASTinterleave_in.class), false);
}

public boolean isParentInterleave() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public ASTtable_name(DdlParser p, int id) {

@Override
public String toString() {
return AstTreeUtils.tokensToString(this);
return AstTreeUtils.tokensToString(this, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -786,4 +786,29 @@ public void ignoreProtoBundles_throwsExceptionWhenNotIgnoring() {
public void ignoreProtoBundles_ignoresWhenFlagIsSet() throws DdlDiffException {
assertThat(getDiff("", "CREATE PROTO BUNDLE (a,b,c)", false, true)).isEmpty();
}

@Test
public void preserveCaseForPseudoReservedLowercaseTableName() throws DdlDiffException {
String originalDdl =
"CREATE TABLE graph (col1 INT64, graph TIMESTAMP) PRIMARY KEY (col1);\n"
+ "CREATE TABLE child (col1 INT64, col2 INT64) PRIMARY KEY (col1, col2), "
+ "INTERLEAVE IN graph;\n"
+ "ALTER TABLE graph ADD ROW DELETION POLICY (OLDER_THAN(graph, INTERVAL 5 DAY));\n"
+ "ALTER DATABASE graph SET OPTIONS (opt1='old');";
String newDdl =
"CREATE TABLE graph (col1 INT64, graph TIMESTAMP) PRIMARY KEY (col1), "
+ "ROW DELETION POLICY (OLDER_THAN(graph, INTERVAL 5 DAY));\n"
+ "CREATE TABLE child (col1 INT64, col2 INT64) PRIMARY KEY (col1, col2), "
+ "INTERLEAVE IN PARENT graph;\n"
+ "CREATE INDEX idx_graph ON graph (col1), INTERLEAVE IN graph;\n"
+ "ALTER TABLE graph ADD CONSTRAINT graph CHECK (col1 > 0);\n"
+ "ALTER DATABASE graph SET OPTIONS (opt1='new');";

assertThat(getDiff(originalDdl, newDdl, false))
.containsExactly(
"ALTER DATABASE graph SET OPTIONS (opt1='new')",
"ALTER TABLE child SET INTERLEAVE IN PARENT graph",
"CREATE INDEX idx_graph ON graph ( col1 ) , INTERLEAVE IN graph",
"ALTER TABLE graph ADD CONSTRAINT graph CHECK (col1 > 0)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,31 @@ public void validateIndex_existingColumn() {
throw new RuntimeException(e);
}
}

@Test
public void validateIndex_pseudoReservedLowercaseTableName() throws DdlDiffException {
String ddl =
"CREATE TABLE graph ( col1 INT64, col2 STRING(100) ) PRIMARY KEY (col1); "
+ "CREATE INDEX myindex ON graph (col1);";
DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false));
}

@Test
public void validateForeignKey_pseudoReservedLowercaseTableNameViaAlter()
throws DdlDiffException {
String ddl =
"CREATE TABLE othertable ( othercol INT64 ) PRIMARY KEY (othercol); "
+ "CREATE TABLE graph ( col1 INT64, col2 INT64 ) PRIMARY KEY (col1); "
+ "ALTER TABLE graph ADD CONSTRAINT graph FOREIGN KEY (col2) REFERENCES othertable (othercol);";
DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false));
}

@Test
public void validateForeignKey_pseudoReservedLowercaseReferencedTable() throws DdlDiffException {
String ddl =
"CREATE TABLE graph ( col1 INT64 ) PRIMARY KEY (col1); "
+ "CREATE TABLE test1 ( col1 INT64, col2 INT64, "
+ "CONSTRAINT fk_graph FOREIGN KEY (col2) REFERENCES graph (col1) ) PRIMARY KEY (col1);";
DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false));
}
}
35 changes: 35 additions & 0 deletions src/test/resources/ddlParserValidation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,39 @@ CREATE TABLE test1 ( keycol INT64 NOT NULL PRIMARY KEY, value INT64 )

CREATE TABLE test1 ( keycol INT64, value INT64 )

== Test 24 create table named graph with reserved word identifiers

CREATE TABLE graph (
intcol INT64,
graph TIMESTAMP,
structcol STRUCT < graph INT64, col2 INT64 >,
CONSTRAINT graph CHECK (intcol > 0)
) PRIMARY KEY (intcol ASC),
ROW DELETION POLICY (OLDER_THAN ( graph, INTERVAL 10 DAY ))

== Test 25 create table interleaved in table named graph

CREATE TABLE child (
intcol INT64,
childcol INT64,
CONSTRAINT graph FOREIGN KEY ( intcol ) REFERENCES graph ( intcol ) ON DELETE NO ACTION
) PRIMARY KEY (intcol ASC, childcol ASC),
INTERLEAVE IN PARENT graph ON DELETE CASCADE

== Test 26 create index on table named graph interleaved in graph

CREATE INDEX idx ON graph ( intcol ASC ) , INTERLEAVE IN graph

== Test 27 create search index on table named graph interleaved in graph

CREATE SEARCH INDEX search_idx ON graph ( tok ) , INTERLEAVE IN graph

== Test 28 alter table named graph

ALTER TABLE graph ADD ROW DELETION POLICY (OLDER_THAN ( graph, INTERVAL 1 DAY ))

== Test 29 alter database named graph

ALTER DATABASE graph SET OPTIONS (opt1=TRUE)

==