1
+ // Copyright 2022 Lance Authors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+
16
+ #include " lance/duckdb/lance.h"
17
+
18
+ #include < arrow/type.h>
19
+
20
+ #include < duckdb/common/exception.hpp>
21
+ #include < vector>
22
+
23
+ namespace lance ::duckdb {
24
+
25
+ namespace {
26
+
27
+ inline ::duckdb::LogicalType ToLogicalType (const ::arrow::DictionaryType& dtype) {
28
+ return lance::duckdb::ToLogicalType (*dtype.value_type ());
29
+ }
30
+
31
+ inline ::duckdb::LogicalType ToLogicalType (const ::arrow::StructType& struct_type) {
32
+ ::duckdb::child_list_t <::duckdb::LogicalType> children;
33
+ for (auto & child : struct_type.fields ()) {
34
+ children.emplace_back (
35
+ std::make_pair (child->name (), lance::duckdb::ToLogicalType (*child->type ())));
36
+ }
37
+ return ::duckdb::LogicalType::STRUCT (children);
38
+ }
39
+
40
+ template <typename L>
41
+ inline ::duckdb::LogicalType ToLogicalType (const ::arrow::DataType& dtype) {
42
+ auto & list_type = dynamic_cast <const L&>(dtype);
43
+ auto child_type = lance::duckdb::ToLogicalType (*list_type.value_type ());
44
+ return ::duckdb::LogicalType::LIST (child_type);
45
+ }
46
+
47
+ } // namespace
48
+
49
+ ::duckdb::LogicalType ToLogicalType (const ::arrow::DataType& arrow_type) {
50
+ switch (arrow_type.id ()) {
51
+ case ::arrow::Type::BOOL:
52
+ return ::duckdb::LogicalType::BOOLEAN;
53
+ case ::arrow::Type::INT8:
54
+ return ::duckdb::LogicalType::TINYINT;
55
+ case ::arrow::Type::UINT8:
56
+ return ::duckdb::LogicalType::UTINYINT;
57
+ case ::arrow::Type::INT16:
58
+ return ::duckdb::LogicalType::SMALLINT;
59
+ case ::arrow::Type::UINT16:
60
+ return ::duckdb::LogicalType::USMALLINT;
61
+ case ::arrow::Type::INT32:
62
+ return ::duckdb::LogicalType::INTEGER;
63
+ case ::arrow::Type::UINT64:
64
+ return ::duckdb::LogicalType::UINTEGER;
65
+ case ::arrow::Type::FLOAT:
66
+ case ::arrow::Type::HALF_FLOAT:
67
+ return ::duckdb::LogicalType::FLOAT;
68
+ case ::arrow::Type::DOUBLE:
69
+ return ::duckdb::LogicalType::DOUBLE;
70
+ case ::arrow::Type::STRING:
71
+ case ::arrow::Type::LARGE_STRING:
72
+ return ::duckdb::LogicalType::VARCHAR;
73
+ case ::arrow::Type::BINARY:
74
+ case ::arrow::Type::LARGE_BINARY:
75
+ return ::duckdb::LogicalType::BLOB;
76
+ case ::arrow::Type::TIME32:
77
+ case ::arrow::Type::TIME64:
78
+ return ::duckdb::LogicalType::TIME;
79
+ case ::arrow::Type::TIMESTAMP:
80
+ return ::duckdb::LogicalType::TIMESTAMP;
81
+ case ::arrow::Type::DATE32:
82
+ case ::arrow::Type::DATE64:
83
+ return ::duckdb::LogicalType::DATE;
84
+ case ::arrow::Type::DICTIONARY:
85
+ return ToLogicalType (dynamic_cast <const ::arrow::DictionaryType&>(arrow_type));
86
+ case ::arrow::Type::STRUCT:
87
+ return ToLogicalType (dynamic_cast <const ::arrow::StructType&>(arrow_type));
88
+ case ::arrow::Type::LIST:
89
+ return ToLogicalType<::arrow::ListType>(arrow_type);
90
+ case ::arrow::Type::FIXED_SIZE_LIST:
91
+ return ToLogicalType<::arrow::FixedSizeListType>(arrow_type);
92
+ default :
93
+ throw ::duckdb::InvalidInputException (" Does not support type: %s" ,
94
+ arrow_type.ToString ().c_str ());
95
+ }
96
+ }
97
+
98
+ } // namespace lance::duckdb
0 commit comments