Skip to content

Commit 77c0f03

Browse files
authored
Merge pull request #934 from dolthub/james/values
support `VALUES()`
2 parents b3627f5 + 5af5be4 commit 77c0f03

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

server/ast/aliased_table_expr.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ func nodeAliasedTableExpr(node *tree.AliasedTableExpr) (*vitess.AliasedTableExpr
5858
return nil, fmt.Errorf("unhandled subquery table expression: `%T`", tableExpr)
5959
}
6060

61+
// If the subquery is a VALUES statement, it should be represented more directly
62+
innerSelect := selectStmt
63+
if parentSelect, ok := innerSelect.(*vitess.ParenSelect); ok {
64+
innerSelect = parentSelect.Select
65+
}
66+
if inSelect, ok := innerSelect.(*vitess.Select); ok {
67+
if len(inSelect.From) == 1 {
68+
if valuesStmt, ok := inSelect.From[0].(*vitess.ValuesStatement); ok {
69+
if len(node.As.Cols) > 0 {
70+
columns := make([]vitess.ColIdent, len(node.As.Cols))
71+
for i := range node.As.Cols {
72+
columns[i] = vitess.NewColIdent(string(node.As.Cols[i]))
73+
}
74+
valuesStmt.Columns = columns
75+
}
76+
aliasExpr = valuesStmt
77+
break
78+
}
79+
}
80+
}
81+
6182
subquery := &vitess.Subquery{
6283
Select: selectStmt,
6384
}

testing/go/values_statement_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package _go
16+
17+
import (
18+
"testing"
19+
20+
"github.com/dolthub/go-mysql-server/sql"
21+
)
22+
23+
func TestValuesStatement(t *testing.T) {
24+
RunScripts(t, ValuesStatementTests)
25+
}
26+
27+
var ValuesStatementTests = []ScriptTest{
28+
{
29+
Name: "basic values statements",
30+
SetUpScript: []string{},
31+
Assertions: []ScriptTestAssertion{
32+
{
33+
Query: `SELECT * FROM (VALUES (1), (2), (3)) sqa;`,
34+
Expected: []sql.Row{
35+
{1},
36+
{2},
37+
{3},
38+
},
39+
},
40+
{
41+
Query: `SELECT * FROM (VALUES (1, 2), (3, 4)) sqa;`,
42+
Expected: []sql.Row{
43+
{1, 2},
44+
{3, 4},
45+
},
46+
},
47+
{
48+
Query: `SELECT i * 10, j * 100 FROM (VALUES (1, 2), (3, 4)) sqa(i, j);`,
49+
Expected: []sql.Row{
50+
{10, 200},
51+
{30, 400},
52+
},
53+
},
54+
},
55+
},
56+
}

0 commit comments

Comments
 (0)