Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/duckdb/generated_extension_loader_package_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int32_t duckdb_extension_core_functions_describe(duckdb_extension_descriptor *de
descriptor->version = 1;
descriptor->name = "core_functions";
descriptor->extension_version = EXT_VERSION_CORE_FUNCTIONS;
descriptor->api_version = "v2.1.0-alpha57331";
descriptor->api_version = "v2.1.0-alpha57387";
descriptor->entry_cpp = (void (*)(void))core_functions_duckdb_cpp_init;
return 0;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ int32_t duckdb_extension_parquet_describe(duckdb_extension_descriptor *descripto
descriptor->version = 1;
descriptor->name = "parquet";
descriptor->extension_version = EXT_VERSION_PARQUET;
descriptor->api_version = "v2.1.0-alpha57331";
descriptor->api_version = "v2.1.0-alpha57387";
descriptor->entry_cpp = (void (*)(void))parquet_duckdb_cpp_init;
return 0;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ int32_t duckdb_extension_icu_describe(duckdb_extension_descriptor *descriptor) {
descriptor->version = 1;
descriptor->name = "icu";
descriptor->extension_version = EXT_VERSION_ICU;
descriptor->api_version = "v2.1.0-alpha57331";
descriptor->api_version = "v2.1.0-alpha57387";
descriptor->entry_cpp = (void (*)(void))icu_duckdb_cpp_init;
return 0;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ int32_t duckdb_extension_json_describe(duckdb_extension_descriptor *descriptor)
descriptor->version = 1;
descriptor->name = "json";
descriptor->extension_version = EXT_VERSION_JSON;
descriptor->api_version = "v2.1.0-alpha57331";
descriptor->api_version = "v2.1.0-alpha57387";
descriptor->entry_cpp = (void (*)(void))json_duckdb_cpp_init;
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/src/common/sort/partitioned_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ class PartitionedSortLocalSinkState : public LocalSinkState {
//===--------------------------------------------------------------------===//
PartitionedSort::PartitionedSort(ClientContext &client, const vector<BoundOrderByNode> &order_bys,
const Types &payload_types, const OperatorPartitionInfo &partition_info,
bool require_payload)
: SortStrategy(payload_types), partition_info(partition_info) {
bool require_payload, optional_idx partition_limit)
: SortStrategy(payload_types, partition_limit), partition_info(partition_info) {
// Pipeline does the partitioning for us, so leave them out
vector<unique_ptr<Expression>> unpartitioned;
vector<unique_ptr<BaseStatistics>> unpartitioned_stats;
OperatorPartitionInfo unpartitioned_info;
child_strategy = SortStrategy::Factory(client, unpartitioned, order_bys, payload_types, unpartitioned_stats,
unpartitioned_info, 0, require_payload);
unpartitioned_info, 0, require_payload, partition_limit);

this->payload_types = child_strategy->payload_types;
scan_ids = child_strategy->scan_ids;
Expand Down
3 changes: 2 additions & 1 deletion src/duckdb/src/common/sort/sort_strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ unique_ptr<SortStrategy> SortStrategy::Factory(ClientContext &client,
const OperatorPartitionInfo &partition_info, idx_t estimated_cardinality,
bool require_payload, optional_idx partition_limit) {
if (partition_info.RequiresPartitionColumns()) {
return make_uniq<PartitionedSort>(client, order_bys, payload_types, partition_info, require_payload);
return make_uniq<PartitionedSort>(client, order_bys, payload_types, partition_info, require_payload,
partition_limit);
} else if (!partition_bys.empty()) {
return make_uniq<HashedSort>(client, partition_bys, order_bys, payload_types, partitions_stats,
estimated_cardinality, require_payload);
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/src/function/table/version/pragma_version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DUCKDB_PATCH_VERSION
#define DUCKDB_PATCH_VERSION "0-alpha57331"
#define DUCKDB_PATCH_VERSION "0-alpha57387"
#endif
#ifndef DUCKDB_MINOR_VERSION
#define DUCKDB_MINOR_VERSION 1
Expand All @@ -8,10 +8,10 @@
#define DUCKDB_MAJOR_VERSION 2
#endif
#ifndef DUCKDB_VERSION
#define DUCKDB_VERSION "v2.1.0-alpha57331"
#define DUCKDB_VERSION "v2.1.0-alpha57387"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "94a1663a4d"
#define DUCKDB_SOURCE_ID "eb318174fa"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
31 changes: 14 additions & 17 deletions src/duckdb/src/function/window/window_segment_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class WindowSegmentTreePart {
using RightEntry = std::pair<idx_t, idx_t>;

enum FramePart : uint8_t { FULL = 0, LEFT = 1, RIGHT = 2 };
enum class LeafPart { LEFT, RIGHT };

WindowSegmentTreePart(ArenaAllocator &allocator, const AggregateObject &aggr, unique_ptr<WindowCursor> cursor,
const ValidityArray &filter_mask);
Expand Down Expand Up @@ -103,7 +104,7 @@ class WindowSegmentTreePart {
void EvaluateUpperLevels(const WindowSegmentTreeGlobalState &tree, const idx_t *begins, const idx_t *ends,
const idx_t *bounds, idx_t count, idx_t row_idx, FramePart frame_part);
void EvaluateLeaves(const WindowSegmentTreeGlobalState &tree, const idx_t *begins, const idx_t *ends,
const idx_t *bounds, idx_t count, idx_t row_idx, FramePart frame_part, FramePart leaf_part);
const idx_t *bounds, idx_t count, idx_t row_idx, FramePart frame_part, LeafPart leaf_part);

static inline const idx_t *FrameBegins(const idx_t *begins, const idx_t *ends, const idx_t *bounds,
FramePart frame_part) {
Expand Down Expand Up @@ -541,17 +542,15 @@ void WindowSegmentTreePart::Evaluate(const WindowSegmentTreeGlobalState &tree, c
Initialize(count);

if (order_insensitive) {
// First pass: aggregate the segment tree nodes with sharing
// Shared states must contain only tree nodes, before either set of leaves is added.
EvaluateUpperLevels(tree, begins, ends, bounds, count, row_idx, frame_part);

// Second pass: aggregate the ragged leaves
EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, FramePart::FULL);
} else {
// Evaluate leaves in order
EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, FramePart::LEFT);
}
// Process each side in a separate pass so the cursor does not seek between both frame ends for every row.
EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, LeafPart::LEFT);
if (!order_insensitive) {
EvaluateUpperLevels(tree, begins, ends, bounds, count, row_idx, frame_part);
EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, FramePart::RIGHT);
}
EvaluateLeaves(tree, begins, ends, bounds, count, row_idx, frame_part, LeafPart::RIGHT);
}

void WindowSegmentTreePart::Initialize(idx_t count) {
Expand Down Expand Up @@ -663,19 +662,17 @@ void WindowSegmentTreePart::EvaluateUpperLevels(const WindowSegmentTreeGlobalSta

void WindowSegmentTreePart::EvaluateLeaves(const WindowSegmentTreeGlobalState &tree, const idx_t *begins,
const idx_t *ends, const idx_t *bounds, idx_t count, idx_t row_idx,
FramePart frame_part, FramePart leaf_part) {
FramePart frame_part, LeafPart leaf_part) {
auto fdata = FlatVector::GetDataMutable<data_ptr_t>(statef);

// For order-sensitive aggregates, we have to process the ragged leaves in two pieces.
// The left side have to be added before the main tree followed by the ragged right sides.
// The current row is the leftmost value of the right hand side.
const bool compute_left = leaf_part != FramePart::RIGHT;
const bool compute_right = leaf_part != FramePart::LEFT;
// Process the ragged leaves one side at a time.
// Order-sensitive aggregates add left leaves before the tree and right leaves after it.
const bool compute_left = leaf_part == LeafPart::LEFT;
const bool compute_right = leaf_part == LeafPart::RIGHT;
const auto exclude_mode = tree.tree.exclude_mode;
const bool begin_on_curr_row = frame_part == FramePart::RIGHT && exclude_mode == WindowExcludeMode::CURRENT_ROW;
const bool end_on_curr_row = frame_part == FramePart::LEFT && exclude_mode == WindowExcludeMode::CURRENT_ROW;
// with EXCLUDE TIES, in addition to the frame part right of the peer group's end, we also need to consider the
// current row
// EXCLUDE TIES adds the current row before the right frame part's other leaves.
const bool add_curr_row = compute_left && frame_part == FramePart::RIGHT && exclude_mode == WindowExcludeMode::TIES;

// We need the full range of the frame to clamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace duckdb {
class PartitionedSort : public SortStrategy {
public:
PartitionedSort(ClientContext &client, const vector<BoundOrderByNode> &order_bys, const Types &payload_types,
const OperatorPartitionInfo &partition_info, bool require_payload = false);
const OperatorPartitionInfo &partition_info, bool require_payload = false,
optional_idx partition_limit = optional_idx());

public:
//===--------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions src/duckdb/src/include/duckdb/parser/parser_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct ParserOptions {
optional_ptr<const ExtensionCallbackManager> extensions;
AllowParserOverride parser_override_setting = AllowParserOverride::DEFAULT_OVERRIDE;
shared_ptr<CompiledGrammar> compiled_grammar;
//! Only check that the statements match the grammar: each yields a PassthroughStatement holding its text, and
//! nothing is transformed. For a client that sends the text on to an engine that knows the grammar's rules.
bool match_only = false;
//! The services the statement is parsed for (not set when parsing without a context)
optional_ptr<Context> context;
};
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/src/include/duckdb/storage/table/scan_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ class CollectionScanState {

//! Optional state for custom row group ordering
unique_ptr<RowGroupReorderer> reorderer;
//! Keeps the transaction-local row groups from being replaced while they are scanned
unique_ptr<StorageLockKey> local_row_groups_lock;

public:
void Initialize(const QueryContext &context_p, const vector<LogicalType> &types);
Expand Down
4 changes: 3 additions & 1 deletion src/duckdb/src/include/duckdb/transaction/local_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ class LocalTableStorage : public enable_shared_from_this<LocalTableStorage> {
OptimisticDataWriter optimistic_writer;

//! Whether or not the storage was dropped
bool is_dropped = false;
atomic<bool> is_dropped {false};

//! Serializes the appends and merges into the primary collection
StorageLock append_lock;
//! Shared by the users of the primary collection, exclusive while its row groups are replaced by a flush
StorageLock flush_lock;

public:
//! Initialize a scan; the context of the scanning query is used for pushed down casts and zonemap checks
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/src/main/capi/v2/capi_v2_func_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "duckdb/parallel/pipeline.hpp"
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/planner/expression/bound_columnref_expression.hpp"
#include "duckdb/planner/expression_iterator.hpp"
#include "duckdb/planner/filter/expression_filter.hpp"
#include "duckdb/planner/filter/table_filter_functions.hpp"
#include "duckdb/planner/operator/logical_get.hpp"
Expand Down
7 changes: 7 additions & 0 deletions src/duckdb/src/optimizer/apply_functional_dependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ void ApplyFunctionalDependencies::VisitWindowExpression(BoundWindowExpression &w
// so there is only one peer group, which is the same as having no ORDER BY clause.
auto &partition_bys = wexpr.Partitions();
auto &order_bys = wexpr.OrderByMutable();
// A RANGE frame with an offset is measured on the ORDER BY value, so it needs the key even then.
const auto range_offset = [](WindowBoundary boundary) {
return boundary == WindowBoundary::EXPR_PRECEDING_RANGE || boundary == WindowBoundary::EXPR_FOLLOWING_RANGE;
};
if (range_offset(wexpr.WindowStart()) || range_offset(wexpr.WindowEnd())) {
return;
}
if (!partition_bys.empty() && order_bys.size() == 1) {
vector<reference<Expression>> refs;
for (auto &arg : partition_bys) {
Expand Down
81 changes: 70 additions & 11 deletions src/duckdb/src/optimizer/window_fusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,74 @@
#include "duckdb/optimizer/topn_window_elimination.hpp"
#include "duckdb/main/client_context.hpp"
#include "duckdb/main/database.hpp"
#include "duckdb/planner/expression/bound_comparison_expression.hpp"
#include "duckdb/planner/expression/bound_columnref_expression.hpp"
#include "duckdb/planner/expression/bound_constant_expression.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/planner/expression/bound_reference_expression.hpp"
#include "duckdb/planner/expression/bound_window_expression.hpp"
#include "duckdb/planner/operator/logical_empty_result.hpp"
#include "duckdb/planner/operator/logical_filter.hpp"
#include "duckdb/planner/operator/logical_projection.hpp"
#include "duckdb/planner/operator/logical_window.hpp"
#include "duckdb/optimizer/remove_unused_columns.hpp"
#include "duckdb/execution/operator/aggregate/physical_streaming_window.hpp"
#include "duckdb/optimizer/apply_functional_dependencies.hpp"

namespace duckdb {

namespace {

using bindings_t = vector<ColumnBinding>;

ColumnBinding GetColumnBinding(const Expression &expr, const bindings_t &bindings) {
switch (expr.GetExpressionType()) {
case ExpressionType::BOUND_COLUMN_REF:
return expr.Cast<BoundColumnRefExpression>().Binding();
case ExpressionType::BOUND_REF:
return bindings.at(expr.Cast<BoundReferenceExpression>().Index());
default:
break;
}

// Return invalid binding, which will not be in the UCCs
return ColumnBinding();
}

bool CanRank(const BoundWindowExpression &wexpr, LogicalOperator &op) {
// Row Number is always rankable
switch (wexpr.GetExpressionType()) {
case ExpressionType::WINDOW_ROW_NUMBER:
return true;
case ExpressionType::WINDOW_RANK:
case ExpressionType::WINDOW_RANK_DENSE:
break;
default:
return false;
}

// Natural orders can always rank
auto &order_bys = wexpr.OrderBy();
if (order_bys.empty()) {
return true;
}

// For the other rankers to apply, we must have a UCC in play
const auto uccs = ApplyFunctionalDependencies::GetUniqueColumnCombinations(op);
const auto bindings = op.GetColumnBindings();
for (const auto &ucc : uccs) {
auto fds = ucc;
for (const auto &order : order_bys) {
fds.erase(GetColumnBinding(*order.expression, bindings));
}
if (fds.empty()) {
return true;
}
}

return false;
}

}; // namespace

WindowFusion::WindowFusion(ClientContext &client, optional_ptr<column_binding_map_t<unique_ptr<BaseStatistics>>> stats)
: client(client), stats(stats) {
D_ASSERT(stats);
Expand All @@ -31,17 +88,23 @@ optional_idx WindowFusion::CanOptimize(LogicalOperator &op) {
}

const auto comparison = filter.expressions[0]->GetExpressionType();
idx_t left_idx = 0;
idx_t right_idx = 1;
switch (comparison) {
case ExpressionType::COMPARE_LESSTHANOREQUALTO:
case ExpressionType::COMPARE_LESSTHAN:
case ExpressionType::COMPARE_EQUAL:
break;
case ExpressionType::COMPARE_BETWEEN:
right_idx = 2;
break;
default:
return cant_optimize;
}

auto &filter_comparison = filter.expressions[0]->Cast<BoundFunctionExpression>();
auto &right = BoundComparisonExpression::Right(filter_comparison);
auto &right = *filter_comparison.GetChildren()[right_idx];

if (right.GetExpressionType() != ExpressionType::VALUE_CONSTANT) {
return cant_optimize;
}
Expand All @@ -57,6 +120,7 @@ optional_idx WindowFusion::CanOptimize(LogicalOperator &op) {
optional_idx can_optimize(0);
switch (comparison) {
case ExpressionType::COMPARE_LESSTHANOREQUALTO:
case ExpressionType::COMPARE_BETWEEN:
if (bigint_value < 1) {
break;
}
Expand All @@ -82,7 +146,7 @@ optional_idx WindowFusion::CanOptimize(LogicalOperator &op) {
return cant_optimize;
}

auto &left = BoundComparisonExpression::LeftMutable(filter_comparison);
auto &left = filter_comparison.GetChildrenMutable()[left_idx];
if (left->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
return cant_optimize;
}
Expand Down Expand Up @@ -121,9 +185,10 @@ optional_idx WindowFusion::CanOptimize(LogicalOperator &op) {
return cant_optimize;
}
auto &window_expr = window.expressions[0]->Cast<BoundWindowExpression>();
if (window_expr.GetExpressionType() != ExpressionType::WINDOW_ROW_NUMBER) {
if (!CanRank(window_expr, *window.children[0])) {
return cant_optimize;
}

if (window.expressions.size() != 1) {
for (idx_t i = 1; i < window.expressions.size(); ++i) {
if (!window.expressions[i]->Equals(window_expr)) {
Expand Down Expand Up @@ -166,12 +231,6 @@ unique_ptr<LogicalOperator> WindowFusion::OptimizeInternal(unique_ptr<LogicalOpe
}
D_ASSERT(child.get().type == LogicalOperatorType::LOGICAL_WINDOW);
auto &window = child.get().Cast<LogicalWindow>();
auto &wexpr = window.expressions[0]->Cast<BoundWindowExpression>();

// Restrict to implemented cases
if (!wexpr.Partitions().empty()) {
return op;
}

// Add the limit to the child Window
window.partition_limit = partition_limit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "duckdb/parser/peg/matcher.hpp"
#include "duckdb/common/to_string.hpp"
#include "duckdb/parser/sql_statement.hpp"
#include "duckdb/parser/statement/passthrough_statement.hpp"
#include "duckdb/parser/token_iterator.hpp"
#include "duckdb/parser/tableref/showref.hpp"
#include "duckdb/common/enums/date_part_specifier.hpp"
Expand Down Expand Up @@ -102,6 +103,14 @@ unique_ptr<SQLStatement> PEGTransformerFactory::TransformTopLevelStatement(Token
}
}

if (options.match_only) {
auto start = stmt_opt.GetResult().GetOffset().GetIndex();
idx_t end_index = terminator_offset.IsValid() ? terminator_offset.GetIndex() : token_iterator.EndOffset();
auto statement = make_uniq<PassthroughStatement>();
statement->stmt_location = QueryLocation(start, end_index - start);
return std::move(statement);
}

ArenaAllocator transformer_allocator(allocator, PARSER_ARENA_INITIAL_CAPACITY);
PEGTransformer transformer(transformer_allocator, token_iterator, options, grammar);

Expand Down
4 changes: 4 additions & 0 deletions src/duckdb/src/planner/binder/statement/bind_create_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ unique_ptr<BoundConstraint> Binder::BindConstraint(const Constraint &constraint,

void Binder::BindGeneratedColumns(BoundCreateTableInfo &info) {
auto &base = info.base->Cast<CreateTableInfo>();
if (base.columns.LogicalColumnCount() == base.columns.PhysicalColumnCount()) {
// no generated columns
return;
}

vector<Identifier> names;
vector<LogicalType> types;
Expand Down
Loading
Loading