Skip to content

Commit 1aa9faa

Browse files
duckdblabs-botgithub-actions[bot]
authored andcommitted
Update vendored DuckDB sources to e144689926
1 parent 97076bf commit 1aa9faa

File tree

164 files changed

+23777
-19995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+23777
-19995
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/duckdb/extension/core_functions/scalar/map/map_extract.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static void MapExtractValueFunc(DataChunk &args, ExpressionState &state, Vector
5757

5858
// Collect the matching positions
5959
Vector pos_vec(LogicalType::INTEGER, count);
60-
ListSearchOp<true>(map_vec, key_vec, arg_vec, pos_vec, args.size());
60+
ListSearchOp<int32_t>(map_vec, key_vec, arg_vec, pos_vec, args.size());
6161

6262
UnifiedVectorFormat pos_format;
6363
UnifiedVectorFormat lst_format;
@@ -68,7 +68,6 @@ static void MapExtractValueFunc(DataChunk &args, ExpressionState &state, Vector
6868
const auto pos_data = UnifiedVectorFormat::GetData<int32_t>(pos_format);
6969
const auto inc_list_data = ListVector::GetData(map_vec);
7070

71-
auto &result_validity = FlatVector::Validity(result);
7271
for (idx_t row_idx = 0; row_idx < count; row_idx++) {
7372
auto lst_idx = lst_format.sel->get_index(row_idx);
7473
if (!lst_format.validity.RowIsValid(lst_idx)) {
@@ -79,7 +78,7 @@ static void MapExtractValueFunc(DataChunk &args, ExpressionState &state, Vector
7978
const auto pos_idx = pos_format.sel->get_index(row_idx);
8079
if (!pos_format.validity.RowIsValid(pos_idx)) {
8180
// We didnt find the key in the map, so return NULL
82-
result_validity.SetInvalid(row_idx);
81+
FlatVector::SetNull(result, row_idx, true);
8382
continue;
8483
}
8584

@@ -118,7 +117,7 @@ static void MapExtractListFunc(DataChunk &args, ExpressionState &state, Vector &
118117

119118
// Collect the matching positions
120119
Vector pos_vec(LogicalType::INTEGER, count);
121-
ListSearchOp<true>(map_vec, key_vec, arg_vec, pos_vec, args.size());
120+
ListSearchOp<int32_t>(map_vec, key_vec, arg_vec, pos_vec, args.size());
122121

123122
UnifiedVectorFormat val_format;
124123
UnifiedVectorFormat pos_format;

src/duckdb/extension/core_functions/scalar/math/numeric.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "duckdb/common/operator/decimal_cast_operators.hpp"
12
#include "duckdb/common/vector_operations/vector_operations.hpp"
23
#include "duckdb/common/algorithm.hpp"
34
#include "duckdb/common/likely.hpp"
@@ -566,6 +567,34 @@ struct RoundDecimalOperator {
566567
}
567568
};
568569

570+
struct RoundIntegerOperator {
571+
template <class TA, class TB, class TR>
572+
static inline TR Operation(TA input, TB precision) {
573+
if (precision < 0) {
574+
// Do all the arithmetic at higher precision
575+
using POWERS_OF_TEN_CLASS = typename DecimalCastTraits<TA>::POWERS_OF_TEN_CLASS;
576+
if (precision <= -POWERS_OF_TEN_CLASS::CACHED_POWERS_OF_TEN) {
577+
return 0;
578+
}
579+
const auto power_of_ten = POWERS_OF_TEN_CLASS::POWERS_OF_TEN[-precision];
580+
auto addition = power_of_ten / 2;
581+
if (input < 0) {
582+
addition = -addition;
583+
}
584+
addition += input;
585+
addition /= power_of_ten;
586+
if (addition) {
587+
return UnsafeNumericCast<TR>(addition * power_of_ten);
588+
} else {
589+
return 0;
590+
}
591+
} else {
592+
// Rounding integers to higher precision is a NOP
593+
return input;
594+
}
595+
}
596+
};
597+
569598
struct RoundPrecisionFunctionData : public FunctionData {
570599
explicit RoundPrecisionFunctionData(int32_t target_scale) : target_scale(target_scale) {
571600
}
@@ -698,10 +727,6 @@ ScalarFunctionSet RoundFun::GetFunctions() {
698727
scalar_function_t round_func = nullptr;
699728
bind_scalar_function_t bind_func = nullptr;
700729
bind_scalar_function_t bind_prec_func = nullptr;
701-
if (type.IsIntegral()) {
702-
// no round for integral numbers
703-
continue;
704-
}
705730
switch (type.id()) {
706731
case LogicalTypeId::FLOAT:
707732
round_func = ScalarFunction::UnaryFunction<float, float, RoundOperator>;
@@ -715,7 +740,31 @@ ScalarFunctionSet RoundFun::GetFunctions() {
715740
bind_func = BindGenericRoundFunctionDecimal<RoundDecimalOperator>;
716741
bind_prec_func = BindDecimalRoundPrecision;
717742
break;
743+
case LogicalTypeId::TINYINT:
744+
round_func = ScalarFunction::NopFunction;
745+
round_prec_func = ScalarFunction::BinaryFunction<int8_t, int32_t, int8_t, RoundIntegerOperator>;
746+
break;
747+
case LogicalTypeId::SMALLINT:
748+
round_func = ScalarFunction::NopFunction;
749+
round_prec_func = ScalarFunction::BinaryFunction<int16_t, int32_t, int16_t, RoundIntegerOperator>;
750+
break;
751+
case LogicalTypeId::INTEGER:
752+
round_func = ScalarFunction::NopFunction;
753+
round_prec_func = ScalarFunction::BinaryFunction<int32_t, int32_t, int32_t, RoundIntegerOperator>;
754+
break;
755+
case LogicalTypeId::BIGINT:
756+
round_func = ScalarFunction::NopFunction;
757+
round_prec_func = ScalarFunction::BinaryFunction<int64_t, int32_t, int64_t, RoundIntegerOperator>;
758+
break;
759+
case LogicalTypeId::HUGEINT:
760+
round_func = ScalarFunction::NopFunction;
761+
round_prec_func = ScalarFunction::BinaryFunction<hugeint_t, int32_t, hugeint_t, RoundIntegerOperator>;
762+
break;
718763
default:
764+
if (type.IsIntegral()) {
765+
// no round for integral numbers
766+
continue;
767+
}
719768
throw InternalException("Unimplemented numeric type for function \"floor\"");
720769
}
721770
round.AddFunction(ScalarFunction({type}, type, round_func, bind_func));

src/duckdb/extension/icu/icu-table-range.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "duckdb/common/exception.hpp"
2+
#include "duckdb/common/operator/subtract.hpp"
23
#include "duckdb/common/types/interval.hpp"
34
#include "duckdb/common/types/timestamp.hpp"
45
#include "duckdb/main/extension_util.hpp"
56
#include "duckdb/function/function_set.hpp"
67
#include "duckdb/function/table_function.hpp"
7-
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
88
#include "include/icu-datefunc.hpp"
99
#include "unicode/calendar.h"
1010
#include "tz_calendar.hpp"
@@ -17,10 +17,10 @@ struct ICUTableRange {
1717
struct ICURangeBindData : public TableFunctionData {
1818
ICURangeBindData(const ICURangeBindData &other)
1919
: TableFunctionData(other), tz_setting(other.tz_setting), cal_setting(other.cal_setting),
20-
calendar(other.calendar->clone()) {
20+
calendar(other.calendar->clone()), cardinality(other.cardinality) {
2121
}
2222

23-
explicit ICURangeBindData(ClientContext &context) {
23+
explicit ICURangeBindData(ClientContext &context, const vector<Value> &inputs) {
2424
Value tz_value;
2525
if (context.TryGetCurrentSetting("TimeZone", tz_value)) {
2626
tz_setting = tz_value.ToString();
@@ -43,11 +43,36 @@ struct ICUTableRange {
4343
if (U_FAILURE(success)) {
4444
throw InternalException("Unable to create ICU calendar.");
4545
}
46+
47+
timestamp_tz_t bounds[2];
48+
interval_t step;
49+
for (idx_t i = 0; i < inputs.size(); i++) {
50+
if (inputs[i].IsNull()) {
51+
return;
52+
}
53+
if (i >= 2) {
54+
step = inputs[i].GetValue<interval_t>();
55+
} else {
56+
bounds[i] = inputs[i].GetValue<timestamp_tz_t>();
57+
}
58+
}
59+
// Estimate cardinality using micros.
60+
int64_t increment = 0;
61+
if (!Interval::TryGetMicro(step, increment) || !increment) {
62+
return;
63+
}
64+
int64_t delta = 0;
65+
if (!TrySubtractOperator::Operation(bounds[1].value, bounds[0].value, delta)) {
66+
return;
67+
}
68+
69+
cardinality = idx_t(delta / increment);
4670
}
4771

4872
string tz_setting;
4973
string cal_setting;
5074
CalendarPtr calendar;
75+
idx_t cardinality;
5176
};
5277

5378
struct ICURangeLocalState : public LocalTableFunctionState {
@@ -130,7 +155,7 @@ struct ICUTableRange {
130155
template <bool GENERATE_SERIES>
131156
static unique_ptr<FunctionData> Bind(ClientContext &context, TableFunctionBindInput &input,
132157
vector<LogicalType> &return_types, vector<string> &names) {
133-
auto result = make_uniq<ICURangeBindData>(context);
158+
auto result = make_uniq<ICURangeBindData>(context, input.inputs);
134159

135160
return_types.push_back(LogicalType::TIMESTAMP_TZ);
136161
if (GENERATE_SERIES) {
@@ -147,6 +172,14 @@ struct ICUTableRange {
147172
return make_uniq<ICURangeLocalState>();
148173
}
149174

175+
static unique_ptr<NodeStatistics> Cardinality(ClientContext &context, const FunctionData *bind_data_p) {
176+
if (!bind_data_p) {
177+
return nullptr;
178+
}
179+
auto &bind_data = bind_data_p->Cast<ICURangeBindData>();
180+
return make_uniq<NodeStatistics>(bind_data.cardinality, bind_data.cardinality);
181+
}
182+
150183
template <bool GENERATE_SERIES>
151184
static OperatorResultType ICUTableRangeFunction(ExecutionContext &context, TableFunctionInput &data_p,
152185
DataChunk &input, DataChunk &output) {
@@ -201,6 +234,7 @@ struct ICUTableRange {
201234
TableFunction range_function({LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP_TZ, LogicalType::INTERVAL},
202235
nullptr, Bind<false>, nullptr, RangeDateTimeLocalInit);
203236
range_function.in_out_function = ICUTableRangeFunction<false>;
237+
range_function.cardinality = Cardinality;
204238
range.AddFunction(range_function);
205239
ExtensionUtil::RegisterFunction(db, range);
206240

@@ -210,6 +244,7 @@ struct ICUTableRange {
210244
{LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP_TZ, LogicalType::INTERVAL}, nullptr, Bind<true>, nullptr,
211245
RangeDateTimeLocalInit);
212246
generate_series_function.in_out_function = ICUTableRangeFunction<true>;
247+
generate_series_function.cardinality = Cardinality;
213248
generate_series.AddFunction(generate_series_function);
214249
ExtensionUtil::RegisterFunction(db, generate_series);
215250
}

src/duckdb/extension/icu/third_party/icu/common/unistr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,11 @@ UnicodeString::doAppend(const UChar *srcChars, int32_t srcStart, int32_t srcLeng
15631563
}
15641564

15651565
int32_t oldLength = length();
1566-
int32_t newLength = oldLength + srcLength;
1566+
int32_t newLength;
1567+
if (uprv_add32_overflow(oldLength, srcLength, &newLength)) {
1568+
setToBogus();
1569+
return *this;
1570+
}
15671571

15681572
// Check for append onto ourself
15691573
const UChar* oldArray = getArrayStart();

src/duckdb/extension/json/include/json_common.hpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,23 @@ using namespace duckdb_yyjson; // NOLINT
1818

1919
namespace duckdb {
2020

21+
class JSONAllocator;
22+
23+
class JSONStringVectorBuffer : public VectorBuffer {
24+
public:
25+
explicit JSONStringVectorBuffer(shared_ptr<JSONAllocator> allocator_p)
26+
: VectorBuffer(VectorBufferType::OPAQUE_BUFFER), allocator(std::move(allocator_p)) {
27+
}
28+
29+
private:
30+
shared_ptr<JSONAllocator> allocator;
31+
};
32+
2133
//! JSON allocator is a custom allocator for yyjson that prevents many tiny allocations
22-
class JSONAllocator {
34+
class JSONAllocator : public enable_shared_from_this<JSONAllocator> {
2335
public:
2436
explicit JSONAllocator(Allocator &allocator)
25-
: arena_allocator(allocator), yyjson_allocator({Allocate, Reallocate, Free, &arena_allocator}) {
37+
: arena_allocator(allocator), yyjson_allocator({Allocate, Reallocate, Free, this}) {
2638
}
2739

2840
inline yyjson_alc *GetYYAlc() {
@@ -33,15 +45,26 @@ class JSONAllocator {
3345
arena_allocator.Reset();
3446
}
3547

48+
void AddBuffer(Vector &vector) {
49+
if (vector.GetType().InternalType() == PhysicalType::VARCHAR) {
50+
StringVector::AddBuffer(vector, make_buffer<JSONStringVectorBuffer>(shared_from_this()));
51+
}
52+
}
53+
54+
static void AddBuffer(Vector &vector, yyjson_alc *alc) {
55+
auto alloc = (JSONAllocator *)alc->ctx; // NOLINT
56+
alloc->AddBuffer(vector);
57+
}
58+
3659
private:
3760
static inline void *Allocate(void *ctx, size_t size) {
38-
auto alloc = (ArenaAllocator *)ctx;
39-
return alloc->AllocateAligned(size);
61+
auto alloc = (JSONAllocator *)ctx; // NOLINT
62+
return alloc->arena_allocator.AllocateAligned(size);
4063
}
4164

4265
static inline void *Reallocate(void *ctx, void *ptr, size_t old_size, size_t size) {
43-
auto alloc = (ArenaAllocator *)ctx;
44-
return alloc->ReallocateAligned(data_ptr_cast(ptr), old_size, size);
66+
auto alloc = (JSONAllocator *)ctx; // NOLINT
67+
return alloc->arena_allocator.ReallocateAligned(data_ptr_cast(ptr), old_size, size);
4568
}
4669

4770
static inline void Free(void *ctx, void *ptr) {
@@ -304,6 +327,20 @@ struct JSONCommon {
304327
//! Validate JSON Path ($.field[index]... syntax), returns true if there are wildcards in the path
305328
static JSONPathType ValidatePath(const char *ptr, const idx_t &len, const bool binder);
306329

330+
public:
331+
//! Same as BigQuery json_value
332+
static inline string_t JSONValue(yyjson_val *val, yyjson_alc *alc, Vector &, ValidityMask &mask, idx_t idx) {
333+
switch (yyjson_get_tag(val)) {
334+
case YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE:
335+
case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE:
336+
case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE:
337+
mask.SetInvalid(idx);
338+
return string_t {};
339+
default:
340+
return JSONCommon::WriteVal<yyjson_val>(val, alc);
341+
}
342+
}
343+
307344
private:
308345
//! Get JSON pointer (/field/index/... syntax)
309346
static inline yyjson_val *GetPointer(yyjson_val *val, const char *ptr, const idx_t &len) {

0 commit comments

Comments
 (0)