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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if (EMSCRIPTEN)
# this solves by basically re-linking (at the moment the Wasm binary is actually produced)
# avro-c is vendored (third_party/avro-c), not a vcpkg port: its archive has to be named here too, or the
# side module is produced without it and imports every avro_* symbol from the host
set (DUCKDB_EXTENSION_AVRO_LINKED_LIBS "../../vcpkg_installed/wasm32-emscripten/lib/lib*.a" "$<TARGET_FILE:avro-static>")
set (DUCKDB_EXTENSION_AVRO_LINKED_LIBS "../../vcpkg_installed/${VCPKG_TARGET_TRIPLET}/lib/lib*.a" "$<TARGET_FILE:avro-static>")
endif()

# Disable avro-c examples, tests, docs
Expand Down
31 changes: 31 additions & 0 deletions src/avro_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,30 @@ WriteAvroLocalState::WriteAvroLocalState(FunctionData &bind_data_p) {
}
}

//! When a row does not fit in the rest of the datum buffer, avro-c writes the block so far into the file buffer on its
//! own. Keeps the file buffer large enough for that block, which is always empty at this point.
static void ReserveBlockSpace(WriteAvroGlobalState &global_state) {
//! Room for a compressed block that is larger than its input, plus the counts and the sync marker
auto needed = 2 * global_state.datum_buffer.GetCapacity() + WriteAvroGlobalState::BUFFER_SIZE;
auto &buffer = global_state.memory_buffer;
if (buffer.GetCapacity() < needed) {
buffer.Resize(NextPowerOfTwo(needed));
avro_writer_memory_set_dest(global_state.writer, (const char *)buffer.GetData(), buffer.GetCapacity());
}
}

//! Writes out a block avro-c wrote into the file buffer on its own, so it is not lost when the buffer is reused
static bool WriteBlockFromAvro(WriteAvroGlobalState &global_state) {
auto size = static_cast<idx_t>(avro_writer_tell(global_state.writer));
if (size == 0) {
return false;
}
auto &buffer = global_state.memory_buffer;
global_state.WriteData(buffer.GetData(), size);
avro_writer_memory_set_dest(global_state.writer, (const char *)buffer.GetData(), buffer.GetCapacity());
return true;
}

static void WriteAvroSink(ExecutionContext &context, FunctionData &bind_data_p, GlobalFunctionData &gstate_p,
LocalFunctionData &lstate_p, DataChunk &input) {
auto &global_state = gstate_p.Cast<WriteAvroGlobalState>();
Expand Down Expand Up @@ -1047,19 +1071,26 @@ static void WriteAvroSink(ExecutionContext &context, FunctionData &bind_data_p,
new_capacity += value_size;
datum_buffer.ResizeAndCopy(NextPowerOfTwo(new_capacity));
}
ReserveBlockSpace(global_state);
avro_writer_memory_set_dest_with_offset(global_state.datum_writer, (const char *)datum_buffer.GetData(),
datum_buffer.GetCapacity(), offset_in_datum_buffer);

int ret;
while ((ret = avro_file_writer_append_value(global_state.file_writer.get(), &local_state.row.value)) == ENOSPC) {
if (WriteBlockFromAvro(global_state)) {
//! The datum buffer was reset when avro-c wrote the block, so the row now starts at its beginning
offset_in_datum_buffer = 0;
}
auto current_capacity = datum_buffer.GetCapacity();
datum_buffer.ResizeAndCopy(NextPowerOfTwo(current_capacity * 2));
ReserveBlockSpace(global_state);
avro_writer_memory_set_dest_with_offset(global_state.datum_writer, (const char *)datum_buffer.GetData(),
datum_buffer.GetCapacity(), offset_in_datum_buffer);
}
if (ret) {
throw InvalidInputException(avro_strerror());
}
WriteBlockFromAvro(global_state);

offset_in_datum_buffer = avro_writer_tell(global_state.datum_writer);
avro_value_reset(&local_state.row.value);
Expand Down
52 changes: 8 additions & 44 deletions src/avro_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,51 +401,15 @@ static void TransformValue(avro_value *avro_val, const AvroType &avro_type, Vect
break;
}
case PhysicalType::INT128: {
int64_t upper_val = 0;
uint64_t lower_val = 0;

// Calculate how many bytes go into upper and lower parts
idx_t upper_bytes;
if (bytes_size == 8) {
int64_t lower_val_signed = 0;
for (idx_t i = 0; i < bytes_size; i++) {
int64_t raw_byte = raw[i];
lower_val_signed |= (raw_byte << ((7 - i) * 8));
}
upper_bytes = 0;
auto ret = hugeint_t(lower_val_signed);
FlatVector::GetDataMutable<hugeint_t>(target)[out_idx] = ret;
break;
} else {
upper_bytes = (bytes_size <= sizeof(uint64_t)) ? bytes_size : (bytes_size - sizeof(uint64_t));
}

// Read upper part (big-endian)
// TODO: upper part might be sign extended bti.
for (idx_t i = 0; i < upper_bytes; i++) {
upper_val = (upper_val << 8) | raw[i];
}

// Handle sign extension for negative numbers
if (bytes_size > 0 && (raw[0] & 0x80)) {
// Fill remaining bytes with 1s for negative numbers
if (upper_bytes < sizeof(int64_t)) {
// Create a mask with 1s in the upper bits that need to be filled
int64_t mask = ((int64_t)1 << ((sizeof(int64_t) - upper_bytes) * 8)) - 1;
mask = mask << (upper_bytes * 8);
upper_val |= mask;
}
}

// Read lower part if there are remaining bytes
if (bytes_size > sizeof(int64_t)) {
for (idx_t i = upper_bytes; i < bytes_size; i++) {
lower_val = (lower_val << 8) | raw[i];
}
// Sign-extend, then shift each big-endian byte in through the lower 64 bits
uint64_t upper_val = negative ? ~0ULL : 0;
uint64_t lower_val = negative ? ~0ULL : 0;
for (idx_t i = 0; i < bytes_size; i++) {
upper_val = (upper_val << 8) | (lower_val >> 56);
lower_val = (lower_val << 8) | raw[i];
}

auto ret = hugeint_t(upper_val, lower_val);
FlatVector::GetDataMutable<hugeint_t>(target)[out_idx] = ret;
FlatVector::GetDataMutable<hugeint_t>(target)[out_idx] =
hugeint_t(static_cast<int64_t>(upper_val), lower_val);
break;
}
default:
Expand Down
41 changes: 41 additions & 0 deletions test/sql/copy/block_boundary.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# name: test/sql/copy/block_boundary.test
# description: COPY ... TO (FORMAT AVRO) keeps every row when a row does not fit in the rest of the block buffer
# group: [copy]

require avro

statement ok
SET threads = 1;

statement ok
COPY (SELECT repeat('x', 1) AS s FROM range(8192)) TO '{TEST_DIR}/block_boundary_1.avro' (FORMAT AVRO);

query I
SELECT count(*) FROM read_avro('{TEST_DIR}/block_boundary_1.avro');
----
8192

statement ok
COPY (SELECT repeat('x', 8) AS s FROM range(8192)) TO '{TEST_DIR}/block_boundary_8.avro' (FORMAT AVRO);

query I
SELECT count(*) FROM read_avro('{TEST_DIR}/block_boundary_8.avro');
----
8192

statement ok
COPY (SELECT repeat('x', 31) AS s FROM range(8192)) TO '{TEST_DIR}/block_boundary_31.avro' (FORMAT AVRO);

query I
SELECT count(*) FROM read_avro('{TEST_DIR}/block_boundary_31.avro');
----
8192

# a partitioned write starts many small files, each crossing the same boundaries
statement ok
COPY (SELECT range AS id, md5(range::VARCHAR) AS s, range % 30 AS p FROM range(50000)) TO '{TEST_DIR}/block_boundary_partitioned' (FORMAT AVRO, PARTITION_BY (p));

query II
SELECT count(*), sum(id) FROM read_avro('{TEST_DIR}/block_boundary_partitioned/*/*');
----
50000 1249975000
Loading