1#include "duckdb/execution/operator/scan/physical_index_scan.hpp"
2
3#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
4#include "duckdb/transaction/transaction.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9class PhysicalIndexScanOperatorState : public PhysicalOperatorState {
10public:
11 PhysicalIndexScanOperatorState() : PhysicalOperatorState(nullptr), initialized(false) {
12 }
13
14 bool initialized;
15 TableIndexScanState scan_state;
16};
17
18void PhysicalIndexScan::GetChunkInternal(ClientContext &context, DataChunk &chunk, PhysicalOperatorState *state_) {
19 auto state = reinterpret_cast<PhysicalIndexScanOperatorState *>(state_);
20 if (column_ids.size() == 0) {
21 return;
22 }
23
24 auto &transaction = Transaction::GetTransaction(context);
25 if (!state->initialized) {
26 // initialize the scan state of the index
27 if (low_index && high_index) {
28 // two predicates
29 table.InitializeIndexScan(transaction, state->scan_state, index, low_value, low_expression_type, high_value,
30 high_expression_type, column_ids);
31 } else {
32 // single predicate
33 Value value;
34 ExpressionType type;
35 if (low_index) {
36 // > or >=
37 value = low_value;
38 type = low_expression_type;
39 } else if (high_index) {
40 // < or <=
41 value = high_value;
42 type = high_expression_type;
43 } else {
44 // equality
45 assert(equal_index);
46 value = equal_value;
47 type = ExpressionType::COMPARE_EQUAL;
48 }
49 table.InitializeIndexScan(transaction, state->scan_state, index, value, type, column_ids);
50 }
51 state->initialized = true;
52 }
53 // scan the index
54 table.IndexScan(transaction, chunk, state->scan_state);
55}
56
57string PhysicalIndexScan::ExtraRenderInformation() const {
58 return tableref.name + "[" + low_value.ToString() + "]";
59}
60
61unique_ptr<PhysicalOperatorState> PhysicalIndexScan::GetOperatorState() {
62 return make_unique<PhysicalIndexScanOperatorState>();
63}
64