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
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ struct ArgMinMaxBase {
auto &context = input.GetClientContext();
auto &function = input.GetBoundFunction();
auto &arguments = input.GetArguments();
ExpressionBinder::PushCollation(context, arguments[1], arguments[1]->GetReturnType());
ExpressionBinder::PushCollation(context, arguments[1], arguments[1]->GetReturnType(), input.GetCollationType());
function.GetArguments()[0] = arguments[0]->GetReturnType();
function.GetArguments()[1] = arguments[1]->GetReturnType();
function.SetReturnType(arguments[0]->GetReturnType());
Expand Down Expand Up @@ -377,7 +377,7 @@ struct VectorArgMinMaxBase : ArgMinMaxBase<COMPARATOR> {
auto &context = input.GetClientContext();
auto &function = input.GetBoundFunction();
auto &arguments = input.GetArguments();
ExpressionBinder::PushCollation(context, arguments[1], arguments[1]->GetReturnType());
ExpressionBinder::PushCollation(context, arguments[1], arguments[1]->GetReturnType(), input.GetCollationType());
function.GetArguments()[0] = arguments[0]->GetReturnType();
function.GetArguments()[1] = arguments[1]->GetReturnType();
function.SetReturnType(arguments[0]->GetReturnType());
Expand Down Expand Up @@ -899,7 +899,7 @@ unique_ptr<FunctionData> ArgMinMaxNBind(BindAggregateFunctionInput &input) {
throw ParameterNotResolvedException();
}
}
ExpressionBinder::PushCollation(context, arguments[1], arguments[1]->GetReturnType());
ExpressionBinder::PushCollation(context, arguments[1], arguments[1]->GetReturnType(), input.GetCollationType());

const auto val_type = arguments[0]->GetReturnType().InternalType();
const auto arg_type = arguments[1]->GetReturnType().InternalType();
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/extension/json/json_functions/json_transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,8 @@ static bool JSONToAnyCast(Vector &source, Vector &result, idx_t count, CastParam
options.delay_error = true;

auto success = TransformFunctionInternal(source, count, result, alc, options);
// strings of the result point into the JSON allocator, which must outlive the cast's local state
JSONAllocator::AddBuffer(result, alc);
if (!success) {
HandleCastError::AssignError(options.error_message, parameters);
}
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.1.0-alpha57486";
descriptor->api_version = "v2.1.0-alpha57538";
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-alpha57486";
descriptor->api_version = "v2.1.0-alpha57538";
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-alpha57486";
descriptor->api_version = "v2.1.0-alpha57538";
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-alpha57486";
descriptor->api_version = "v2.1.0-alpha57538";
descriptor->entry_cpp = (void (*)(void))json_duckdb_cpp_init;
return 0;
}
Expand Down
16 changes: 10 additions & 6 deletions src/duckdb/src/function/aggregate/distributive/minmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,13 @@ shared_ptr<const AggregateFunction> GetCollatedMinMaxFunction(ClientContext &con
}

//! Whether min/max over the type are computed through arg_min/arg_max over the collated value
static bool MinMaxUsesCollation(ClientContext &context, const LogicalType &input_type) {
static bool MinMaxUsesCollation(ClientContext &context, const LogicalType &input_type, CollationType collation_type) {
// The generic non-VARCHAR collation path is not ready yet (see internal #8704). BIT, TIMETZ and VARIANT use
// explicit binary-comparable keys so min/max follows the same logical order as comparisons and ORDER BY.
const auto varchar_collation =
input_type.id() == LogicalTypeId::VARCHAR &&
(!StringType::GetCollation(input_type).empty() || !Settings::Get<DefaultCollationSetting>(context).empty());
(!StringType::GetCollation(input_type).empty() || (collation_type != CollationType::EXPLICIT_COLLATIONS &&
!Settings::Get<DefaultCollationSetting>(context).empty()));
const auto nested_collation = StructType::IsStruct(input_type) || input_type.id() == LogicalTypeId::LIST ||
input_type.id() == LogicalTypeId::ARRAY;
return input_type.id() == LogicalTypeId::BIT || input_type.id() == LogicalTypeId::TIME_TZ ||
Expand All @@ -370,9 +371,11 @@ unique_ptr<FunctionData> BindMinMax(BindAggregateFunctionInput &input) {
auto &arguments = input.GetArguments();
auto input_type = arguments[0]->GetReturnType();

const auto collation = MinMaxUsesCollation(context, input_type);
const auto collation_type = input.GetCollationType();
const auto collation = MinMaxUsesCollation(context, input_type, collation_type);
auto collated_arg = collation ? arguments[0]->Copy() : nullptr;
if (collation && ExpressionBinder::PushCollation(context, collated_arg, collated_arg->GetReturnType())) {
if (collation &&
ExpressionBinder::PushCollation(context, collated_arg, collated_arg->GetReturnType(), collation_type)) {
// If aggr function is min/max and uses collations, replace bound_function with arg_min/arg_max
// to make sure the result's correctness.
vector<LogicalType> types {arguments[0]->GetReturnType(), collated_arg->GetReturnType()};
Expand Down Expand Up @@ -572,9 +575,10 @@ unique_ptr<FunctionData> MinMaxNBind(BindAggregateFunctionInput &input) {
throw ParameterNotResolvedException();
}
}
if (MinMaxUsesCollation(context, arguments[0]->GetReturnType())) {
const auto collation_type = input.GetCollationType();
if (MinMaxUsesCollation(context, arguments[0]->GetReturnType(), collation_type)) {
auto collated_arg = arguments[0]->Copy();
if (ExpressionBinder::PushCollation(context, collated_arg, collated_arg->GetReturnType())) {
if (ExpressionBinder::PushCollation(context, collated_arg, collated_arg->GetReturnType(), collation_type)) {
vector<LogicalType> types {arguments[0]->GetReturnType(), collated_arg->GetReturnType(),
arguments[1]->GetReturnType()};
auto collated_function = GetCollatedMinMaxFunction(context, function.GetName(), types);
Expand Down
5 changes: 3 additions & 2 deletions src/duckdb/src/function/aggregate_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ BindAggregateFunctionInput::BindAggregateFunctionInput(ClientContext &context_p,
BoundAggregateFunction &bound_function_p,
vector<unique_ptr<Expression>> &arguments_p,
optional_ptr<Binder> binder_p,
optional_ptr<const DecimalWidthLimit> decimal_width_limit_p)
optional_ptr<const DecimalWidthLimit> decimal_width_limit_p,
bool arguments_collated_p)
: BindFunctionInput(context_p, bound_function_p, bound_function_p.GetDefinition()->GetSignature(), arguments_p,
binder_p, decimal_width_limit_p),
bound_function(bound_function_p) {
bound_function(bound_function_p), arguments_collated(arguments_collated_p) {
}

} // namespace duckdb
3 changes: 2 additions & 1 deletion src/duckdb/src/function/function_binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,8 @@ FunctionBinder::ResolveAggregateFunction(const FunctionBinding &binding, vector<
unique_ptr<FunctionData> bind_info;

if (bound_function.GetCallbacks().HasBindCallback()) {
BindAggregateFunctionInput input(context, bound_function, children, binder, decimal_width_limit);
BindAggregateFunctionInput input(context, bound_function, children, binder, decimal_width_limit,
arguments_collated);
bind_info = bound_function.GetCallbacks().GetBindCallback()(input);

// we may have lost some arguments in the bind
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/src/function/scalar/system/aggregate_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ unique_ptr<ExportAggregateBindData> BindExportedAggregate(FunctionBinder &functi
}
}

// The recorded signature already holds the collations the state was built with
function_binder.arguments_collated = true;
vector<pair<Identifier, unique_ptr<Expression>>> keyword_args;
auto [bound_aggr, bind_info] =
function_binder.ResolveAggregateFunction(best_function.GetBinding(), args, keyword_args);
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-alpha57486"
#define DUCKDB_PATCH_VERSION "0-alpha57538"
#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-alpha57486"
#define DUCKDB_VERSION "v2.1.0-alpha57538"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "f0b39a6d67"
#define DUCKDB_SOURCE_ID "5e78b7f833"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace duckdb {

enum class CollationType { ALL_COLLATIONS, COMBINABLE_COLLATIONS };
//! EXPLICIT_COLLATIONS applies the collations of the type, but not the default collation
enum class CollationType { ALL_COLLATIONS, COMBINABLE_COLLATIONS, EXPLICIT_COLLATIONS };

} // namespace duckdb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "duckdb/common/array.hpp"
#include "duckdb/common/enums/collation_type.hpp"
#include "duckdb/common/enums/optimizer_type.hpp"
#include "duckdb/common/vector_operations/aggregate_executor.hpp"
#include "duckdb/function/aggregate_state.hpp"
Expand Down Expand Up @@ -64,14 +65,20 @@ class BindAggregateFunctionInput : public BindFunctionInput {
// Defined out-of-line: converting to the BindFunctionInput base requires the complete BoundAggregateFunction.
BindAggregateFunctionInput(ClientContext &context_p, BoundAggregateFunction &bound_function_p,
vector<unique_ptr<Expression>> &arguments_p, optional_ptr<Binder> binder_p = nullptr,
optional_ptr<const DecimalWidthLimit> decimal_width_limit_p = nullptr);
optional_ptr<const DecimalWidthLimit> decimal_width_limit_p = nullptr,
bool arguments_collated_p = false);

BoundAggregateFunction &GetBoundFunction() const {
return bound_function;
}
//! The collations to push into the arguments: arguments bound before do not get the default collation again
CollationType GetCollationType() const {
return arguments_collated ? CollationType::EXPLICIT_COLLATIONS : CollationType::ALL_COLLATIONS;
}

private:
BoundAggregateFunction &bound_function;
bool arguments_collated;
};

class FunctionExpression;
Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/src/planner/collation_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static vector<CollationFunctionName> GetVarcharCollationFunctions(ClientContext
// replace default collation with system collation
auto str_collation = StringType::GetCollation(sql_type);
string collation;
if (str_collation.empty()) {
if (str_collation.empty() && type != CollationType::EXPLICIT_COLLATIONS) {
collation = Settings::Get<DefaultCollationSetting>(context);
} else {
collation = str_collation;
Expand Down
70 changes: 41 additions & 29 deletions src/duckdb/src/planner/table_filter_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "duckdb/storage/table/column_segment.hpp"
#include "duckdb/common/shared_ptr_ipp.hpp"
#include "duckdb/optimizer/expression_heuristics.hpp"
#include "duckdb/execution/adaptive_filter.hpp"

namespace duckdb {

Expand Down Expand Up @@ -264,10 +265,28 @@ bool ExpressionFilterState::TryFilterDictionary(SelectionVector &sel, Vector &ve
return true;
}

template <class CHILD, class FILTER>
static void FilterConjunction(AdaptiveFilter &adaptive_filter, vector<unique_ptr<CHILD>> &children, idx_t &count,
FILTER filter, bool adapt = true) {
if (count == 0) {
return;
}
auto state = adapt ? adaptive_filter.BeginFilter() : AdaptiveFilterState();
for (auto child_idx : adaptive_filter.GetPermutation()) {
filter(*children[child_idx]);
if (count == 0) {
break;
}
}
if (adapt) {
adaptive_filter.EndFilter(state);
}
}

class ConjunctionAndFilterExecutor final : public ExpressionFilterExecutor {
public:
explicit ConjunctionAndFilterExecutor(vector<unique_ptr<ExpressionFilterExecutor>> children_p)
: children(std::move(children_p)) {
ConjunctionAndFilterExecutor(const Expression &expression, vector<unique_ptr<ExpressionFilterExecutor>> children_p)
: children(std::move(children_p)), adaptive_filter(expression) {
}

bool CanCache() const override {
Expand All @@ -282,17 +301,15 @@ class ConjunctionAndFilterExecutor final : public ExpressionFilterExecutor {

idx_t FilterSelection(SelectionVector &sel, Vector &vector, idx_t scan_count,
idx_t &approved_tuple_count) override {
for (auto &child : children) {
child->FilterSelection(sel, vector, scan_count, approved_tuple_count);
if (approved_tuple_count == 0) {
break;
}
}
FilterConjunction(adaptive_filter, children, approved_tuple_count, [&](ExpressionFilterExecutor &child) {
child.FilterSelection(sel, vector, scan_count, approved_tuple_count);
});
return approved_tuple_count;
}

private:
vector<unique_ptr<ExpressionFilterExecutor>> children;
AdaptiveFilter adaptive_filter;
};

class OptionalFilterExecutor final : public ExpressionFilterExecutor {
Expand Down Expand Up @@ -599,7 +616,8 @@ class SelectivityOptionalFilterExecutor final : public ExpressionFilterExecutor

class ConjunctionFilterCache final : public TableFilterCache {
public:
explicit ConjunctionFilterCache(vector<unique_ptr<TableFilterCache>> children_p) : children(std::move(children_p)) {
ConjunctionFilterCache(vector<unique_ptr<TableFilterCache>> children_p, AdaptiveFilter &adaptive_filter_p)
: children(std::move(children_p)), adaptive_filter(adaptive_filter_p) {
}

protected:
Expand All @@ -613,35 +631,29 @@ class ConjunctionFilterCache final : public TableFilterCache {
}

void FilterConstantInternal(idx_t value_index, idx_t &count) override {
for (auto &child : children) {
if (count == 0) {
break;
}
child->FilterConstant(value_index, count);
}
// A cached constant only requires a mask lookup per child.
FilterConjunction(
adaptive_filter.get(), children, count,
[&](TableFilterCache &child) { child.FilterConstant(value_index, count); }, false);
}

void FilterInternal(const SelectionVector &values, SelectionVector &sel, idx_t &count) override {
for (auto &child : children) {
if (count == 0) {
break;
}
child->Filter(values, sel, count);
}
FilterConjunction(
adaptive_filter.get(), children, count, [&](TableFilterCache &child) { child.Filter(values, sel, count); },
!CanAcceptAllInternal());
}

void FilterRunsInternal(const SelectionVector &values, array_ptr<const idx_t> row_counts, SelectionVector &sel,
idx_t &count) override {
for (auto &child : children) {
if (count == 0) {
break;
}
child->FilterRuns(values, row_counts, sel, count);
}
FilterConjunction(
adaptive_filter.get(), children, count,
[&](TableFilterCache &child) { child.FilterRuns(values, row_counts, sel, count); },
!CanAcceptAllInternal());
}

private:
vector<unique_ptr<TableFilterCache>> children;
reference<AdaptiveFilter> adaptive_filter;
};

static idx_t CountRunRows(array_ptr<const idx_t> row_counts, const SelectionVector &sel, idx_t count) {
Expand Down Expand Up @@ -733,7 +745,7 @@ unique_ptr<TableFilterCache> ConjunctionAndFilterExecutor::CreateCache(Vector &v
for (auto &child : children) {
caches.push_back(child->CreateCache(values, count));
}
return make_uniq<ConjunctionFilterCache>(std::move(caches));
return make_uniq<ConjunctionFilterCache>(std::move(caches), adaptive_filter);
}

unique_ptr<TableFilterCache> SelectivityOptionalFilterExecutor::CreateCache(Vector &values, idx_t count) {
Expand Down Expand Up @@ -1185,7 +1197,7 @@ static unique_ptr<ExpressionFilterExecutor> TryCreateFastExecutor(const Expressi
children.push_back(std::move(filter.executor));
}
if (is_and) {
return make_uniq<ConjunctionAndFilterExecutor>(std::move(children));
return make_uniq<ConjunctionAndFilterExecutor>(expression, std::move(children));
}
return make_uniq<ConjunctionOrFilterExecutor>(std::move(children));
}
Expand Down
21 changes: 20 additions & 1 deletion src/duckdb/third_party/yyjson/include/yyjson_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include "yyjson.hpp"
#include "duckdb/common/optional.hpp"
#include "duckdb/common/vector/array_vector.hpp"
#include "duckdb/common/vector/list_vector.hpp"
#include "duckdb/common/vector/struct_vector.hpp"

using namespace duckdb_yyjson; // NOLINT

Expand Down Expand Up @@ -41,9 +44,25 @@ class JSONAllocator : public enable_shared_from_this<JSONAllocator> {
arena_allocator.Reset();
}

//! Retains the allocator in every string vector of a (possibly nested) vector
void AddBuffer(Vector &vector) {
if (vector.GetType().InternalType() == PhysicalType::VARCHAR) {
switch (vector.GetType().InternalType()) {
case PhysicalType::VARCHAR:
StringVector::AddAuxiliaryData(vector, make_uniq<JSONStringVectorBuffer>(shared_from_this()));
break;
case PhysicalType::STRUCT:
for (auto &child : StructVector::GetEntries(vector)) {
AddBuffer(child);
}
break;
case PhysicalType::LIST:
AddBuffer(ListVector::GetChildMutable(vector));
break;
case PhysicalType::ARRAY:
AddBuffer(ArrayVector::GetChildMutable(vector));
break;
default:
break;
}
}

Expand Down
Loading