diff --git a/server/ast/aliased_table_expr.go b/server/ast/aliased_table_expr.go index c88c879648..6f703bd0cc 100644 --- a/server/ast/aliased_table_expr.go +++ b/server/ast/aliased_table_expr.go @@ -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, } diff --git a/testing/go/values_statement_test.go b/testing/go/values_statement_test.go new file mode 100644 index 0000000000..cf995bd301 --- /dev/null +++ b/testing/go/values_statement_test.go @@ -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}, + }, + }, + }, + }, +}