From 63efbd654ebeccecf15ec9f9ed6c9b041f296803 Mon Sep 17 00:00:00 2001 From: gurminder71 <86370145+gurminder71@users.noreply.github.com> Date: Fri, 27 Sep 2024 06:44:01 -0700 Subject: [PATCH] feat: support annotations in SQL file (#171) * feat: support annotations in SQL file Column Annotations are NOT a feature of Cloud Spanner. This is an additional feature of the Cloud Spanner Schema parser exclusively in this tool so that users of this tool can add metadata to columns, and have that metadata represented in the parsed schema. See description in src/main/jjtree-sources/ddl_annotation.jjt --------- Co-authored-by: Niel Markwick --- pom.xml | 1 + .../solutions/spannerddl/diff/DdlDiff.java | 22 ++++++ .../spannerddl/parser/ASTannotation.java | 74 ++++++++++++++++++ .../parser/ASTannotation_param.java | 43 +++++++++++ .../parser/ASTcreate_table_statement.java | 3 +- src/main/jjtree-sources/DdlParser.head | 1 + src/main/jjtree-sources/ddl_annotation.jjt | 61 +++++++++++++++ src/main/jjtree-sources/ddl_parser.jjt | 5 ++ .../spannerddl/parser/DDLAnnotationTest.java | 76 +++++++++++++++++++ .../testUtils/ReadTestDatafile.java | 16 +++- src/test/resources/annotations.txt | 12 +++ src/test/resources/expectedAnnotations.txt | 9 +++ src/test/resources/expectedDdlDiff.txt | 10 ++- src/test/resources/newDdl.txt | 29 +++++++ src/test/resources/originalDdl.txt | 27 +++++++ 15 files changed, 384 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation.java create mode 100644 src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation_param.java create mode 100644 src/main/jjtree-sources/ddl_annotation.jjt create mode 100644 src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLAnnotationTest.java create mode 100644 src/test/resources/annotations.txt create mode 100644 src/test/resources/expectedAnnotations.txt diff --git a/pom.xml b/pom.xml index e7d35e5..1477da5 100644 --- a/pom.xml +++ b/pom.xml @@ -220,6 +220,7 @@ src/main/jjtree-sources/ddl_keywords.jjt \ src/main/jjtree-sources/ddl_string_bytes_tokens.jjt \ src/main/jjtree-sources/ddl_expression.jjt \ + src/main/jjtree-sources/ddl_annotation.jjt \ src/main/jjtree-sources/ddl_parser.jjt \ > ${project.build.directory}/generated-sources/jjtree-src/DdlParser.jjt diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java index 54fdcd0..0895a83 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java @@ -55,6 +55,7 @@ import java.util.Set; import java.util.TreeMap; import java.util.function.Function; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -707,6 +708,27 @@ private static String getDatabaseNameFromAlterDatabase(List st * @throws DdlDiffException if there is an error in parsing the DDL */ public static List parseDdl(String original) throws DdlDiffException { + return parseDdl(original, false); + } + + /** + * Parses the Cloud Spanner Schema (DDL) string to a list of AST DDL statements. + * + * @param original DDL to parse + * @param parseAnnotationInComments If true then the annotations that appear as comments + * "-- @ANNOTATION annotation" will be parsed + * @return List of parsed DDL statements + */ + public static List parseDdl(String original, boolean parseAnnotationInComments) + throws DdlDiffException { + // the annotations are prefixed with "--" so that SQL file remains valid. + // strip the comment prefix before so that annotations can be parsed. + // otherwise they will be ignored as comment lines + if (parseAnnotationInComments) { + original = + Pattern.compile("^\\s*--\\s+@", Pattern.MULTILINE).matcher(original).replaceAll("@"); + } + // Remove "--" comments and split by ";" List statements = Splitter.on(';').splitToList(original.replaceAll("--.*(\n|$)", "")); ArrayList ddlStatements = new ArrayList<>(statements.size()); diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation.java new file mode 100644 index 0000000..eafc36d --- /dev/null +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation.java @@ -0,0 +1,74 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.solutions.spannerddl.parser; + +import static com.google.cloud.solutions.spannerddl.diff.AstTreeUtils.getChildByType; + +import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils; +import java.util.ArrayList; +import java.util.List; + +/** + * Abstract Syntax Tree parser object for "annotation" token + * + *

Column Annotations are NOT a feature of Cloud Spanner. + * + *

This is an additional feature of the Cloud Spanner Schema parser exclusively in this tool so + * that users of this tool can add metadata to colums, and have that metadata represented in the + * parsed schema. + * + *

To use Annotations, they should be added to a CREATE TABLE statement as follows: + * + *

+ *  CREATE TABLE Albums (
+ *   -- @ANNOTATION SOMETEXT,
+ *    id STRING(36),
+ *  ) PRIMARY KEY (id)
+ * 
+ * + * Annotations need to be on their own line, and terminate with a comma. (This is because the '-- ' + * prefix is removed before using the JJT parser). + * + *

As they are comments, they are ignored by the diff generator and by Spanner itself. + */ +public class ASTannotation extends SimpleNode { + public ASTannotation(int id) { + super(id); + } + + public ASTannotation(DdlParser p, int id) { + super(p, id); + } + + public String getName() { + return AstTreeUtils.tokensToString(getChildByType(children, ASTname.class)); + } + + public List getParams() { + List params = new ArrayList<>(); + for (Node child : children) { + if (child instanceof ASTannotation_param) { + params.add((ASTannotation_param) child); + } + } + return params; + } + + public String getAnnotation() { + return AstTreeUtils.tokensToString(this, false).replaceAll(" ", ""); + } +} diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation_param.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation_param.java new file mode 100644 index 0000000..905996f --- /dev/null +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTannotation_param.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.solutions.spannerddl.parser; + +import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils; + +public class ASTannotation_param extends SimpleNode { + public ASTannotation_param(int id) { + super(id); + } + + public ASTannotation_param(DdlParser p, int id) { + super(p, id); + } + + public String getKey() { + ASTparam_key key = AstTreeUtils.getChildByType(this, ASTparam_key.class); + return AstTreeUtils.tokensToString(key); + } + + public String getValue() { + ASTparam_val val = AstTreeUtils.getOptionalChildByType(this, ASTparam_val.class); + if (val != null) { + return AstTreeUtils.tokensToString(val); + } else { + return null; + } + } +} diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_table_statement.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_table_statement.java index 58726b5..c2381ce 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_table_statement.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_table_statement.java @@ -140,7 +140,8 @@ private void validateChildren() { ASTcheck_constraint.class, ASTprimary_key.class, ASTtable_interleave_clause.class, - ASTrow_deletion_policy_clause.class)); + ASTrow_deletion_policy_clause.class, + ASTannotation.class)); } @Override diff --git a/src/main/jjtree-sources/DdlParser.head b/src/main/jjtree-sources/DdlParser.head index 516f4f7..761de7a 100644 --- a/src/main/jjtree-sources/DdlParser.head +++ b/src/main/jjtree-sources/DdlParser.head @@ -25,6 +25,7 @@ cat src/main/jjtree-sources/DdlParser.head \ src/main/jjtree-sources/ddl_keywords.jjt \ src/main/jjtree-sources/ddl_string_bytes_tokens.jjt \ src/main/jjtree-sources/ddl_expression.jjt \ + src/main/jjtree-sources/ddl_annotation.jjt \ src/main/jjtree-sources/ddl_parser.jjt \ > src/main/jjtree/DdlParser.jjt diff --git a/src/main/jjtree-sources/ddl_annotation.jjt b/src/main/jjtree-sources/ddl_annotation.jjt new file mode 100644 index 0000000..949221d --- /dev/null +++ b/src/main/jjtree-sources/ddl_annotation.jjt @@ -0,0 +1,61 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +// Column Annotations are NOT a feature of Cloud Spanner. +// +// This is an additional feature of the Cloud Spanner Schema parser exclusively +// in this tool so that users of this tool can add metadata to colums, and have +// that metadata represented in the parsed schema. +// +// To use Annotations, they should be added to a CREATE TABLE statement as +// follows: +// +// CREATE TABLE Albums ( +// -- @ANNOTATION SOMETEXT, +// id STRING(36), +// ) PRIMARY KEY (id) +// +// Annotations need to be on their own line, and terminate with a comma. +// (This is because the '-- ' prefix is removed before using the JJT parser). +// +// As they are comments, they are ignored by the diff generator and by +// Spanner itself. +// + +TOKEN: +{ + +} + +void column_annotation() #void: {} +{ + annotation() +} + +void annotation(): {} +{ + qualified_identifier() #name [ annotation_params() ] +} + +void annotation_params() #void: {} +{ + "(" annotation_param() ( "," annotation_param() )* ")" +} + +void annotation_param(): {} +{ + identifier() #param_key [ "=" identifier() #param_val ] +} diff --git a/src/main/jjtree-sources/ddl_parser.jjt b/src/main/jjtree-sources/ddl_parser.jjt index a94ae08..bca7bd0 100644 --- a/src/main/jjtree-sources/ddl_parser.jjt +++ b/src/main/jjtree-sources/ddl_parser.jjt @@ -212,6 +212,11 @@ void table_element() #void : LOOKAHEAD(3) foreign_key() | LOOKAHEAD(3) check_constraint() | LOOKAHEAD(3) synonym_clause() + + // Column annotations are not a Spanner feature. + // See file ddl_annotation.jjt for more details. + | column_annotation() + | column_def() } diff --git a/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLAnnotationTest.java b/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLAnnotationTest.java new file mode 100644 index 0000000..d30a7ce --- /dev/null +++ b/src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLAnnotationTest.java @@ -0,0 +1,76 @@ +package com.google.cloud.solutions.spannerddl.parser; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; +import static org.junit.Assert.fail; + +import com.google.cloud.solutions.spannerddl.diff.DdlDiff; +import com.google.cloud.solutions.spannerddl.diff.DdlDiffException; +import com.google.cloud.solutions.spannerddl.testUtils.ReadTestDatafile; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.junit.Test; + +public class DDLAnnotationTest { + + @Test + public void validateAnnotations() throws IOException { + Map tests = ReadTestDatafile.readDdlSegmentsFromFile("annotations.txt"); + Map expects = + ReadTestDatafile.readDdlSegmentsFromFile("expectedAnnotations.txt"); + + Iterator> testIt = tests.entrySet().iterator(); + Iterator> expectedIt = expects.entrySet().iterator(); + + while (testIt.hasNext() && expectedIt.hasNext()) { + Entry test = testIt.next(); + String expected = expectedIt.next().getValue(); + String segmentName = test.getKey(); + + try { + // first get all the annotations without removing the comment prefix + List annotations = getTableAnnotations(test.getValue(), false); + + // annotations should be empty + assertThat(annotations).isEmpty(); + + // now get all the annotations after removing the comment prefix + annotations = getTableAnnotations(test.getValue(), true); + + List expectedList = + expected != null ? Arrays.asList(expected.split("\n")) : Collections.emptyList(); + + assertWithMessage("Mismatch for section " + segmentName) + .that(annotations) + .isEqualTo(expectedList); + } catch (DdlDiffException e) { + fail("Failed to parse section: '" + segmentName + "': " + e); + } + } + } + + private List getTableAnnotations(String ddl, boolean parseAnnotations) + throws DdlDiffException { + List annotations = new ArrayList<>(); + + List statements = DdlDiff.parseDdl(ddl, parseAnnotations); + for (ASTddl_statement statement : statements) { + if (statement.jjtGetChild(0).getId() == DdlParserTreeConstants.JJTCREATE_TABLE_STATEMENT) { + Node tableStatement = statement.jjtGetChild(0); + for (int i = 0, count = tableStatement.jjtGetNumChildren(); i < count; i++) { + Node child = tableStatement.jjtGetChild(i); + if (child instanceof ASTannotation) { + annotations.add(((ASTannotation) child).getAnnotation()); + } + } + } + } + return annotations; + } +} diff --git a/src/test/java/com/google/cloud/solutions/spannerddl/testUtils/ReadTestDatafile.java b/src/test/java/com/google/cloud/solutions/spannerddl/testUtils/ReadTestDatafile.java index 5162c5f..3e313d9 100644 --- a/src/test/java/com/google/cloud/solutions/spannerddl/testUtils/ReadTestDatafile.java +++ b/src/test/java/com/google/cloud/solutions/spannerddl/testUtils/ReadTestDatafile.java @@ -32,6 +32,16 @@ public abstract class ReadTestDatafile { * @return LinkedHashMap of segment name => contents */ public static Map readDdlSegmentsFromFile(String filename) throws IOException { + return readDdlSegmentsFromFile(filename, false); + } + + /** + * Reads the test data file, parsing out the test titles and data from the file. + * + * @return LinkedHashMap of segment name => contents + */ + public static Map readDdlSegmentsFromFile(String filename, boolean preserveSpace) + throws IOException { File file = new File("src/test/resources/" + filename).getAbsoluteFile(); LinkedHashMap output = new LinkedHashMap<>(); @@ -39,9 +49,9 @@ public static Map readDdlSegmentsFromFile(String filename) throw String sectionName = null; StringBuilder section = new StringBuilder(); - String line; - while (null != (line = in.readLine())) { - line = line.replaceAll("#.*", "").trim(); + String rawLine; + while (null != (rawLine = in.readLine())) { + String line = preserveSpace ? rawLine : rawLine.replaceAll("#.*", "").trim(); if (line.isEmpty()) { continue; } diff --git a/src/test/resources/annotations.txt b/src/test/resources/annotations.txt new file mode 100644 index 0000000..de7c7bd --- /dev/null +++ b/src/test/resources/annotations.txt @@ -0,0 +1,12 @@ +== Test 1 all column annotations + +CREATE TABLE test1 ( + -- @ANNOTATION DEPRECATED, + -- @ANNOTATION PII, + -- @ANNOTATION TAG.business(internal), + -- @ANNOTATION TAG.business(key1,key2), + -- @ANNOTATION TAG.business(key1=val1,key2=value), + id STRING(36), +) PRIMARY KEY (id) + +== diff --git a/src/test/resources/expectedAnnotations.txt b/src/test/resources/expectedAnnotations.txt new file mode 100644 index 0000000..35c26c2 --- /dev/null +++ b/src/test/resources/expectedAnnotations.txt @@ -0,0 +1,9 @@ +== Test 1 all column annotations + +DEPRECATED +PII +TAG.business(internal) +TAG.business(key1,key2) +TAG.business(key1=val1,key2=value) + +== diff --git a/src/test/resources/expectedDdlDiff.txt b/src/test/resources/expectedDdlDiff.txt index 78e4c31..e609603 100644 --- a/src/test/resources/expectedDdlDiff.txt +++ b/src/test/resources/expectedDdlDiff.txt @@ -325,6 +325,14 @@ ALTER SEARCH INDEX AlbumsIndex ADD STORED COLUMN scol1 ALTER SEARCH INDEX AlbumsIndex DROP STORED COLUMN scol1 -== +== TEST 61 Add annotations to columns should not generate a diff + +== TEST 62 Remove annotations from columns should not generate a diff + +== TEST 63 Recorder annotations should not generate a diff +== TEST 64 Adding annotation as well as column should only generate the column diff +ALTER TABLE AlbumsIndex ADD COLUMN new_col STRING(255) + +== diff --git a/src/test/resources/newDdl.txt b/src/test/resources/newDdl.txt index 7452fd7..39ada79 100644 --- a/src/test/resources/newDdl.txt +++ b/src/test/resources/newDdl.txt @@ -511,4 +511,33 @@ STORING (scol1); CREATE SEARCH INDEX AlbumsIndex ON Albums (col1, col2) +== TEST 61 Add annotations to columns should not generate a diff + +CREATE TABLE AlbumsIndex ( + -- @ANNOTATION DEPRECATED, + id STRING(36), +) PRIMARY KEY (id) + +== TEST 62 Remove annotations from columns should not generate a diff + +CREATE TABLE AlbumsIndex ( + id STRING(36), +) PRIMARY KEY (id) + +== TEST 63 Recorder annotations should not generate a diff + +CREATE TABLE AlbumsIndex ( + -- @ANNOTATION PII, + -- @ANNOTATION DEPRECATED, + id STRING(36), +) PRIMARY KEY (id) + +== TEST 64 Adding annotation as well as column should only generate the column diff + +CREATE TABLE AlbumsIndex ( + -- @ANNOTATION PII, + id STRING(36), + new_col STRING(255), +) PRIMARY KEY (id) + == diff --git a/src/test/resources/originalDdl.txt b/src/test/resources/originalDdl.txt index 861b823..1188274 100644 --- a/src/test/resources/originalDdl.txt +++ b/src/test/resources/originalDdl.txt @@ -509,4 +509,31 @@ CREATE SEARCH INDEX AlbumsIndex ON Albums (col1, col2) STORING (scol1) +== TEST 61 Add annotations to columns should not generate a diff + +CREATE TABLE AlbumsIndex ( + id STRING(36), +) PRIMARY KEY (id) + +== TEST 62 Remove annotations from columns should not generate a diff + +CREATE TABLE AlbumsIndex ( + -- @ANNOTATION DEPRECATED, + id STRING(36), +) PRIMARY KEY (id) + +== TEST 63 Recorder annotations should not generate a diff + +CREATE TABLE AlbumsIndex ( + -- @ANNOTATION DEPRECATED, + -- @ANNOTATION PII, + id STRING(36), +) PRIMARY KEY (id) + +== TEST 64 Adding annotation as well as column should only generate the column diff + +CREATE TABLE AlbumsIndex ( + id STRING(36), +) PRIMARY KEY (id) + ==