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

Implemented a no-op node for statements that can be safely ignored #1103

Merged
merged 6 commits into from
Jan 8, 2025
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
76 changes: 61 additions & 15 deletions server/ast/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

// nodeAlterTable handles *tree.AlterTable nodes.
func nodeAlterTable(ctx *Context, node *tree.AlterTable) (*vitess.AlterTable, error) {
func nodeAlterTable(ctx *Context, node *tree.AlterTable) (vitess.Statement, error) {
if node == nil {
return nil, nil
}
Expand All @@ -34,11 +34,24 @@ func nodeAlterTable(ctx *Context, node *tree.AlterTable) (*vitess.AlterTable, er
return nil, err
}

statements, err := nodeAlterTableCmds(ctx, node.Cmds, tableName, node.IfExists)
statements, noOps, err := nodeAlterTableCmds(ctx, node.Cmds, tableName, node.IfExists)
if err != nil {
return nil, err
}

// If there are no valid statements return a no-op statement
if len(noOps) > 0 && len(statements) == 0 {
return NewNoOp(noOps), nil
}

// Otherwise emit warnings now, then return an AlterTable statement
// TODO: we don't have a way to send or store the warnings alongside a valid AlterTable statement. We could either
// get a *sql.Context here and emit warnings, or we could store the warnings in the Context and make the caller
// emit them before it sends |ReadyForQuery|
// if len(noOps) > 0 {
// emit warnings
// }

return &vitess.AlterTable{
Table: tableName,
Statements: statements,
Expand All @@ -53,54 +66,87 @@ func nodeAlterTableSetSchema(ctx *Context, node *tree.AlterTableSetSchema) (vite
return nil, fmt.Errorf("ALTER TABLE SET SCHEMA is not yet supported")
}

// nodeAlterTableCmds converts tree.AlterTableCmds into a slice of vitess.DDL
// instances that can be executed by GMS. |tableName| identifies the table
// being altered, and |ifExists| indicates whether the IF EXISTS clause was
// specified.
// nodeAlterTableCmds converts tree.AlterTableCmds into a slice of vitess.DDL instances that can be executed by GMS.
// |tableName| identifies the table being altered, and |ifExists| indicates whether the IF EXISTS clause was specified.
// A second slice of unsupported but safely ignored statements is return as well.
func nodeAlterTableCmds(
ctx *Context,
node tree.AlterTableCmds,
tableName vitess.TableName,
ifExists bool) ([]*vitess.DDL, error) {
ifExists bool) ([]*vitess.DDL, []string, error) {

if len(node) == 0 {
return nil, fmt.Errorf("no commands specified for ALTER TABLE statement")
return nil, nil, fmt.Errorf("no commands specified for ALTER TABLE statement")
}

vitessDdlCmds := make([]*vitess.DDL, 0, len(node))
var unsupportedWarnings []string
for _, cmd := range node {
var err error
var statement *vitess.DDL
switch cmd := cmd.(type) {
case *tree.AlterTableAddConstraint:
statement, err = nodeAlterTableAddConstraint(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableAddColumn:
statement, err = nodeAlterTableAddColumn(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableDropColumn:
statement, err = nodeAlterTableDropColumn(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableDropConstraint:
statement, err = nodeAlterTableDropConstraint(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableRenameColumn:
statement, err = nodeAlterTableRenameColumn(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableSetDefault:
statement, err = nodeAlterTableSetDefault(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableDropNotNull:
statement, err = nodeAlterTableDropNotNull(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableSetNotNull:
statement, err = nodeAlterTableSetNotNull(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableAlterColumnType:
statement, err = nodeAlterTableAlterColumnType(ctx, cmd, tableName, ifExists)
if err != nil {
return nil, nil, err
}
vitessDdlCmds = append(vitessDdlCmds, statement)
case *tree.AlterTableOwner:
unsupportedWarnings = append(unsupportedWarnings, fmt.Sprintf("ALTER TABLE %s OWNER TO %s", tableName.String(), cmd.Owner))
default:
return nil, fmt.Errorf("ALTER TABLE with unsupported command type %T", cmd)
}

if err != nil {
return nil, err
return nil, nil, fmt.Errorf("ALTER TABLE with unsupported command type %T", cmd)
}
vitessDdlCmds = append(vitessDdlCmds, statement)
}

return vitessDdlCmds, nil
return vitessDdlCmds, unsupportedWarnings, nil
}

// nodeAlterTableAddConstraint converts a tree.AlterTableAddConstraint instance
Expand Down
31 changes: 31 additions & 0 deletions server/ast/no_op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2025 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 ast

import (
vitess "github.com/dolthub/vitess/go/vt/sqlparser"

pgnodes "github.com/dolthub/doltgresql/server/node"
)

// NewNoOp returns a new NoOp statement which does nothing and issues zero or more warnings when run.
// Used for statements that aren't directly supported but which we don't want to cause errors.
func NewNoOp(warnings []string) vitess.InjectedStatement {
zachmu marked this conversation as resolved.
Show resolved Hide resolved
return vitess.InjectedStatement{
Statement: pgnodes.NoOp{
Warnings: warnings,
},
}
}
79 changes: 79 additions & 0 deletions server/node/no_op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2025 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 node

import (
"fmt"
"io"

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

var _ vitess.Injectable = (*NoOp)(nil)
var _ sql.ExecSourceRel = (*NoOp)(nil)

// NoOp is a node that does nothing and issues zero or more warnings when run.
// Used when a statement should parse but isn't expected to do anything, for compatibility with Postgres dumps / tools.
type NoOp struct {
zachmu marked this conversation as resolved.
Show resolved Hide resolved
Warnings []string
}

func (n NoOp) Resolved() bool {
return true
}

func (n NoOp) String() string {
return fmt.Sprintf("%v", n.Warnings)
}

func (n NoOp) Schema() sql.Schema {
return nil
}

func (n NoOp) Children() []sql.Node {
return nil
}

func (n NoOp) WithChildren(children ...sql.Node) (sql.Node, error) {
return n, nil
}

func (n NoOp) IsReadOnly() bool {
return true
}

func (n NoOp) WithResolvedChildren(children []any) (any, error) {
return n, nil
}

type noOpRowIter struct {
warnings []string
}

func (n noOpRowIter) Next(ctx *sql.Context) (sql.Row, error) {
return nil, io.EOF
}

func (n noOpRowIter) Close(ctx *sql.Context) error {
for _, warning := range n.warnings {
ctx.Warn(0, fmt.Sprintf("%s is unimplemented", warning))
}
return nil
}

func (n NoOp) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) {
return noOpRowIter{warnings: n.Warnings}, nil
}
11 changes: 11 additions & 0 deletions testing/go/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,16 @@ func TestAlterTable(t *testing.T) {
},
},
},
{
Name: "alter table owner",
SetUpScript: []string{
"CREATE TABLE t1 (a INT, b INT);",
},
Assertions: []ScriptTestAssertion{
{
Query: "ALTER TABLE t1 OWNER TO new_owner;", // no error is all we expect here
},
},
},
})
}
Loading