1#include "duckdb/storage/table/persistent_segment.hpp"
2#include "duckdb/common/exception.hpp"
3#include "duckdb/common/types/vector.hpp"
4#include "duckdb/common/vector_operations/vector_operations.hpp"
5#include "duckdb/common/types/null_value.hpp"
6#include "duckdb/storage/checkpoint/table_data_writer.hpp"
7#include "duckdb/storage/meta_block_reader.hpp"
8
9#include "duckdb/storage/numeric_segment.hpp"
10#include "duckdb/storage/string_segment.hpp"
11
12using namespace duckdb;
13using namespace std;
14
15PersistentSegment::PersistentSegment(BufferManager &manager, block_id_t id, idx_t offset, TypeId type, idx_t start,
16 idx_t count, data_t stats_min[], data_t stats_max[])
17 : ColumnSegment(type, ColumnSegmentType::PERSISTENT, start, count, stats_min, stats_max), manager(manager),
18 block_id(id), offset(offset) {
19 assert(offset == 0);
20 if (type == TypeId::VARCHAR) {
21 data = make_unique<StringSegment>(manager, start, id);
22 data->max_vector_count = count / STANDARD_VECTOR_SIZE + (count % STANDARD_VECTOR_SIZE == 0 ? 0 : 1);
23 } else {
24 data = make_unique<NumericSegment>(manager, type, start, id);
25 }
26 data->tuple_count = count;
27}
28
29void PersistentSegment::InitializeScan(ColumnScanState &state) {
30 data->InitializeScan(state);
31}
32
33void PersistentSegment::Scan(Transaction &transaction, ColumnScanState &state, idx_t vector_index, Vector &result) {
34 data->Scan(transaction, state, vector_index, result);
35}
36
37void PersistentSegment::FilterScan(Transaction &transaction, ColumnScanState &state, Vector &result,
38 SelectionVector &sel, idx_t &approved_tuple_count) {
39 data->FilterScan(transaction, state, result, sel, approved_tuple_count);
40}
41
42void PersistentSegment::IndexScan(ColumnScanState &state, Vector &result) {
43 data->IndexScan(state, state.vector_index, result);
44}
45
46void PersistentSegment::Select(Transaction &transaction, ColumnScanState &state, Vector &result, SelectionVector &sel,
47 idx_t &approved_tuple_count, vector<TableFilter> &tableFilter) {
48 data->Select(transaction, result, tableFilter, sel, approved_tuple_count, state);
49}
50
51void PersistentSegment::Fetch(ColumnScanState &state, idx_t vector_index, Vector &result) {
52 data->Fetch(state, vector_index, result);
53}
54
55void PersistentSegment::FetchRow(ColumnFetchState &state, Transaction &transaction, row_t row_id, Vector &result,
56 idx_t result_idx) {
57 data->FetchRow(state, transaction, row_id - this->start, result, result_idx);
58}
59
60void PersistentSegment::Update(ColumnData &column_data, Transaction &transaction, Vector &updates, row_t *ids,
61 idx_t count) {
62 // update of persistent segment: check if the table has been updated before
63 if (block_id == data->block_id) {
64 // data has not been updated before! convert the segment from one that refers to an on-disk block to one that
65 // refers to a in-memory buffer
66 data->ToTemporary();
67 }
68 data->Update(column_data, stats, transaction, updates, ids, count, this->start);
69}
70