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
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.0.1-alpha54043";
descriptor->api_version = "v2.0.1-alpha54527";
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.0.1-alpha54043";
descriptor->api_version = "v2.0.1-alpha54527";
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.0.1-alpha54043";
descriptor->api_version = "v2.0.1-alpha54527";
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.0.1-alpha54043";
descriptor->api_version = "v2.0.1-alpha54527";
descriptor->entry_cpp = (void (*)(void))json_duckdb_cpp_init;
return 0;
}
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 @@ -1066,8 +1066,10 @@ void ColumnDataCollection::Append(ColumnDataAppendState &state, DataChunk &input
for (idx_t vector_idx = 0; vector_idx < types.size(); vector_idx++) {
ColumnDataMetaData meta_data(copy_functions[vector_idx], segment, state, chunk_data,
chunk_data.vector_data[vector_idx]);
const auto allocated_before = segment.total_allocated;
copy_functions[vector_idx].function(meta_data, state.vector_data[vector_idx], input.data[vector_idx],
offset, append_amount);
chunk_data.allocation_sizes[vector_idx] += segment.total_allocated - allocated_before;
}
chunk_data.count += append_amount;
}
Expand Down Expand Up @@ -1320,6 +1322,10 @@ idx_t ColumnDataChunkCursor::AllocationSize() const {
return collection.GetSegments()[position.segment_index]->GetChunkAllocationSize(position.chunk_index);
}

idx_t ColumnDataChunkCursor::AllocationSize(const vector<column_t> &column_ids) const {
return collection.GetSegments()[position.segment_index]->GetChunkAllocationSize(position.chunk_index, column_ids);
}

ColumnDataRowCollection ColumnDataCollection::GetRows() const {
return ColumnDataRowCollection(*this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ void ColumnDataCollectionSegment::AllocateNewChunk() {
ChunkMetaData meta_data;
meta_data.count = 0;
meta_data.vector_data.reserve(types.size());
meta_data.allocation_sizes.reserve(types.size());
for (idx_t i = 0; i < types.size(); i++) {
const auto allocated_before = total_allocated;
auto vector_idx = AllocateVector(types[i], meta_data);
meta_data.vector_data.push_back(vector_idx);
meta_data.allocation_sizes.push_back(total_allocated - allocated_before);
}
chunk_data.push_back(std::move(meta_data));
}
Expand Down Expand Up @@ -323,6 +326,16 @@ idx_t ColumnDataCollectionSegment::GetChunkAllocationSize(const idx_t chunk_idx)
return total_allocated - last_chunk_total_allocated;
}

idx_t ColumnDataCollectionSegment::GetChunkAllocationSize(const idx_t chunk_idx,
const vector<column_t> &column_ids) const {
const auto &allocation_sizes = chunk_data[chunk_idx].allocation_sizes;
idx_t result = 0;
for (const auto &column_id : column_ids) {
result += allocation_sizes[column_id];
}
return result;
}

void ColumnDataCollectionSegment::FetchChunk(idx_t chunk_idx, DataChunk &result) {
vector<column_t> column_ids;
column_ids.reserve(types.size());
Expand All @@ -344,6 +357,11 @@ void ColumnDataCollectionSegment::Verify() {
idx_t total_count = 0;
for (idx_t i = 0; i < chunk_data.size(); i++) {
total_count += chunk_data[i].count;
idx_t allocation_size = 0;
for (const auto &column_size : chunk_data[i].allocation_sizes) {
allocation_size += column_size;
}
D_ASSERT(allocation_size == GetChunkAllocationSize(i));
}
D_ASSERT(total_count == this->count);
#endif
Expand Down
122 changes: 84 additions & 38 deletions src/duckdb/src/execution/operator/persistent/physical_copy_to_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,11 +1240,11 @@ class PartitionedCopyGroupCursor {
virtual PartitionedCopyChunkPosition Position() const = 0;
//! The number of rows in the current chunk
virtual idx_t Count() const = 0;
//! An estimate of the bytes the rows of the current chunk take when written
//! An estimate of the bytes the written columns of the current chunk take
virtual idx_t ByteEstimate() const = 0;
//! The row offsets within the current chunk at which a new partition key starts (none in a single-key group)
virtual void PartitionBoundaries(vector<idx_t> &boundaries) const = 0;
//! The byte size of every row of the current chunk (may pin and read the chunk)
//! The byte size of every written row of the current chunk (may pin and read the chunk)
virtual void RowSizes(vector<idx_t> &sizes) = 0;
};

Expand All @@ -1266,7 +1266,7 @@ class PartitionedCopyGroup {
virtual void FinishPreparation(idx_t step) = 0;

//! Chunk access, valid after the preparation steps: a cursor over the chunks, and scans from a chunk on
virtual unique_ptr<PartitionedCopyGroupCursor> InitializeCursor() const = 0;
virtual unique_ptr<PartitionedCopyGroupCursor> InitializeCursor(const CopyBatchSlicer &slicer) const = 0;
virtual unique_ptr<PartitionedCopyGroupScanState>
InitializeScan(const PartitionedCopyChunkPosition &position) const = 0;
virtual void InitializeScanChunk(DataChunk &chunk) const = 0;
Expand Down Expand Up @@ -2195,8 +2195,7 @@ class SortedBinGroup : public PartitionedCopyGroup {
ColumnDataScanState state;
};

//! Walks the chunks of the materialized collection through the column data cursor; rows are sized by scanning
//! the chunk and computing the sizes of its written columns
//! Walks the chunks of the materialized collection through the column data cursor
class Cursor : public PartitionedCopyGroupCursor {
public:
explicit Cursor(const SortedBinGroup &group) : group(group), cursor(*group.collection) {
Expand All @@ -2214,7 +2213,7 @@ class SortedBinGroup : public PartitionedCopyGroup {
return cursor.Count();
}
idx_t ByteEstimate() const override {
return cursor.AllocationSize();
return cursor.AllocationSize(group.input.partitioned_copy.write_columns);
}
void PartitionBoundaries(vector<idx_t> &boundaries) const override {
group.PartitionBoundaries(cursor.Position().row_offset, Count(), boundaries);
Expand Down Expand Up @@ -2247,7 +2246,7 @@ class SortedBinGroup : public PartitionedCopyGroup {
idx_t end_block) override;
void FinishPreparation(idx_t step) override;

unique_ptr<PartitionedCopyGroupCursor> InitializeCursor() const override {
unique_ptr<PartitionedCopyGroupCursor> InitializeCursor(const CopyBatchSlicer &) const override {
return make_uniq<Cursor>(*this);
}
unique_ptr<PartitionedCopyGroupScanState>
Expand Down Expand Up @@ -2624,13 +2623,13 @@ class DirectPartitionGroup : public PartitionedCopyGroup {
TupleDataScanState state;
};

//! Walks the tuple chunks: the byte estimate is the row width plus the heap bytes the storage accounts for, and
//! the row sizes come from the stored per-row heap sizes
//! Walks the tuple chunks without gathering their values
class Cursor : public PartitionedCopyGroupCursor {
public:
explicit Cursor(TupleDataCollection &data_p)
: data(data_p), cursor(data_p), row_width(data_p.GetLayout().GetRowWidth()),
has_heap(!data_p.GetLayout().AllConstant()) {
Cursor(const DirectPartitionGroup &group, const CopyBatchSlicer &slicer)
: data(*group.data), cursor(data), fixed_row_bytes(slicer.GetFixedRowBytes()),
omitted_heap_bytes(slicer.FixedRowSize().IsValid() ? 0 : group.OmittedHeapBytes()),
has_heap(!data.GetLayout().AllConstant()) {
}

public:
Expand All @@ -2650,7 +2649,10 @@ class DirectPartitionGroup : public PartitionedCopyGroup {
return cursor.Count();
}
idx_t ByteEstimate() const override {
return cursor.Count() * row_width + cursor.HeapSize();
const auto heap_bytes = cursor.HeapSize();
// Clamp before multiplying so an estimated key size cannot overflow or underflow the heap total.
const auto omitted_bytes = MinValue(omitted_heap_bytes, heap_bytes / Count()) * Count();
return fixed_row_bytes * Count() + heap_bytes - omitted_bytes;
}
void PartitionBoundaries(vector<idx_t> &boundaries) const override {
// a single key: no boundaries
Expand All @@ -2660,16 +2662,18 @@ class DirectPartitionGroup : public PartitionedCopyGroup {
private:
TupleDataCollection &data;
TupleDataChunkCursor cursor;
const idx_t row_width;
const idx_t fixed_row_bytes;
const idx_t omitted_heap_bytes;
const bool has_heap;
idx_t row_offset = 0;
bool started = false;
};

public:
DirectPartitionGroup(ClientContext &context, unique_ptr<TupleDataCollection> data_p,
DirectPartitionGroup(const PartitionedCopy &partitioned_copy, unique_ptr<TupleDataCollection> data_p,
vector<column_t> column_ids_p = {})
: context(context), data(std::move(data_p)), count(data->Count()), column_ids(std::move(column_ids_p)) {
: partitioned_copy(partitioned_copy), data(std::move(data_p)), count(data->Count()),
column_ids(std::move(column_ids_p)) {
if (column_ids.empty()) {
const auto column_count = data->GetLayout().ColumnCount();
for (column_t col_idx = 0; col_idx < column_count; col_idx++) {
Expand All @@ -2695,18 +2699,21 @@ class DirectPartitionGroup : public PartitionedCopyGroup {
throw InternalException("DirectPartitionGroup has no preparation steps");
}

unique_ptr<PartitionedCopyGroupCursor> InitializeCursor() const override {
return make_uniq<Cursor>(*data);
unique_ptr<PartitionedCopyGroupCursor> InitializeCursor(const CopyBatchSlicer &slicer) const override {
return make_uniq<Cursor>(*this, slicer);
}
unique_ptr<PartitionedCopyGroupScanState>
InitializeScan(const PartitionedCopyChunkPosition &position) const override;
void InitializeScanChunk(DataChunk &chunk) const override {
chunk.Initialize(Allocator::Get(context), scan_types);
chunk.Initialize(Allocator::Get(partitioned_copy.context), scan_types);
}
bool Scan(PartitionedCopyGroupScanState &state, DataChunk &chunk) const override;

private:
ClientContext &context;
idx_t OmittedHeapBytes() const;

private:
const PartitionedCopy &partitioned_copy;
unique_ptr<TupleDataCollection> data;
const idx_t count;
//! The columns read from the tuples, and their types
Expand Down Expand Up @@ -2736,8 +2743,8 @@ class OverflowSortGroup : public PartitionedCopyGroup {
idx_t end_block) override;
void FinishPreparation(idx_t step) override;

unique_ptr<PartitionedCopyGroupCursor> InitializeCursor() const override {
return sorted->InitializeCursor();
unique_ptr<PartitionedCopyGroupCursor> InitializeCursor(const CopyBatchSlicer &slicer) const override {
return sorted->InitializeCursor(slicer);
}
unique_ptr<PartitionedCopyGroupScanState>
InitializeScan(const PartitionedCopyChunkPosition &position) const override {
Expand Down Expand Up @@ -2852,26 +2859,57 @@ vector<unique_ptr<PartitionedCopyGroup>> IntegerPartitionInput::Finalize(ClientC
if (partition_idx == overflow_idx) {
groups.push_back(make_uniq<OverflowSortGroup>(*this, context, std::move(partition)));
} else {
groups.push_back(make_uniq<DirectPartitionGroup>(context, std::move(partition)));
groups.push_back(make_uniq<DirectPartitionGroup>(partitioned_copy, std::move(partition)));
}
}
return groups;
}

idx_t DirectPartitionGroup::OmittedHeapBytes() const {
const auto &op = partitioned_copy.op;
if (!op.batch_size_bytes.IsValid() || op.write_partition_columns) {
return 0;
}
vector<column_t> omitted_columns;
const auto &variable_columns = data->GetLayout().GetVariableColumns();
for (const auto &partition_column : op.partition_columns) {
const auto column_id = column_ids[partition_column];
if (std::find(variable_columns.begin(), variable_columns.end(), column_id) != variable_columns.end()) {
omitted_columns.push_back(column_id);
}
}
if (omitted_columns.empty()) {
return 0;
}

// Every row has the same partition key. Gather its variable columns once.
TupleDataChunkIterator iterator(*data, TupleDataPinProperties::UNPIN_AFTER_DONE, {0, 0}, true);
DataChunk key_chunk;
data->InitializeChunk(key_chunk, omitted_columns);
auto &chunk_state = iterator.GetChunkState();
const auto &selection = *FlatVector::IncrementalSelectionVector();
for (idx_t i = 0; i < omitted_columns.size(); i++) {
data->Gather(chunk_state.row_locations, selection, 1, omitted_columns[i], key_chunk.data[i], selection,
nullptr);
}
key_chunk.SetChildCardinality(1);
CopyBatchSlicer slicer(key_chunk.GetTypes(), op.batch_size_bytes);
return slicer.ComputeRowSizes(key_chunk).Values<idx_t>()[0].GetValue() - slicer.GetFixedRowBytes();
}

void DirectPartitionGroup::Cursor::RowSizes(vector<idx_t> &sizes) {
const auto chunk_rows = cursor.Count();
sizes.assign(chunk_rows, row_width);
sizes.assign(chunk_rows, fixed_row_bytes);
if (!has_heap) {
return;
}
// the stored per-row heap sizes, through the tuple iterator: no values are gathered
TupleDataChunkIterator iterator(data, TupleDataPinProperties::UNPIN_AFTER_DONE, cursor.Position(), true);
if (iterator.GetCurrentChunkCount() != chunk_rows) {
throw InternalException("Partitioned copy could not read a chunk to size its rows");
}
const auto heap_sizes = iterator.GetHeapSizes();
for (idx_t row = 0; row < chunk_rows; row++) {
sizes[row] += heap_sizes[row];
sizes[row] += heap_sizes[row] - MinValue(heap_sizes[row], omitted_heap_bytes);
}
}

Expand Down Expand Up @@ -2980,7 +3018,7 @@ vector<unique_ptr<PartitionedCopyGroup>> SortStrategyInput::Finalize(ClientConte
// a bin proven to hold a single key is read straight from its tuple data, like a direct partition
auto single_key = strategy->TryTakeSinglePartition(bin, *gstate.source);
if (single_key) {
groups[bin] = make_uniq<DirectPartitionGroup>(context, std::move(single_key), strategy->scan_ids);
groups[bin] = make_uniq<DirectPartitionGroup>(partitioned_copy, std::move(single_key), strategy->scan_ids);
} else {
groups[bin] = make_uniq<SortedBinGroup>(*this, gstate, vector<idx_t> {bin}, chunk_rows[bin].count);
}
Expand Down Expand Up @@ -3173,9 +3211,9 @@ void PartitionedCopyHashGroup::AddPlannedBatch(idx_t batch_state_idx, Partitione
planned_batches.push_back(batch);
}

//! Plans the batches of the group: one partition per batch, split by the row and byte limits of the COPY. Rows are
//! added by the chunk estimates until a run would cross the byte limit; from then on the chunk's exact row sizes are
//! used. Both limits go through the batch analyzer: like the Parquet writer and the delayed partition path, a batch
//! Plans the batches of the group: one partition per batch, split by the row and byte limits of the COPY. Cached
//! written-column estimates are refined near byte boundaries. Fixed-width rows need no scan. Both limits go through
//! the batch analyzer: like the Parquet writer and the delayed partition path, a batch
//! is closed once it reaches a limit, so it may exceed the row limit by less than one input vector.
void PartitionedCopyHashGroup::Plan() {
const auto &op = partitioned_copy.op;
Expand Down Expand Up @@ -3206,7 +3244,9 @@ void PartitionedCopyHashGroup::Plan() {

vector<idx_t> boundaries;
vector<idx_t> row_sizes;
auto cursor = group->InitializeCursor();
CopyBatchSlicer slicer(partitioned_copy.write_types, batch_size_bytes);
const auto fixed_row_size = slicer.FixedRowSize();
auto cursor = group->InitializeCursor(slicer);
while (cursor->Next()) {
const auto position = cursor->Position();
const auto chunk_rows = cursor->Count();
Expand All @@ -3217,7 +3257,9 @@ void PartitionedCopyHashGroup::Plan() {
begin = {position, 0};
begin_at_next_chunk = false;
}
const auto chunk_bytes = cursor->ByteEstimate();
const auto chunk_bytes = fixed_row_size.IsValid() ? chunk_rows * fixed_row_size.GetIndex()
: batch_size_bytes.IsValid() ? cursor->ByteEstimate()
: 0;
boundaries.clear();
cursor->PartitionBoundaries(boundaries);
boundaries.push_back(chunk_rows);
Expand All @@ -3234,16 +3276,20 @@ void PartitionedCopyHashGroup::Plan() {
idx_t run_rows = boundary - row;
if (!sized && batch_size_bytes.IsValid() && run_rows > 1 &&
batch_bytes + chunk_bytes * run_rows / chunk_rows >= batch_size_bytes.GetIndex()) {
// the run would cross the byte limit: use the exact row sizes for the rest of this chunk
cursor->RowSizes(row_sizes);
if (row_sizes.size() != chunk_rows) {
throw InternalException("Partitioned copy sized %llu rows of a chunk of %llu rows",
row_sizes.size(), chunk_rows);
// The run would cross the byte limit. Refine the remaining rows of this chunk.
if (fixed_row_size.IsValid()) {
row_sizes.assign(chunk_rows, fixed_row_size.GetIndex());
} else {
cursor->RowSizes(row_sizes);
if (row_sizes.size() != chunk_rows) {
throw InternalException("Partitioned copy sized %llu rows of a chunk of %llu rows",
row_sizes.size(), chunk_rows);
}
}
sized = true;
}
if (sized) {
// take rows by their exact size until a limit is reached
// take rows by their refined size until a limit is reached
while (run_rows) {
batch_bytes += row_sizes[row++];
++batch_rows;
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/src/function/table/version/pragma_version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DUCKDB_PATCH_VERSION
#define DUCKDB_PATCH_VERSION "1-alpha54043"
#define DUCKDB_PATCH_VERSION "1-alpha54527"
#endif
#ifndef DUCKDB_MINOR_VERSION
#define DUCKDB_MINOR_VERSION 0
Expand All @@ -8,10 +8,10 @@
#define DUCKDB_MAJOR_VERSION 2
#endif
#ifndef DUCKDB_VERSION
#define DUCKDB_VERSION "v2.0.1-alpha54043"
#define DUCKDB_VERSION "v2.0.1-alpha54527"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "29b26cb106"
#define DUCKDB_SOURCE_ID "a7e6fc10d2"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
Loading
Loading