Skip to content

Commit

Permalink
feat(spanner/spansql): support default_leader database option (#7187)
Browse files Browse the repository at this point in the history
Co-authored-by: rahul2393 <[email protected]>
  • Loading branch information
glindstedt and rahul2393 committed Jan 10, 2023
1 parent 2ce3606 commit 88adaa2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
15 changes: 15 additions & 0 deletions spanner/spansql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,21 @@ func (p *parser) parseDatabaseOptions() (DatabaseOptions, *parseError) {
*retentionPeriod = tok.string
}
opts.VersionRetentionPeriod = retentionPeriod
} else if p.eat("default_leader", "=") {
tok := p.next()
if tok.err != nil {
return DatabaseOptions{}, tok.err
}
defaultLeader := new(string)
if tok.value == "null" {
*defaultLeader = ""
} else {
if tok.typ != stringToken {
return DatabaseOptions{}, p.errorf("invalid default_leader: %v", tok.value)
}
*defaultLeader = tok.string
}
opts.DefaultLeader = defaultLeader
} else {
tok := p.next()
return DatabaseOptions{}, p.errorf("unknown database option: %v", tok.value)
Expand Down
21 changes: 14 additions & 7 deletions spanner/spansql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,23 +438,27 @@ func TestParseExpr(t *testing.T) {
},
},
},
{`COALESCE(NULL, "B", "C")`,
{
`COALESCE(NULL, "B", "C")`,
Coalesce{ExprList: []Expr{Null, StringLiteral("B"), StringLiteral("C")}},
},
{`IF(A < B, TRUE, FALSE)`,
{
`IF(A < B, TRUE, FALSE)`,
If{
Expr: ComparisonOp{LHS: ID("A"), Op: Lt, RHS: ID("B")},
TrueResult: True,
ElseResult: False,
},
},
{`IFNULL(NULL, TRUE)`,
{
`IFNULL(NULL, TRUE)`,
IfNull{
Expr: Null,
NullResult: True,
},
},
{`NULLIF("a", "b")`,
{
`NULLIF("a", "b")`,
NullIf{
Expr: StringLiteral("a"),
ExprToMatch: StringLiteral("b"),
Expand Down Expand Up @@ -1012,7 +1016,7 @@ func TestParseDDL(t *testing.T) {
},
}}},
{
`ALTER DATABASE dbname SET OPTIONS (optimizer_version=2, version_retention_period='7d', enable_key_visualizer=true)`,
`ALTER DATABASE dbname SET OPTIONS (optimizer_version=2, version_retention_period='7d', enable_key_visualizer=true, default_leader='europe-west1')`,
&DDL{
Filename: "filename", List: []DDLStmt{
&AlterDatabase{
Expand All @@ -1022,6 +1026,7 @@ func TestParseDDL(t *testing.T) {
OptimizerVersion: func(i int) *int { return &i }(2),
VersionRetentionPeriod: func(s string) *string { return &s }("7d"),
EnableKeyVisualizer: func(b bool) *bool { return &b }(true),
DefaultLeader: func(s string) *string { return &s }("europe-west1"),
},
},
Position: line(1),
Expand All @@ -1030,7 +1035,7 @@ func TestParseDDL(t *testing.T) {
},
},
{
`ALTER DATABASE dbname SET OPTIONS (optimizer_version=2, version_retention_period='7d', enable_key_visualizer=true); CREATE TABLE users (UserId STRING(MAX) NOT NULL,) PRIMARY KEY (UserId);`,
`ALTER DATABASE dbname SET OPTIONS (optimizer_version=2, version_retention_period='7d', enable_key_visualizer=true, default_leader='europe-west1'); CREATE TABLE users (UserId STRING(MAX) NOT NULL,) PRIMARY KEY (UserId);`,
&DDL{
Filename: "filename", List: []DDLStmt{
&AlterDatabase{
Expand All @@ -1040,6 +1045,7 @@ func TestParseDDL(t *testing.T) {
OptimizerVersion: func(i int) *int { return &i }(2),
VersionRetentionPeriod: func(s string) *string { return &s }("7d"),
EnableKeyVisualizer: func(b bool) *bool { return &b }(true),
DefaultLeader: func(s string) *string { return &s }("europe-west1"),
},
},
Position: line(1),
Expand All @@ -1057,7 +1063,7 @@ func TestParseDDL(t *testing.T) {
},
},
{
`ALTER DATABASE dbname SET OPTIONS (optimizer_version=null, version_retention_period=null, enable_key_visualizer=null)`,
`ALTER DATABASE dbname SET OPTIONS (optimizer_version=null, version_retention_period=null, enable_key_visualizer=null, default_leader=null)`,
&DDL{
Filename: "filename", List: []DDLStmt{
&AlterDatabase{
Expand All @@ -1067,6 +1073,7 @@ func TestParseDDL(t *testing.T) {
OptimizerVersion: func(i int) *int { return &i }(0),
VersionRetentionPeriod: func(s string) *string { return &s }(""),
EnableKeyVisualizer: func(b bool) *bool { return &b }(false),
DefaultLeader: func(s string) *string { return &s }(""),
},
},
Position: line(1),
Expand Down
11 changes: 11 additions & 0 deletions spanner/spansql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ func (do DatabaseOptions) SQL() string {
str += "enable_key_visualizer=null"
}
}
if do.DefaultLeader != nil {
if hasOpt {
str += ", "
}
hasOpt = true
if *do.DefaultLeader == "" {
str += "default_leader=null"
} else {
str += fmt.Sprintf("default_leader='%s'", *do.DefaultLeader)
}
}
str += ")"
return str
}
Expand Down
12 changes: 8 additions & 4 deletions spanner/spansql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,11 @@ func TestSQL(t *testing.T) {
VersionRetentionPeriod: func(s string) *string { return &s }("7d"),
OptimizerVersion: func(i int) *int { return &i }(2),
EnableKeyVisualizer: func(b bool) *bool { return &b }(true),
DefaultLeader: func(s string) *string { return &s }("europe-west1"),
}},
Position: line(1),
},
"ALTER DATABASE dbname SET OPTIONS (optimizer_version=2, version_retention_period='7d', enable_key_visualizer=true)",
"ALTER DATABASE dbname SET OPTIONS (optimizer_version=2, version_retention_period='7d', enable_key_visualizer=true, default_leader='europe-west1')",
reparseDDL,
},
{
Expand All @@ -405,10 +406,11 @@ func TestSQL(t *testing.T) {
VersionRetentionPeriod: func(s string) *string { return &s }(""),
OptimizerVersion: func(i int) *int { return &i }(0),
EnableKeyVisualizer: func(b bool) *bool { return &b }(false),
DefaultLeader: func(s string) *string { return &s }(""),
}},
Position: line(1),
},
"ALTER DATABASE dbname SET OPTIONS (optimizer_version=null, version_retention_period=null, enable_key_visualizer=null)",
"ALTER DATABASE dbname SET OPTIONS (optimizer_version=null, version_retention_period=null, enable_key_visualizer=null, default_leader=null)",
reparseDDL,
},
{
Expand Down Expand Up @@ -635,7 +637,8 @@ func TestSQL(t *testing.T) {
{Cond: IntegerLiteral(2), Result: StringLiteral("Y")},
},
ElseResult: Null,
}},
},
},
},
},
`SELECT CASE X WHEN 1 THEN "X" WHEN 2 THEN "Y" ELSE NULL END`,
Expand All @@ -650,7 +653,8 @@ func TestSQL(t *testing.T) {
{Cond: True, Result: StringLiteral("X")},
{Cond: False, Result: StringLiteral("Y")},
},
}},
},
},
},
},
`SELECT CASE WHEN TRUE THEN "X" WHEN FALSE THEN "Y" END`,
Expand Down
1 change: 1 addition & 0 deletions spanner/spansql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ type DatabaseOptions struct {
OptimizerVersion *int
VersionRetentionPeriod *string
EnableKeyVisualizer *bool
DefaultLeader *string
}

// Delete represents a DELETE statement.
Expand Down

0 comments on commit 88adaa2

Please sign in to comment.