Skip to content

Commit 38c0111

Browse files
authored
Merge pull request #2597 from dolthub/jennifer/fixes
allow SELECT; and column name for cast result fix
2 parents ed88d18 + c7e44ef commit 38c0111

7 files changed

Lines changed: 1029 additions & 979 deletions

File tree

postgres/parser/parser/sql.y

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10685,6 +10685,12 @@ empty_select:
1068510685
Window: $7.window(),
1068610686
}
1068710687
}
10688+
| SELECT
10689+
{
10690+
$$.val = &tree.SelectClause{
10691+
Exprs: make(tree.SelectExprs, 0, 0),
10692+
}
10693+
}
1068810694

1068910695
// %Help: SELECT - retrieve rows from a data source and compute a result
1069010696
// %Category: DML
@@ -10747,7 +10753,6 @@ simple_select_clause:
1074710753
Window: $9.window(),
1074810754
}
1074910755
}
10750-
| SELECT error // SHOW HELP: SELECT
1075110756

1075210757
opt_join_hint_comment:
1075310758
{

server/ast/select.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,30 @@ func nodeSelectExpr(ctx *Context, node tree.SelectExpr) (vitess.SelectExpr, erro
133133
if err != nil {
134134
return nil, err
135135
}
136-
// cast part is not part of column name, e.g. `id::INT2` should create column name as `id`.
136+
137137
if ce, ok := expr.(*tree.CastExpr); ok && node.As == "" {
138-
node.As = tree.UnrestrictedName(tree.AsString(ce.Expr))
138+
hasConst := false
139+
_, _ = tree.SimpleVisit(expr, func(visitingExpr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
140+
switch visitingExpr.(type) {
141+
case tree.Constant:
142+
hasConst = true
143+
return false, visitingExpr, nil
144+
}
145+
return true, visitingExpr, nil
146+
})
147+
if hasConst {
148+
_, dt, err := nodeResolvableTypeReference(ctx, ce.Type, false)
149+
if err != nil {
150+
return nil, err
151+
}
152+
// constant value is not part of column name
153+
// e.g. `1::INT2` should create column name as `int2`.
154+
node.As = tree.UnrestrictedName(dt.Name())
155+
} else {
156+
// cast type is not part of column name
157+
// e.g. `id::INT2` should create column name as `id`.
158+
node.As = tree.UnrestrictedName(tree.AsString(ce.Expr))
159+
}
139160
}
140161

141162
return &vitess.AliasedExpr{

testing/generation/command_docs/output/delete_test.go

Lines changed: 336 additions & 336 deletions
Large diffs are not rendered by default.

testing/generation/command_docs/output/insert_test.go

Lines changed: 61 additions & 61 deletions
Large diffs are not rendered by default.

testing/generation/command_docs/output/update_test.go

Lines changed: 573 additions & 573 deletions
Large diffs are not rendered by default.

testing/go/select_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ import (
2323
// TestSelect covers SELECT syntax not covered by our MySQL select tests
2424
func TestSelect(t *testing.T) {
2525
RunScripts(t, []ScriptTest{
26+
{
27+
Name: "SELECT empty",
28+
Assertions: []ScriptTestAssertion{
29+
{
30+
Query: "SELECT;",
31+
Expected: []sql.Row{nil},
32+
},
33+
},
34+
},
2635
{
2736
Name: "SELECT DISTINCT ON",
2837
SetUpScript: []string{

testing/go/types_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ var typesTests = []ScriptTest{
4141
},
4242
},
4343
{
44-
Query: `SELECT 1::pg_catalog.int8;`,
45-
Expected: []sql.Row{{1}},
44+
Query: `SELECT 1::pg_catalog.int8;`,
45+
ExpectedColNames: []string{"int8"},
46+
Expected: []sql.Row{{1}},
4647
},
4748
},
4849
},
@@ -415,13 +416,15 @@ var typesTests = []ScriptTest{
415416
},
416417
},
417418
{
418-
Query: "SELECT '!'::bpchar;",
419+
Query: "SELECT '!'::bpchar;",
420+
ExpectedColNames: []string{"bpchar"},
419421
Expected: []sql.Row{
420422
{"!"},
421423
},
422424
},
423425
{
424-
Query: "SELECT '!'::bpchar(1);",
426+
Query: "SELECT '!'::bpchar(1);",
427+
ExpectedColNames: []string{"bpchar"},
425428
Expected: []sql.Row{
426429
{"!"},
427430
},
@@ -547,7 +550,8 @@ var typesTests = []ScriptTest{
547550
},
548551
},
549552
{
550-
Query: `SELECT 'def'::name::"char";`,
553+
Query: `SELECT 'def'::name::"char";`,
554+
ExpectedColNames: []string{"char"},
551555
Expected: []sql.Row{
552556
{"d"},
553557
},
@@ -799,7 +803,8 @@ var typesTests = []ScriptTest{
799803
},
800804
},
801805
{
802-
Query: "SELECT date '2022-02-02'",
806+
Query: "SELECT date '2022-02-02'",
807+
ExpectedColNames: []string{"date"},
803808
Expected: []sql.Row{
804809
{"2022-02-02"},
805810
},
@@ -3669,6 +3674,16 @@ var enumTypeTests = []ScriptTest{
36693674
Query: `INSERT INTO person VALUES ('Moe', 'happy'), ('Larry', 'sad'), ('Curly', 'ok');`,
36703675
Expected: []sql.Row{},
36713676
},
3677+
{
3678+
Query: `SELECT 'happy'::mood;`,
3679+
ExpectedColNames: []string{"mood"},
3680+
Expected: []sql.Row{{"happy"}},
3681+
},
3682+
{
3683+
Query: `SELECT current_mood::mood from person where name = 'Moe';`,
3684+
ExpectedColNames: []string{"current_mood"},
3685+
Expected: []sql.Row{{"happy"}},
3686+
},
36723687
{
36733688
Query: `SELECT * FROM person order by current_mood;`,
36743689
Expected: []sql.Row{{"Larry", "sad"}, {"Curly", "ok"}, {"Moe", "happy"}},

0 commit comments

Comments
 (0)