be/src/format_v2/table/adbc_reader.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <arrow/c/abi.h> |
21 | | #include <arrow/record_batch.h> |
22 | | #include <cctz/time_zone.h> |
23 | | |
24 | | #include <cstddef> |
25 | | #include <functional> |
26 | | #include <memory> |
27 | | #include <string> |
28 | | #include <unordered_map> |
29 | | #include <vector> |
30 | | |
31 | | #include "common/status.h" |
32 | | #include "format_v2/file_reader.h" |
33 | | #include "format_v2/table_reader.h" |
34 | | #include "gen_cpp/PlanNodes_types.h" |
35 | | |
36 | | namespace doris { |
37 | | class Block; |
38 | | class RuntimeProfile; |
39 | | class RuntimeState; |
40 | | class SlotDescriptor; |
41 | | } // namespace doris |
42 | | |
43 | | namespace doris::format::adbc { |
44 | | |
45 | | // Replaces *stream with one that delegates to it and, on release, clears its own release callback |
46 | | // the way the Arrow C data interface requires. *stream is left released, as a move leaves it. |
47 | | // |
48 | | // Not a nicety: Arrow C++ aborts the process (ArrowArrayStreamRelease's assertion) when a release |
49 | | // callback returns without clearing itself, and the Flight SQL driver's stream does exactly that -- |
50 | | // observed, and the reason a scan used to take the BE down. The ADBC driver manager's own wrapper |
51 | | // hides the flaw, but this connector loads drivers through the registry that owns their lifetime and |
52 | | // so calls their entry points directly. Applied to every driver on purpose: any of them may have the |
53 | | // same flaw, and a third-party driver's bug must not be able to abort the BE. |
54 | | void enforce_stream_release_contract(ArrowArrayStream* stream); |
55 | | |
56 | | // Small abstraction around the ADBC C API so the block materialization path stays unit-testable |
57 | | // without a live database. Production uses the real ADBC stream; tests can drive the same path from |
58 | | // plain RecordBatches. Mirrors RemoteDorisStream deliberately -- the two readers differ only in |
59 | | // where the Arrow stream comes from. |
60 | | class AdbcStream { |
61 | | public: |
62 | 10 | virtual ~AdbcStream() = default; |
63 | | // Sets *batch to nullptr at end of stream. |
64 | | virtual Status next(std::shared_ptr<arrow::RecordBatch>* batch) = 0; |
65 | | virtual Status close() = 0; |
66 | | }; |
67 | | |
68 | | using AdbcStreamFactory = |
69 | | std::function<Status(const TFileRangeDesc&, std::unique_ptr<AdbcStream>*)>; |
70 | | |
71 | | class AdbcFileReader final : public FileReader { |
72 | | public: |
73 | | AdbcFileReader(std::shared_ptr<io::FileSystemProperties>& system_properties, |
74 | | std::unique_ptr<io::FileDescription>& file_description, |
75 | | std::shared_ptr<io::IOContext> io_ctx, RuntimeProfile* profile, |
76 | | const TFileRangeDesc& range, const std::vector<SlotDescriptor*>& file_slot_descs, |
77 | | AdbcStreamFactory stream_factory = {}); |
78 | | ~AdbcFileReader() override; |
79 | | |
80 | | Status init(RuntimeState* state) override; |
81 | | Status get_schema(std::vector<ColumnDefinition>* file_schema) const override; |
82 | | Status open(std::shared_ptr<FileScanRequest> request) override; |
83 | | Status get_block(Block* file_block, size_t* rows, bool* eof) override; |
84 | | Status close() override; |
85 | | |
86 | | private: |
87 | | void _init_profile() override; |
88 | | Status _open_stream(); |
89 | | Status _materialize_record_batch(const arrow::RecordBatch& batch, Block* file_block, |
90 | | size_t* rows) const; |
91 | | // Takes the already-normalized array rather than the batch column: third-party drivers emit |
92 | | // Arrow variants the serdes reject, so normalization has to happen before this point. |
93 | | Status _materialize_arrow_column(const std::string& column_name, |
94 | | const std::shared_ptr<arrow::Array>& array, int64_t num_rows, |
95 | | LocalColumnId file_column_id, const LocalIndex& block_position, |
96 | | Block* file_block) const; |
97 | | Status _build_col_name_to_file_id(); |
98 | | |
99 | | const TFileRangeDesc _range; |
100 | | const std::vector<SlotDescriptor*> _file_slot_descs; |
101 | | AdbcStreamFactory _stream_factory; |
102 | | cctz::time_zone _ctz; |
103 | | RuntimeProfile::Counter* _total_time = nullptr; |
104 | | RuntimeProfile::Counter* _open_stream_time = nullptr; |
105 | | RuntimeProfile::Counter* _next_batch_time = nullptr; |
106 | | RuntimeProfile::Counter* _io_time = nullptr; |
107 | | RuntimeProfile::Counter* _normalize_time = nullptr; |
108 | | RuntimeProfile::Counter* _materialize_time = nullptr; |
109 | | RuntimeProfile::Counter* _filter_time = nullptr; |
110 | | RuntimeState* _runtime_state = nullptr; |
111 | | std::unique_ptr<AdbcStream> _stream; |
112 | | std::unordered_map<std::string, LocalColumnId> _col_name_to_file_id; |
113 | | }; |
114 | | |
115 | | class AdbcReader final : public TableReader { |
116 | | public: |
117 | | explicit AdbcReader(AdbcStreamFactory stream_factory = {}); |
118 | | |
119 | | Status init(TableReadOptions&& options) override; |
120 | | Status prepare_split(const SplitReadOptions& options) override; |
121 | | |
122 | | protected: |
123 | | Status create_file_reader(std::unique_ptr<FileReader>* reader) override; |
124 | | |
125 | | private: |
126 | | AdbcStreamFactory _stream_factory; |
127 | | }; |
128 | | |
129 | | } // namespace doris::format::adbc |