diff --git a/enginetest/queries/trigger_queries.go b/enginetest/queries/trigger_queries.go index ed4d8d80fa..41efecd5bd 100644 --- a/enginetest/queries/trigger_queries.go +++ b/enginetest/queries/trigger_queries.go @@ -3898,6 +3898,26 @@ end; }, }, }, + { + // https://github.com/dolthub/dolt/issues/10175 + Name: "trigger with joined view (views are subquery aliases)", + SetUpScript: []string{ + "CREATE TABLE t_1 (id BINARY(2), data VARCHAR(10));", + "CREATE TABLE t_2 (id BINARY(2), other BINARY(2), status TINYINT UNSIGNED default 0);", + "CREATE TABLE queue (id BINARY(2), data varchar(10));", + "CREATE VIEW v AS SELECT t_2.id, t_1.data, status FROM t_2 inner join t_1 on other=t_1.id;", + "INSERT INTO t_1 (id, data) VALUES (0xC336, 'Test');", + "INSERT INTO t_2 (id, other) VALUES ROW(0x3E32, 0xC336), ROW(0xEDC1, 0xC336), ROW(0x9B15, 0xC336);", + "CREATE TRIGGER t_change_status after UPDATE ON t_2 FOR EACH ROW INSERT INTO queue(id, data) select id, data from v where v.id = NEW.id;", + "UPDATE t_2 SET status = 1 WHERE id = 0x3E32;", + }, + Assertions: []ScriptTestAssertion{ + { + Query: `SELECT * FROM queue`, + Expected: []sql.Row{{[]uint8{0x3e, 0x32}, "Test"}}, + }, + }, + }, } var TriggerCreateInSubroutineTests = []ScriptTest{ diff --git a/sql/analyzer/fix_exec_indexes.go b/sql/analyzer/fix_exec_indexes.go index 62dd4d6a8c..a74a7d3d58 100644 --- a/sql/analyzer/fix_exec_indexes.go +++ b/sql/analyzer/fix_exec_indexes.go @@ -341,17 +341,18 @@ func (s *idxScope) visitChildren(n sql.Node) error { } case *plan.SubqueryAlias: sqScope := s.copy() + if s.inTrigger() { + // TODO: this might not necessarily be true. But we do need the trigger scope(s) to be passed to the child + // nodes during rowexec, where there's currently no easy way to separate out the trigger scopes from the + // other scopes because it's all one parent row + n.OuterScopeVisibility = true + } if !n.OuterScopeVisibility && !n.IsLateral { // TODO: this should not apply to subqueries inside of lateral joins // Subqueries with no visibility have no parent scopes. Lateral // join subquery aliases continue to enjoy full visibility. sqScope.parentScopes = sqScope.parentScopes[:0] sqScope.lateralScopes = sqScope.lateralScopes[:0] - for _, p := range s.parentScopes { - if p.triggerScope { - sqScope.parentScopes = append(sqScope.parentScopes, p) - } - } } newC, cScope, err := assignIndexesHelper(n.Child, sqScope) if err != nil { diff --git a/sql/analyzer/resolve_subqueries.go b/sql/analyzer/resolve_subqueries.go index e0b3294be8..c15136cf60 100644 --- a/sql/analyzer/resolve_subqueries.go +++ b/sql/analyzer/resolve_subqueries.go @@ -286,7 +286,7 @@ func analyzeSubqueryAlias(ctx *sql.Context, a *Analyzer, sqa *plan.SubqueryAlias return newn, transform.NewTree, err } -// cacheSubqueryAlisesInJoins will look for joins against subquery aliases that +// cacheSubqueryAliasesInJoins will look for joins against subquery aliases that // will repeatedly execute the subquery, and will insert a *plan.CachedResults // node on top of those nodes. The left-most child of a join root is an exception // that cannot be cached.