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
13 changes: 11 additions & 2 deletions src/duckdb/extension/parquet/parquet_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,11 @@ bool ParquetReader::FiltersExcludeRowGroup(ClientContext &context, ParquetReader
prune_result == FilterPropagateResult::FILTER_FALSE_OR_NULL) {
return true;
}
if (prune_result == FilterPropagateResult::FILTER_ALWAYS_TRUE) {
scan_filter.filter_state.reset();
} else if (!scan_filter.filter_state) {
scan_filter.filter_state = TableFilterState::Initialize(context, scan_filter.filter);
}
}
return false;
}
Expand Down Expand Up @@ -2757,8 +2762,12 @@ idx_t ParquetReader::EvaluateFilters(ParquetReaderScanState &state, DataChunk &r

auto &result_vector = result.data[local_idx.GetIndex()];
ColumnReaderInput reader_input(scan_count, define_ptr, repeat_ptr);
child_reader.Filter(reader_input, result_vector, scan_filter.filter, *scan_filter.filter_state, state.sel,
filter_count, is_first_filter);
if (scan_filter.filter_state) {
child_reader.Filter(reader_input, result_vector, scan_filter.filter, *scan_filter.filter_state, state.sel,
filter_count, is_first_filter);
} else {
child_reader.Select(reader_input, result_vector, state.sel, filter_count);
}
if (log_prefetch) {
auto &filters_used = state.prefetch_metrics.logger.filters_used;
if (filters_used.size() != state.scan_filters.size()) {
Expand Down
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.0.2-alpha57018";
descriptor->api_version = "v2.0.2-alpha57292";
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.0.2-alpha57018";
descriptor->api_version = "v2.0.2-alpha57292";
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.0.2-alpha57018";
descriptor->api_version = "v2.0.2-alpha57292";
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.0.2-alpha57018";
descriptor->api_version = "v2.0.2-alpha57292";
descriptor->entry_cpp = (void (*)(void))json_duckdb_cpp_init;
return 0;
}
Expand Down
35 changes: 22 additions & 13 deletions src/duckdb/src/execution/operator/join/physical_hash_join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,21 +1718,24 @@ static void PublishDeferredRuntimeFilters(ClientContext &context, JoinHashTable
gstate.deferred_runtime_filters.clear();
}

enum class DynamicMinMaxFilterMode { PRUNING_ONLY, ROW_FILTERING, ADAPTIVE };

static void CreateDynamicMinMaxFilter(const PhysicalComparisonJoin &op, const JoinFilterPushdownFilter &info,
const ProjectionIndex &filter_col_idx, unique_ptr<Expression> filter_expr,
const LogicalType &column_type, bool selectivity_optional) {
const LogicalType &column_type, DynamicMinMaxFilterMode mode) {
if (!filter_expr) {
return;
}
if (selectivity_optional) {
if (mode == DynamicMinMaxFilterMode::ADAPTIVE) {
info.dynamic_filters->PushFilter(
op, filter_col_idx,
CreateSelectivityOptionalExpressionFilter(std::move(filter_expr), column_type,
SelectivityOptionalFilterType::MIN_MAX));
} else {
info.dynamic_filters->PushFilter(
op, filter_col_idx,
make_uniq<ExpressionFilter>(CreateOptionalFilterExpression(std::move(filter_expr), column_type)));
if (mode == DynamicMinMaxFilterMode::PRUNING_ONLY) {
filter_expr = CreateOptionalFilterExpression(std::move(filter_expr), column_type);
}
info.dynamic_filters->PushFilter(op, filter_col_idx, make_uniq<ExpressionFilter>(std::move(filter_expr)));
}
}

Expand Down Expand Up @@ -1834,7 +1837,7 @@ static void CreateDynamicMinMaxFilters(const PhysicalComparisonJoin &op, const J
ClientContext &context, const JoinFilterPushdownColumn &column,
ProjectionIndex filter_col_idx, ExpressionType cmp, const Value &min_val,
const Value &max_val, const LogicalType &condition_type,
bool reconstruct_expression, bool selectivity_optional) {
bool reconstruct_expression, DynamicMinMaxFilterMode mode) {
auto filter_column_type = reconstruct_expression ? column.storage_type : condition_type;
const auto retain_nulls = cmp == ExpressionType::COMPARE_NOT_DISTINCT_FROM;
switch (cmp) {
Expand All @@ -1846,7 +1849,7 @@ static void CreateDynamicMinMaxFilters(const PhysicalComparisonJoin &op, const J
op, info, filter_col_idx,
CreateJoinFilterComparisonExpression(context, column, ExpressionType::COMPARE_GREATERTHANOREQUALTO, min_val,
condition_type, reconstruct_expression, retain_nulls),
filter_column_type, selectivity_optional);
filter_column_type, mode);
break;
}
default:
Expand All @@ -1861,7 +1864,7 @@ static void CreateDynamicMinMaxFilters(const PhysicalComparisonJoin &op, const J
op, info, filter_col_idx,
CreateJoinFilterComparisonExpression(context, column, ExpressionType::COMPARE_LESSTHANOREQUALTO, max_val,
condition_type, reconstruct_expression, retain_nulls),
filter_column_type, selectivity_optional);
filter_column_type, mode);
break;
}
default:
Expand Down Expand Up @@ -1945,7 +1948,8 @@ unique_ptr<DataChunk> JoinFilterPushdownInfo::FinalizeFilters(ClientContext &con
// min != max - generate range filters for non-equality comparisons.
// For non-equalities, the range must be half-open.
CreateDynamicMinMaxFilters(op, info, context, pushdown_column, filter_col_idx, cmp, min_val,
max_val, condition_type, reconstruct_filter_expression, true);
max_val, condition_type, reconstruct_filter_expression,
DynamicMinMaxFilterMode::ADAPTIVE);
continue;
}

Expand Down Expand Up @@ -1983,12 +1987,17 @@ unique_ptr<DataChunk> JoinFilterPushdownInfo::FinalizeFilters(ClientContext &con
}
}

if (!pushed_in_filter) {
const bool emit_bloom_filter = allow_bloom_filters && can_emit_runtime_filters && ht && gstate &&
CanUseBloomFilter(info, context, op, cmp, ht) &&
(ht->HasPublishableBloomFilter() || allow_prefix_range_filters);
// An IN filter only prunes statistics; retain row filtering when there is no Bloom/PRF.
if (!pushed_in_filter || !emit_bloom_filter) {
const auto mode = emit_bloom_filter ? DynamicMinMaxFilterMode::PRUNING_ONLY
: DynamicMinMaxFilterMode::ROW_FILTERING;
CreateDynamicMinMaxFilters(op, info, context, pushdown_column, filter_col_idx, cmp, min_val,
max_val, condition_type, reconstruct_filter_expression, false);
max_val, condition_type, reconstruct_filter_expression, mode);
}
if (allow_bloom_filters && can_emit_runtime_filters && ht && gstate &&
CanUseBloomFilter(info, context, op, cmp, ht)) {
if (emit_bloom_filter) {
if (ht->HasPublishableBloomFilter()) {
// a complete filter was accumulated during Sink (external joins build it there)
DeferRuntimeFilter(DeferredRuntimeFilterType::BLOOM_FILTER, op, info, pushdown_column,
Expand Down
65 changes: 50 additions & 15 deletions src/duckdb/src/execution/radix_ht_adaptivity.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
#include "duckdb/execution/radix_ht_adaptivity.hpp"

#include "duckdb/common/exception.hpp"
#include "duckdb/common/helper.hpp"
#include "duckdb/common/limits.hpp"
#include "duckdb/execution/radix_ht_sink_state.hpp"

namespace duckdb {

bool RadixHTAdaptivity::HandleSinkBoundary(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate,
const bool memory_pressure) {
auto &ht = *lstate.ht;
if (!memory_pressure) {
if (TryGrow(gstate, lstate)) {
return true;
}
MaybeSkipLookups(gstate, ht);
}

if (gstate.number_of_threads > RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD || gstate.external) {
// Keep the materialized rows and clear only the local pointer table.
gstate.any_abandoned = true;
ht.Abandon();
MaybeResumeLookups(ht);
}
return false;
}

bool RadixHTAdaptivity::TryGrow(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate) {
if (gstate.number_of_threads <= RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD || gstate.external) {
return false;
Expand Down Expand Up @@ -77,31 +97,46 @@ bool RadixHTAdaptivity::TryGrow(RadixHTGlobalSinkState &gstate, RadixHTLocalSink
return true;
}

void RadixHTAdaptivity::MaybeSkipLookups(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate) {
auto &ht = *lstate.ht;
const auto &adaptivity = ht.GetAdaptivityState();
if (gstate.external || !adaptivity.HLLEnabled() || adaptivity.LookupsSkipped() ||
gstate.number_of_threads <= RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD ||
adaptivity.GetSinkCount() < LOOKUP_SAMPLE_SIZE || ht.Capacity() != gstate.config.sink_capacity) {
void RadixHTAdaptivity::MaybeSkipLookups(const RadixHTGlobalSinkState &gstate, GroupedAggregateHashTable &ht) {
const auto &state = ht.GetAdaptivityState();
if (state.LookupsSkipped() || gstate.number_of_threads <= RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD ||
ht.Capacity() != gstate.config.sink_capacity) {
return;
}

const auto cycle_input = adaptivity.GetCycleInputCount();
const auto cycle_input = state.GetCycleInputCount();
D_ASSERT(cycle_input >= ht.Count());
const auto reduction =
cycle_input ? static_cast<double>(cycle_input - ht.Count()) / static_cast<double>(cycle_input) : 0;
if (reduction < MINIMUM_SMALL_TABLE_REDUCTION) {
if (cycle_input == 0) {
// Failed growth already abandoned this cycle.
return;
}
if (recent_input_rows >= LOOKUP_HISTORY_DECAY_THRESHOLD_ROWS) {
recent_input_rows /= 2;
recent_reduced_rows /= 2;
}
recent_input_rows += cycle_input;
recent_reduced_rows += cycle_input - ht.Count();
if (state.GetSinkCount() < MIN_INPUT_ROWS_BEFORE_LOOKUP_SKIP) {
return;
}
const auto recent_reduction = static_cast<double>(recent_reduced_rows) / static_cast<double>(recent_input_rows);
const auto reduction = static_cast<double>(cycle_input - ht.Count()) / static_cast<double>(cycle_input);
if (reduction < MINIMUM_SMALL_TABLE_REDUCTION && recent_reduction < MINIMUM_SMALL_TABLE_REDUCTION) {
retry_interval_rows = ClampValue(ht.GetMaterializedCount() / LOOKUP_RETRY_MATERIALIZED_ROWS_DIVISOR,
MIN_LOOKUP_RETRY_ROWS, MAX_LOOKUP_RETRY_ROWS);
ht.SkipLookups();
}
}

void RadixHTAdaptivity::MaybeResumeLookups(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate) {
auto &ht = *lstate.ht;
const auto &adaptivity = ht.GetAdaptivityState();
if (!gstate.external && !lstate.spilling && adaptivity.HLLEnabled() && adaptivity.LookupsSkipped() &&
adaptivity.GetSkippedInputCount() >= LOOKUP_SAMPLE_SIZE) {
void RadixHTAdaptivity::MaybeResumeLookups(GroupedAggregateHashTable &ht) const {
const auto &state = ht.GetAdaptivityState();
if (state.LookupsSkipped() && state.GetSkippedInputCount() >= retry_interval_rows) {
ht.ResumeLookups();
}
}

void RadixHTAdaptivity::Reset() {
*this = RadixHTAdaptivity();
}

} // namespace duckdb
18 changes: 3 additions & 15 deletions src/duckdb/src/execution/radix_partitioned_hashtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ void RadixHTLocalSinkState::ResetForReuse(const RadixPartitionedHashTable &radix
}
registered = false;
spilling = false;
adaptivity.Reset();
abandoned_data.reset();
abandoned_exported_data.clear();
if (!ht) {
Expand Down Expand Up @@ -656,21 +657,8 @@ void RadixPartitionedHashTable::Sink(ExecutionContext &context, DataChunk &chunk
return;
}

if (!memory_pressure) {
if (RadixHTAdaptivity::TryGrow(gstate, lstate)) {
return;
}
RadixHTAdaptivity::MaybeSkipLookups(gstate, lstate);
}

if (gstate.number_of_threads > RadixHTConfig::GROW_STRATEGY_THREAD_THRESHOLD || gstate.external) {
// Keep the materialized rows and clear only the local pointer table
// We don't do this when running with 1 or 2 threads, it only makes sense when there's many threads
gstate.any_abandoned = true;
ht.Abandon();
if (!memory_pressure) {
RadixHTAdaptivity::MaybeResumeLookups(gstate, lstate);
}
if (lstate.adaptivity.HandleSinkBoundary(gstate, lstate, memory_pressure)) {
return;
}

// Check if we need to repartition
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 "2-alpha57018"
#define DUCKDB_PATCH_VERSION "2-alpha57292"
#endif
#ifndef DUCKDB_MINOR_VERSION
#define DUCKDB_MINOR_VERSION 0
Expand All @@ -8,10 +8,10 @@
#define DUCKDB_MAJOR_VERSION 2
#endif
#ifndef DUCKDB_VERSION
#define DUCKDB_VERSION "v2.0.2-alpha57018"
#define DUCKDB_VERSION "v2.0.2-alpha57292"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "b3a028e6c8"
#define DUCKDB_SOURCE_ID "5fd385deb0"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
31 changes: 25 additions & 6 deletions src/duckdb/src/include/duckdb/execution/radix_ht_adaptivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,44 @@

namespace duckdb {

class GroupedAggregateHashTable;
class RadixHTGlobalSinkState;
class RadixHTLocalSinkState;

//! Policy for the local sink HT. Called at fill-cycle boundaries without memory pressure.
//! Policy for growth, abandonment and lookups of one local sink HT.
//! Lookup decisions are independent of HLL and external aggregation.
class RadixHTAdaptivity {
public:
//! Handle a full local table or memory pressure, returning whether the table grew.
bool HandleSinkBoundary(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate, bool memory_pressure);
void Reset();

//! Minimum cumulative input before lookups may first be skipped
static constexpr idx_t MIN_INPUT_ROWS_BEFORE_LOOKUP_SKIP = 1024 * 1024;
//! Minimum input processed while skipping before retrying lookups
static constexpr idx_t MIN_LOOKUP_RETRY_ROWS = 128 * 1024;

private:
//! Grow a profitable table within the memory budget, resuming lookups on success.
static bool TryGrow(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate);
//! Consider skipping lookups using the completed cycle, before abandonment.
static void MaybeSkipLookups(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate);
void MaybeSkipLookups(const RadixHTGlobalSinkState &gstate, GroupedAggregateHashTable &ht);
//! Periodically retry lookups at the existing capacity, after abandonment.
static void MaybeResumeLookups(RadixHTGlobalSinkState &gstate, RadixHTLocalSinkState &lstate);

//! Minimum input before skipping lookups and between subsequent retries
static constexpr idx_t LOOKUP_SAMPLE_SIZE = 1048576;
void MaybeResumeLookups(GroupedAggregateHashTable &ht) const;

private:
static constexpr idx_t LOOKUP_HISTORY_DECAY_THRESHOLD_ROWS = 128 * 1024;
static constexpr idx_t MAX_LOOKUP_RETRY_ROWS = 1024 * 1024;
static constexpr idx_t LOOKUP_RETRY_MATERIALIZED_ROWS_DIVISOR = 8;
static constexpr double GROWTH_DUPLICATE_BYTES_FACTOR = 4.0;
static constexpr double MINIMUM_SMALL_TABLE_REDUCTION = 0.05;

private:
//! Decayed observations from completed lookup-enabled cycles
idx_t recent_input_rows = 0;
idx_t recent_reduced_rows = 0;
//! Input rows before retrying, chosen when skipping starts
idx_t retry_interval_rows = MIN_LOOKUP_RETRY_ROWS;
};

} // namespace duckdb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "duckdb/execution/radix_partitioned_hashtable.hpp"
#include "duckdb/execution/aggregate_hashtable.hpp"
#include "duckdb/execution/aggregate_state_spilling.hpp"
#include "duckdb/execution/radix_ht_adaptivity.hpp"
#include "duckdb/storage/temporary_memory_manager.hpp"

namespace duckdb {
Expand Down Expand Up @@ -174,6 +175,8 @@ class RadixHTLocalSinkState : public LocalSinkState {
bool spilling = false;
//! Sink capacity for this thread
idx_t local_sink_capacity;
//! Adaptivity policy for this local sink
RadixHTAdaptivity adaptivity;
//! Data that is abandoned ends up here (only if we're doing external aggregation)
unique_ptr<PartitionedTupleData> abandoned_data;
//! Exported abandoned aggregate states, aligned one-to-one with the partitions of abandoned_data
Expand Down
Loading