Skip to content

Commit

Permalink
Add testdata for NEW operators
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Sep 28, 2024
1 parent 6b278af commit 4a5a44e
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 0 deletions.
17 changes: 17 additions & 0 deletions testdata/input/expr/braced_new_constructor.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Example from https://cloud.google.com/spanner/docs/reference/standard-sql/operators#new_operator
NEW Universe {
name: "Sol"
closest_planets: ["Mercury", "Venus", "Earth" ]
star {
radius_miles: 432690
age: 4603000000
}
constellations: [{
name: "Libra"
index: 0
}, {
name: "Scorpio"
index: 1
}]
all_planets: (SELECT planets FROM SolTable)
}
1 change: 1 addition & 0 deletions testdata/input/expr/new_constructor.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEW googlesql.examples.music.Chart(key AS rank, name AS chart_name)
271 changes: 271 additions & 0 deletions testdata/result/expr/braced_new_constructor.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
--- braced_new_constructor.sql
-- Example from https://cloud.google.com/spanner/docs/reference/standard-sql/operators#new_operator
NEW Universe {
name: "Sol"
closest_planets: ["Mercury", "Venus", "Earth" ]
star {
radius_miles: 432690
age: 4603000000
}
constellations: [{
name: "Libra"
index: 0
}, {
name: "Scorpio"
index: 1
}]
all_planets: (SELECT planets FROM SolTable)
}
--- AST
&ast.BracedNewConstructor{
New: 100,
TypeName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 104,
NameEnd: 112,
Name: "Universe",
},
},
},
Body: &ast.BracedConstructor{
Lbrace: 113,
Rbrace: 380,
Fields: []*ast.BracedConstructorField{
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 117,
NameEnd: 121,
Name: "name",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 121,
Expr: &ast.StringLiteral{
ValuePos: 123,
ValueEnd: 128,
Value: "Sol",
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 131,
NameEnd: 146,
Name: "closest_planets",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 146,
Expr: &ast.ArrayLiteral{
Array: 0,
Lbrack: 148,
Rbrack: 177,
Type: nil,
Values: []ast.Expr{
&ast.StringLiteral{
ValuePos: 149,
ValueEnd: 158,
Value: "Mercury",
},
&ast.StringLiteral{
ValuePos: 160,
ValueEnd: 167,
Value: "Venus",
},
&ast.StringLiteral{
ValuePos: 169,
ValueEnd: 176,
Value: "Earth",
},
},
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 181,
NameEnd: 185,
Name: "star",
},
Value: &ast.BracedConstructor{
Lbrace: 186,
Rbrace: 235,
Fields: []*ast.BracedConstructorField{
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 192,
NameEnd: 204,
Name: "radius_miles",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 204,
Expr: &ast.IntLiteral{
ValuePos: 206,
ValueEnd: 212,
Base: 10,
Value: "432690",
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 217,
NameEnd: 220,
Name: "age",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 220,
Expr: &ast.IntLiteral{
ValuePos: 222,
ValueEnd: 232,
Base: 10,
Value: "4603000000",
},
},
},
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 239,
NameEnd: 253,
Name: "constellations",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 253,
Expr: &ast.ArrayLiteral{
Array: 0,
Lbrack: 255,
Rbrack: 332,
Type: nil,
Values: []ast.Expr{
&ast.BracedConstructor{
Lbrace: 256,
Rbrace: 291,
Fields: []*ast.BracedConstructorField{
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 262,
NameEnd: 266,
Name: "name",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 266,
Expr: &ast.StringLiteral{
ValuePos: 268,
ValueEnd: 275,
Value: "Libra",
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 280,
NameEnd: 285,
Name: "index",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 285,
Expr: &ast.IntLiteral{
ValuePos: 287,
ValueEnd: 288,
Base: 10,
Value: "0",
},
},
},
},
},
&ast.BracedConstructor{
Lbrace: 294,
Rbrace: 331,
Fields: []*ast.BracedConstructorField{
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 300,
NameEnd: 304,
Name: "name",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 304,
Expr: &ast.StringLiteral{
ValuePos: 306,
ValueEnd: 315,
Value: "Scorpio",
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 320,
NameEnd: 325,
Name: "index",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 325,
Expr: &ast.IntLiteral{
ValuePos: 327,
ValueEnd: 328,
Base: 10,
Value: "1",
},
},
},
},
},
},
},
},
},
&ast.BracedConstructorField{
Name: &ast.Ident{
NamePos: 336,
NameEnd: 347,
Name: "all_planets",
},
Value: &ast.BracedConstructorFieldValueExpr{
Colon: 347,
Expr: &ast.ScalarSubQuery{
Lparen: 349,
Rparen: 378,
Query: &ast.Select{
Select: 350,
Distinct: false,
AsStruct: false,
Results: []ast.SelectItem{
&ast.ExprSelectItem{
Expr: &ast.Ident{
NamePos: 357,
NameEnd: 364,
Name: "planets",
},
},
},
From: &ast.From{
From: 365,
Source: &ast.TableName{
Table: &ast.Ident{
NamePos: 370,
NameEnd: 378,
Name: "SolTable",
},
Hint: (*ast.Hint)(nil),
As: (*ast.AsAlias)(nil),
Sample: (*ast.TableSample)(nil),
},
},
Where: (*ast.Where)(nil),
GroupBy: (*ast.GroupBy)(nil),
Having: (*ast.Having)(nil),
OrderBy: (*ast.OrderBy)(nil),
Limit: (*ast.Limit)(nil),
},
},
},
},
},
},
}

--- SQL
NEW Universe {name: "Sol", closest_planets: ARRAY["Mercury", "Venus", "Earth"], star {radius_miles: 432690, age: 4603000000}, constellations: ARRAY[{name: "Libra", index: 0}, {name: "Scorpio", index: 1}], all_planets: (SELECT planets FROM SolTable)}
66 changes: 66 additions & 0 deletions testdata/result/expr/new_constructor.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
--- new_constructor.sql
NEW googlesql.examples.music.Chart(key AS rank, name AS chart_name)
--- AST
&ast.NewConstructor{
New: 0,
TypeName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 4,
NameEnd: 13,
Name: "googlesql",
},
&ast.Ident{
NamePos: 14,
NameEnd: 22,
Name: "examples",
},
&ast.Ident{
NamePos: 23,
NameEnd: 28,
Name: "music",
},
&ast.Ident{
NamePos: 29,
NameEnd: 34,
Name: "Chart",
},
},
},
Args: []*ast.NewConstructorArg{
&ast.NewConstructorArg{
Expr: &ast.Ident{
NamePos: 35,
NameEnd: 38,
Name: "key",
},
Alias: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 42,
NameEnd: 46,
Name: "rank",
},
},
},
&ast.NewConstructorArg{
Expr: &ast.Ident{
NamePos: 48,
NameEnd: 52,
Name: "name",
},
Alias: &ast.AsAlias{
As: -1,
Alias: &ast.Ident{
NamePos: 56,
NameEnd: 66,
Name: "chart_name",
},
},
},
},
Rparen: 66,
}

--- SQL
NEW googlesql.examples.music.Chart(key AS rank, name AS chart_name)

0 comments on commit 4a5a44e

Please sign in to comment.