Skip to content
Closed
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ set(DUCKDB_SRC_FILES
src/duckdb/src/common/vector_operations/null_operations.cpp
src/duckdb/src/common/vector_operations/numeric_inplace_operators.cpp
src/duckdb/src/common/vector_operations/scalar_executor.cpp
src/duckdb/src/common/vector_operations/string_comparison.cpp
src/duckdb/src/common/vector_operations/vector_cast.cpp
src/duckdb/src/common/vector_operations/vector_copy.cpp
src/duckdb/src/common/vector_operations/vector_hash.cpp
src/duckdb/src/common/vector_operations/vector_storage.cpp
src/duckdb/src/execution/operator/csv_scanner/encode/csv_encoder.cpp
src/duckdb/src/execution/operator/filter/physical_filter.cpp
src/duckdb/src/function/aggregate/sorted_aggregate_function.cpp
src/duckdb/src/function/cast/union/from_struct.cpp
src/duckdb/src/function/scalar/date/strftime.cpp
Expand Down Expand Up @@ -420,6 +420,7 @@ set(DUCKDB_SRC_FILES
src/duckdb/ub_src_execution_operator_csv_scanner_state_machine.cpp
src/duckdb/ub_src_execution_operator_csv_scanner_table_function.cpp
src/duckdb/ub_src_execution_operator_csv_scanner_util.cpp
src/duckdb/ub_src_execution_operator_filter.cpp
src/duckdb/ub_src_execution_operator_helper.cpp
src/duckdb/ub_src_execution_operator_join.cpp
src/duckdb/ub_src_execution_operator_order.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ static long double BignumSumToLongDouble(const BignumIntermediate &value) {
magnitude = (magnitude << 8) | uhugeint_t(value.GetAbsoluteByte(NumericCast<int64_t>(i)));
}
if (magnitude <= uhugeint_t(NumericLimits<hugeint_t>::Maximum())) {
auto sum = hugeint_t(magnitude.upper, magnitude.lower);
auto sum = hugeint_t(static_cast<int64_t>(magnitude.upper), magnitude.lower);
return Hugeint::Cast<long double>(value.is_negative ? -sum : sum);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ AggregateFunction GetGenericArgMinMaxFunction(const ArgMinMaxNullHandling null_h
function.SetStatisticsCallback(AggregateFunction::PropagateInputValueStats);
function.GetProperties().SetProperty(BoundAggregateExpression::NON_NULL_SELECTOR_DOMAIN_PROPERTY,
Value::BOOLEAN(true));
function.GetProperties().SetProperty(BoundAggregateExpression::ARG_STORED_AS_SORT_KEY_PROPERTY,
Value::BOOLEAN(true));
return function;
}

Expand All @@ -430,6 +432,8 @@ AggregateFunction GetVectorArgMinMaxFunctionInternal(const LogicalType &by_type,
function.SetStatisticsCallback(AggregateFunction::PropagateInputValueStats);
function.GetProperties().SetProperty(BoundAggregateExpression::NON_NULL_SELECTOR_DOMAIN_PROPERTY,
Value::BOOLEAN(true));
function.GetProperties().SetProperty(BoundAggregateExpression::ARG_STORED_AS_SORT_KEY_PROPERTY,
Value::BOOLEAN(true));
return function;
#else
auto function = GetGenericArgMinMaxFunction<OP>(null_handling);
Expand Down Expand Up @@ -754,11 +758,18 @@ void ArgMinMaxNUpdate(Vector inputs[], AggregateInputData &aggr_input, idx_t inp
//------------------------------------------------------------------------------
// Bind
//------------------------------------------------------------------------------
template <class VAL_TYPE>
void SetArgStorageProperty(BoundAggregateFunction &function) {
function.GetProperties().SetProperty(BoundAggregateExpression::ARG_STORED_AS_SORT_KEY_PROPERTY,
Value::BOOLEAN(std::is_same<VAL_TYPE, MinMaxFallbackValue>::value));
}

template <class VAL_TYPE, class ARG_TYPE, class COMPARATOR>
void SpecializeArgMinMaxNFunction(BoundAggregateFunction &function) {
using STATE = ArgMinMaxNState<VAL_TYPE, ARG_TYPE, COMPARATOR>;
using OP = MinMaxNOperation;

SetArgStorageProperty<VAL_TYPE>(function);
function.SetStateSizeCallback(AggregateFunction::StateSize<STATE>);
function.SetStateInitCallback(AggregateFunction::StateInitialize<STATE, OP, AggregateDestructorType::LEGACY>);
function.SetStateCombineCallback(AggregateFunction::StateCombine<STATE, OP>);
Expand Down Expand Up @@ -825,6 +836,7 @@ void SpecializeArgMinMaxNullNFunction(BoundAggregateFunction &function) {
using STATE = ArgMinMaxNState<VAL_TYPE, ARG_TYPE, COMPARATOR>;
using OP = MinMaxNOperation;

SetArgStorageProperty<VAL_TYPE>(function);
function.SetStateSizeCallback(AggregateFunction::StateSize<STATE>);
function.SetStateInitCallback(AggregateFunction::StateInitialize<STATE, OP, AggregateDestructorType::LEGACY>);
function.SetStateCombineCallback(AggregateFunction::StateCombine<STATE, OP>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "duckdb/common/algorithm.hpp"
#include "duckdb/common/arena_containers/arena_vector.hpp"
#include "duckdb/common/smaller_binary.hpp"
#include "duckdb/common/types/sql_value_map.hpp"
#include "duckdb/common/types/vector.hpp"
#include "duckdb/common/vector/flat_vector.hpp"
#include "duckdb/common/vector/list_vector.hpp"
Expand Down Expand Up @@ -54,7 +55,7 @@ struct HistogramBinState {
new_boundaries.push_back(OP::template ExtractValue<T>(bin_child_data, bin_list.offset + i, aggr_input));
}
// sort the bin boundaries
std::sort(new_boundaries.begin(), new_boundaries.end());
std::sort(new_boundaries.begin(), new_boundaries.end(), SQLValueLessThan<T>());
// ensure there are no duplicate bin boundaries
for (idx_t i = 1; i < new_boundaries.size(); i++) {
if (Equals::Operation(new_boundaries[i - 1], new_boundaries[i])) {
Expand Down Expand Up @@ -92,7 +93,11 @@ struct HistogramBinFunction {
target.counts = &counts;
} else {
// both source and target have bin boundaries
if (*target.bin_boundaries != *source.bin_boundaries) {
auto &target_bins = *target.bin_boundaries;
auto &source_bins = *source.bin_boundaries;
if (target_bins.size() != source_bins.size() ||
!std::equal(target_bins.begin(), target_bins.end(), source_bins.begin(),
SQLValueEquality<typename STATE::TYPE>())) {
throw NotImplementedException(
"Histogram - cannot combine histograms with different bin boundaries. "
"Bin boundaries must be the same for all histograms within the same group");
Expand All @@ -113,7 +118,7 @@ struct HistogramRange {

template <class T>
static idx_t GetBin(T value, const unsafe_arena_vector<T> &bin_boundaries) {
auto entry = std::lower_bound(bin_boundaries.begin(), bin_boundaries.end(), value);
auto entry = std::lower_bound(bin_boundaries.begin(), bin_boundaries.end(), value, SQLValueLessThan<T>());
return UnsafeNumericCast<idx_t>(entry - bin_boundaries.begin());
}
};
Expand All @@ -123,8 +128,8 @@ struct HistogramExact {

template <class T>
static idx_t GetBin(T value, const unsafe_arena_vector<T> &bin_boundaries) {
auto entry = std::lower_bound(bin_boundaries.begin(), bin_boundaries.end(), value);
if (entry == bin_boundaries.end() || !(*entry == value)) {
auto entry = std::lower_bound(bin_boundaries.begin(), bin_boundaries.end(), value, SQLValueLessThan<T>());
if (entry == bin_boundaries.end() || !SQLValueEquality<T>()(*entry, value)) {
// entry not found - return last bucket
return bin_boundaries.size();
}
Expand Down Expand Up @@ -344,6 +349,9 @@ AggregateFunction GetHistogramBinFunction(const LogicalType &type) {
AggregateFunction::StateCombine<STATE_TYPE, HistogramBinFunction>,
HistogramBinFinalizeFunction<OP, T>, nullptr, nullptr, nullptr);
function.GetSignature().AddParameter("arg", type).AddParameter("bins", LogicalType::LIST(type));
// a NULL bin list or bin entry throws in the update, different bin boundaries of one group in the combine, and a
// bin that is the key of the "other values" bucket while that bucket has entries in the finalize
function.SetFallible();
return function;
}

Expand Down
9 changes: 9 additions & 0 deletions src/duckdb/extension/core_functions/scalar/blob/encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "duckdb/function/encoding_function.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/exception/conversion_exception.hpp"
#include "duckdb/storage/statistics/base_statistics.hpp"

namespace duckdb {

Expand All @@ -14,6 +15,13 @@ void EncodeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
result.Reinterpret(args.data[0]);
}

unique_ptr<BaseStatistics> EncodeStats(ClientContext &context, FunctionStatisticsInput &input) {
// the bytes are unchanged, so the input stats carry over to the blob
auto result = BaseStatistics::CreateEmpty(LogicalType::BLOB);
result.Merge(input.child_stats[0]);
return result.ToUnique();
}

enum class DecodeErrorBehavior : uint8_t {
STRICT = 1, // raise an error
REPLACE = 2, // replace invalid characters with '?'
Expand Down Expand Up @@ -118,6 +126,7 @@ ScalarFunctionSet EncodeFun::GetFunctions() {

ScalarFunction unary_function({}, LogicalType::BLOB, EncodeFunction);
unary_function.GetSignature().AddParameter("string", LogicalType::VARCHAR);
unary_function.SetStatisticsCallback(EncodeStats);

// the encoding is bound to the function of the encoding in system.encodings
ScalarFunction binary_function({}, LogicalType::BLOB, nullptr);
Expand Down
31 changes: 25 additions & 6 deletions src/duckdb/extension/core_functions/scalar/list/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "core_functions/scalar/list_functions.hpp"
#include "duckdb/common/types/data_chunk.hpp"
#include "duckdb/common/types/vector.hpp"
#include "duckdb/execution/expression_executor_state.hpp"
#include "duckdb/main/context.hpp"

namespace duckdb {

Expand All @@ -18,7 +20,8 @@ struct NumericRangeInfo {
return 1;
}

static uint64_t ListLength(int64_t start_value, int64_t end_value, int64_t increment_value, bool inclusive_bound) {
static uint64_t ListLength(Context &, int64_t start_value, int64_t end_value, int64_t increment_value,
bool inclusive_bound) {
if (increment_value == 0) {
return 0;
}
Expand Down Expand Up @@ -58,8 +61,8 @@ struct TimestampRangeInfo {
static interval_t DefaultIncrement() {
throw InternalException("Default increment not implemented for timestamp range");
}
static uint64_t ListLength(timestamp_t start_value, timestamp_t end_value, interval_t increment_value,
bool inclusive_bound) {
static uint64_t ListLength(Context &context, timestamp_t start_value, timestamp_t end_value,
interval_t increment_value, bool inclusive_bound) {
bool is_positive = increment_value.months > 0 || increment_value.days > 0 || increment_value.micros > 0;
bool is_negative = increment_value.months < 0 || increment_value.days < 0 || increment_value.micros < 0;
if (!is_negative && !is_positive) {
Expand Down Expand Up @@ -90,6 +93,10 @@ struct TimestampRangeInfo {
if (total_values > NumericLimits<uint32_t>::Maximum()) {
throw InvalidInputException("Lists larger than 2^32 elements are not supported");
}
// counting the elements can take billions of steps
if (total_values % STANDARD_VECTOR_SIZE == 0) {
context.FastInterruptCheck();
}
}
} else {
// positive interval, start_value is going up
Expand All @@ -99,6 +106,10 @@ struct TimestampRangeInfo {
if (total_values > NumericLimits<uint32_t>::Maximum()) {
throw InvalidInputException("Lists larger than 2^32 elements are not supported");
}
// counting the elements can take billions of steps
if (total_values % STANDARD_VECTOR_SIZE == 0) {
context.FastInterruptCheck();
}
}
}
return total_values;
Expand Down Expand Up @@ -175,12 +186,12 @@ class RangeInfoStruct {
increment_value = ListIncrementValue(row_idx);
}

uint64_t ListLength(idx_t row_idx) {
uint64_t ListLength(Context &context, idx_t row_idx) {
typename OP::TYPE start_value;
typename OP::TYPE end_value;
typename OP::INCREMENT_TYPE increment_value;
GetListValues(row_idx, start_value, end_value, increment_value);
return OP::ListLength(start_value, end_value, increment_value, INCLUSIVE_BOUND);
return OP::ListLength(context, start_value, end_value, increment_value, INCLUSIVE_BOUND);
}

private:
Expand All @@ -192,17 +203,19 @@ template <class OP, bool INCLUSIVE_BOUND>
void ListRangeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
D_ASSERT(result.GetType().id() == LogicalTypeId::LIST);

auto &context = state.GetContext();
RangeInfoStruct<OP, INCLUSIVE_BOUND> info(args);
auto args_size = args.size();
auto list_writer = FlatVector::Writer<VectorListType<typename OP::TYPE>>(result, args_size);
idx_t written_since_check = 0;
for (idx_t i = 0; i < args_size; i++) {
if (!info.RowIsValid(i)) {
list_writer.WriteNull();
continue;
}
typename OP::TYPE start_value = info.StartListValue(i);
typename OP::INCREMENT_TYPE increment = info.ListIncrementValue(i);
const auto length = info.ListLength(i);
const auto length = info.ListLength(context, i);

typename OP::TYPE range_value = start_value;
bool seen_value = false;
Expand All @@ -213,6 +226,12 @@ void ListRangeFunction(DataChunk &args, ExpressionState &state, Vector &result)
child_writer.WriteValue(range_value);
seen_value = true;
}
// writing long lists can take billions of steps
written_since_check += length;
if (written_since_check >= STANDARD_VECTOR_SIZE) {
context.FastInterruptCheck();
written_since_check = 0;
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/duckdb/extension/core_functions/scalar/math/numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ ScalarFunctionSet CeilFun::GetFunctions() {
ceil_function.SetResolveTypesCallback(resolve_func);
ceil.AddFunction(NameXArgument(std::move(ceil_function), type));
}
ceil.SetUnaryArgProperties(ArgProperties().NonDecreasing());
ceil.SetUnaryArgProperties(ArgProperties().NonDecreasing().RoundsToInteger(IntegerRounding::CEIL));
return ceil;
}

Expand Down Expand Up @@ -670,7 +670,7 @@ ScalarFunctionSet FloorFun::GetFunctions() {
floor_function.SetResolveTypesCallback(resolve_func);
floor.AddFunction(NameXArgument(std::move(floor_function), type));
}
floor.SetUnaryArgProperties(ArgProperties().NonDecreasing());
floor.SetUnaryArgProperties(ArgProperties().NonDecreasing().RoundsToInteger(IntegerRounding::FLOOR));
return floor;
}

Expand Down Expand Up @@ -961,12 +961,14 @@ ScalarFunctionSet TruncFun::GetFunctions() {
}
ScalarFunction trunc_function({}, type, trunc_func);
trunc_function.SetResolveTypesCallback(resolve_func);
trunc_function.SetUnaryArgProperties(
ArgProperties().NonDecreasing().RoundsToInteger(IntegerRounding::TRUNCATE));
trunc.AddFunction(NameXArgument(std::move(trunc_function), type));
ScalarFunction trunc_prec_function({}, type, trunc_prec_func, bind_prec_func);
trunc_prec_function.SetResolveTypesCallback(resolve_prec_func);
trunc_prec_function.SetUnaryArgProperties(ArgProperties().NonDecreasing());
trunc.AddFunction(NameXPrecisionArguments(std::move(trunc_prec_function), type));
}
trunc.SetUnaryArgProperties(ArgProperties().NonDecreasing());
return trunc;
}

Expand Down
4 changes: 2 additions & 2 deletions src/duckdb/extension/icu/collation/collation_collator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ Collator Collator::FromLanguageTag(const string &tag) {
// the "u" extension holds attributes, then keywords: a two character key followed by the subtags of its value
idx_t pos = locale_end;
while (pos < subtags.size()) {
auto singleton = subtags[pos++];
const auto &singleton = subtags[pos++];
if (singleton == "x") {
// the rest of the tag is for private use
break;
Expand All @@ -931,7 +931,7 @@ Collator Collator::FromLanguageTag(const string &tag) {
// when a key is repeated, its first value counts
vector<string> keys;
while (pos < end) {
auto key = subtags[pos++];
const auto &key = subtags[pos++];
string value;
while (pos < end && subtags[pos].size() != 2) {
value += (value.empty() ? "" : "-") + subtags[pos++];
Expand Down
Loading
Loading