featurebase/sql3/planner/opaltertable.go
pokeeffe-molecula 7c936471cc
sql3 changes (#2211)
* first cut of working (slowly) bulk insert; table valued functions and a tuple data type to support time quantums

* oversight

* filter pushdown implementation; bulk insert

* addressed some linter issues
2022-09-06 11:16:11 -05:00

106 lines
2.6 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package planner
import (
"context"
"fmt"
"github.com/molecula/featurebase/v3/sql3"
"github.com/molecula/featurebase/v3/sql3/planner/types"
)
// PlanOpAlterTable plan operator to alter a table.
type PlanOpAlterTable struct {
planner *ExecutionPlanner
tableName string
operation alterOperation
oldColumnName string
newColumnName string
columnDef *createTableField
warnings []string
}
func NewPlanOpAlterTable(p *ExecutionPlanner, tableName string, operation alterOperation, oldColumnName string, newColumnName string, columnDef *createTableField) *PlanOpAlterTable {
return &PlanOpAlterTable{
planner: p,
tableName: tableName,
operation: operation,
oldColumnName: oldColumnName,
newColumnName: newColumnName,
columnDef: columnDef,
warnings: make([]string, 0),
}
}
func (p *PlanOpAlterTable) Plan() map[string]interface{} {
result := make(map[string]interface{})
result["_op"] = fmt.Sprintf("%T", p)
result["tableName"] = p.tableName
return result
}
func (p *PlanOpAlterTable) AddWarning(warning string) {
p.warnings = append(p.warnings, warning)
}
func (p *PlanOpAlterTable) Warnings() []string {
return p.warnings
}
func (p *PlanOpAlterTable) String() string {
return ""
}
func (p *PlanOpAlterTable) Schema() types.Schema {
return types.Schema{}
}
func (p *PlanOpAlterTable) Children() []types.PlanOperator {
return []types.PlanOperator{}
}
func (p *PlanOpAlterTable) Iterator(ctx context.Context, row types.Row) (types.RowIterator, error) {
return &alterTableRowIter{
planner: p.planner,
operation: p.operation,
tableName: p.tableName,
columnDef: p.columnDef,
oldColumnName: p.oldColumnName,
}, nil
}
func (p *PlanOpAlterTable) WithChildren(children ...types.PlanOperator) (types.PlanOperator, error) {
return nil, nil
}
type alterTableRowIter struct {
planner *ExecutionPlanner
operation alterOperation
tableName string
columnDef *createTableField
oldColumnName string
}
var _ types.RowIterator = (*alterTableRowIter)(nil)
func (i *alterTableRowIter) Next(ctx context.Context) (types.Row, error) {
switch i.operation {
case alterOpAdd:
_, err := i.planner.schemaAPI.CreateField(ctx, i.tableName, i.columnDef.name, i.columnDef.fos...)
if err != nil {
return nil, err
}
case alterOpDrop:
err := i.planner.schemaAPI.DeleteField(ctx, i.tableName, i.oldColumnName)
if err != nil {
return nil, err
}
case alterOpRename:
return nil, sql3.NewErrInternal("column rename is unimplemented")
}
return nil, types.ErrNoMoreRows
}