-
Notifications
You must be signed in to change notification settings - Fork 180
Cast pushdown for duckdb #8620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Cast pushdown for duckdb #8620
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
| #include "cast_pushdown.hpp" | ||
| #include "table_function.hpp" | ||
|
|
||
| #include "duckdb/planner/operator/logical_get.hpp" | ||
| #include "duckdb/planner/operator/logical_projection.hpp" | ||
| #include "duckdb/planner/expression/bound_cast_expression.hpp" | ||
| #include "duckdb/planner/expression/bound_columnref_expression.hpp" | ||
|
|
||
| // A GET reachable through a single-child chain of filters/projections. A join | ||
| // (or any other multi-child operator) breaks the chain. | ||
| // See test/sql/copy/csv/test_insert_into_types.test in duckdb (cast not pushed past a join) | ||
| static bool ReachesPushdownGet(const LogicalOperator &op) { | ||
| const LogicalOperator *cur = &op; | ||
| while (cur->children.size() == 1) { | ||
| cur = cur->children[0].get(); | ||
| switch (cur->type) { | ||
| case LogicalOperatorType::LOGICAL_GET: | ||
| return cur->Cast<LogicalGet>().function.bind == duckdb_vx_table_function_bind; | ||
| case LogicalOperatorType::LOGICAL_FILTER: | ||
| case LogicalOperatorType::LOGICAL_PROJECTION: | ||
| continue; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void CastCollect::VisitOperator(LogicalOperator &op) { | ||
| /* | ||
| * Logical projection expressions are columns which reference underlying | ||
| * GETs. Don't process them, as they would add conflicts for every column | ||
| * used in projection. Example: PROJECTION(col) -> GET(col). We don't want | ||
| * to visit BoundColumnRefExpression in PROJECTION to avoid registering a | ||
| * non-existent conflict. | ||
| * | ||
| * However, CastReplace will visit them because we need to update their | ||
| * types if pushdown succeeded. | ||
| */ | ||
| if (op.type != LogicalOperatorType::LOGICAL_PROJECTION) { | ||
| return LogicalOperatorVisitor::VisitOperator(op); | ||
| } | ||
| auto &projection = op.Cast<LogicalProjection>(); | ||
|
|
||
| // Only push casts from a projection that forwards just column refs and | ||
| // casts and reaches a GET without a join in between. A constant or other | ||
| // expression makes the projection ineligible. | ||
| // See test/sql/copy/csv/test_csv_error_message_type.test (top-level cast | ||
| // to VARCHAR must still push) and test_large_integer_detection.test (a | ||
| // nested cast to VARCHAR must not) in duckdb. | ||
| bool clean = ReachesPushdownGet(projection); | ||
| for (const auto &e : projection.expressions) { | ||
| switch (e->GetExpressionClass()) { | ||
| case ExpressionClass::BOUND_COLUMN_REF: | ||
| case ExpressionClass::BOUND_CAST: | ||
| continue; | ||
| default: | ||
| clean = false; | ||
| break; | ||
| } | ||
| } | ||
| if (clean) { | ||
| for (const auto &e : projection.expressions) { | ||
| if (e->GetExpressionClass() == ExpressionClass::BOUND_CAST) { | ||
| top_level_casts.insert(e.get()); | ||
| } | ||
| } | ||
| } | ||
| if (projections.count(projection.table_index)) { | ||
| VisitOperatorChildren(op); | ||
| return; | ||
| } | ||
|
|
||
| LogicalOperatorVisitor::VisitOperator(op); | ||
| } | ||
|
|
||
| ExpressionPtr CastCollect::VisitReplace(BoundColumnRefExpression &expr, ExpressionPtr *ptr) { | ||
| if (const auto binding = Resolve(expr.binding, analyses, projections)) { | ||
| // Column is used without cast applied to it, register a conflict. | ||
| // Not emplace() as we need to update the value if it was present | ||
| binding->analysis.col_to_expr[binding->column_index] = nullptr; | ||
| } | ||
| return std::move(*ptr); | ||
| } | ||
|
|
||
| ExpressionPtr CastCollect::VisitReplace(BoundCastExpression &expr, ExpressionPtr *ptr) { | ||
| if (expr.child->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) { | ||
| // Descend into children so e.g. fn(col, other) still sees "col" and | ||
| // registers a conflict | ||
| return nullptr; | ||
| } | ||
| const auto &bound_col = expr.child->Cast<BoundColumnRefExpression>(); | ||
| const auto binding = Resolve(bound_col.binding, analyses, projections); | ||
| if (!binding) { | ||
| return nullptr; | ||
| } | ||
| auto &col_to_expr = binding->analysis.col_to_expr; | ||
|
|
||
| if (auto it = col_to_expr.find(binding->column_index); it == col_to_expr.end()) { | ||
| // Only a top-level projection cast starts a candidate. | ||
| if (top_level_casts.count(&expr)) { | ||
| col_to_expr.emplace(binding->column_index, &expr); | ||
| } | ||
| } else if (it->second == nullptr || | ||
| it->second->Cast<BoundCastExpression>().return_type != expr.return_type || | ||
| // TODO(myrrc) this line needs upstreaming | ||
| it->second->Cast<BoundCastExpression>().try_cast != expr.try_cast) { | ||
| // Different target type, or already a conflict. | ||
| it->second = nullptr; | ||
| } | ||
|
|
||
| return std::move(*ptr); | ||
| } | ||
|
|
||
| ExpressionPtr CastReplace::VisitReplace(BoundColumnRefExpression &expr, ExpressionPtr *ptr) { | ||
| const auto binding = Resolve(expr.binding, analyses, projections); | ||
| if (!binding) { | ||
| return std::move(*ptr); | ||
| } | ||
|
|
||
| const auto &[analysis, column_index, projection] = *binding; | ||
| if (CanPushdownColumn(analysis, column_index)) { | ||
| const idx_t storage_index = analysis.get.GetColumnIds()[column_index].GetPrimaryIndex(); | ||
| const LogicalType return_type = analysis.get.returned_types[storage_index]; | ||
| expr.return_type = return_type; | ||
| // LogicalProjection types are resolved by calling | ||
| // LogicalProjection::ResolveTypes, so we need to check whether types in | ||
| // projection have been resolved, and updated them only if needed. | ||
| if (projection != nullptr && !projection->types.empty()) { | ||
| projection->types[column_index] = return_type; | ||
| } | ||
| } | ||
|
|
||
| return std::move(*ptr); | ||
| } | ||
|
|
||
| ExpressionPtr CastReplace::VisitReplace(BoundCastExpression &expr, ExpressionPtr *ptr) { | ||
| if (expr.child->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) { | ||
| return nullptr; // Same as in ScalarFnCollect::VisitReplace | ||
| } | ||
| auto &bound_col_base = expr.child; | ||
| const auto &bound_col = bound_col_base->Cast<BoundColumnRefExpression>(); | ||
| const auto binding = Resolve(bound_col.binding, analyses, projections); | ||
| if (!binding) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| const auto &[analysis, column_index, projection] = *binding; | ||
| if (!CanPushdownColumn(analysis, column_index)) { | ||
| return std::move(*ptr); | ||
| } | ||
|
|
||
| const idx_t storage_index = analysis.get.GetColumnIds()[column_index].GetPrimaryIndex(); | ||
| const LogicalType return_type = analysis.get.returned_types[storage_index]; | ||
| bound_col_base->return_type = return_type; | ||
| // Same as in CastReplace::VisitReplace(BoundColumnRefExpression) | ||
| if (projection != nullptr && !projection->types.empty()) { | ||
| projection->types[column_index] = return_type; | ||
| } | ||
| return std::move(bound_col_base); | ||
| } | ||
|
|
||
| CastCollect::CastCollect(Analyses &analyses, const Projections &projections) | ||
| : analyses(analyses), projections(projections) { | ||
| } | ||
|
|
||
| CastReplace::CastReplace(Analyses &analyses, const Projections &projections) | ||
| : analyses(analyses), projections(projections) { | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
| #pragma once | ||
| #include "optimizer.hpp" | ||
|
|
||
| #include "duckdb/common/unordered_set.hpp" | ||
| #include "duckdb/main/client_context.hpp" | ||
| #include "duckdb/planner/expression.hpp" | ||
| #include "duckdb/planner/logical_operator.hpp" | ||
|
|
||
| using namespace duckdb; | ||
|
|
||
| /** | ||
| * Collect CAST(col) expressions. If "col" is used without CAST in "plan", | ||
| * record in "analyses.conflicts" | ||
| */ | ||
| struct CastCollect final : LogicalOperatorVisitor { | ||
| Analyses &analyses; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the ownership model here? Can we access a stale ref? Same for all other refs and pointers.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the analysis lives on the stack of pushdown function, and the visitor's lifetime is less than lifetime of the function. For pointers, we reference the query plan which will outlive the optimizer, and we either read (Collect) or modify (Replace) the pointers. Optimizers run sequentially so there's no risk the pointers will be invalidated.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we drop a doc line concisely what the ownership model is? |
||
| const Projections &projections; | ||
| // Casts that are direct outputs of a clean projection over a GET. Only these | ||
| // start a pushdown candidate; a nested cast may push down a different value. | ||
| // See test/sql/copy/csv/auto/test_large_integer_detection.test in duckdb | ||
| unordered_set<const Expression *> top_level_casts; | ||
|
|
||
| CastCollect(Analyses &analyses, const Projections &projections); | ||
| void VisitOperator(LogicalOperator &op) override; | ||
| ExpressionPtr VisitReplace(BoundColumnRefExpression &expr, ExpressionPtr *ptr) override; | ||
| ExpressionPtr VisitReplace(BoundCastExpression &expr, ExpressionPtr *ptr) override; | ||
| }; | ||
|
|
||
| /* | ||
| * For "col" in columns collected by CastCollect, replace CAST(col) to "col" | ||
| * if "col" doesn't have conflicting usage. Update return types for bound | ||
| * columns and logical projections referencing this column. | ||
| */ | ||
| struct CastReplace final : LogicalOperatorVisitor { | ||
| Analyses &analyses; | ||
|
0ax1 marked this conversation as resolved.
|
||
| const Projections &projections; | ||
|
|
||
| CastReplace(Analyses &analyses, const Projections &aliases); | ||
| ExpressionPtr VisitReplace(BoundColumnRefExpression &expr, ExpressionPtr *ptr) override; | ||
| ExpressionPtr VisitReplace(BoundCastExpression &expr, ExpressionPtr *ptr) override; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.