Skip to content

Commit 92af000

Browse files
authored
Merge branch 'main' into main
2 parents f5fb6cc + 8cff889 commit 92af000

File tree

681 files changed

+66257
-3574
lines changed

Some content is hidden

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

681 files changed

+66257
-3574
lines changed

.github/workflows/Java.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ jobs:
2020
fetch-depth: 0
2121
ref: ${{ inputs.git_ref }}
2222

23-
- run: make format-check
23+
- run: |
24+
python3 -m pip install --user clang_format==11.0.1
25+
make format-check
2426
2527
java-linux-amd64:
2628
name: Java Linux (amd64)

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

src/duckdb/extension/core_functions/aggregate/algebraic/avg.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ struct AvgState {
2222
}
2323
};
2424

25+
struct IntervalAvgState {
26+
int64_t count;
27+
interval_t value;
28+
29+
void Initialize() {
30+
this->count = 0;
31+
this->value = interval_t();
32+
}
33+
34+
void Combine(const IntervalAvgState &other) {
35+
this->count += other.count;
36+
this->value = AddOperator::Operation<interval_t, interval_t, interval_t>(this->value, other.value);
37+
}
38+
};
39+
2540
struct KahanAvgState {
2641
uint64_t count;
2742
double value;
@@ -105,6 +120,20 @@ struct IntegerAverageOperationHugeint : public BaseSumOperation<AverageSetOperat
105120
}
106121
};
107122

123+
struct DiscreteAverageOperation : public BaseSumOperation<AverageSetOperation, AddToHugeint> {
124+
template <class T, class STATE>
125+
static void Finalize(STATE &state, T &target, AggregateFinalizeData &finalize_data) {
126+
if (state.count == 0) {
127+
finalize_data.ReturnNull();
128+
} else {
129+
uint64_t remainder;
130+
target = Hugeint::Cast<T>(Hugeint::DivModPositive(state.value, state.count, remainder));
131+
// Round the result
132+
target += (remainder > (state.count / 2));
133+
}
134+
}
135+
};
136+
108137
struct HugeintAverageOperation : public BaseSumOperation<AverageSetOperation, HugeintAdd> {
109138
template <class T, class STATE>
110139
static void Finalize(STATE &state, T &target, AggregateFinalizeData &finalize_data) {
@@ -139,6 +168,45 @@ struct KahanAverageOperation : public BaseSumOperation<AverageSetOperation, Kaha
139168
}
140169
};
141170

171+
struct IntervalAverageOperation : public BaseSumOperation<AverageSetOperation, IntervalAdd> {
172+
// Override BaseSumOperation::Initialize because
173+
// IntervalAvgState does not have an assignment constructor from 0
174+
static void Initialize(IntervalAvgState &state) {
175+
AverageSetOperation::Initialize<IntervalAvgState>(state);
176+
}
177+
178+
template <class RESULT_TYPE, class STATE>
179+
static void Finalize(STATE &state, RESULT_TYPE &target, AggregateFinalizeData &finalize_data) {
180+
if (state.count == 0) {
181+
finalize_data.ReturnNull();
182+
} else {
183+
// DivideOperator does not borrow fractions right,
184+
// TODO: Maybe it should?
185+
// Copy PG implementation.
186+
const auto &value = state.value;
187+
const auto count = UnsafeNumericCast<int64_t>(state.count);
188+
189+
target.months = value.months / count;
190+
auto months_remainder = value.months % count;
191+
192+
target.days = value.days / count;
193+
auto days_remainder = value.days % count;
194+
195+
target.micros = value.micros / count;
196+
auto micros_remainder = value.micros % count;
197+
198+
// Shift the remainders right
199+
months_remainder *= Interval::DAYS_PER_MONTH;
200+
target.days += months_remainder / count;
201+
days_remainder += months_remainder % count;
202+
203+
days_remainder *= Interval::MICROS_PER_DAY;
204+
micros_remainder += days_remainder / count;
205+
target.micros += micros_remainder;
206+
}
207+
}
208+
};
209+
142210
AggregateFunction GetAverageAggregate(PhysicalType type) {
143211
switch (type) {
144212
case PhysicalType::INT16: {
@@ -157,6 +225,10 @@ AggregateFunction GetAverageAggregate(PhysicalType type) {
157225
return AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, hugeint_t, double, HugeintAverageOperation>(
158226
LogicalType::HUGEINT, LogicalType::DOUBLE);
159227
}
228+
case PhysicalType::INTERVAL: {
229+
return AggregateFunction::UnaryAggregate<IntervalAvgState, interval_t, interval_t, IntervalAverageOperation>(
230+
LogicalType::INTERVAL, LogicalType::INTERVAL);
231+
}
160232
default:
161233
throw InternalException("Unimplemented average aggregate");
162234
}
@@ -183,8 +255,17 @@ AggregateFunctionSet AvgFun::GetFunctions() {
183255
avg.AddFunction(GetAverageAggregate(PhysicalType::INT32));
184256
avg.AddFunction(GetAverageAggregate(PhysicalType::INT64));
185257
avg.AddFunction(GetAverageAggregate(PhysicalType::INT128));
258+
avg.AddFunction(GetAverageAggregate(PhysicalType::INTERVAL));
186259
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<double>, double, double, NumericAverageOperation>(
187260
LogicalType::DOUBLE, LogicalType::DOUBLE));
261+
262+
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, int64_t, DiscreteAverageOperation>(
263+
LogicalType::TIMESTAMP, LogicalType::TIMESTAMP));
264+
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, int64_t, DiscreteAverageOperation>(
265+
LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP_TZ));
266+
avg.AddFunction(AggregateFunction::UnaryAggregate<AvgState<hugeint_t>, int64_t, int64_t, DiscreteAverageOperation>(
267+
LogicalType::TIME, LogicalType::TIME));
268+
188269
return avg;
189270
}
190271

src/duckdb/extension/core_functions/function_list.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ static const StaticFunctionDefinition core_functions[] = {
266266
DUCKDB_SCALAR_FUNCTION(MapConcatFun),
267267
DUCKDB_SCALAR_FUNCTION(MapEntriesFun),
268268
DUCKDB_SCALAR_FUNCTION(MapExtractFun),
269+
DUCKDB_SCALAR_FUNCTION(MapExtractValueFun),
269270
DUCKDB_SCALAR_FUNCTION(MapFromEntriesFun),
270271
DUCKDB_SCALAR_FUNCTION(MapKeysFun),
271272
DUCKDB_SCALAR_FUNCTION(MapValuesFun),

src/duckdb/extension/core_functions/include/core_functions/aggregate/sum_helpers.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
#pragma once
1010

11-
#include "duckdb/common/common.hpp"
12-
#include "duckdb/common/types.hpp"
13-
#include "duckdb/common/types/vector.hpp"
11+
#include "duckdb/common/types/hugeint.hpp"
12+
#include "duckdb/common/operator/add.hpp"
13+
#include "duckdb/common/operator/multiply.hpp"
14+
#include "duckdb/function/aggregate_state.hpp"
15+
#include "duckdb/common/operator/cast_operators.hpp"
1416

1517
namespace duckdb {
1618

@@ -77,6 +79,20 @@ struct HugeintAdd {
7779
}
7880
};
7981

82+
struct IntervalAdd {
83+
template <class STATE, class T>
84+
static void AddNumber(STATE &state, T input) {
85+
state.value = AddOperator::Operation<interval_t, interval_t, interval_t>(state.value, input);
86+
}
87+
88+
template <class STATE, class T>
89+
static void AddConstant(STATE &state, T input, idx_t count) {
90+
const auto count64 = Cast::Operation<idx_t, int64_t>(count);
91+
input = MultiplyOperator::Operation<interval_t, int64_t, interval_t>(input, count64);
92+
state.value = AddOperator::Operation<interval_t, interval_t, interval_t>(state.value, input);
93+
}
94+
};
95+
8096
struct KahanAdd {
8197
template <class STATE, class T>
8298
static void AddNumber(STATE &state, T input) {

src/duckdb/extension/core_functions/include/core_functions/scalar/map_functions.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ struct ElementAtFun {
5757
static constexpr const char *Name = "element_at";
5858
};
5959

60+
struct MapExtractValueFun {
61+
static constexpr const char *Name = "map_extract_value";
62+
static constexpr const char *Parameters = "map,key";
63+
static constexpr const char *Description = "Returns the value for a given key or NULL if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map’s keys else an error is returned";
64+
static constexpr const char *Example = "map_extract_value(map(['key'], ['val']), 'key')";
65+
66+
static ScalarFunction GetFunction();
67+
};
68+
6069
struct MapFromEntriesFun {
6170
static constexpr const char *Name = "map_from_entries";
6271
static constexpr const char *Parameters = "map";

src/duckdb/extension/core_functions/scalar/date/current.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "duckdb/main/client_context.hpp"
77
#include "duckdb/planner/expression/bound_function_expression.hpp"
88
#include "duckdb/transaction/meta_transaction.hpp"
9+
#include "duckdb/planner/expression/bound_cast_expression.hpp"
910

1011
namespace duckdb {
1112

src/duckdb/extension/core_functions/scalar/generic/can_implicitly_cast.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ static void CanCastImplicitlyFunction(DataChunk &args, ExpressionState &state, V
1818
}
1919

2020
unique_ptr<Expression> BindCanCastImplicitlyExpression(FunctionBindExpressionInput &input) {
21-
auto &source_type = input.function.children[0]->return_type;
22-
auto &target_type = input.function.children[1]->return_type;
21+
auto &source_type = input.children[0]->return_type;
22+
auto &target_type = input.children[1]->return_type;
2323
if (source_type.id() == LogicalTypeId::UNKNOWN || source_type.id() == LogicalTypeId::SQLNULL ||
2424
target_type.id() == LogicalTypeId::UNKNOWN || target_type.id() == LogicalTypeId::SQLNULL) {
2525
// parameter - unknown return type

src/duckdb/extension/core_functions/scalar/generic/typeof.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static void TypeOfFunction(DataChunk &args, ExpressionState &state, Vector &resu
1010
}
1111

1212
unique_ptr<Expression> BindTypeOfFunctionExpression(FunctionBindExpressionInput &input) {
13-
auto &return_type = input.function.children[0]->return_type;
13+
auto &return_type = input.children[0]->return_type;
1414
if (return_type.id() == LogicalTypeId::UNKNOWN || return_type.id() == LogicalTypeId::SQLNULL) {
1515
// parameter - unknown return type
1616
return nullptr;

0 commit comments

Comments
 (0)