chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:32:25 +08:00
commit e014feafe1
2285 changed files with 1131979 additions and 0 deletions
+157
View File
@@ -0,0 +1,157 @@
// 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 hook
import (
"github.com/cockroachdb/errors"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/functions"
"github.com/dolthub/doltgresql/core/id"
"github.com/dolthub/doltgresql/core/procedures"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// BeforeTableDeletion performs all validation necessary to ensure that table deletion does not leave the database in an
// invalid state.
func BeforeTableDeletion(ctx *sql.Context, _ sql.StatementRunner, nodeInterface sql.Node) (sql.Node, error) {
// TODO: handle casts using a table name
n, ok := nodeInterface.(*plan.DropTable)
if !ok {
return nil, errors.Newf("DROP TABLE pre-hook expected `*plan.DropTable` but received `%T`", nodeInterface)
}
var resolvedTables []*sqle.DoltTable
var allTableNames []doltdb.TableName
for _, tbl := range n.Tables {
doltTable := core.SQLNodeToDoltTable(tbl)
if doltTable == nil {
// If this table isn't a Dolt table then we ignore it
continue
}
resolvedTables = append(resolvedTables, doltTable)
allTableNames = append(allTableNames, doltTable.TableName())
}
// TODO: handle DROP TABLE CASCADE
for _, doltTable := range resolvedTables {
// Check if the table is in a column
if err := beforeTableDeletionCheckTableColumns(ctx, doltTable, allTableNames); err != nil {
return nil, err
}
// Check if the table is in a function/procedure parameter
if err := beforeTableDeletionCheckFuncsProcs(ctx, doltTable, allTableNames); err != nil {
return nil, err
}
}
return n, nil
}
// beforeTableDeletionCheckTableColumns is called from BeforeTableDeletion and handles checking for columns that use a
// table's type.
func beforeTableDeletionCheckTableColumns(ctx *sql.Context, doltTable *sqle.DoltTable, allDeletedTables []doltdb.TableName) error {
tableName := doltTable.TableName()
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return err
}
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return err
}
OuterLoop:
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue OuterLoop
}
for _, deletedTable := range allDeletedTables {
if deletedTable.EqualFold(otherTableName) {
// If we're also deleting this table, then it doesn't matter what the columns have
continue OuterLoop
}
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return err
}
if !ok {
return errors.Newf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return err
}
for _, col := range otherTableSch.GetAllCols().GetColumns() {
colType := col.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID == tableAsType {
// TODO: portion after newline should be in DETAILS but we don't yet support that in our error messages
return errors.Newf("cannot drop table %s because other objects depend on it\ncolumn %s of table %s depends on type %s",
tableName.Name, col.Name, otherTableName.Name, tableName.Name)
}
}
}
return nil
}
// beforeTableDeletionCheckFuncsProcs is called from BeforeTableDeletion and handles checking for function and procedure
// parameters that use a table's type.
func beforeTableDeletionCheckFuncsProcs(ctx *sql.Context, doltTable *sqle.DoltTable, allDeletedTables []doltdb.TableName) error {
tableName := doltTable.TableName()
tableAsType := id.NewType(tableName.Schema, tableName.Name)
funcsColl, err := core.GetFunctionsCollectionFromContext(ctx, "")
if err != nil {
return err
}
err = funcsColl.IterateFunctions(ctx, func(f functions.Function) (stop bool, err error) {
for _, paramType := range f.ParameterTypes {
if paramType == tableAsType {
// TODO: portion after newline should be in DETAILS but we don't yet support that in our error messages
return true, errors.Newf("cannot drop table %s because other objects depend on it\nfunction %s depends on type %s",
tableName.Name, f.Name().Name, tableName.Name)
}
}
return false, nil
})
if err != nil {
return err
}
procsColl, err := core.GetProceduresCollectionFromContext(ctx, "")
if err != nil {
return err
}
err = procsColl.IterateProcedures(ctx, func(p procedures.Procedure) (stop bool, err error) {
for _, paramType := range p.ParameterTypes {
if paramType == tableAsType {
// TODO: portion after newline should be in DETAILS but we don't yet support that in our error messages
return true, errors.Newf("cannot drop table %s because other objects depend on it\nfunction %s depends on type %s",
tableName.Name, p.Name().Name, tableName.Name)
}
}
return false, nil
})
if err != nil {
return err
}
return nil
}
+106
View File
@@ -0,0 +1,106 @@
// Copyright 2026 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 hook
import (
"fmt"
"github.com/cockroachdb/errors"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/id"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// AfterTableRename handles updating various columns using the table type, alongside other validation that's unique
// to Doltgres.
func AfterTableRename(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error {
// TODO: handle casts using table names
n, ok := nodeInterface.(*plan.RenameTable)
if !ok {
return errors.Errorf("RENAME TABLE post-hook expected `*plan.RenameTable` but received `%T`", nodeInterface)
}
// Grab the table being altered (so we know the schema)
sqlTable, ok := n.TableExists(ctx, n.NewNames[0])
if !ok {
// Views do not manifest as tables, so we'll return here if this isn't a table
return nil
}
doltTable := core.SQLTableToDoltTable(sqlTable)
if doltTable == nil {
// If this table isn't a Dolt table then we don't have anything to do
return nil
}
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return err
}
tableName := doltTable.TableName()
tableName.Name = n.OldNames[0]
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return err
}
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return err
}
if !ok {
return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return err
}
for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
colType := otherCol.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID != tableAsType {
// This column isn't our table type, so we can ignore it
continue
}
// The ALTER updates the type on the schema since it still has the old one
alterStr := fmt.Sprintf(`ALTER TABLE "%s"."%s" ALTER COLUMN "%s" TYPE "%s"."%s";`,
otherTableName.Schema, otherTableName.Name, otherCol.Name, tableName.Schema, n.NewNames[0])
// We run the statement as though it's interpreted since we're running new statements inside the original
_, err = sql.RunInterpreted(ctx, func(subCtx *sql.Context) ([]sql.Row, error) {
_, rowIter, _, err := runner.QueryWithBindings(subCtx, alterStr, nil, nil, nil)
if err != nil {
return nil, err
}
return sql.RowIterToRows(subCtx, rowIter)
})
if err != nil {
return err
}
}
}
return nil
}
+179
View File
@@ -0,0 +1,179 @@
// Copyright 2026 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 hook
import (
"fmt"
"strings"
"github.com/cockroachdb/errors"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/id"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// BeforeTableAddColumn handles validation that's unique to Doltgres.
func BeforeTableAddColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) (sql.Node, error) {
n, ok := nodeInterface.(*plan.AddColumn)
if !ok {
return nil, errors.Errorf("ADD COLUMN pre-hook expected `*plan.AddColumn` but received `%T`", nodeInterface)
}
// If the column being added doesn't have a default value, then we don't have anything to check (for now)
if n.Column().Default == nil {
return n, nil
}
// Grab the table being altered
doltTable := core.SQLNodeToDoltTable(n.Table)
if doltTable == nil {
// If this table isn't a Dolt table then we don't have anything to do
return n, nil
}
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return n, nil
}
tableName := doltTable.TableName()
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return nil, err
}
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return nil, err
}
for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
colType := otherCol.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID != tableAsType {
// This column isn't our table type, so we can ignore it
continue
}
return nil, errors.Errorf(`cannot alter table "%s" because column "%s.%s" uses its row type`,
tableName.Name, otherTableName.Name, otherCol.Name)
}
}
return n, nil
}
// AfterTableAddColumn handles updating various table columns, alongside other validation that's unique to Doltgres.
func AfterTableAddColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error {
n, ok := nodeInterface.(*plan.AddColumn)
if !ok {
return errors.Errorf("ADD COLUMN post-hook expected `*plan.AddColumn` but received `%T`", nodeInterface)
}
// Grab the table being altered
doltTable := core.SQLNodeToDoltTable(n.Table)
if doltTable == nil {
// If this table isn't a Dolt table then we don't have anything to do
return nil
}
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return err
}
tableName := doltTable.TableName()
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return err
}
sch := doltTable.Schema(ctx)
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return err
}
if !ok {
return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return err
}
for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
colType := otherCol.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID != tableAsType {
// This column isn't our table type, so we can ignore it
continue
}
// Build the UPDATE statement that we'll run for this table
rowValues := make([]string, len(sch)+1)
for i, col := range sch {
rowValues[i] = fmt.Sprintf(`("%s")."%s"`, otherCol.Name, col.Name)
}
rowValues[len(rowValues)-1] = "NULL"
// The UPDATE changes the values in the table
updateStr := fmt.Sprintf(`UPDATE "%s"."%s" SET "%s" = ROW(%s)::"%s"."%s" WHERE length("%s"::text) > 0;`,
otherTableName.Schema, otherTableName.Name, otherCol.Name, strings.Join(rowValues, ","), tableName.Schema, tableName.Name, otherCol.Name)
// The ALTER updates the type on the schema since it still has the old one
alterStr := fmt.Sprintf(`ALTER TABLE "%s"."%s" ALTER COLUMN "%s" TYPE "%s"."%s";`,
otherTableName.Schema, otherTableName.Name, otherCol.Name, tableName.Schema, tableName.Name)
// We run the statements as though they were interpreted since we're running new statements inside the original
_, err = sql.RunInterpreted(ctx, func(subCtx *sql.Context) ([]sql.Row, error) {
_, rowIter, _, err := runner.QueryWithBindings(subCtx, updateStr, nil, nil, nil)
if err != nil {
return nil, err
}
_, err = sql.RowIterToRows(subCtx, rowIter)
if err != nil {
return nil, err
}
_, rowIter, _, err = runner.QueryWithBindings(subCtx, alterStr, nil, nil, nil)
if err != nil {
return nil, err
}
return sql.RowIterToRows(subCtx, rowIter)
})
if err != nil {
return err
}
}
}
return nil
}
+121
View File
@@ -0,0 +1,121 @@
// Copyright 2026 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 hook
import (
"fmt"
"github.com/cockroachdb/errors"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/id"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// AfterTableDropColumn handles updating various table columns, alongside other validation that's unique to Doltgres.
func AfterTableDropColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error {
n, ok := nodeInterface.(*plan.DropColumn)
if !ok {
return errors.Errorf("DROP COLUMN post-hook expected `*plan.DropColumn` but received `%T`", nodeInterface)
}
// Grab the table being altered
doltTable := core.SQLNodeToDoltTable(n.Table)
if doltTable == nil {
// If this table isn't a Dolt table then we don't have anything to do
return nil
}
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return err
}
tableName := doltTable.TableName()
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return err
}
sch := n.TargetSchema()
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return err
}
if !ok {
return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return err
}
for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
colType := otherCol.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID != tableAsType {
// This column isn't our table type, so we can ignore it
continue
}
// Build the UPDATE statement that we'll run for this table
trimIdx := -1
for i, col := range sch {
if col.Name == n.Column {
trimIdx = i
break
}
}
if trimIdx == -1 {
return errors.New("DROP COLUMN post-hook could not find the index of the column to remove")
}
// The UPDATE changes the values in the table
updateStr := fmt.Sprintf(`UPDATE "%s"."%s" SET "%s" = dolt_recordtrim("%s", %d)::"%s"."%s";`,
otherTableName.Schema, otherTableName.Name, otherCol.Name, otherCol.Name, trimIdx, tableName.Schema, tableName.Name)
// The ALTER updates the type on the schema since it still has the old one
alterStr := fmt.Sprintf(`ALTER TABLE "%s"."%s" ALTER COLUMN "%s" TYPE "%s"."%s";`,
otherTableName.Schema, otherTableName.Name, otherCol.Name, tableName.Schema, tableName.Name)
// We run the statements as though they were interpreted since we're running new statements inside the original
_, err = sql.RunInterpreted(ctx, func(subCtx *sql.Context) ([]sql.Row, error) {
_, rowIter, _, err := runner.QueryWithBindings(subCtx, updateStr, nil, nil, nil)
if err != nil {
return nil, err
}
_, err = sql.RowIterToRows(subCtx, rowIter)
if err != nil {
return nil, err
}
_, rowIter, _, err = runner.QueryWithBindings(subCtx, alterStr, nil, nil, nil)
if err != nil {
return nil, err
}
return sql.RowIterToRows(subCtx, rowIter)
})
if err != nil {
return err
}
}
}
return nil
}
+106
View File
@@ -0,0 +1,106 @@
// Copyright 2026 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 hook
import (
"github.com/cockroachdb/errors"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/id"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// beforeTableModifyColumnChange represents what properties of a column changed when a call is made to BeforeTableModifyColumn.
type beforeTableModifyColumnChange uint8
const (
beforeTableModifyColumnChange_None beforeTableModifyColumnChange = iota
beforeTableModifyColumnChange_Type
)
// BeforeTableModifyColumn handles validation that's unique to Doltgres.
func BeforeTableModifyColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) (sql.Node, error) {
n, ok := nodeInterface.(*plan.ModifyColumn)
if !ok {
return nil, errors.Errorf("MODIFY COLUMN pre-hook expected `*plan.ModifyColumn` but received `%T`", nodeInterface)
}
// Figure out what was changed. We know it's not the name because we have a dedicated *RenameColumn node.
changed := beforeTableModifyColumnChange_None
newColumn := n.NewColumn()
for _, col := range n.TargetSchema() {
if col.Name == newColumn.Name {
if !col.Type.Equals(newColumn.Type) {
changed = beforeTableModifyColumnChange_Type
}
}
}
if changed == beforeTableModifyColumnChange_None {
return n, nil
}
// Grab the table being altered (so we know the schema)
doltTable := core.SQLNodeToDoltTable(n.Table)
if doltTable == nil {
// If this table isn't a Dolt table then we don't have anything to do
return n, nil
}
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return n, nil
}
tableName := doltTable.TableName()
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return nil, err
}
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return nil, err
}
for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
colType := otherCol.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID != tableAsType {
// This column isn't our table type, so we can ignore it
continue
}
return nil, errors.Errorf(`cannot alter table "%s" because column "%s.%s" uses its row type`,
tableName.Name, otherTableName.Name, otherCol.Name)
}
}
return n, nil
}
+101
View File
@@ -0,0 +1,101 @@
// Copyright 2026 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 hook
import (
"fmt"
"github.com/cockroachdb/errors"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/id"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// AfterTableRenameColumn handles updating various table columns, alongside other validation that's unique to Doltgres.
func AfterTableRenameColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error {
n, ok := nodeInterface.(*plan.RenameColumn)
if !ok {
return errors.Errorf("RENAME COLUMN post-hook expected `*plan.RenameColumn` but received `%T`", nodeInterface)
}
if n.ColumnName == n.NewColumnName {
return nil
}
// Grab the table being altered
doltTable := core.SQLNodeToDoltTable(n.Table)
if doltTable == nil {
// If this table isn't a Dolt table then we don't have anything to do
return nil
}
_, root, err := core.GetRootFromContext(ctx)
if err != nil {
return err
}
tableName := doltTable.TableName()
tableAsType := id.NewType(tableName.Schema, tableName.Name)
allTableNames, err := root.GetAllTableNames(ctx, false)
if err != nil {
return err
}
for _, otherTableName := range allTableNames {
if doltdb.IsSystemTable(otherTableName) {
// System tables don't use any table types
continue
}
otherTable, ok, err := root.GetTable(ctx, otherTableName)
if err != nil {
return err
}
if !ok {
return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
}
otherTableSch, err := otherTable.GetSchema(ctx)
if err != nil {
return err
}
for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
colType := otherCol.TypeInfo.ToSqlType()
dgtype, ok := colType.(*pgtypes.DoltgresType)
if !ok {
// If this isn't a Doltgres type, then it can't be a table type so we can ignore it
continue
}
if dgtype.ID != tableAsType {
// This column isn't our table type, so we can ignore it
continue
}
// The ALTER updates the type on the schema since it still has the old one
alterStr := fmt.Sprintf(`ALTER TABLE "%s"."%s" ALTER COLUMN "%s" TYPE "%s"."%s";`,
otherTableName.Schema, otherTableName.Name, otherCol.Name, tableName.Schema, tableName.Name)
// We run the statement as though it were interpreted since we're running new statements inside the original
_, err = sql.RunInterpreted(ctx, func(subCtx *sql.Context) ([]sql.Row, error) {
_, rowIter, _, err := runner.QueryWithBindings(subCtx, alterStr, nil, nil, nil)
if err != nil {
return nil, err
}
return sql.RowIterToRows(subCtx, rowIter)
})
if err != nil {
return err
}
}
}
return nil
}