Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support VALUES() #934

Merged
merged 6 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions server/ast/aliased_table_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ func nodeAliasedTableExpr(node *tree.AliasedTableExpr) (*vitess.AliasedTableExpr
return nil, fmt.Errorf("unhandled subquery table expression: `%T`", tableExpr)
}

// If the subquery is a VALUES statement, it should be represented more directly
innerSelect := selectStmt
if parentSelect, ok := innerSelect.(*vitess.ParenSelect); ok {
innerSelect = parentSelect.Select
}
if inSelect, ok := innerSelect.(*vitess.Select); ok {
if len(inSelect.From) == 1 {
if valuesStmt, ok := inSelect.From[0].(*vitess.ValuesStatement); ok {
if len(node.As.Cols) > 0 {
columns := make([]vitess.ColIdent, len(node.As.Cols))
for i := range node.As.Cols {
columns[i] = vitess.NewColIdent(string(node.As.Cols[i]))
}
valuesStmt.Columns = columns
}
aliasExpr = valuesStmt
break
}
}
}

subquery := &vitess.Subquery{
Select: selectStmt,
}
Expand Down
56 changes: 56 additions & 0 deletions testing/go/values_statement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _go

import (
"testing"

"github.com/dolthub/go-mysql-server/sql"
)

func TestValuesStatement(t *testing.T) {
RunScripts(t, ValuesStatementTests)
}

var ValuesStatementTests = []ScriptTest{
{
Name: "basic values statements",
SetUpScript: []string{},
Assertions: []ScriptTestAssertion{
{
Query: `SELECT * FROM (VALUES (1), (2), (3)) sqa;`,
Expected: []sql.Row{
{1},
{2},
{3},
},
},
{
Query: `SELECT * FROM (VALUES (1, 2), (3, 4)) sqa;`,
Expected: []sql.Row{
{1, 2},
{3, 4},
},
},
{
Query: `SELECT i * 10, j * 100 FROM (VALUES (1, 2), (3, 4)) sqa(i, j);`,
Expected: []sql.Row{
{10, 200},
{30, 400},
},
},
},
},
}
Loading