Skip to content

Commit f9008a7

Browse files
committed
MERGE: improve scope handling.
1 parent 3fbf40f commit f9008a7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

grammar.cc

+18-6
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,10 @@ void insert_stmt::out(std::ostream &out)
405405
out << ")";
406406
}
407407

408-
set_list::set_list(modifying_stmt *pprod) : prod(pprod)
408+
set_list::set_list(prod *p, table *target) : prod(p)
409409
{
410410
do {
411-
for (auto col : pprod->victim->columns()) {
411+
for (auto col : target->columns()) {
412412
if (d6() < 4)
413413
continue;
414414
auto expr = value_expr::factory(this, col.type);
@@ -434,7 +434,7 @@ update_stmt::update_stmt(prod *p, struct scope *s, table *v)
434434
: modifying_stmt(p, s, v) {
435435
scope->refs.push_back(victim);
436436
search = bool_expr::factory(this);
437-
set_list = make_shared<struct set_list>(this);
437+
set_list = make_shared<struct set_list>(this, victim);
438438
}
439439

440440
void update_stmt::out(std::ostream &out)
@@ -458,7 +458,7 @@ upsert_stmt::upsert_stmt(prod *p, struct scope *s, table *v)
458458
if (!victim->constraints.size())
459459
fail("need table w/ constraint for upsert");
460460

461-
set_list = std::make_shared<struct set_list>(this);
461+
set_list = std::make_shared<struct set_list>(this, victim);
462462
search = bool_expr::factory(this);
463463
constraint = random_pick(victim->constraints);
464464
}
@@ -539,11 +539,18 @@ void common_table_expression::out(std::ostream &out)
539539

540540
merge_stmt::merge_stmt(prod *p, struct scope *s, table *v)
541541
: modifying_stmt(p,s,v) {
542+
542543
target_table_ = make_shared<target_table>(this, victim);
543544
data_source = table_ref::factory(this);
544545
// join_condition = join_cond::factory(this, *target_table_, *data_source);
545546
join_condition = make_shared<simple_join_cond>(this, *target_table_, *data_source);
546547

548+
549+
/* Put data_source into scope but not target_table. Visibility of
550+
the latter varies depending on kind of when clause. */
551+
// for (auto r : data_source->refs)
552+
// scope->refs.push_back(&*r);
553+
547554
clauselist.push_back(when_clause::factory(this));
548555
while (d6()>4)
549556
clauselist.push_back(when_clause::factory(this));
@@ -598,9 +605,14 @@ void when_clause::accept(prod_visitor *v)
598605
}
599606

600607
when_clause_update::when_clause_update(merge_stmt *p)
601-
: when_clause(p)
608+
: when_clause(p), myscope(p->scope)
602609
{
603-
set_list = std::make_shared<struct set_list>(p);
610+
myscope.tables = scope->tables;
611+
myscope.refs = scope->refs;
612+
scope = &myscope;
613+
scope->refs.push_back(&*(p->target_table_->refs[0]));
614+
615+
set_list = std::make_shared<struct set_list>(this, p->victim);
604616
}
605617

606618
void when_clause_update::out(std::ostream &out) {

grammar.hh

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ struct insert_stmt : modifying_stmt {
217217
struct set_list : prod {
218218
vector<shared_ptr<value_expr> > value_exprs;
219219
vector<string> names;
220-
set_list(modifying_stmt *pprod);
220+
set_list(prod *p, table *target);
221221
virtual ~set_list() { }
222222
virtual void out(std::ostream &out);
223223
virtual void accept(prod_visitor *v) {
@@ -269,6 +269,7 @@ struct when_clause : prod {
269269

270270
struct when_clause_update : when_clause {
271271
shared_ptr<struct set_list> set_list;
272+
struct scope myscope;
272273
when_clause_update(struct merge_stmt *p);
273274
virtual ~when_clause_update() { }
274275
virtual void out(std::ostream &out);

0 commit comments

Comments
 (0)