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

prevent panic on triggers with declare statements #2442

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 17 additions & 0 deletions enginetest/queries/trigger_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,23 @@ INSERT INTO t0 (v1, v2) VALUES (i, s); END;`,
},
},
},

{
Name: "delete me",
SetUpScript: []string{
"create table t (i int primary key);",
},
Assertions: []ScriptTestAssertion{
{
Query: "create trigger trig1 before insert on t for each row begin declare x int; select new.i + 10 into x; set new.i = x; end;",
ExpectedErr: sql.ErrUnsupportedFeature,
},
{
Query: "create trigger trig2 before insert on t for each row begin declare x int; set x = new.i * 10; set new.i = x; end;",
ExpectedErr: sql.ErrUnsupportedFeature,
},
},
},
}

// RollbackTriggerTests are trigger tests that require rollback logic to work correctly
Expand Down
9 changes: 8 additions & 1 deletion sql/expression/procedurereference.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type ProcedureReferencable interface {

// InitializeVariable sets the initial value for the variable.
func (ppr *ProcedureReference) InitializeVariable(name string, sqlType sql.Type, val interface{}) error {
if ppr == nil || ppr.InnermostScope == nil {
return fmt.Errorf("cannot initialize variable `%s` in an empty procedure reference", name)
}
convertedVal, _, err := sqlType.Convert(val)
if err != nil {
return err
Expand Down Expand Up @@ -271,7 +274,11 @@ var _ sql.CollationCoercible = (*ProcedureParam)(nil)

// NewProcedureParam creates a new ProcedureParam expression.
func NewProcedureParam(name string, typ sql.Type) *ProcedureParam {
return &ProcedureParam{name: strings.ToLower(name), typ: typ}
return &ProcedureParam{
name: strings.ToLower(name),
typ: typ,
//pRef: NewProcedureReference(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo?

}
}

// Children implements the sql.Expression interface.
Expand Down
10 changes: 10 additions & 0 deletions sql/planbuilder/create_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ func (b *Builder) buildCreateTrigger(inScope *scope, query string, c *ast.DDL) (
}
}

// TODO: https://github.com/dolthub/dolt/issues/7720
if block, isBEBlock := bodyScope.node.(*plan.BeginEndBlock); isBEBlock {
for _, child := range block.Children() {
if _, ok := child.(*plan.DeclareVariables); ok {
err := sql.ErrUnsupportedFeature.New("DECLARE in BEGIN END block")
b.handleErr(err)
}
}
}

outScope.node = plan.NewCreateTrigger(
db,
c.TriggerSpec.TrigName.Name.String(),
Expand Down
Loading