Skip to content

Commit 49ecdfb

Browse files
committed
fix ExistsExpr buildScalar to use subquery impl
1 parent d44af18 commit 49ecdfb

File tree

5 files changed

+56
-38
lines changed

5 files changed

+56
-38
lines changed

enginetest/memory_engine_test.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -200,37 +200,23 @@ func TestSingleQueryPrepared(t *testing.T) {
200200

201201
// Convenience test for debugging a single query. Unskip and set to the desired query.
202202
func TestSingleScript(t *testing.T) {
203-
//t.Skip()
203+
t.Skip()
204204
var scripts = []queries.ScriptTest{
205205
{
206-
// https://github.com/dolthub/dolt/issues/10113
207-
Name: "DELETE with NOT EXISTS subquery",
208-
SetUpScript: []string{
209-
`CREATE TABLE IF NOT EXISTS student (
210-
id BIGINT AUTO_INCREMENT,
211-
name VARCHAR(50) NOT NULL,
212-
PRIMARY KEY (id)
213-
);`,
214-
`CREATE TABLE IF NOT EXISTS student_hobby (
215-
id BIGINT AUTO_INCREMENT,
216-
student_id BIGINT NOT NULL,
217-
hobby VARCHAR(50) NOT NULL,
218-
PRIMARY KEY (id)
219-
);`,
220-
"INSERT INTO student (id, name) VALUES (1, 'test1');",
221-
"INSERT INTO student (id, name) VALUES (2, 'test2');",
222-
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (1, 1, 'test1');",
223-
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (2, 2, 'test2');",
224-
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (3, 100, 'test3');",
225-
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (4, 100, 'test3');",
226-
},
206+
Name: "AS OF propagates to nested CALLs",
207+
SetUpScript: []string{},
227208
Assertions: []queries.ScriptTestAssertion{
228209
{
229-
Query: "delete from student_hobby where not exists (select 1 from student where student.id = student_hobby.student_id);",
210+
Query: "create procedure create_proc() create table t (i int primary key, j int);",
211+
Expected: []sql.Row{
212+
{types.NewOkResult(0)},
213+
},
230214
},
231215
{
232-
Query: "SELECT * FROM student_hobby ORDER BY id;",
233-
Expected: []sql.Row{{1, 1, "test1"}, {2, 2, "test2"}},
216+
Query: "call create_proc()",
217+
Expected: []sql.Row{
218+
{types.NewOkResult(0)},
219+
},
234220
},
235221
},
236222
},

enginetest/queries/script_queries.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,38 @@ type ScriptTestAssertion struct {
122122
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
123123
// the tests.
124124
var ScriptTests = []ScriptTest{
125+
{
126+
// https://github.com/dolthub/dolt/issues/10113
127+
Name: "DELETE with NOT EXISTS subquery",
128+
SetUpScript: []string{
129+
`CREATE TABLE IF NOT EXISTS student (
130+
id BIGINT AUTO_INCREMENT,
131+
name VARCHAR(50) NOT NULL,
132+
PRIMARY KEY (id)
133+
);`,
134+
`CREATE TABLE IF NOT EXISTS student_hobby (
135+
id BIGINT AUTO_INCREMENT,
136+
student_id BIGINT NOT NULL,
137+
hobby VARCHAR(50) NOT NULL,
138+
PRIMARY KEY (id)
139+
);`,
140+
"INSERT INTO student (id, name) VALUES (1, 'test1');",
141+
"INSERT INTO student (id, name) VALUES (2, 'test2');",
142+
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (1, 1, 'test1');",
143+
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (2, 2, 'test2');",
144+
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (3, 100, 'test3');",
145+
"INSERT INTO student_hobby (id, student_id, hobby) VALUES (4, 100, 'test3');",
146+
},
147+
Assertions: []ScriptTestAssertion{
148+
{
149+
Query: "delete from student_hobby where not exists (select 1 from student where student.id = student_hobby.student_id);",
150+
},
151+
{
152+
Query: "SELECT * FROM student_hobby ORDER BY id;",
153+
Expected: []sql.Row{{1, 1, "test1"}, {2, 2, "test2"}},
154+
},
155+
},
156+
},
125157
{
126158
// https://github.com/dolthub/dolt/issues/9987
127159
Name: "GROUP BY nil pointer dereference in Dispose when Next() never called",

sql/analyzer/node_batches.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ func getBatchesForNode(node sql.Node) ([]*Batch, bool) {
107107
Id: validateReadOnlyTransactionId,
108108
Apply: validateReadOnlyTransaction,
109109
},
110-
{
111-
Id: resolveSubqueriesId,
112-
Apply: resolveSubqueries,
113-
},
114110
{
115111
Id: processTruncateId,
116112
Apply: processTruncate,

sql/planbuilder/dml.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ func (b *Builder) buildDelete(inScope *scope, d *ast.Delete) (outScope *scope) {
447447
b.qFlags.Set(sql.QFlagDelete)
448448

449449
outScope = b.buildFrom(inScope, d.TableExprs)
450+
451+
// Capture the table node for simple DELETEs before buildWhere wraps it
452+
var targets []sql.Node
453+
if len(d.Targets) == 0 {
454+
targets = []sql.Node{outScope.node}
455+
}
456+
450457
b.buildWhere(outScope, d.Where)
451458
orderByScope := b.analyzeOrderBy(outScope, outScope, d.OrderBy)
452459
b.buildOrderBy(outScope, orderByScope)
@@ -459,7 +466,6 @@ func (b *Builder) buildDelete(inScope *scope, d *ast.Delete) (outScope *scope) {
459466
outScope.node = plan.NewLimit(limit, outScope.node)
460467
}
461468

462-
var targets []sql.Node
463469
if len(d.Targets) > 0 {
464470
targets = make([]sql.Node, len(d.Targets))
465471
for i, tableName := range d.Targets {

sql/planbuilder/scalar.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,12 @@ func (b *Builder) buildScalar(inScope *scope, e ast.Expr) (ex sql.Expression) {
372372
return values
373373
}
374374
case *ast.ExistsExpr:
375-
sqScope := inScope.push()
376-
sqScope.initSubquery()
377-
selScope := b.buildSelectStmt(sqScope, v.Subquery.Select)
378-
selectString := ast.String(v.Subquery.Select)
379-
sq := plan.NewSubquery(selScope.node, selectString)
380-
sq = sq.WithCorrelated(sqScope.correlated())
381-
b.qFlags.Set(sql.QFlagScalarSubquery)
382-
return plan.NewExistsSubquery(sq)
375+
sq := b.buildScalar(inScope, v.Subquery)
376+
sqPlan, ok := sq.(*plan.Subquery)
377+
if !ok {
378+
b.handleErr(fmt.Errorf("expected Subquery from ExistsExpr, got %T", sq))
379+
}
380+
return plan.NewExistsSubquery(sqPlan)
383381
case *ast.TimestampFuncExpr:
384382
var (
385383
unit sql.Expression

0 commit comments

Comments
 (0)