forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_am.cpp
More file actions
997 lines (862 loc) · 37.2 KB
/
Copy pathtable_am.cpp
File metadata and controls
997 lines (862 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
#ifdef __cplusplus
extern "C" {
#endif
// Must be first to avoid macro conflicts
#include <postgres.h>
#include <access/parallel.h>
#include <access/reloptions.h> // For relation options
#include <access/tableam.h>
#include <access/xact.h>
#include <catalog/namespace.h>
#include <catalog/storage.h>
#include <miscadmin.h>
#include <nodes/bitmapset.h> // For bitmap operations
#include <nodes/nodes.h> // For node types
#include <nodes/parsenodes.h> // For parse nodes
#include <storage/block.h>
#include <storage/relfilelocator.h>
#include <utils/builtins.h> // For text conversion functions
#include <utils/lsyscache.h>
#include <utils/rel.h>
#include <utils/relcache.h>
#include <utils/varlena.h> // For text functions
#ifdef __cplusplus
}
#endif
#include "table_am.hpp"
#include "exceptions.hpp"
#include "logger.hpp"
#include "memory_tracker.hpp"
#include "pg_deeplake.hpp"
#include "pg_version_compat.h"
#include "progress_utils.hpp"
#include "table_scan.hpp"
#include "table_storage.hpp"
#include "table_version.hpp"
#include <cstddef>
namespace {
static constexpr int64_t num_tuples_to_reset_memory_context = 10000;
// Extended TableScanDesc with embedded Deeplake scan data
struct DeeplakeScanData
{
TableScanDescData postgres_scan; // keep this first
pg::table_scan scan_state;
MemoryContext memory_context = nullptr;
// TID range scanning state
bool tid_range_scan_active = false;
int64_t min_tid = 0;
int64_t max_tid = 0;
int64_t current_tid = 0;
// Bitmap scanning state
bool bitmap_scan_active = false;
struct TBMIterateResult* current_tbmres = nullptr;
int32_t current_offset = InvalidOffsetNumber;
std::vector<int64_t> bitmap_row_numbers;
// Sample scanning state
bool sample_scan_active = false;
struct SampleScanState* current_sample_scanstate = nullptr;
bool print_progress = false;
pg::utils::progress_display progress_bar;
int64_t num_rescans = 0;
void reset()
{
tid_range_scan_active = false;
min_tid = 0;
max_tid = 0;
current_tid = 0;
bitmap_scan_active = false;
bitmap_row_numbers.clear();
current_tbmres = nullptr;
current_offset = InvalidOffsetNumber;
sample_scan_active = false;
current_sample_scanstate = nullptr;
}
DeeplakeScanData(pg::table_scan&& state)
: scan_state(std::move(state))
{
memory_context = AllocSetContextCreate(CurrentMemoryContext, "scan_context", ALLOCSET_SMALL_SIZES);
const auto num_rows = scan_state.get_table_data().num_rows();
print_progress = pg::print_progress_during_seq_scan && num_rows > 100000;
if (print_progress) {
progress_bar = pg::utils::progress_display(num_rows,
"Progress of sequential scan for table '" +
scan_state.get_table_data().get_table_name() + "' " +
"(" + std::to_string(num_rows) + " rows)");
}
}
DeeplakeScanData(const DeeplakeScanData&) = delete;
DeeplakeScanData& operator=(const DeeplakeScanData&) = delete;
~DeeplakeScanData()
{
if (memory_context != nullptr) {
MemoryContextDelete(memory_context);
}
}
};
inline DeeplakeScanData* get_scan_data(TableScanDesc scan)
{
return reinterpret_cast<DeeplakeScanData*>(scan);
}
struct DeeplakeIndexFetchData
{
IndexFetchTableData base;
pg::table_scan scan_state;
MemoryContext memory_context = nullptr;
DeeplakeIndexFetchData(pg::table_scan&& state)
: scan_state(std::move(state))
{
memory_context = AllocSetContextCreate(CurrentMemoryContext, "scan_context", ALLOCSET_SMALL_SIZES);
}
~DeeplakeIndexFetchData()
{
if (memory_context != nullptr) {
MemoryContextDelete(memory_context);
}
}
void reset()
{
scan_state.reset_scan();
MemoryContextReset(memory_context);
}
};
inline DeeplakeIndexFetchData* get_index_fetch_data(IndexFetchTableData* data)
{
return reinterpret_cast<DeeplakeIndexFetchData*>(data);
}
std::string get_qualified_table_name(Relation rel)
{
Oid nspid = RelationGetNamespace(rel);
char* nspname = get_namespace_name(nspid);
std::string qualified_name = std::string(nspname ? nspname : "public") + "." + RelationGetRelationName(rel);
if (nspname) {
pfree(nspname);
}
return qualified_name;
}
bool deeplake_relation_needs_toast_table(Relation)
{
return false;
}
uint64_t deeplake_relation_size(Relation rel, ForkNumber)
{
auto table_id = RelationGetRelid(rel);
if (pg::table_storage::instance().table_exists(table_id)) {
auto& table_data = pg::table_storage::instance().get_table_data(table_id);
auto total_bytes = heimdall::dataset_total_bytes(*table_data.get_read_only_dataset());
return total_bytes / BLCKSZ;
}
return BLCKSZ; // Default block size in bytes
}
void deeplake_index_validate_scan(Relation heap_rel,
Relation index_rel,
struct IndexInfo* index_info,
Snapshot snapshot,
struct ValidateIndexState* state)
{
// No-op for now, we'll need to implement proper index validation later
}
void deeplake_estimate_rel_size(Relation rel,
int32_t* attr_widths,
BlockNumber* pages,
double* tuples,
double* allvisfrac)
{
auto table_id = RelationGetRelid(rel);
if (pg::table_storage::instance().table_exists(table_id)) {
auto& table_data = pg::table_storage::instance().get_table_data(table_id);
if (tuples != nullptr) {
*tuples = table_data.num_rows();
}
if (allvisfrac != nullptr) {
*allvisfrac = 1.0; // Assume all tuples are visible
}
auto avg_row_width = 0;
if (attr_widths != nullptr) {
for (int32_t i = 0; i < table_data.num_columns(); ++i) {
attr_widths[i] = pg::utils::get_column_width(table_data.get_base_atttypid(i), table_data.get_atttypmod(i));
attr_widths[i] = std::max(8, attr_widths[i]);
avg_row_width += attr_widths[i];
}
avg_row_width /= table_data.num_columns();
}
if (pages != nullptr) {
constexpr uint32_t min_pages = 1;
const uint32_t num_blocks = std::ceil(table_data.num_rows() / 65536.0);
*pages = std::max(min_pages, num_blocks);
}
return;
}
// Conservative estimates
if (pages != nullptr) {
*pages = 10;
}
if (tuples != nullptr) {
*tuples = 1000; // Assume small table by default
}
if (allvisfrac != nullptr) {
*allvisfrac = 0.9;
}
}
bool deeplake_scan_analyze_next_tuple(TableScanDesc scan,
TransactionId OldestXmin,
double* liverows,
double* deadrows,
TupleTableSlot* slot)
{
CHECK_FOR_INTERRUPTS();
DeeplakeScanData* scan_data = get_scan_data(scan);
if (scan_data == nullptr) {
return false;
}
// Try to fetch next tuple from your columnar storage
if (!scan_data->scan_state.get_next_tuple(slot)) {
return false; // no more tuples
}
/*
* For columnar DeepLake we don’t track tuple visibility like heap.
* So treat everything as live.
*/
if (liverows) {
*liverows += 1.0;
}
return true;
}
#if PG_VERSION_NUM >= PG_VERSION_NUM_17
bool deeplake_scan_analyze_next_block(TableScanDesc scan, ReadStream* stream)
{
return false;
}
#endif
double deeplake_index_build_range_scan(Relation heap_rel,
Relation index_rel,
struct IndexInfo* index_info,
bool allow_sync,
bool anyvisible,
bool progress,
BlockNumber start_blockno,
BlockNumber numblocks,
IndexBuildCallback callback,
void* callback_state,
TableScanDesc scan)
{
const int32_t nkeys = index_info->ii_NumIndexKeyAttrs;
AttrNumber* indexkeys = index_info->ii_IndexAttrNumbers;
const auto table_id = RelationGetRelid(heap_rel);
auto& td = pg::table_storage::instance().get_table_data(table_id);
for (int32_t i = 0; i < nkeys; ++i) {
int32_t attnum = indexkeys[i] - 1;
if (attnum >= 0 && !td.column_has_streamer(attnum) && td.can_stream_column(attnum)) {
td.create_streamer(attnum, -1);
}
}
std::vector<Datum> values(nkeys, 0);
std::vector<uint8_t> nulls(nkeys, 0);
pg::table_scan tscan(table_id, false, false);
const auto num_rows = td.num_rows();
ItemPointerData tid;
for (auto row = 0; row < num_rows; ++row) {
auto [block_number, offset_number] = pg::utils::row_number_to_tid(row);
ItemPointerSet(&tid, block_number, offset_number);
for (int32_t i = 0; i < nkeys; ++i) {
int32_t attnum = indexkeys[i] - 1;
if (attnum < 0) [[unlikely]] {
nulls[i] = true;
values[i] = 0;
} else [[likely]] {
auto [value, null] = tscan.get_datum(attnum, row);
values[i] = value;
nulls[i] = null;
}
}
callback(index_rel, &tid, values.data(), reinterpret_cast<bool*>(nulls.data()), true, callback_state);
}
return static_cast<double>(num_rows);
}
struct DeeplakeParallelScanDesc
{
ParallelTableScanDescData base;
bool is_initialized = false;
void reset()
{
is_initialized = false;
}
};
Size parallelscan_initialize(Relation rel, ParallelTableScanDesc pscan)
{
if (!pg::use_parallel_workers) {
return 0;
}
return sizeof(DeeplakeParallelScanDesc);
}
Size parallelscan_estimate(Relation rel)
{
if (!pg::use_parallel_workers) {
return 0;
}
return sizeof(DeeplakeParallelScanDesc);
}
void parallelscan_reinitialize(Relation rel, ParallelTableScanDesc pscan)
{
// Reinitialize parallel scan state
auto custom_pscan = (DeeplakeParallelScanDesc*)pscan;
custom_pscan->reset();
}
TM_Result tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
TupleTableSlot* slot, CommandId cid, LockTupleMode mode,
LockWaitPolicy wait_policy, uint8_t flags, TM_FailureData* tmfd)
{
return TM_Ok; // Just allow the lock for now
}
bool tuple_fetch_row_version(Relation relation,
ItemPointer tid,
Snapshot snapshot,
TupleTableSlot* slot)
{
CHECK_FOR_INTERRUPTS();
// Switch to the slot's memory context
pg::utils::memory_context_switcher context_switcher(slot->tts_mcxt);
// Fetch the tuple using the existing table storage mechanism
if (!pg::table_storage::instance().fetch_tuple(RelationGetRelid(relation), tid, slot)) {
return false;
}
ExecStoreVirtualTuple(slot);
return true;
}
bool tuple_tid_valid(TableScanDesc scan, ItemPointer tid)
{
OffsetNumber off = ItemPointerGetOffsetNumber(tid);
return off > 0 && off <= pg::DEEPLAKE_TUPLES_PER_BLOCK;
}
bool tuple_satisfies_snapshot(Relation relation,
TupleTableSlot* slot,
Snapshot snapshot)
{
// Check if tuple satisfies snapshot
return true;
}
void tuple_get_latest_tid(TableScanDesc scan, ItemPointer tid)
{
auto table_id = RelationGetRelid(scan->rs_rd);
auto& td = pg::table_storage::instance().get_table_data(table_id);
auto [block_number, offset_number] = pg::utils::row_number_to_tid(td.num_total_rows());
ItemPointerSet(tid, block_number, offset_number);
}
void convert_schema(TupleDesc tupdesc)
{
for (auto i = 0; i < tupdesc->natts; ++i) {
Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
if (attr->atttypid == NUMERICOID && pg::treat_numeric_as_double) {
auto typmod = attr->atttypmod;
auto precision = -1;
auto scale = -1;
if (typmod >= VARHDRSZ) {
precision = ((typmod - VARHDRSZ) >> 16) & 0xffff;
scale = (typmod - VARHDRSZ) & 0xffff;
}
// Convert to FLOAT8
attr->atttypid = FLOAT8OID;
attr->attlen = sizeof(float8); // 8 bytes
attr->attbyval = true; // pass by value
attr->attalign = TYPALIGN_DOUBLE; // 'd' - double alignment
attr->atttypmod = -1; // no type modifier for FLOAT8
attr->attndims = 0; // not an array
const char* column_name = NameStr(attr->attname);
if (precision >= 0) {
elog(WARNING, "Column '%s' converted from NUMERIC(%d,%d) to FLOAT8 - precision may be lost", column_name, precision, scale);
} else {
elog(WARNING, "Column '%s' converted from NUMERIC to FLOAT8 - precision may be lost", column_name);
}
} else if (attr->atttypid == CHAROID || attr->atttypid == BPCHAROID) {
// Convert CHAR and BPCHAR to VARCHAR
attr->atttypid = VARCHAROID;
attr->attlen = -1; // variable length
attr->attbyval = false; // pass by reference
attr->attalign = TYPALIGN_INT; // 'i' - int4 alignment
attr->attstorage = TYPSTORAGE_EXTENDED;
const char* column_name = NameStr(attr->attname);
elog(WARNING, "Column '%s' converted from CHAR/BPCHAR to VARCHAR as no fixed length string is supported", column_name);
}
Oid base_typeid = pg::utils::get_base_type(attr->atttypid);
// For domain types over arrays, adjust attndims since PostgreSQL doesn't preserve it
const bool is_domain_over_array = (base_typeid != attr->atttypid && type_is_array(base_typeid));
if (is_domain_over_array && attr->attndims == 0) {
// Extract dimensionality from domain's CHECK constraint
// Query pg_constraint and use pg_get_constraintdef() to get the constraint text
auto query_str = fmt::format("SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE contypid = {} AND contype = 'c'", attr->atttypid);
pg::utils::spi_connector connector;
bool found_constraint = false;
if (SPI_execute(query_str.c_str(), true, 0) == SPI_OK_SELECT && SPI_processed > 0) {
// Parse the constraint expression to find array_ndims
for (uint64_t row = 0; row < SPI_processed; ++row) {
HeapTuple tuple = SPI_tuptable->vals[row];
bool isnull = false;
Datum condef_datum = SPI_getbinval(tuple, SPI_tuptable->tupdesc, 1, &isnull);
if (isnull) {
continue;
}
const char* condef = TextDatumGetCString(condef_datum);
// Look for patterns like "array_ndims(...) = N" or "CHECK (array_ndims(...) = N)"
const char* ndims_pos = strstr(condef, "array_ndims");
if (ndims_pos != nullptr) {
const char* eq_pos = strchr(ndims_pos, '=');
if (eq_pos != nullptr) {
eq_pos++;
while (*eq_pos == ' ' || *eq_pos == '\t') eq_pos++;
// Parse the integer (atoi handles leading whitespace and stops at non-digit)
char* endptr;
auto ndims = strtol(eq_pos, &endptr, 10);
// Verify we actually parsed a number and it's reasonable
if (endptr != eq_pos && ndims > 0 && ndims <= 2) {
attr->attndims = static_cast<int32_t>(ndims);
found_constraint = true;
break;
}
}
}
}
}
// If no array_ndims constraint found, require user to add one
if (!found_constraint) {
const char* tname = format_type_with_typemod(attr->atttypid, attr->atttypmod);
const char* column_name = NameStr(attr->attname);
elog(ERROR,
"Column '%s': Domain type '%s' over array type lacks array_ndims constraint.\n"
"Please add a CHECK constraint, e.g.:\n"
" ALTER DOMAIN %s ADD CHECK (array_ndims(VALUE) = 1);",
column_name, tname, tname);
}
}
}
}
} // unnamed namespace
namespace pg {
TableAmRoutine deeplake_table_am_routine::routine;
void deeplake_table_am_routine::initialize()
{
// Set the routine properties
routine.type = T_TableAmRoutine;
// Set up the slot callbacks
routine.slot_callbacks = slot_callbacks;
// Scan related callbacks
routine.scan_begin = scan_begin;
routine.scan_rescan = scan_rescan;
routine.scan_end = scan_end;
routine.scan_getnextslot = scan_getnextslot;
// TID range scanning callbacks
routine.scan_set_tidrange = scan_set_tidrange;
routine.scan_getnextslot_tidrange = scan_getnextslot_tidrange;
// Index scan related callbacks
routine.index_fetch_begin = begin_index_fetch;
routine.index_fetch_end = end_index_fetch;
routine.index_fetch_tuple = index_fetch_tuple;
routine.index_fetch_reset = index_fetch_reset;
// Table operations
routine.tuple_insert = tuple_insert;
routine.multi_insert = multi_insert;
routine.tuple_delete = tuple_delete;
routine.tuple_update = tuple_update;
routine.tuple_lock = tuple_lock;
routine.tuple_fetch_row_version = tuple_fetch_row_version;
routine.tuple_get_latest_tid = tuple_get_latest_tid;
routine.tuple_tid_valid = tuple_tid_valid;
routine.tuple_satisfies_snapshot = tuple_satisfies_snapshot;
// Table creation and maintenance
routine.relation_set_new_filelocator = relation_set_new_node;
routine.relation_nontransactional_truncate = relation_nontransactional_truncate;
// TOAST table support - disable TOAST tables completely
routine.relation_needs_toast_table = deeplake_relation_needs_toast_table;
// Index support
routine.index_build_range_scan = deeplake_index_build_range_scan;
routine.index_validate_scan = deeplake_index_validate_scan;
// Optional scan methods
///routine.scan_bitmap_next_block = scan_bitmap_next_block;
#if PG_VERSION_NUM >= PG_VERSION_NUM_18
routine.scan_bitmap_next_tuple = scan_bitmap_next_tuple;
#endif
routine.relation_size = deeplake_relation_size;
routine.relation_estimate_size = deeplake_estimate_rel_size;
routine.scan_analyze_next_tuple = deeplake_scan_analyze_next_tuple;
#if PG_VERSION_NUM >= PG_VERSION_NUM_17
routine.scan_analyze_next_block = deeplake_scan_analyze_next_block;
#endif
routine.parallelscan_initialize = parallelscan_initialize;
routine.parallelscan_estimate = parallelscan_estimate;
routine.parallelscan_reinitialize = parallelscan_reinitialize;
}
const TupleTableSlotOps* deeplake_table_am_routine::slot_callbacks(Relation rel)
{
// TODO: check if we use virtual table slot ops will be better
return &TTSOpsHeapTuple;
}
TableScanDesc deeplake_table_am_routine::scan_begin(Relation relation,
Snapshot snapshot,
int32_t nkeys,
struct ScanKeyData* key,
ParallelTableScanDesc parallel_scan,
uint32_t flags)
{
DeeplakeScanData* extended_scan = (DeeplakeScanData*)palloc0(sizeof(DeeplakeScanData));
auto table_id = RelationGetRelid(relation);
bool is_parallel = (pg::use_parallel_workers && parallel_scan != nullptr);
// Initialize extended structure with embedded scan data
new (extended_scan) DeeplakeScanData(table_scan(table_id, is_parallel, query_info::current().receiver_registered()));
const_cast<pg::table_data&>(extended_scan->scan_state.get_table_data()).refresh();
TableScanDesc scan_desc = &extended_scan->postgres_scan;
scan_desc->rs_rd = relation;
scan_desc->rs_snapshot = snapshot;
scan_desc->rs_nkeys = nkeys;
scan_desc->rs_key = key;
scan_desc->rs_flags = flags;
scan_desc->rs_parallel = parallel_scan;
if (parallel_scan != nullptr) {
DeeplakeParallelScanDesc* custom_pscan = (DeeplakeParallelScanDesc*)parallel_scan;
if (IsParallelWorker()) {
custom_pscan->is_initialized = true;
}
}
auto& td = table_storage::instance().get_table_data(table_id);
if (!pg::query_info::current().is_count_star() && td.is_star_selected()) {
for (auto i = 0; i < td.num_columns(); ++i) {
if (!td.is_column_requested(i)) {
td.set_column_requested(i, true);
if (td.can_stream_column(i)) {
td.create_streamer(i, -1);
}
}
}
}
if (nkeys > 0) {
extended_scan->scan_state.nkeys = nkeys;
// copy ScanKeyData because Postgres only gave us a pointer
extended_scan->scan_state.keys = (ScanKeyData*) palloc(sizeof(ScanKeyData) * nkeys);
std::memcpy(extended_scan->scan_state.keys, key, sizeof(ScanKeyData) * nkeys);
}
return scan_desc;
}
void deeplake_table_am_routine::scan_rescan(TableScanDesc scan, struct ScanKeyData* key,
bool set_params, bool allow_strat,
bool allow_sync, bool allow_pagemode)
{
DeeplakeScanData* scan_data = get_scan_data(scan);
if (scan_data) {
scan_data->scan_state.set_current_position(0);
MemoryContextReset(scan_data->memory_context);
scan_data->reset();
if (scan_data->print_progress) [[unlikely]] {
++scan_data->num_rescans;
std::string pre_msg = "Progress of sequential scan for table '" +
scan_data->scan_state.get_table_data().get_table_name() + "'" +
" (rescan " + std::to_string(scan_data->num_rescans) + ")";
scan_data->progress_bar.restart(scan_data->scan_state.get_table_data().num_rows(), std::move(pre_msg));
}
}
}
void deeplake_table_am_routine::scan_end(TableScanDesc scan)
{
DeeplakeScanData* extended_scan = get_scan_data(scan);
extended_scan->~DeeplakeScanData();
pfree(extended_scan);
}
bool deeplake_table_am_routine::scan_getnextslot(TableScanDesc scan, ScanDirection direction, TupleTableSlot* slot)
{
CHECK_FOR_INTERRUPTS();
DeeplakeScanData* scan_data = get_scan_data(scan);
// Reset memory context to prevent unbounded growth
if (scan_data->scan_state.get_current_position() % num_tuples_to_reset_memory_context == 0) {
MemoryContextReset(scan_data->memory_context);
}
// Switch to the dedicated memory context for this scan
pg::utils::memory_context_switcher context_switcher(scan_data->memory_context);
if (scan_data->print_progress) [[unlikely]] {
++scan_data->progress_bar;
}
return scan_data->scan_state.get_next_tuple(slot);
}
void deeplake_table_am_routine::scan_set_tidrange(TableScanDesc scan, ItemPointer mintid, ItemPointer maxtid)
{
CHECK_FOR_INTERRUPTS();
DeeplakeScanData* scan_data = get_scan_data(scan);
scan_data->min_tid = utils::tid_to_row_number(mintid);
scan_data->max_tid = utils::tid_to_row_number(maxtid);
scan_data->current_tid = utils::tid_to_row_number(mintid);
if (scan_data->min_tid > scan_data->max_tid ||
scan_data->current_tid > scan_data->max_tid ||
scan_data->current_tid < scan_data->min_tid) {
return;
}
scan_data->tid_range_scan_active = true;
// Reset the scan state to start from the beginning of the range
scan_data->scan_state.set_current_position(scan_data->current_tid);
MemoryContextReset(scan_data->memory_context);
}
bool deeplake_table_am_routine::scan_getnextslot_tidrange(TableScanDesc scan, ScanDirection direction, TupleTableSlot* slot)
{
CHECK_FOR_INTERRUPTS();
DeeplakeScanData* scan_data = get_scan_data(scan);
if (!scan_data || !scan_data->tid_range_scan_active) {
return false;
}
const auto current = scan_data->scan_state.get_current_position();
if (current > scan_data->max_tid || current < scan_data->min_tid) {
return false;
}
if (current % num_tuples_to_reset_memory_context == 0) {
MemoryContextReset(scan_data->memory_context);
}
// Switch to the dedicated memory context for this scan
pg::utils::memory_context_switcher context_switcher(scan_data->memory_context);
return scan_data->scan_state.get_next_tuple(slot);
}
#if PG_VERSION_NUM >= PG_VERSION_NUM_18
bool deeplake_table_am_routine::scan_bitmap_next_tuple(TableScanDesc scan, TupleTableSlot* slot, bool* recheck, uint64* lossy_pages, uint64* exact_pages)
{
CHECK_FOR_INTERRUPTS();
DeeplakeScanData* scan_data = get_scan_data(scan);
if (!scan_data->bitmap_scan_active) {
scan_data->bitmap_scan_active = true;
scan_data->current_offset = 0;
*lossy_pages = 0;
*exact_pages = 0;
// Get the merged TBMIterator from scan descriptor
TBMIterator* iter = &scan->st.rs_tbmiterator;
TBMIterateResult tbmres;
while (tbm_iterate(iter, &tbmres)) {
if (tbmres.lossy) {
// Lossy page - all tuples in this block
int64_t block_start = static_cast<int64_t>(tbmres.blockno) * pg::DEEPLAKE_TUPLES_PER_BLOCK;
int64_t block_end = block_start + pg::DEEPLAKE_TUPLES_PER_BLOCK;
for (int64_t row = block_start; row < block_end; ++row) {
scan_data->bitmap_row_numbers.push_back(row);
}
++(*lossy_pages);
} else {
// Exact tuples - extract offsets
OffsetNumber offsets[TBM_MAX_TUPLES_PER_PAGE];
int32_t ntuples = tbm_extract_page_tuple(&tbmres, offsets, TBM_MAX_TUPLES_PER_PAGE);
for (int32_t i = 0; i < ntuples; i++) {
// Convert (block, offset) to row number
ItemPointerData tid;
ItemPointerSet(&tid, tbmres.blockno, offsets[i]);
int64_t row_num = pg::utils::tid_to_row_number(&tid);
scan_data->bitmap_row_numbers.push_back(row_num);
}
++(*exact_pages);
}
}
}
if (scan_data->current_offset >= scan_data->bitmap_row_numbers.size()) {
return false; // No more tuples
}
*recheck = false;
// Get next row number
int64_t row_num = scan_data->bitmap_row_numbers[scan_data->current_offset++];
scan_data->scan_state.set_current_position(row_num);
return scan_data->scan_state.get_next_tuple(slot);
}
#endif
bool deeplake_table_am_routine::scan_sample_next_block(TableScanDesc scan, struct SampleScanState* scanstate)
{
DeeplakeScanData* scan_data = get_scan_data(scan);
if (!scan_data || scan_data->sample_scan_active) { // behaves as a single-block
return false;
}
scan_data->current_sample_scanstate = scanstate;
scan_data->sample_scan_active = true;
return true;
}
bool deeplake_table_am_routine::scan_sample_next_tuple(TableScanDesc scan, struct SampleScanState* scanstate, TupleTableSlot* slot)
{
CHECK_FOR_INTERRUPTS();
DeeplakeScanData* scan_data = get_scan_data(scan);
if (!scan_data || !scan_data->sample_scan_active) {
return false;
}
// Reset memory context to prevent unbounded growth
if (scan_data->scan_state.get_current_position() % num_tuples_to_reset_memory_context == 0) {
MemoryContextReset(scan_data->memory_context);
}
// Switch to the dedicated memory context for this scan
pg::utils::memory_context_switcher context_switcher(scan_data->memory_context);
double sample_fraction = 0.1; // default fallback
if (scan_data->current_sample_scanstate && scan_data->current_sample_scanstate->tsm_state) {
// If your TAM stores the fraction in tsm_state, cast and read it here
struct SampleState { double fraction; };
SampleState* sampler = (SampleState*)scan_data->current_sample_scanstate->tsm_state;
sample_fraction = sampler->fraction;
}
while (scan_data->scan_state.get_next_tuple(slot)) {
// random fraction, should come from scanstate->tsm_state
const double random_value = (double)random() / RAND_MAX;
if (random_value <= sample_fraction) {
return true;
}
}
return false;
}
IndexFetchTableData* deeplake_table_am_routine::begin_index_fetch(Relation rel)
{
DeeplakeIndexFetchData* idx_scan = (DeeplakeIndexFetchData*)palloc0(sizeof(DeeplakeIndexFetchData));
new (idx_scan) DeeplakeIndexFetchData(table_scan(RelationGetRelid(rel), false, query_info::current().receiver_registered()));
idx_scan->base.rel = rel;
return &idx_scan->base;
}
void deeplake_table_am_routine::index_fetch_reset(IndexFetchTableData* data)
{
DeeplakeIndexFetchData* idx_scan = get_index_fetch_data(data);
idx_scan->reset();
}
void deeplake_table_am_routine::end_index_fetch(IndexFetchTableData* data)
{
DeeplakeIndexFetchData* idx_scan = get_index_fetch_data(data);
idx_scan->~DeeplakeIndexFetchData();
pfree(idx_scan);
}
bool deeplake_table_am_routine::index_fetch_tuple(struct IndexFetchTableData* scan,
ItemPointer tid,
Snapshot snapshot,
TupleTableSlot* slot,
bool* call_again,
bool* all_dead)
{
CHECK_FOR_INTERRUPTS();
DeeplakeIndexFetchData* idx_scan = get_index_fetch_data(scan);
if (idx_scan->scan_state.get_current_position() % num_tuples_to_reset_memory_context == 0) {
MemoryContextReset(idx_scan->memory_context);
}
pg::utils::memory_context_switcher context_switcher(idx_scan->memory_context);
idx_scan->scan_state.set_current_position(utils::tid_to_row_number(tid));
if (!idx_scan->scan_state.get_next_tuple(slot)) {
*all_dead = true;
return false;
}
*call_again = false;
*all_dead = false;
return true;
}
void deeplake_table_am_routine::tuple_insert(Relation rel,
TupleTableSlot* slot,
CommandId cid,
int32_t options,
struct BulkInsertStateData* bistate)
{
const auto table_id = RelationGetRelid(rel);
const auto& table_data = table_storage::instance().get_table_data(table_id);
const auto row_number = table_data.num_total_rows();
table_storage::instance().insert_slot(table_id, slot);
const auto [block_number, offset_number] = utils::row_number_to_tid(row_number);
ItemPointerSet(&slot->tts_tid, block_number, offset_number);
// Increment version to notify other backends
table_version_tracker::increment_version(table_id);
}
void deeplake_table_am_routine::multi_insert(Relation rel,
TupleTableSlot** slots,
int32_t nslots,
CommandId cid,
int32_t options,
struct BulkInsertStateData* bistate)
{
// Check PostgreSQL memory limits before multi-insert
memory_tracker::check_memory_limit();
const auto table_id = RelationGetRelid(rel);
const auto& table_data = table_storage::instance().get_table_data(table_id);
const auto row_number = table_data.num_total_rows();
try {
table_storage::instance().insert_slots(table_id, nslots, slots);
} catch (const std::exception& e) {
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Failed to insert tuples: %s", e.what()),
errdetail("Table name: %s, Dataset path: %s",
get_qualified_table_name(rel).c_str(),
pg::table_options::current().dataset_path().c_str()),
errhint("Check if the dataset path is accessible and has proper permissions")));
}
for (auto i = 0; i < nslots; ++i) {
const auto [block_number, offset_number] = utils::row_number_to_tid(row_number + i);
ItemPointerSet(&slots[i]->tts_tid, block_number, offset_number);
}
// Increment version to notify other backends
table_version_tracker::increment_version(table_id);
}
TM_Result deeplake_table_am_routine::tuple_delete(Relation rel,
ItemPointer tid,
CommandId cid,
Snapshot snapshot,
Snapshot crosscheck,
bool wait,
TM_FailureData* tmfd,
bool changingPart)
{
const auto table_id = RelationGetRelid(rel);
// Delete the tuple from our storage
try {
if (!table_storage::instance().delete_tuple(table_id, tid)) {
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Failed to delete tuple")));
}
} catch (const std::exception& e) {
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Failed to delete tuple: %s", e.what())));
}
// Increment version to notify other backends
table_version_tracker::increment_version(table_id);
return TM_Ok;
}
TM_Result deeplake_table_am_routine::tuple_update(Relation rel,
ItemPointer otid,
TupleTableSlot* slot,
CommandId cid,
Snapshot snapshot,
Snapshot crosscheck,
bool wait,
TM_FailureData* tmfd,
LockTupleMode* lockmode,
TU_UpdateIndexes* update_indexes)
{
const auto table_id = RelationGetRelid(rel);
// Convert slot to HeapTuple
HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, nullptr);
if (tuple == nullptr) {
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Failed to fetch tuple from slot")));
}
// Update the tuple in our storage
try {
if (!table_storage::instance().update_tuple(table_id, otid, tuple)) {
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Failed to update tuple")));
}
} catch (const std::exception& e) {
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Failed to update tuple: %s", e.what())));
}
// Increment version to notify other backends
table_version_tracker::increment_version(table_id);
return TM_Ok;
}
void deeplake_table_am_routine::relation_set_new_node(Relation rel,
const RelFileLocator* newrnode,
char persistence,
TransactionId* freezeXid,
MultiXactId* minmulti)
{
// Get the schema-qualified table name
const std::string table_name = get_qualified_table_name(rel);
// Get the tuple descriptor
TupleDesc tupdesc = RelationGetDescr(rel);
if (tupdesc == nullptr) {
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("could not get tuple descriptor for relation \"%s\"", RelationGetRelationName(rel))));
}
convert_schema(tupdesc);
try {
table_storage::instance().create_table(table_name, RelationGetRelid(rel), tupdesc);
} catch (const std::exception& e) {
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("failed to create table storage: %s", e.what()),
errdetail("Table name: %s, Dataset path: %s",
table_name.c_str(),
pg::table_options::current().dataset_path().c_str()),
errhint("Check if the dataset path is accessible and has proper permissions")));
}
}
void deeplake_table_am_routine::relation_nontransactional_truncate(Relation rel)
{
return;
}
} // namespace pg