Skip to content

Commit

Permalink
Parse and represent window functions in the LQP (#2574)
Browse files Browse the repository at this point in the history
Add the representation of SQL:2003 window functions in the LQP, including the translation in the SQLTranslator.
  • Loading branch information
dey4ss committed Jul 26, 2023
1 parent 61e7c29 commit b685721
Show file tree
Hide file tree
Showing 58 changed files with 2,161 additions and 1,051 deletions.
28 changes: 14 additions & 14 deletions src/benchmark/operators/aggregate_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ using namespace expression_functional; // NOLINT(build/namespaces)
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateHash)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {ColumnID{0} /* "a" */};

Expand All @@ -33,8 +33,8 @@ BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateHash)(benchmark::State& stat
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortNotSortedNoGroupBy)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {};

Expand All @@ -49,8 +49,8 @@ BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortNotSortedNoGroupBy)(benc
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortSortedNoGroupBy)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {};

Expand All @@ -73,8 +73,8 @@ BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortSortedNoGroupBy)(benchma
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortNotSortedOneGroupBy)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {ColumnID{1}};

Expand All @@ -89,8 +89,8 @@ BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortNotSortedOneGroupBy)(ben
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortSortedOneGroupBy)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {ColumnID{1}};

Expand All @@ -113,8 +113,8 @@ BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortSortedOneGroupBy)(benchm
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortNotSortedMultipleGroupBy)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {ColumnID{0}, ColumnID{1}};

Expand All @@ -129,8 +129,8 @@ BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortNotSortedMultipleGroupBy
BENCHMARK_F(MicroBenchmarkBasicFixture, BM_AggregateSortSortedMultipleGroupBy)(benchmark::State& state) {
_clear_cache();

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(min_(pqp_column_(ColumnID{1}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {ColumnID{0}, ColumnID{1}};

Expand Down
4 changes: 2 additions & 2 deletions src/benchmark/operators/join_aggregate_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ void BM_Join_Aggregate(benchmark::State& state) {
auto operator_join_predicate =
OperatorJoinPredicate(std::make_pair(ColumnID{0}, ColumnID{0}), PredicateCondition::Equals);

auto aggregates = std::vector<std::shared_ptr<AggregateExpression>>{
std::static_pointer_cast<AggregateExpression>(avg_(pqp_column_(ColumnID{0}, DataType::Int, false, "b")))};
auto aggregates = std::vector<std::shared_ptr<WindowFunctionExpression>>{
std::static_pointer_cast<WindowFunctionExpression>(avg_(pqp_column_(ColumnID{0}, DataType::Int, false, "b")))};

std::vector<ColumnID> groupby = {ColumnID{0}, ColumnID{2}};

Expand Down
4 changes: 2 additions & 2 deletions src/benchmark/tpch_data_micro_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "micro_benchmark_basic_fixture.hpp"

#include "benchmark_config.hpp"
#include "expression/aggregate_expression.hpp"
#include "expression/expression_functional.hpp"
#include "expression/window_function_expression.hpp"
#include "hyrise.hpp"
#include "logical_query_plan/join_node.hpp"
#include "logical_query_plan/lqp_translator.hpp"
Expand Down Expand Up @@ -218,7 +218,7 @@ BENCHMARK_F(TPCHDataMicroBenchmarkFixture, BM_ScanAggregate)(benchmark::State& s
const auto mocked_table_scan_output = sorted_lineitem->get_output();
const ColumnID group_by_column = l_orderkey_id;
const std::vector<ColumnID> group_by = {l_orderkey_id};
const auto aggregate_expressions = std::vector<std::shared_ptr<AggregateExpression>>{
const auto aggregate_expressions = std::vector<std::shared_ptr<WindowFunctionExpression>>{
count_(pqp_column_(group_by_column, mocked_table_scan_output->column_data_type(group_by_column),
mocked_table_scan_output->column_is_nullable(group_by_column),
mocked_table_scan_output->column_name(group_by_column)))};
Expand Down
83 changes: 44 additions & 39 deletions src/bin/console/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,64 +767,69 @@ int Console::_visualize(const std::string& input) {
}

if (no_execute && !sql.empty() && _sql_pipeline->requires_execution()) {
out("Error: We do not support the visualization of multiple dependant statements in 'noexec' mode.\n");
out("Error: We do not support the visualization of multiple dependent statements in 'noexec' mode.\n");
return ReturnCode::Error;
}

const auto img_filename = plan_type_str + ".png";

switch (plan_type) {
case PlanType::LQP:
case PlanType::UnoptLQP: {
auto lqp_roots = std::vector<std::shared_ptr<AbstractLQPNode>>{};
try {
switch (plan_type) {
case PlanType::LQP:
case PlanType::UnoptLQP: {
auto lqp_roots = std::vector<std::shared_ptr<AbstractLQPNode>>{};

const auto& lqps = (plan_type == PlanType::LQP) ? _sql_pipeline->get_optimized_logical_plans()
: _sql_pipeline->get_unoptimized_logical_plans();
const auto& lqps = (plan_type == PlanType::LQP) ? _sql_pipeline->get_optimized_logical_plans()
: _sql_pipeline->get_unoptimized_logical_plans();

lqp_roots.reserve(lqps.size());
lqp_roots.reserve(lqps.size());

for (const auto& lqp : lqps) {
lqp_roots.emplace_back(lqp);
}
for (const auto& lqp : lqps) {
lqp_roots.emplace_back(lqp);
}

auto visualizer = LQPVisualizer{};
visualizer.visualize(lqp_roots, img_filename);
} break;
auto visualizer = LQPVisualizer{};
visualizer.visualize(lqp_roots, img_filename);
} break;

case PlanType::PQP: {
if (!no_execute) {
_sql_pipeline->get_result_table();
case PlanType::PQP: {
if (!no_execute) {
_sql_pipeline->get_result_table();

// Store the transaction context as potentially modified by the pipeline. It might be a new context if a
// transaction was started or nullptr if we are in auto-commit mode or the last transaction was finished.
_explicitly_created_transaction_context = _sql_pipeline->transaction_context();
}
// Store the transaction context as potentially modified by the pipeline. It might be a new context if a
// transaction was started or nullptr if we are in auto-commit mode or the last transaction was finished.
_explicitly_created_transaction_context = _sql_pipeline->transaction_context();
}

auto visualizer = PQPVisualizer{};
visualizer.visualize(_sql_pipeline->get_physical_plans(), img_filename);
} break;
auto visualizer = PQPVisualizer{};
visualizer.visualize(_sql_pipeline->get_physical_plans(), img_filename);
} break;

case PlanType::Joins: {
out("NOTE: Join graphs will show only Cross and Inner joins, not Semi, Left, Right, Full outer, "
"AntiNullAsTrue and AntiNullAsFalse joins.\n");
case PlanType::Joins: {
out("NOTE: Join graphs will show only Cross and Inner joins, not Semi, Left, Right, Full outer, "
"AntiNullAsTrue and AntiNullAsFalse joins.\n");

auto join_graphs = std::vector<JoinGraph>{};
auto join_graphs = std::vector<JoinGraph>{};

const auto& lqps = _sql_pipeline->get_optimized_logical_plans();
for (const auto& lqp : lqps) {
const auto sub_lqps = lqp_find_subplan_roots(lqp);
const auto& lqps = _sql_pipeline->get_optimized_logical_plans();
for (const auto& lqp : lqps) {
const auto sub_lqps = lqp_find_subplan_roots(lqp);

for (const auto& sub_lqp : sub_lqps) {
const auto sub_lqp_join_graphs = JoinGraph::build_all_in_lqp(sub_lqp);
for (const auto& sub_lqp_join_graph : sub_lqp_join_graphs) {
join_graphs.emplace_back(sub_lqp_join_graph);
for (const auto& sub_lqp : sub_lqps) {
const auto sub_lqp_join_graphs = JoinGraph::build_all_in_lqp(sub_lqp);
for (const auto& sub_lqp_join_graph : sub_lqp_join_graphs) {
join_graphs.emplace_back(sub_lqp_join_graph);
}
}
}
}

auto visualizer = JoinGraphVisualizer{};
visualizer.visualize(join_graphs, img_filename);
} break;
auto visualizer = JoinGraphVisualizer{};
visualizer.visualize(join_graphs, img_filename);
} break;
}
} catch (const InvalidInputException& exception) {
out(std::string(exception.what()) + '\n');
return false;
}

// NOLINTBEGIN(concurrency-mt-unsafe) - system() is not thread-safe, but it's not used concurrently here.
Expand Down
10 changes: 7 additions & 3 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ set(
expression/abstract_expression.hpp
expression/abstract_predicate_expression.cpp
expression/abstract_predicate_expression.hpp
expression/aggregate_expression.cpp
expression/aggregate_expression.hpp
expression/arithmetic_expression.cpp
expression/arithmetic_expression.hpp
expression/between_expression.cpp
Expand Down Expand Up @@ -81,6 +79,10 @@ set(
expression/unary_minus_expression.hpp
expression/value_expression.cpp
expression/value_expression.hpp
expression/window_expression.cpp
expression/window_expression.hpp
expression/window_function_expression.cpp
expression/window_function_expression.hpp
hyrise.cpp
hyrise.hpp
import_export/binary/binary_parser.cpp
Expand Down Expand Up @@ -164,6 +166,8 @@ set(
logical_query_plan/update_node.hpp
logical_query_plan/validate_node.cpp
logical_query_plan/validate_node.hpp
logical_query_plan/window_node.cpp
logical_query_plan/window_node.hpp
lossless_cast.cpp
lossless_cast.hpp
lossy_cast.hpp
Expand All @@ -180,7 +184,7 @@ set(
operators/abstract_read_only_operator.hpp
operators/abstract_read_write_operator.cpp
operators/abstract_read_write_operator.hpp
operators/aggregate/aggregate_traits.hpp
operators/aggregate/window_function_traits.hpp
operators/aggregate_hash.cpp
operators/aggregate_hash.hpp
operators/aggregate_sort.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/lib/expression/abstract_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include "expression_utils.hpp"
#include "utils/assert.hpp"

using namespace std::string_literals; // NOLINT

namespace hyrise {

using namespace std::string_literals; // NOLINT(build/namespaces)

AbstractExpression::AbstractExpression(const ExpressionType init_type,
const std::vector<std::shared_ptr<AbstractExpression>>& init_arguments)
: type(init_type), arguments(init_arguments) {}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/expression/abstract_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class AbstractLQPNode;
class AbstractOperator;

enum class ExpressionType {
Aggregate,
Arithmetic,
Cast,
Case,
Expand All @@ -33,7 +32,9 @@ enum class ExpressionType {
PQPSubquery,
LQPSubquery,
UnaryMinus,
Value
Value,
Window,
WindowFunction
};

/**
Expand Down
Loading

0 comments on commit b685721

Please sign in to comment.