be/src/format/arrow/arrow_row_batch.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 <memory> |
21 | | #include <string> |
22 | | #include <utility> |
23 | | |
24 | | #include "common/status.h" |
25 | | #include "core/block/block.h" |
26 | | #include "exprs/vexpr_fwd.h" |
27 | | |
28 | | // This file will convert Doris RowBatch to/from Arrow's RecordBatch |
29 | | // RowBatch is used by Doris query engine to exchange data between |
30 | | // each execute node. |
31 | | |
32 | | namespace arrow { |
33 | | |
34 | | class DataType; |
35 | | class Field; |
36 | | class RecordBatch; |
37 | | class Schema; |
38 | | |
39 | | } // namespace arrow |
40 | | |
41 | | namespace doris { |
42 | | |
43 | | constexpr size_t MAX_ARROW_UTF8 = (1ULL << 31); // 2G |
44 | | |
45 | | class RowDescriptor; |
46 | | |
47 | | // Each protocol owns its schema source and conversion rules. Table formats retain their |
48 | | // authoritative schemas instead of reconstructing field IDs or physical layouts from Doris types. |
49 | | class ArrowSchemaConvertor { |
50 | | public: |
51 | 503 | virtual ~ArrowSchemaConvertor() = default; |
52 | | virtual Status get_arrow_schema(std::shared_ptr<arrow::Schema>* result) const = 0; |
53 | | }; |
54 | | |
55 | | class DorisArrowSchemaConvertor : public ArrowSchemaConvertor { |
56 | | public: |
57 | 460 | explicit DorisArrowSchemaConvertor(std::string timezone) : _timezone(std::move(timezone)) {} |
58 | | DorisArrowSchemaConvertor(const Block& header, std::string timezone) |
59 | 17 | : _header(header.clone_empty()), _timezone(std::move(timezone)) {} |
60 | | |
61 | | Status get_arrow_schema(std::shared_ptr<arrow::Schema>* result) const override; |
62 | | Status get_arrow_schema_from_block(const Block& block, |
63 | | std::shared_ptr<arrow::Schema>* result) const; |
64 | | Status get_arrow_schema_from_expr_ctxs(const VExprContextSPtrs& output_vexpr_ctxs, |
65 | | std::shared_ptr<arrow::Schema>* result) const; |
66 | | virtual Status convert_to_arrow_type(const DataTypePtr& type, |
67 | | std::shared_ptr<arrow::DataType>* result) const; |
68 | | |
69 | | protected: |
70 | | virtual std::string timestamp_timezone(PrimitiveType type) const; |
71 | | virtual std::shared_ptr<arrow::Field> make_field(const std::string& name, |
72 | | const std::shared_ptr<arrow::DataType>& type, |
73 | | bool nullable, PrimitiveType primitive) const; |
74 | | virtual std::shared_ptr<arrow::Field> make_child_field( |
75 | | const std::string& name, const std::shared_ptr<arrow::DataType>& type, bool nullable, |
76 | | PrimitiveType primitive) const; |
77 | | |
78 | | private: |
79 | | Block _header; |
80 | | const std::string _timezone; |
81 | | }; |
82 | | |
83 | | class ArrowFlightSchemaConvertor : public DorisArrowSchemaConvertor { |
84 | | public: |
85 | | explicit ArrowFlightSchemaConvertor(std::string timezone) |
86 | 105 | : DorisArrowSchemaConvertor(std::move(timezone)) {} |
87 | | ArrowFlightSchemaConvertor(const Block& header, std::string timezone) |
88 | 4 | : DorisArrowSchemaConvertor(header, std::move(timezone)) {} |
89 | | |
90 | | Status convert_to_arrow_type(const DataTypePtr& type, |
91 | | std::shared_ptr<arrow::DataType>* result) const override; |
92 | | |
93 | | protected: |
94 | | std::string timestamp_timezone(PrimitiveType type) const override; |
95 | | }; |
96 | | |
97 | | // Old FEs require the pre-capability metadata layout, including metadata-free nested fields. |
98 | | class LegacyArrowFlightSchemaConvertor final : public ArrowFlightSchemaConvertor { |
99 | | public: |
100 | | using ArrowFlightSchemaConvertor::ArrowFlightSchemaConvertor; |
101 | | |
102 | | protected: |
103 | | std::shared_ptr<arrow::Field> make_field(const std::string& name, |
104 | | const std::shared_ptr<arrow::DataType>& type, |
105 | | bool nullable, PrimitiveType primitive) const override; |
106 | | std::shared_ptr<arrow::Field> make_child_field(const std::string& name, |
107 | | const std::shared_ptr<arrow::DataType>& type, |
108 | | bool nullable, |
109 | | PrimitiveType primitive) const override; |
110 | | }; |
111 | | |
112 | | Status register_arrow_variant_extension(); |
113 | | |
114 | | std::shared_ptr<arrow::Field> create_arrow_field_with_metadata( |
115 | | const std::string& field_name, const std::shared_ptr<arrow::DataType>& arrow_type, |
116 | | bool is_nullable, PrimitiveType primitive_type); |
117 | | |
118 | | Status serialize_record_batch(const arrow::RecordBatch& record_batch, std::string* result); |
119 | | |
120 | | Status serialize_arrow_schema(std::shared_ptr<arrow::Schema>* schema, std::string* result); |
121 | | |
122 | | } // namespace doris |