Skip to content

Commit 828be60

Browse files
[Catalog] Add pre- and post-optimizations
This commit introduces pre- and post-optimizations as new `Catalog` Components. Pre-optimizations are implemented as callback functions that accept the QueryGraph as an argument, prior to its optimization. Similarly, post-optimizations are defined as callback functions that receive the resulting `Producer` after the optimization process. Notably, the commit also removes the `plan->minimize_schema()` operation at the end of optimization, registering it as a post-optimization step.
1 parent d078805 commit 828be60

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

include/mutable/catalog/Catalog.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ struct M_EXPORT Catalog
327327
using TableFactoryDecoratorCallback = std::function<std::unique_ptr<TableFactory>(std::unique_ptr<TableFactory>)>;
328328
ComponentSet<TableFactoryDecoratorCallback> table_properties_; // stores callback functions that decorate a table with the given decorator
329329

330+
using PreOptimizationCallback = std::function<void(const QueryGraph &)>;
331+
ComponentSet<PreOptimizationCallback> pre_optimizations_;
332+
333+
using PostOptimizationCallback = std::function<std::unique_ptr<Producer>(std::unique_ptr<Producer>)>;
334+
ComponentSet<PostOptimizationCallback> post_optimizations_;
335+
330336
public:
331337
/*===== Stores ===================================================================================================*/
332338
/** Registers a new `Store` with the given `name`. */
@@ -592,6 +598,42 @@ struct M_EXPORT Catalog
592598
auto table_properties_end() const { return table_properties_.end(); }
593599
auto table_properties_cbegin() const { return table_properties_begin(); }
594600
auto table_properties_cend() const { return table_properties_end(); }
601+
602+
/*===== Pre-Optimizations ========================================================================================*/
603+
/** Registers a new pre-optimization with the given `name`. */
604+
void register_pre_optimization(const char *name, PreOptimizationCallback optimization, const char *description = nullptr)
605+
{
606+
pre_optimizations_.add(
607+
pool(name),
608+
Component<PreOptimizationCallback>(description, std::make_unique<PreOptimizationCallback>(std::move(optimization)))
609+
);
610+
}
611+
612+
auto pre_optimizations() { return range(pre_optimizations_.begin(), pre_optimizations_.end()); }
613+
auto pre_optimizations_begin() { return pre_optimizations_.begin(); }
614+
auto pre_optimizations_end() { return pre_optimizations_.end(); }
615+
auto pre_optimizations_begin() const { return pre_optimizations_.begin(); }
616+
auto pre_optimizations_end() const { return pre_optimizations_.end(); }
617+
auto pre_optimizations_cbegin() const { return pre_optimizations_begin(); }
618+
auto pre_optimizations_cend() const { return pre_optimizations_end(); }
619+
620+
/*===== Post-Optimizations ========================================================================================*/
621+
/** Registers a new post-optimization with the given `name`. */
622+
void register_post_optimization(const char *name, PostOptimizationCallback optimization, const char *description = nullptr)
623+
{
624+
post_optimizations_.add(
625+
pool(name),
626+
Component<PostOptimizationCallback>(description, std::make_unique<PostOptimizationCallback>(std::move(optimization)))
627+
);
628+
}
629+
630+
auto post_optimizations() { return range(post_optimizations_.begin(), post_optimizations_.end()); }
631+
auto post_optimizations_begin() { return post_optimizations_.begin(); }
632+
auto post_optimizations_end() { return post_optimizations_.end(); }
633+
auto post_optimizations_begin() const { return post_optimizations_.begin(); }
634+
auto post_optimizations_end() const { return post_optimizations_.end(); }
635+
auto post_optimizations_cbegin() const { return post_optimizations_begin(); }
636+
auto post_optimizations_cend() const { return post_optimizations_end(); }
595637
};
596638

597639
}

src/IR/Operator.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,14 @@ void Operator::minimize_schema()
543543
SchemaMinimizer M;
544544
M(*this);
545545
}
546+
547+
__attribute__((constructor(202)))
548+
void register_post_optimization()
549+
{
550+
Catalog &C = Catalog::Get();
551+
552+
C.register_post_optimization("minimize schema", [](std::unique_ptr<Producer> plan) {
553+
plan->minimize_schema();
554+
return plan;
555+
}, "minimizes the schema of an operator tree");
556+
}

src/IR/Optimizer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ Optimizer::optimize_with_plantable(const QueryGraph &G) const
165165
return { std::make_unique<ProjectionOperator>(G.projections()), std::move(plan_table) };
166166
}
167167

168+
/*----- Perform pre-optimizations on the QueryGraph. -------------------------------------------------------------*/
169+
for (auto &pre_opt : C.pre_optimizations())
170+
(*pre_opt.second).operator()(G);
171+
168172
/*----- Initialize plan table and compute plans for data sources. ------------------------------------------------*/
169173
Producer **source_plans = new Producer*[num_sources];
170174
for (auto &ds : G.sources()) {
@@ -356,7 +360,10 @@ Optimizer::optimize_with_plantable(const QueryGraph &G) const
356360
plan = std::move(projection);
357361
}
358362

359-
plan->minimize_schema();
363+
/*----- Perform post-optimizations on the Operator tree. ---------------------------------------------------------*/
364+
for (auto &post_opt : C.post_optimizations())
365+
plan = (*post_opt.second).operator()(std::move(plan)); // TODO add plan_table to post-optimizations
366+
360367
delete[] source_plans;
361368
return { std::move(plan), std::move(plan_table) };
362369
}

0 commit comments

Comments
 (0)