Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mytherin committed Jan 11, 2020
1 parent e194762 commit 4e68447
Show file tree
Hide file tree
Showing 50 changed files with 174 additions and 101 deletions.
12 changes: 10 additions & 2 deletions benchmark/tpch/lineitem_aggregate.cpp
Expand Up @@ -71,7 +71,11 @@ void Load(DuckDBBenchmarkState *state) override {
tpch::dbgen(SF, state->db);
}
string GetQuery() override {
return "SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem, orders WHERE l_orderkey=o_orderkey GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag, l_linestatus";
return "SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, "
"sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + "
"l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS "
"avg_disc, count(*) AS count_order FROM lineitem, orders WHERE l_orderkey=o_orderkey GROUP BY l_returnflag, "
"l_linestatus ORDER BY l_returnflag, l_linestatus";
}
string VerifyResult(QueryResult *result) override {
if (!result->success) {
Expand All @@ -90,7 +94,11 @@ void Load(DuckDBBenchmarkState *state) override {
tpch::dbgen(SF, state->db);
}
string GetQuery() override {
return "SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS avg_disc, count(*) AS count_order FROM lineitem, orders WHERE l_orderkey=o_orderkey AND l_shipdate <= cast('1998-09-02' AS date) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag, l_linestatus";
return "SELECT l_returnflag, l_linestatus, sum(l_quantity) AS sum_qty, sum(l_extendedprice) AS sum_base_price, "
"sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + "
"l_tax)) AS sum_charge, avg(l_quantity) AS avg_qty, avg(l_extendedprice) AS avg_price, avg(l_discount) AS "
"avg_disc, count(*) AS count_order FROM lineitem, orders WHERE l_orderkey=o_orderkey AND l_shipdate <= "
"cast('1998-09-02' AS date) GROUP BY l_returnflag, l_linestatus ORDER BY l_returnflag, l_linestatus";
}
string VerifyResult(QueryResult *result) override {
if (!result->success) {
Expand Down
18 changes: 10 additions & 8 deletions src/execution/column_binding_resolver.cpp
Expand Up @@ -18,7 +18,7 @@ ColumnBindingResolver::ColumnBindingResolver() {
void ColumnBindingResolver::VisitOperator(LogicalOperator &op) {
if (op.type == LogicalOperatorType::COMPARISON_JOIN || op.type == LogicalOperatorType::DELIM_JOIN) {
// special case: comparison join
auto &comp_join = (LogicalComparisonJoin&) op;
auto &comp_join = (LogicalComparisonJoin &)op;
// first get the bindings of the LHS and resolve the LHS expressions
VisitOperator(*comp_join.children[0]);
for (auto &cond : comp_join.conditions) {
Expand All @@ -40,16 +40,17 @@ void ColumnBindingResolver::VisitOperator(LogicalOperator &op) {
bindings = op.GetColumnBindings();
return;
} else if (op.type == LogicalOperatorType::ANY_JOIN) {
// ANY join, this join is different because we evaluate the expression on the bindings of BOTH join sides at once
// i.e. we set the bindings first to the bindings of the entire join, and then resolve the expressions of this operator
// ANY join, this join is different because we evaluate the expression on the bindings of BOTH join sides at
// once i.e. we set the bindings first to the bindings of the entire join, and then resolve the expressions of
// this operator
VisitOperatorChildren(op);
bindings = op.GetColumnBindings();
VisitOperatorExpressions(op);
return;
} else if (op.type == LogicalOperatorType::CREATE_INDEX) {
// CREATE INDEX statement, add the columns of the table with table index 0 to the binding set
// afterwards bind the expressions of the CREATE INDEX statement
auto &create_index = (LogicalCreateIndex&) op;
auto &create_index = (LogicalCreateIndex &)op;
bindings = LogicalOperator::GenerateColumnBindings(0, create_index.table.columns.size());
VisitOperatorExpressions(op);
return;
Expand All @@ -67,21 +68,22 @@ unique_ptr<Expression> ColumnBindingResolver::VisitReplace(BoundColumnRefExpress
unique_ptr<Expression> *expr_ptr) {
assert(expr.depth == 0);
// check the current set of column bindings to see which index corresponds to the column reference
for(index_t i = 0; i < bindings.size(); i++) {
for (index_t i = 0; i < bindings.size(); i++) {
if (expr.binding == bindings[i]) {
return make_unique<BoundReferenceExpression>(expr.alias, expr.return_type, i);
}
}
// could not bind the column reference, this should never happen and indicates a bug in the code
// generate an error message
string bound_columns = "[";
for(index_t i = 0; i < bindings.size(); i++) {
string bound_columns = "[";
for (index_t i = 0; i < bindings.size(); i++) {
if (i != 0) {
bound_columns += " ";
}
bound_columns += to_string(bindings[i].table_index) + "." + to_string(bindings[i].column_index);
}
bound_columns += "]";

throw InternalException("Failed to bind column reference \"%s\" [%d.%d] (bindings: %s)", expr.alias.c_str(), (int) expr.binding.table_index, (int) expr.binding.column_index, bound_columns.c_str());
throw InternalException("Failed to bind column reference \"%s\" [%d.%d] (bindings: %s)", expr.alias.c_str(),
(int)expr.binding.table_index, (int)expr.binding.column_index, bound_columns.c_str());
}
3 changes: 1 addition & 2 deletions src/execution/operator/filter/physical_filter.cpp
Expand Up @@ -26,7 +26,6 @@ PhysicalFilter::PhysicalFilter(vector<TypeId> types, vector<unique_ptr<Expressio
} else {
expression = move(select_list[0]);
}

}

void PhysicalFilter::GetChunkInternal(ClientContext &context, DataChunk &chunk, PhysicalOperatorState *state_) {
Expand All @@ -42,7 +41,7 @@ void PhysicalFilter::GetChunkInternal(ClientContext &context, DataChunk &chunk,
}
initial_count = chunk.size();
result_count = state->executor.SelectExpression(chunk, chunk.owned_sel_vector);
} while(result_count == 0);
} while (result_count == 0);

if (result_count == initial_count) {
// nothing was filtered: skip adding any selection vectors
Expand Down
14 changes: 8 additions & 6 deletions src/execution/operator/join/physical_hash_join.cpp
Expand Up @@ -23,20 +23,22 @@ class PhysicalHashJoinState : public PhysicalComparisonJoinState {

PhysicalHashJoin::PhysicalHashJoin(ClientContext &context, LogicalOperator &op, unique_ptr<PhysicalOperator> left,
unique_ptr<PhysicalOperator> right, vector<JoinCondition> cond, JoinType join_type,
vector<index_t> left_projection_map, vector<index_t> right_projection_map)
: PhysicalComparisonJoin(op, PhysicalOperatorType::HASH_JOIN, move(cond), join_type), right_projection_map(right_projection_map) {
vector<index_t> left_projection_map, vector<index_t> right_projection_map)
: PhysicalComparisonJoin(op, PhysicalOperatorType::HASH_JOIN, move(cond), join_type),
right_projection_map(right_projection_map) {
children.push_back(move(left));
children.push_back(move(right));

assert(left_projection_map.size() == 0);

hash_table =
make_unique<JoinHashTable>(*context.db.storage->buffer_manager, conditions, LogicalOperator::MapTypes(children[1]->GetTypes(), right_projection_map), type);
make_unique<JoinHashTable>(*context.db.storage->buffer_manager, conditions,
LogicalOperator::MapTypes(children[1]->GetTypes(), right_projection_map), type);
}

PhysicalHashJoin::PhysicalHashJoin(ClientContext &context, LogicalOperator &op, unique_ptr<PhysicalOperator> left,
unique_ptr<PhysicalOperator> right, vector<JoinCondition> cond, JoinType join_type) :
PhysicalHashJoin(context, op, move(left), move(right), move(cond), join_type, {}, {}) {
unique_ptr<PhysicalOperator> right, vector<JoinCondition> cond, JoinType join_type)
: PhysicalHashJoin(context, op, move(left), move(right), move(cond), join_type, {}, {}) {
}

void PhysicalHashJoin::BuildHashTable(ClientContext &context, PhysicalOperatorState *state_) {
Expand Down Expand Up @@ -66,7 +68,7 @@ void PhysicalHashJoin::BuildHashTable(ClientContext &context, PhysicalOperatorSt
if (right_projection_map.size() > 0) {
// there is a projection map: fill the build chunk with the projected columns
build_chunk.Reset();
for(index_t i = 0; i < right_projection_map.size(); i++) {
for (index_t i = 0; i < right_projection_map.size(); i++) {
build_chunk.data[i].Reference(right_chunk.data[right_projection_map[i]]);
}
build_chunk.sel_vector = right_chunk.sel_vector;
Expand Down
9 changes: 6 additions & 3 deletions src/execution/physical_plan/plan_comparison_join.cpp
Expand Up @@ -39,12 +39,15 @@ unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalComparison
unique_ptr<PhysicalOperator> plan;
if (has_equality) {
// equality join: use hash join
plan = make_unique<PhysicalHashJoin>(context, op, move(left), move(right), move(op.conditions), op.join_type, op.left_projection_map, op.right_projection_map);
plan = make_unique<PhysicalHashJoin>(context, op, move(left), move(right), move(op.conditions), op.join_type,
op.left_projection_map, op.right_projection_map);
} else {
assert(!has_null_equal_conditions); // don't support this for anything but hash joins for now
if (op.conditions.size() == 1 && (op.join_type == JoinType::MARK || op.join_type == JoinType::INNER) && !has_inequality) {
if (op.conditions.size() == 1 && (op.join_type == JoinType::MARK || op.join_type == JoinType::INNER) &&
!has_inequality) {
// range join: use piecewise merge join
plan = make_unique<PhysicalPiecewiseMergeJoin>(op, move(left), move(right), move(op.conditions), op.join_type);
plan =
make_unique<PhysicalPiecewiseMergeJoin>(op, move(left), move(right), move(op.conditions), op.join_type);
} else {
// inequality join: use nested loop
plan = make_unique<PhysicalNestedLoopJoin>(op, move(left), move(right), move(op.conditions), op.join_type);
Expand Down
2 changes: 1 addition & 1 deletion src/execution/physical_plan/plan_filter.cpp
Expand Up @@ -21,7 +21,7 @@ unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalFilter &op
if (op.projection_map.size() > 0) {
// there is a projection map, generate a physical projection
vector<unique_ptr<Expression>> select_list;
for(index_t i = 0; i < op.projection_map.size(); i++) {
for (index_t i = 0; i < op.projection_map.size(); i++) {
select_list.push_back(make_unique<BoundReferenceExpression>(op.types[i], op.projection_map[i]));
}
auto proj = make_unique<PhysicalProjection>(op.types, move(select_list));
Expand Down
5 changes: 3 additions & 2 deletions src/include/duckdb/common/exception.hpp
Expand Up @@ -55,8 +55,9 @@ enum class ExceptionType {
NULL_POINTER = 27, // nullptr exception
IO = 28, // IO exception
INTERRUPT = 29, // interrupt
FATAL = 30, // Fatal exception: fatal exceptions are non-recoverable, and render the entire DB in an unusable state
INTERNAL = 31 // Internal exception: exception that indicates something went wrong internally (i.e. bug in the code base)
FATAL = 30, // Fatal exception: fatal exceptions are non-recoverable, and render the entire DB in an unusable state
INTERNAL =
31 // Internal exception: exception that indicates something went wrong internally (i.e. bug in the code base)
};

class Exception : public std::exception {
Expand Down
1 change: 1 addition & 0 deletions src/include/duckdb/execution/column_binding_resolver.hpp
Expand Up @@ -21,6 +21,7 @@ class ColumnBindingResolver : public LogicalOperatorVisitor {
ColumnBindingResolver();

void VisitOperator(LogicalOperator &op) override;

protected:
vector<ColumnBinding> bindings;

Expand Down
Expand Up @@ -21,6 +21,7 @@ class PhysicalFilter : public PhysicalOperator {

//! The filter expression
unique_ptr<Expression> expression;

public:
void GetChunkInternal(ClientContext &context, DataChunk &chunk, PhysicalOperatorState *state) override;

Expand Down
Expand Up @@ -21,7 +21,7 @@ class PhysicalHashJoin : public PhysicalComparisonJoin {
public:
PhysicalHashJoin(ClientContext &context, LogicalOperator &op, unique_ptr<PhysicalOperator> left,
unique_ptr<PhysicalOperator> right, vector<JoinCondition> cond, JoinType join_type,
vector<index_t> left_projection_map, vector<index_t> right_projection_map);
vector<index_t> left_projection_map, vector<index_t> right_projection_map);
PhysicalHashJoin(ClientContext &context, LogicalOperator &op, unique_ptr<PhysicalOperator> left,
unique_ptr<PhysicalOperator> right, vector<JoinCondition> cond, JoinType join_type);

Expand Down
1 change: 1 addition & 0 deletions src/include/duckdb/execution/physical_operator.hpp
Expand Up @@ -61,6 +61,7 @@ class PhysicalOperator {
vector<unique_ptr<PhysicalOperator>> children;
//! The types returned by this physical operator
vector<TypeId> types;

public:
string ToString(index_t depth = 0) const;
void Print();
Expand Down
15 changes: 11 additions & 4 deletions src/include/duckdb/optimizer/column_lifetime_optimizer.hpp
Expand Up @@ -14,24 +14,31 @@
namespace duckdb {
class BoundColumnRefExpression;

//! The ColumnLifetimeAnalyzer optimizer traverses the logical operator tree and ensures that columns are removed from the plan when no longer required
//! The ColumnLifetimeAnalyzer optimizer traverses the logical operator tree and ensures that columns are removed from
//! the plan when no longer required
class ColumnLifetimeAnalyzer : public LogicalOperatorVisitor {
public:
ColumnLifetimeAnalyzer(bool is_root = false) : everything_referenced(is_root) {}
ColumnLifetimeAnalyzer(bool is_root = false) : everything_referenced(is_root) {
}

void VisitOperator(LogicalOperator &op) override;

protected:
unique_ptr<Expression> VisitReplace(BoundColumnRefExpression &expr, unique_ptr<Expression> *expr_ptr) override;
unique_ptr<Expression> VisitReplace(BoundReferenceExpression &expr, unique_ptr<Expression> *expr_ptr) override;

private:
//! Whether or not all the columns are referenced. This happens in the case of the root expression (because the output implicitly refers all the columns below it)
//! Whether or not all the columns are referenced. This happens in the case of the root expression (because the
//! output implicitly refers all the columns below it)
bool everything_referenced;
//! The set of column references
column_binding_set_t column_references;

private:
void StandardVisitOperator(LogicalOperator &op);

void ExtractUnusedColumnBindings(vector<ColumnBinding> bindings, column_binding_set_t &unused_bindings);
void GenerateProjectionMap(vector<ColumnBinding> bindings, column_binding_set_t &unused_bindings, vector<index_t> &map);
void GenerateProjectionMap(vector<ColumnBinding> bindings, column_binding_set_t &unused_bindings,
vector<index_t> &map);
};
} // namespace duckdb
17 changes: 11 additions & 6 deletions src/include/duckdb/optimizer/remove_unused_columns.hpp
Expand Up @@ -17,22 +17,27 @@ class BoundColumnRefExpression;
//! The RemoveUnusedColumns optimizer traverses the logical operator tree and removes any columns that are not required
class RemoveUnusedColumns : public LogicalOperatorVisitor {
public:
RemoveUnusedColumns(bool is_root = false) : everything_referenced(is_root) {}
RemoveUnusedColumns(bool is_root = false) : everything_referenced(is_root) {
}

void VisitOperator(LogicalOperator &op) override;

protected:
unique_ptr<Expression> VisitReplace(BoundColumnRefExpression &expr, unique_ptr<Expression> *expr_ptr) override;
unique_ptr<Expression> VisitReplace(BoundReferenceExpression &expr, unique_ptr<Expression> *expr_ptr) override;

private:
//! Whether or not all the columns are referenced. This happens in the case of the root expression (because the output implicitly refers all the columns below it)
//! Whether or not all the columns are referenced. This happens in the case of the root expression (because the
//! output implicitly refers all the columns below it)
bool everything_referenced;
//! The map of column references
column_binding_map_t<vector<BoundColumnRefExpression*>> column_references;
column_binding_map_t<vector<BoundColumnRefExpression *>> column_references;

private:
template<class T>
void ClearUnusedExpressions(vector<T> &list, index_t table_idx);
template <class T> void ClearUnusedExpressions(vector<T> &list, index_t table_idx);

//! Perform a replacement of the ColumnBinding, iterating over all the currently found column references and replacing the bindings
//! Perform a replacement of the ColumnBinding, iterating over all the currently found column references and
//! replacing the bindings
void ReplaceBinding(ColumnBinding current_binding, ColumnBinding new_binding);
};
} // namespace duckdb
1 change: 1 addition & 0 deletions src/include/duckdb/planner/bound_query_node.hpp
Expand Up @@ -49,6 +49,7 @@ class BoundQueryNode {
vector<string> names;
//! The types returned by this QueryNode.
vector<SQLType> types;

public:
virtual index_t GetRootIndex() = 0;
};
Expand Down
1 change: 1 addition & 0 deletions src/include/duckdb/planner/operator/logical_aggregate.hpp
Expand Up @@ -30,6 +30,7 @@ class LogicalAggregate : public LogicalOperator {
string ParamsToString() const override;

vector<ColumnBinding> GetColumnBindings() override;

protected:
void ResolveTypes() override;
};
Expand Down
1 change: 1 addition & 0 deletions src/include/duckdb/planner/operator/logical_chunk_get.hpp
Expand Up @@ -33,6 +33,7 @@ class LogicalChunkGet : public LogicalOperator {
vector<ColumnBinding> GetColumnBindings() override {
return GenerateColumnBindings(table_index, chunk_types.size());
}

protected:
void ResolveTypes() override {
// types are resolved in the constructor
Expand Down
Expand Up @@ -22,8 +22,10 @@ class LogicalComparisonJoin : public LogicalJoin {

//! The conditions of the join
vector<JoinCondition> conditions;

public:
string ParamsToString() const override;

public:
static unique_ptr<LogicalOperator> CreateJoin(JoinType type, unique_ptr<LogicalOperator> left_child,
unique_ptr<LogicalOperator> right_child,
Expand Down
Expand Up @@ -16,7 +16,8 @@ namespace duckdb {
class LogicalCopyFromFile : public LogicalOperator {
public:
LogicalCopyFromFile(index_t table_index, unique_ptr<CopyInfo> info, vector<SQLType> sql_types)
: LogicalOperator(LogicalOperatorType::COPY_FROM_FILE), table_index(table_index), info(move(info)), sql_types(sql_types) {
: LogicalOperator(LogicalOperatorType::COPY_FROM_FILE), table_index(table_index), info(move(info)),
sql_types(sql_types) {
}

index_t table_index;
Expand All @@ -27,6 +28,7 @@ class LogicalCopyFromFile : public LogicalOperator {
vector<ColumnBinding> GetColumnBindings() override {
return GenerateColumnBindings(table_index, sql_types.size());
}

protected:
void ResolveTypes() override {
for (auto &type : sql_types) {
Expand Down
Expand Up @@ -19,6 +19,7 @@ class LogicalCrossProduct : public LogicalOperator {

public:
vector<ColumnBinding> GetColumnBindings() override;

protected:
void ResolveTypes() override;
};
Expand Down
2 changes: 2 additions & 0 deletions src/include/duckdb/planner/operator/logical_delim_get.hpp
Expand Up @@ -25,10 +25,12 @@ class LogicalDelimGet : public LogicalOperator {
index_t table_index;
//! The types of the chunk
vector<TypeId> chunk_types;

public:
vector<ColumnBinding> GetColumnBindings() override {
return GenerateColumnBindings(table_index, chunk_types.size());
}

protected:
void ResolveTypes() override {
// types are resolved in the constructor
Expand Down
1 change: 1 addition & 0 deletions src/include/duckdb/planner/operator/logical_distinct.hpp
Expand Up @@ -27,6 +27,7 @@ class LogicalDistinct : public LogicalOperator {
vector<ColumnBinding> GetColumnBindings() override {
return children[0]->GetColumnBindings();
}

protected:
void ResolveTypes() override {
types = children[0]->types;
Expand Down
2 changes: 2 additions & 0 deletions src/include/duckdb/planner/operator/logical_empty_result.hpp
Expand Up @@ -22,10 +22,12 @@ class LogicalEmptyResult : public LogicalOperator {
vector<TypeId> return_types;
//! The columns that would be bound at this location (if the subtree was not optimized away)
vector<ColumnBinding> bindings;

public:
vector<ColumnBinding> GetColumnBindings() override {
return bindings;
}

protected:
void ResolveTypes() override {
this->types = return_types;
Expand Down
Expand Up @@ -26,10 +26,12 @@ class LogicalExpressionGet : public LogicalOperator {
vector<TypeId> expr_types;
//! The set of expressions
vector<vector<unique_ptr<Expression>>> expressions;

public:
vector<ColumnBinding> GetColumnBindings() override {
return GenerateColumnBindings(table_index, expr_types.size());
}

protected:
void ResolveTypes() override {
// types are resolved in the constructor
Expand Down

0 comments on commit 4e68447

Please sign in to comment.