Skip to content

Commit 596afc1

Browse files
committed
feat: add support for inline PRIMARY KEYs
1 parent 04d7cf6 commit 596afc1

6 files changed

Lines changed: 21 additions & 23 deletions

File tree

src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcolumn_def.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public String toString() {
8181
getGenerationClause(),
8282
getColumnDefaultClause(),
8383
(isHidden() ? "HIDDEN" : null),
84+
AstTreeUtils.getOptionalChildByType(children, ASTprimary_key.class) == null
85+
? null
86+
: "PRIMARY KEY",
8487
getOptionsClause());
8588
}
8689

src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_table_statement.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ public synchronized ASTprimary_key getPrimaryKey() {
8080
// nothing (no primary keys)
8181
ASTprimary_key primary_key =
8282
AstTreeUtils.getOptionalChildByType(children, ASTprimary_key.class);
83-
if (primary_key == null) {
84-
throw new UnsupportedOperationException("not implemented");
85-
}
8683
return primary_key;
8784
}
8885

@@ -136,7 +133,8 @@ public String toStringOptionalExistClause(boolean includeExists) {
136133
(withConstraints
137134
? getOptionalChildByType(children, ASTrow_deletion_policy_clause.class)
138135
: null),
139-
getOptions()));
136+
getOptions()))
137+
.trim();
140138
}
141139

142140
public ASToptions_clause getOptions() {

src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTprimary_key.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils;
2020
import com.google.common.base.Joiner;
21+
import java.util.List;
2122

2223
/** Abstract Syntax Tree parser object for "primary_Key" token */
2324
public class ASTprimary_key extends SimpleNode {
@@ -32,8 +33,11 @@ public ASTprimary_key(DdlParser p, int id) {
3233

3334
@Override
3435
public String toString() {
35-
return "PRIMARY KEY ("
36-
+ Joiner.on(", ").join(AstTreeUtils.getChildrenAssertType(children, ASTkey_part.class))
37-
+ ")";
36+
List<ASTkey_part> keyparts = AstTreeUtils.getChildrenAssertType(children, ASTkey_part.class);
37+
if (keyparts.size() > 0) {
38+
return "PRIMARY KEY (" + Joiner.on(", ").join(keyparts) + ")";
39+
} else {
40+
return "PRIMARY KEY";
41+
}
3842
}
3943
}

src/test/java/com/google/cloud/solutions/spannerddl/parser/DDLParsertValidateDdlFromFileTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ public void validateDDL() {
4343
DdlParser parser = new DdlParser(in);
4444
parser.ddl_statement();
4545
ASTddl_statement parsedStatement = (ASTddl_statement) parser.jjtree.rootNode();
46-
4746
assertWithMessage("Mismatch for section %s:", segmentName)
4847
.that(parsedStatement.toString())
4948
.isEqualTo(ddlStatement);
50-
} catch (Throwable e) {
49+
} catch (ParseException | UnsupportedOperationException | IllegalArgumentException e) {
5150
System.err.println("Exception in section " + segmentName);
5251
e.printStackTrace(System.err);
5352
assertWithMessage(

src/test/resources/ddlParserUnsupported.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,6 @@ CREATE TABLE test1 (
222222
value STRING(MAX) NOT NULL PLACEMENT KEY
223223
) PRIMARY KEY (keycol ASC)
224224

225-
== Test 27 create table inline primary key
226-
227-
CREATE TABLE test1 (
228-
keycol INT64 NOT NULL PRIMARY KEY,
229-
value INT64 PLACEMENT KEY
230-
)
231-
232-
== Test 28 create table optional primary key
233-
234-
CREATE TABLE test1 (
235-
keycol INT64,
236-
value INT64 DEFAULT (keycol * 100 + PI ( ))
237-
)
238-
239225
== Test 30 create table column on update clause
240226

241227
CREATE TABLE test1 (

src/test/resources/ddlParserValidation.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,12 @@ CREATE TABLE test1 (
207207

208208
CREATE INDEX myIndex ON mytable ( key ASC, index ASC ) STORING ( table ) , INTERLEAVE IN parent_table OPTIONS (some_option='enabled')
209209

210+
== Test 22 create table inline primary key
211+
212+
CREATE TABLE test1 ( keycol INT64 NOT NULL PRIMARY KEY, value INT64 )
213+
214+
== Test 23 create table optional primary key
215+
216+
CREATE TABLE test1 ( keycol INT64, value INT64 )
217+
210218
==

0 commit comments

Comments
 (0)