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 @@ -382,7 +382,7 @@ auto LTTBStateLayout(AggregateLayoutInput &input) -> AggregateStateLayout {
auto &function = input.function;
using ST = LTTBState::STATE_TYPE;
AggregateStateLayout layout;
if (function.GetReturnType().IsAggregateState()) {
if (function.ReturnsExportedState()) {
// The function has been modified for state export, its return type IS the state type already
layout.type = function.GetReturnType();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ struct ModeString {
AggregateInputData &aggr_input_data) {
if (key.IsInlined()) {
if (!mode) {
mode = aggr_input_data.allocator.Make<string_t>(nullptr, 0);
mode = aggr_input_data.allocator.Make<string_t>();
}
*mode = key;
alloc_size = 0;
Expand All @@ -177,7 +177,7 @@ struct ModeString {
alloc_size = NextPowerOfTwo(len);
ptr = char_ptr_cast(aggr_input_data.allocator.Allocate(alloc_size));
if (!mode) {
mode = aggr_input_data.allocator.Make<string_t>(nullptr, 0);
mode = aggr_input_data.allocator.Make<string_t>();
}
}
memcpy(ptr, key.GetData(), len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void VariantObjClusterUpdate(Vector inputs[], AggregateInputData &aggr_input_dat

AggregateStateLayout VariantObjStateLayout(AggregateLayoutInput &input) {
AggregateStateLayout layout;
if (input.function.GetReturnType().IsAggregateState()) {
if (input.function.ReturnsExportedState()) {
layout.type = input.function.GetReturnType();
} else {
layout.type = LogicalType::LIST(GetInternalBufferType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ template <class STATE, class STATE_FIELD = StateListType<StateInputType<0>>>
AggregateStateLayout QuantileStateLayout(AggregateLayoutInput &input) {
auto &function = input.function;
AggregateStateLayout layout;
if (function.GetReturnType().IsAggregateState()) {
if (function.ReturnsExportedState()) {
// the function has been modified for state export (see ExportAggregateFunction::SetStateExport) -
// its return type IS the state type already
layout.type = function.GetReturnType();
Expand Down
38 changes: 32 additions & 6 deletions src/duckdb/extension/icu/icu-datesub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@
namespace duckdb {

struct ICUCalendarSub : public ICUDateFunc {
static bool SubtractsElapsedTime(DatePartSpecifier part) {
switch (part) {
case DatePartSpecifier::HOUR:
case DatePartSpecifier::MINUTE:
case DatePartSpecifier::SECOND:
case DatePartSpecifier::EPOCH:
case DatePartSpecifier::MILLISECONDS:
case DatePartSpecifier::MICROSECONDS:
return true;
default:
return false;
}
}

static unique_ptr<FunctionData> Bind(BindScalarFunctionInput &input) {
auto part_value = input.TryGetConstant(0);
if (part_value && !part_value->IsNull()) {
DatePartSpecifier part;
if (TryGetDatePartSpecifier(part_value->GetValue<string>(), part) && part == DatePartSpecifier::ERA) {
// date_sub is not monotone for eras because era boundaries can occur partway through a year
if (!TryGetDatePartSpecifier(part_value->GetValue<string>(), part) || !SubtractsElapsedTime(part)) {
// calendar parts count whole local units, so moving one argument can step the result back,
// e.g. a month boundary that falls between two timestamps or a local day that repeats
input.GetBoundFunction().SetArgProperties({});
}
}
Expand Down Expand Up @@ -221,14 +236,25 @@ ICUDateFunc::part_sub_t ICUDateFunc::SubtractFactory(DatePartSpecifier type) {
// MS-SQL differences can be computed using ICU by truncating both arguments
// to the desired part precision and then applying ICU subtraction/difference
struct ICUCalendarDiff : public ICUDateFunc {
static bool TruncatesToSecondOrFiner(DatePartSpecifier part) {
switch (part) {
case DatePartSpecifier::SECOND:
case DatePartSpecifier::EPOCH:
case DatePartSpecifier::MILLISECONDS:
case DatePartSpecifier::MICROSECONDS:
return true;
default:
return false;
}
}

static unique_ptr<FunctionData> Bind(BindScalarFunctionInput &input) {
auto part_value = input.TryGetConstant(0);
if (part_value && !part_value->IsNull()) {
DatePartSpecifier part;
if (TryGetDatePartSpecifier(part_value->GetValue<string>(), part) &&
(part == DatePartSpecifier::DECADE || part == DatePartSpecifier::CENTURY ||
part == DatePartSpecifier::MILLENNIUM)) {
// date_diff is not monotone for these parts because ICU years skip year 0 at the BC/AD boundary
if (!TryGetDatePartSpecifier(part_value->GetValue<string>(), part) || !TruncatesToSecondOrFiner(part)) {
// date_diff truncates both arguments first, and truncating to a coarser part follows the local
// time, which repeats or moves backwards when the UTC offset changes
input.GetBoundFunction().SetArgProperties({});
}
}
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-alpha57387";
descriptor->api_version = "v2.1.0-alpha57486";
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-alpha57387";
descriptor->api_version = "v2.1.0-alpha57486";
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-alpha57387";
descriptor->api_version = "v2.1.0-alpha57486";
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-alpha57387";
descriptor->api_version = "v2.1.0-alpha57486";
descriptor->entry_cpp = (void (*)(void))json_duckdb_cpp_init;
return 0;
}
Expand Down
42 changes: 35 additions & 7 deletions src/duckdb/src/common/multi_file/multi_file_column_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ struct ColumnMapper {
virtual ~ColumnMapper() = default;
virtual unique_ptr<ColumnMapper> Create(const vector<MultiFileColumnDefinition> &columns) const = 0;
virtual MultiFileLocalIndex Find(const MultiFileColumnDefinition &column) const = 0;
//! Find the element of a LIST among the children of the list
virtual MultiFileLocalIndex FindListChild(const MultiFileColumnDefinition &column) const {
return Find(column);
}
virtual unique_ptr<Expression> GetDefaultExpression(ClientContext &context, const MultiFileColumnDefinition &column,
bool is_root) const = 0;
virtual idx_t MapCount() const = 0;
Expand Down Expand Up @@ -169,6 +173,14 @@ struct NameMapper : public ColumnMapper {
}
return entry->second;
}
MultiFileLocalIndex FindListChild(const MultiFileColumnDefinition &column) const override {
if (name_map.size() != 1) {
return Find(column);
}
// A LIST has exactly one child, and a file names it after its repeated group ("list", "array", "bag", ...).
// That name is not part of the logical schema, so the single child is the element whatever it is called.
return MultiFileLocalIndex(0);
}
unique_ptr<Expression> GetDefaultExpression(ClientContext &context, const MultiFileColumnDefinition &column,
bool is_root) const override {
if (column.default_expression) {
Expand Down Expand Up @@ -249,6 +261,13 @@ static ColumnMapResult MapColumn(ClientContext &context, const MultiFileColumnDe
const vector<MultiFileColumnDefinition> &local_columns, const ColumnMapper &mapper,
MultiFileLocalIndex top_level_index = MultiFileLocalIndex());

//! Map a global column to the local column the mapper found for it (invalid if the file does not have it)
static ColumnMapResult MapColumnToLocal(ClientContext &context, const MultiFileColumnDefinition &global_column,
const ColumnIndex &global_index,
const vector<MultiFileColumnDefinition> &local_columns,
const ColumnMapper &mapper, MultiFileLocalIndex local_idx,
MultiFileLocalIndex top_level_index = MultiFileLocalIndex());

static ColumnMapResult MapColumnList(ClientContext &context, const MultiFileColumnDefinition &global_column,
const ColumnIndex &global_index, const MultiFileColumnDefinition &local_column,
const MultiFileLocalIndex &local_id, const ColumnMapper &mapper,
Expand Down Expand Up @@ -289,7 +308,8 @@ static ColumnMapResult MapColumnList(ClientContext &context, const MultiFileColu

ColumnMapResult child_map;
if (is_selected) {
child_map = MapColumn(context, global_child, global_child_index.get(), local_column.children, *nested_mapper);
child_map = MapColumnToLocal(context, global_child, global_child_index.get(), local_column.children,
*nested_mapper, nested_mapper->FindListChild(global_child));
} else {
// column is not relevant for the query - push a NULL value
child_map.default_value = make_uniq<BoundConstantExpression>(Value(global_child.type));
Expand All @@ -306,8 +326,8 @@ static ColumnMapResult MapColumnList(ClientContext &context, const MultiFileColu

ColumnMapResult result;
result.local_column = local_column;
if (!column_mapping.empty()) {
// we have column mappings at this level - construct the struct
if (!column_mapping.empty() || !is_root) {
// A nested source still supplies the list entries when the element comes from a default.
result.column_map = Value::STRUCT(std::move(column_mapping));
if (!is_root) {
// if this is nested we need to refer to the current column at this level
Expand Down Expand Up @@ -385,8 +405,8 @@ static ColumnMapResult MapColumnMap(ClientContext &context, const MultiFileColum
global_column.children.size());
}

// the single child is the repeated group of the map, which most files call "key_value" and some "map"
D_ASSERT(local_column.children.size() == 1);
D_ASSERT(local_column.children[0].name == "key_value");
auto &local_key_value = local_column.children[0];

auto nested_mapper = mapper.Create(local_key_value.children);
Expand Down Expand Up @@ -435,8 +455,8 @@ static ColumnMapResult MapColumnMap(ClientContext &context, const MultiFileColum

ColumnMapResult result;
result.local_column = local_column;
if (!column_mapping.empty()) {
// we have column mappings at this level - construct the struct
if (!column_mapping.empty() || !is_root) {
// A nested source still supplies the map entries when the key and value come from defaults.
result.column_map = Value::STRUCT(std::move(column_mapping));
if (!is_root) {
// if this is nested we need to refer to the current column at this level
Expand Down Expand Up @@ -582,9 +602,17 @@ static ColumnMapResult MapColumn(ClientContext &context, const MultiFileColumnDe
const ColumnIndex &global_index,
const vector<MultiFileColumnDefinition> &local_columns, const ColumnMapper &mapper,
MultiFileLocalIndex top_level_index) {
return MapColumnToLocal(context, global_column, global_index, local_columns, mapper, mapper.Find(global_column),
top_level_index);
}

static ColumnMapResult MapColumnToLocal(ClientContext &context, const MultiFileColumnDefinition &global_column,
const ColumnIndex &global_index,
const vector<MultiFileColumnDefinition> &local_columns,
const ColumnMapper &mapper, MultiFileLocalIndex local_idx,
MultiFileLocalIndex top_level_index) {
bool is_root = top_level_index.IsValid();
ColumnMapResult result;
auto local_idx = mapper.Find(global_column);
if (!local_idx.IsValid()) {
// entry not present in map, use default value
result.default_value = mapper.GetDefaultExpression(context, global_column, is_root);
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/src/common/random_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "duckdb/common/numeric_utils.hpp"
#include "pcg_random.hpp"

#include <chrono>

#ifdef __linux__
#include <sys/syscall.h>
#include <unistd.h>
Expand Down
12 changes: 10 additions & 2 deletions src/duckdb/src/common/types/validity_mask.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "duckdb/common/types/validity_mask.hpp"
#include "duckdb/common/bit_utils.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/common/limits.hpp"
#include "duckdb/common/numeric_utils.hpp"
Expand Down Expand Up @@ -278,13 +279,20 @@ void ValidityMask::Write(WriteStream &writer, idx_t count) {
auto flag = serialize_valid ? ValiditySerialization::VALID_VALUES : ValiditySerialization::INVALID_VALUES;
writer.Write(flag);
writer.Write<uint32_t>(NumericCast<uint32_t>(MinValue(valid_values, invalid_values)));
for (idx_t i = 0; i < count; i++) {
if (RowIsValid(i) == serialize_valid) {
for (idx_t entry_idx = 0; entry_idx < EntryCount(count); entry_idx++) {
auto entry = GetValidityEntry(entry_idx);
validity_t bits = serialize_valid ? entry : ~entry;
while (bits) {
auto i = entry_idx * BITS_PER_VALUE + CountZeros<uint64_t>::Trailing(bits);
if (i >= count) {
break;
}
if (need_u32) {
writer.Write<uint32_t>(UnsafeNumericCast<uint32_t>(i));
} else {
writer.Write<uint16_t>(UnsafeNumericCast<uint16_t>(i));
}
bits &= bits - 1;
}
}
} else {
Expand Down
72 changes: 72 additions & 0 deletions src/duckdb/src/common/vector/dictionary_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "duckdb/common/types/sel_cache.hpp"
#include "duckdb/common/serializer/deserializer.hpp"
#include "duckdb/common/serializer/serializer.hpp"
#include "duckdb/common/bit_utils.hpp"

namespace duckdb {

Expand Down Expand Up @@ -197,6 +198,77 @@ const Vector &DictionaryVector::GetCachedHashes(const Vector &input) {
return *entry.cached_hashes;
}

void DictionaryVector::ApplyValidity(Vector &vector, const ValidityMask &mask) {
VerifyDictionary(vector);
if (mask.CannotHaveNull()) {
return;
}
const auto count = vector.size();
const auto validity = mask.GetData();
const auto full_entry_count = count / ValidityMask::BITS_PER_VALUE;
const auto tail_count = count % ValidityMask::BITS_PER_VALUE;
const auto tail_invalid =
tail_count ? ~validity[full_entry_count] & ValidityMask::EntryWithValidBits(tail_count) : 0;
idx_t first_invalid = 0;
while (first_invalid < full_entry_count && ValidityMask::AllValid(validity[first_invalid])) {
first_invalid++;
}
if (first_invalid == full_entry_count && !tail_invalid) {
return;
}

const auto &child = Child(vector);
idx_t null_index = child.size();
if (child.GetVectorType() == VectorType::FLAT_VECTOR && child.size() &&
!FlatVector::Validity(child).RowIsValid(child.size() - 1)) {
null_index--;
} else {
UnifiedVectorFormat format;
child.ToUnifiedFormat(format);
if (!format.validity.CannotHaveNull()) {
for (idx_t i = 0; i < child.size(); i++) {
if (!format.validity.RowIsValid(format.sel->get_index(i))) {
null_index = i;
break;
}
}
}
}

{
auto &buffer = vector.BufferMutable().Cast<DictionaryBuffer>();
auto &selection = buffer.GetSelVector();
if (vector.GetBufferRef().use_count() != 1 || !selection.sel_data() || selection.sel_data().use_count() != 1) {
SelectionVector owned_selection(count);
for (idx_t i = 0; i < count; i++) {
owned_selection.set_index(i, selection.get_index(i));
}
vector.Dictionary(buffer.GetEntryPtr(), owned_selection, count);
}
}
if (null_index == child.size()) {
// Copy dictionary entries, leaving the original dictionary and its caches unchanged.
auto entry = make_buffer<DictionaryEntry>(Vector(vector.GetType(), child.size() + 1));
VectorOperations::Copy(child, entry->data, child.size(), 0, 0);
FlatVector::SetSize(entry->data, child.size() + 1);
FlatVector::SetNull(entry->data, null_index, true);
vector.BufferMutable().Cast<DictionaryBuffer>().SetEntry(std::move(entry));
}
unsafe_array_ptr<sel_t> target(SelVector(vector).data(), count);
const auto null_code = NumericCast<sel_t>(null_index);
auto apply_invalid = [&](idx_t entry_idx, validity_t invalid) {
const auto start = entry_idx * ValidityMask::BITS_PER_VALUE;
while (invalid) {
target[start + CountZeros<validity_t>::Trailing(invalid)] = null_code;
invalid &= invalid - 1;
}
};
for (idx_t entry_idx = first_invalid; entry_idx < full_entry_count; entry_idx++) {
apply_invalid(entry_idx, ~validity[entry_idx]);
}
apply_invalid(full_entry_count, tail_invalid);
}

bool DictionaryBuffer::TrySerialize(Serializer &serializer, const LogicalType &type,
bool compressed_serialization) const {
auto dictionary_size = GetDictionarySize();
Expand Down
2 changes: 2 additions & 0 deletions src/duckdb/src/execution/index/art/art.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,9 +1615,11 @@ void ART::Verify(IndexLock &l) {
}

void ART::VerifyInternal() {
#ifdef D_ASSERT_IS_ENABLED
if (root_ptr.HasMetadata()) {
root_ptr.Verify(*this);
}
#endif
}

void ART::VerifyAllocations(IndexLock &l) {
Expand Down
1 change: 1 addition & 0 deletions src/duckdb/src/function/function_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ static const StaticFunctionDefinition function[] = {
DUCKDB_SCALAR_FUNCTION(VariantNormalizeFun),
DUCKDB_SCALAR_FUNCTION_SET(VariantTypeFun),
DUCKDB_SCALAR_FUNCTION(VariantTypeofFun),
DUCKDB_SCALAR_FUNCTION_SET(VariantValueFun),
DUCKDB_SCALAR_FUNCTION_SET(VertexExtractFun),
DUCKDB_SCALAR_FUNCTION_SET(WriteLogFun),
DUCKDB_SCALAR_FUNCTION(ConcatOperatorFun),
Expand Down
Loading
Loading