From ae2c8ba96e4eaa042afa19bbe1bc619669273dc4 Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Tue, 31 Oct 2023 14:41:32 +0100 Subject: [PATCH] semantics: Fix missing union pop from scoper (#14401) --- go/vt/vtgate/semantics/analyzer_test.go | 3 +++ go/vt/vtgate/semantics/scoper.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/go/vt/vtgate/semantics/analyzer_test.go b/go/vt/vtgate/semantics/analyzer_test.go index 61dd1ee4a9d..f1227bfe7b0 100644 --- a/go/vt/vtgate/semantics/analyzer_test.go +++ b/go/vt/vtgate/semantics/analyzer_test.go @@ -604,6 +604,9 @@ func TestOrderByBindingTable(t *testing.T) { }, { "select a.id from t1 as a union (select uid from t2, t union (select name from t) order by 1) order by id", MergeTableSets(TS0, TS1, TS3), + }, { + "select * from (SELECT c1, c2 FROM a UNION SELECT c1, c2 FROM b) AS u ORDER BY u.c1", + MergeTableSets(TS0, TS1), }} for _, tc := range tcases { t.Run(tc.sql, func(t *testing.T) { diff --git a/go/vt/vtgate/semantics/scoper.go b/go/vt/vtgate/semantics/scoper.go index c3685913376..458e08b1f15 100644 --- a/go/vt/vtgate/semantics/scoper.go +++ b/go/vt/vtgate/semantics/scoper.go @@ -197,7 +197,7 @@ func (s *scoper) up(cursor *sqlparser.Cursor) error { if isParentSelectStatement(cursor) { s.popScope() } - case *sqlparser.Select, sqlparser.GroupBy, *sqlparser.Update, *sqlparser.Delete, *sqlparser.Insert: + case *sqlparser.Select, sqlparser.GroupBy, *sqlparser.Update, *sqlparser.Delete, *sqlparser.Insert, *sqlparser.Union: id := EmptyTableSet() for _, tableInfo := range s.currentScope().tables { set := tableInfo.getTableSet(s.org)