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 @@ -355,11 +355,11 @@ AggregateFunction GetApproxQuantileListAggregateFunction(const LogicalType &type
return GetTypedApproxQuantileListAggregateFunction<int16_t, int16_t>(type);
case LogicalTypeId::INTEGER:
case LogicalTypeId::DATE:
case LogicalTypeId::TIME:
return GetTypedApproxQuantileListAggregateFunction<int32_t, int32_t>(type);
case LogicalTypeId::BIGINT:
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_TZ:
case LogicalTypeId::TIME:
return GetTypedApproxQuantileListAggregateFunction<int64_t, int64_t>(type);
case LogicalTypeId::TIME_TZ:
// Not binary comparable
Expand Down
19 changes: 14 additions & 5 deletions src/duckdb/extension/icu/icu_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,16 @@ static string NormalizeTimeZone(const string &tz_str) {
}

idx_t pos = 3;
const auto sign = tz_str[pos++];
if (sign != '+' && sign != '-') {
const auto utc = tz_str[pos++];
// Invert the sign (UTC and Etc use opposite sign conventions)
// https://en.wikipedia.org/wiki/Tz_database#Area
auto sign = utc;
if (utc == '+') {
sign = '-';
;
} else if (utc == '-') {
sign = '+';
} else {
break;
}

Expand Down Expand Up @@ -424,12 +432,13 @@ static void LoadInternal(ExtensionLoader &loader) {
auto locales = icu::Collator::getAvailableLocales(count);
for (int32_t i = 0; i < count; i++) {
string collation;
if (string(locales[i].getCountry()).empty()) {
const auto &locale = locales[i]; // NOLINT
if (string(locale.getCountry()).empty()) {
// language only
collation = locales[i].getLanguage();
collation = locale.getLanguage();
} else {
// language + country
collation = locales[i].getLanguage() + string("_") + locales[i].getCountry();
collation = locale.getLanguage() + string("_") + locale.getCountry();
}
collation = StringUtil::Lower(collation);

Expand Down
4 changes: 4 additions & 0 deletions src/duckdb/src/common/encryption_key_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,25 @@ string EncryptionKeyManager::GenerateRandomKeyID() {
}

void EncryptionKeyManager::AddKey(const string &key_name, data_ptr_t key) {
lock_guard<mutex> guard(lock);
derived_keys.emplace(key_name, EncryptionKey(key));
// Zero-out the encryption key
duckdb_mbedtls::MbedTlsWrapper::AESStateMBEDTLS::SecureClearData(key, DERIVED_KEY_LENGTH);
}

bool EncryptionKeyManager::HasKey(const string &key_name) const {
lock_guard<mutex> guard(lock);
return derived_keys.find(key_name) != derived_keys.end();
}

const_data_ptr_t EncryptionKeyManager::GetKey(const string &key_name) const {
D_ASSERT(HasKey(key_name));
lock_guard<mutex> guard(lock);
return derived_keys.at(key_name).GetPtr();
}

void EncryptionKeyManager::DeleteKey(const string &key_name) {
lock_guard<mutex> guard(lock);
derived_keys.erase(key_name);
}

Expand Down
6 changes: 6 additions & 0 deletions src/duckdb/src/common/types/column/column_data_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ void ColumnDataCollection::InitializeScan(ColumnDataParallelScanState &state, ve

bool ColumnDataCollection::Scan(ColumnDataParallelScanState &state, ColumnDataLocalScanState &lstate,
DataChunk &result) const {
D_ASSERT(result.GetTypes() == types);
result.Reset();

idx_t chunk_index;
Expand Down Expand Up @@ -1129,6 +1130,10 @@ void ColumnDataCollection::ScanAtIndex(ColumnDataParallelScanState &state, Colum
}

bool ColumnDataCollection::Scan(ColumnDataScanState &state, DataChunk &result) const {
for (idx_t i = 0; i < state.column_ids.size(); i++) {
D_ASSERT(result.GetTypes()[i] == types[state.column_ids[i]]);
}

result.Reset();

idx_t chunk_index;
Expand Down Expand Up @@ -1213,6 +1218,7 @@ idx_t ColumnDataCollection::ChunkCount() const {
}

void ColumnDataCollection::FetchChunk(idx_t chunk_idx, DataChunk &result) const {
D_ASSERT(result.GetTypes() == types);
D_ASSERT(chunk_idx < ChunkCount());
for (auto &segment : segments) {
if (chunk_idx >= segment->ChunkCount()) {
Expand Down
2 changes: 1 addition & 1 deletion src/duckdb/src/common/types/conflict_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ optional_idx ConflictManager::GetFirstInvalidIndex(const idx_t count, const bool
for (idx_t i = 0; i < count; i++) {
if (negate && !validity.RowIsValid(i)) {
return i;
} else if (validity.RowIsValid(i)) {
} else if (!negate && validity.RowIsValid(i)) {
return i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ class PositionalTableScanner {

InterruptState interrupt_state;
OperatorSourceInput source_input {global_state, *local_state, interrupt_state};
auto source_result = table.GetData(context, source, source_input);
if (source_result == SourceResultType::BLOCKED) {
throw NotImplementedException(
"Unexpected interrupt from table Source in PositionalTableScanner refill");
auto source_result = SourceResultType::HAVE_MORE_OUTPUT;
while (source_result == SourceResultType::HAVE_MORE_OUTPUT && source.size() == 0) {
// TODO: this could as well just be propagated further, but for now iterating it is
source_result = table.GetData(context, source, source_input);
if (source_result == SourceResultType::BLOCKED) {
throw NotImplementedException(
"Unexpected interrupt from table Source in PositionalTableScanner refill");
}
}
}
source_offset = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/duckdb/src/execution/physical_plan/plan_aggregate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ PhysicalOperator &PhysicalPlanGenerator::CreatePlan(LogicalAggregate &op) {
D_ASSERT(op.children.size() == 1);

reference<PhysicalOperator> plan = CreatePlan(*op.children[0]);
plan = ExtractAggregateExpressions(plan, op.expressions, op.groups);
plan = ExtractAggregateExpressions(plan, op.expressions, op.groups, op.grouping_sets);

bool can_use_simple_aggregation = true;
for (auto &expression : op.expressions) {
Expand Down Expand Up @@ -305,7 +305,8 @@ PhysicalOperator &PhysicalPlanGenerator::CreatePlan(LogicalAggregate &op) {

PhysicalOperator &PhysicalPlanGenerator::ExtractAggregateExpressions(PhysicalOperator &child,
vector<unique_ptr<Expression>> &aggregates,
vector<unique_ptr<Expression>> &groups) {
vector<unique_ptr<Expression>> &groups,
optional_ptr<vector<GroupingSet>> grouping_sets) {
vector<unique_ptr<Expression>> expressions;
vector<LogicalType> types;

Expand All @@ -314,7 +315,7 @@ PhysicalOperator &PhysicalPlanGenerator::ExtractAggregateExpressions(PhysicalOpe
auto &bound_aggr = aggr->Cast<BoundAggregateExpression>();
if (bound_aggr.order_bys) {
// sorted aggregate!
FunctionBinder::BindSortedAggregate(context, bound_aggr, groups);
FunctionBinder::BindSortedAggregate(context, bound_aggr, groups, grouping_sets);
}
}
for (auto &group : groups) {
Expand Down
5 changes: 3 additions & 2 deletions src/duckdb/src/execution/physical_plan/plan_distinct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ PhysicalOperator &PhysicalPlanGenerator::CreatePlan(LogicalDistinct &op) {

if (ClientConfig::GetConfig(context).enable_optimizer) {
bool changes_made = false;
auto new_expr = OrderedAggregateOptimizer::Apply(context, *first_aggregate, groups, changes_made);
auto new_expr =
OrderedAggregateOptimizer::Apply(context, *first_aggregate, groups, nullptr, changes_made);
if (new_expr) {
D_ASSERT(new_expr->return_type == first_aggregate->return_type);
D_ASSERT(new_expr->GetExpressionType() == ExpressionType::BOUND_AGGREGATE);
Expand All @@ -81,7 +82,7 @@ PhysicalOperator &PhysicalPlanGenerator::CreatePlan(LogicalDistinct &op) {
}
}

child = ExtractAggregateExpressions(child, aggregates, groups);
child = ExtractAggregateExpressions(child, aggregates, groups, nullptr);

// we add a physical hash aggregation in the plan to select the distinct groups
auto &group_by = Make<PhysicalHashAggregate>(context, aggregate_types, std::move(aggregates), std::move(groups),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,14 +677,15 @@ struct SortedAggregateFunction {
} // namespace

void FunctionBinder::BindSortedAggregate(ClientContext &context, BoundAggregateExpression &expr,
const vector<unique_ptr<Expression>> &groups) {
const vector<unique_ptr<Expression>> &groups,
optional_ptr<vector<GroupingSet>> grouping_sets) {
if (!expr.order_bys || expr.order_bys->orders.empty() || expr.children.empty()) {
// not a sorted aggregate: return
return;
}
// Remove unnecessary ORDER BY clauses and return if nothing remains
if (context.config.enable_optimizer) {
if (expr.order_bys->Simplify(groups)) {
if (expr.order_bys->Simplify(groups, grouping_sets)) {
expr.order_bys.reset();
return;
}
Expand Down Expand Up @@ -741,7 +742,7 @@ void FunctionBinder::BindSortedAggregate(ClientContext &context, BoundWindowExpr
}
// Remove unnecessary ORDER BY clauses and return if nothing remains
if (context.config.enable_optimizer) {
if (BoundOrderModifier::Simplify(expr.arg_orders, expr.partitions)) {
if (BoundOrderModifier::Simplify(expr.arg_orders, expr.partitions, nullptr)) {
expr.arg_orders.clear();
return;
}
Expand Down
22 changes: 20 additions & 2 deletions src/duckdb/src/function/macro_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,31 @@ MacroBindResult MacroFunction::BindMacroFunction(

ExpressionBinder expr_binder(binder, binder.context);
expr_binder.lambda_bindings = binder.lambda_bindings;

// Figure out whether we even need to bind arguments
bool requires_bind = false;
for (auto &function : functions) {
for (const auto &type : function->types) {
if (type.id() != LogicalTypeId::UNKNOWN) {
requires_bind = true;
break;
}
}
if (requires_bind) {
break;
}
}

// Find argument types and separate positional and default arguments
vector<LogicalType> positional_arg_types;
InsertionOrderPreservingMap<LogicalType> named_arg_types;
for (auto &arg : function_expr.children) {
auto arg_copy = arg->Copy();
const auto arg_bind_result = expr_binder.BindExpression(arg_copy, depth + 1);
auto arg_type = arg_bind_result.HasError() ? LogicalType::UNKNOWN : arg_bind_result.expression->return_type;
LogicalType arg_type = LogicalType::UNKNOWN;
if (requires_bind) {
const auto arg_bind_result = expr_binder.BindExpression(arg_copy, depth + 1);
arg_type = arg_bind_result.HasError() ? LogicalType::UNKNOWN : arg_bind_result.expression->return_type;
}
if (!arg->GetAlias().empty()) {
// Default argument
if (named_arguments.find(arg->GetAlias()) != named_arguments.end()) {
Expand Down
33 changes: 22 additions & 11 deletions src/duckdb/src/function/table/system/test_all_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ struct TestAllTypesData : public GlobalTableFunctionState {
idx_t offset;
};

vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum, bool use_large_bignum) {
vector<TestType> TestAllTypesFun::GetTestTypes(const bool use_large_enum, const bool use_large_bignum) {
vector<TestType> result;
// scalar types/numerics

// Numeric types.
result.emplace_back(LogicalType::BOOLEAN, "bool");
result.emplace_back(LogicalType::TINYINT, "tinyint");
result.emplace_back(LogicalType::SMALLINT, "smallint");
Expand All @@ -33,24 +34,31 @@ vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum, bool use_lar
result.emplace_back(LogicalType::USMALLINT, "usmallint");
result.emplace_back(LogicalType::UINTEGER, "uint");
result.emplace_back(LogicalType::UBIGINT, "ubigint");

// BIGNUM.
if (use_large_bignum) {
string data;
idx_t total_data_size = Bignum::BIGNUM_HEADER_SIZE + Bignum::MAX_DATA_SIZE;
constexpr idx_t total_data_size = Bignum::BIGNUM_HEADER_SIZE + Bignum::MAX_DATA_SIZE;
data.resize(total_data_size);
// Let's set our header

// Let's set the max header.
Bignum::SetHeader(&data[0], Bignum::MAX_DATA_SIZE, false);
// Set all our other bits
// Set all other max bits.
memset(&data[Bignum::BIGNUM_HEADER_SIZE], 0xFF, Bignum::MAX_DATA_SIZE);
auto max = Value::BIGNUM(data);
// Let's set our header

// Let's set the min header.
Bignum::SetHeader(&data[0], Bignum::MAX_DATA_SIZE, true);
// Set all our other bits
// Set all other min bits.
memset(&data[Bignum::BIGNUM_HEADER_SIZE], 0x00, Bignum::MAX_DATA_SIZE);
auto min = Value::BIGNUM(data);
result.emplace_back(LogicalType::BIGNUM, "bignum", min, max);

} else {
result.emplace_back(LogicalType::BIGNUM, "bignum");
}

// Time-types.
result.emplace_back(LogicalType::DATE, "date");
result.emplace_back(LogicalType::TIME, "time");
result.emplace_back(LogicalType::TIMESTAMP, "timestamp");
Expand All @@ -59,6 +67,8 @@ vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum, bool use_lar
result.emplace_back(LogicalType::TIMESTAMP_NS, "timestamp_ns");
result.emplace_back(LogicalType::TIME_TZ, "time_tz");
result.emplace_back(LogicalType::TIMESTAMP_TZ, "timestamp_tz");

// More complex numeric types.
result.emplace_back(LogicalType::FLOAT, "float");
result.emplace_back(LogicalType::DOUBLE, "double");
result.emplace_back(LogicalType::DECIMAL(4, 1), "dec_4_1");
Expand All @@ -67,7 +77,7 @@ vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum, bool use_lar
result.emplace_back(LogicalType::DECIMAL(38, 10), "dec38_10");
result.emplace_back(LogicalType::UUID, "uuid");

// interval
// Interval.
interval_t min_interval;
min_interval.months = 0;
min_interval.days = 0;
Expand All @@ -79,14 +89,15 @@ vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum, bool use_lar
max_interval.micros = 999999999;
result.emplace_back(LogicalType::INTERVAL, "interval", Value::INTERVAL(min_interval),
Value::INTERVAL(max_interval));
// strings/blobs/bitstrings

// VARCHAR / BLOB / Bitstrings.
result.emplace_back(LogicalType::VARCHAR, "varchar", Value("🦆🦆🦆🦆🦆🦆"),
Value(string("goo\x00se", 6)));
result.emplace_back(LogicalType::BLOB, "blob", Value::BLOB("thisisalongblob\\x00withnullbytes"),
Value::BLOB("\\x00\\x00\\x00a"));
result.emplace_back(LogicalType::BIT, "bit", Value::BIT("0010001001011100010101011010111"), Value::BIT("10101"));

// enums
// ENUMs.
Vector small_enum(LogicalType::VARCHAR, 2);
auto small_enum_ptr = FlatVector::GetData<string_t>(small_enum);
small_enum_ptr[0] = StringVector::AddStringOrBlob(small_enum, "DUCK_DUCK_ENUM");
Expand Down Expand Up @@ -116,7 +127,7 @@ vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum, bool use_lar
result.emplace_back(LogicalType::ENUM(large_enum, 2), "large_enum");
}

// arrays
// ARRAYs.
auto int_list_type = LogicalType::LIST(LogicalType::INTEGER);
auto empty_int_list = Value::LIST(LogicalType::INTEGER, vector<Value>());
auto int_list =
Expand Down
Loading