1#include "duckdb/main/capi/capi_internal.hpp"
2
3namespace duckdb {
4
5LogicalTypeId ConvertCTypeToCPP(duckdb_type c_type) {
6 switch (c_type) {
7 case DUCKDB_TYPE_BOOLEAN:
8 return LogicalTypeId::BOOLEAN;
9 case DUCKDB_TYPE_TINYINT:
10 return LogicalTypeId::TINYINT;
11 case DUCKDB_TYPE_SMALLINT:
12 return LogicalTypeId::SMALLINT;
13 case DUCKDB_TYPE_INTEGER:
14 return LogicalTypeId::INTEGER;
15 case DUCKDB_TYPE_BIGINT:
16 return LogicalTypeId::BIGINT;
17 case DUCKDB_TYPE_UTINYINT:
18 return LogicalTypeId::UTINYINT;
19 case DUCKDB_TYPE_USMALLINT:
20 return LogicalTypeId::USMALLINT;
21 case DUCKDB_TYPE_UINTEGER:
22 return LogicalTypeId::UINTEGER;
23 case DUCKDB_TYPE_UBIGINT:
24 return LogicalTypeId::UBIGINT;
25 case DUCKDB_TYPE_HUGEINT:
26 return LogicalTypeId::HUGEINT;
27 case DUCKDB_TYPE_FLOAT:
28 return LogicalTypeId::FLOAT;
29 case DUCKDB_TYPE_DOUBLE:
30 return LogicalTypeId::DOUBLE;
31 case DUCKDB_TYPE_TIMESTAMP:
32 return LogicalTypeId::TIMESTAMP;
33 case DUCKDB_TYPE_DATE:
34 return LogicalTypeId::DATE;
35 case DUCKDB_TYPE_TIME:
36 return LogicalTypeId::TIME;
37 case DUCKDB_TYPE_VARCHAR:
38 return LogicalTypeId::VARCHAR;
39 case DUCKDB_TYPE_BLOB:
40 return LogicalTypeId::BLOB;
41 case DUCKDB_TYPE_INTERVAL:
42 return LogicalTypeId::INTERVAL;
43 case DUCKDB_TYPE_TIMESTAMP_S:
44 return LogicalTypeId::TIMESTAMP_SEC;
45 case DUCKDB_TYPE_TIMESTAMP_MS:
46 return LogicalTypeId::TIMESTAMP_MS;
47 case DUCKDB_TYPE_TIMESTAMP_NS:
48 return LogicalTypeId::TIMESTAMP_NS;
49 case DUCKDB_TYPE_UUID:
50 return LogicalTypeId::UUID;
51 default: // LCOV_EXCL_START
52 D_ASSERT(0);
53 return LogicalTypeId::INVALID;
54 } // LCOV_EXCL_STOP
55}
56
57duckdb_type ConvertCPPTypeToC(const LogicalType &sql_type) {
58 switch (sql_type.id()) {
59 case LogicalTypeId::BOOLEAN:
60 return DUCKDB_TYPE_BOOLEAN;
61 case LogicalTypeId::TINYINT:
62 return DUCKDB_TYPE_TINYINT;
63 case LogicalTypeId::SMALLINT:
64 return DUCKDB_TYPE_SMALLINT;
65 case LogicalTypeId::INTEGER:
66 return DUCKDB_TYPE_INTEGER;
67 case LogicalTypeId::BIGINT:
68 return DUCKDB_TYPE_BIGINT;
69 case LogicalTypeId::UTINYINT:
70 return DUCKDB_TYPE_UTINYINT;
71 case LogicalTypeId::USMALLINT:
72 return DUCKDB_TYPE_USMALLINT;
73 case LogicalTypeId::UINTEGER:
74 return DUCKDB_TYPE_UINTEGER;
75 case LogicalTypeId::UBIGINT:
76 return DUCKDB_TYPE_UBIGINT;
77 case LogicalTypeId::HUGEINT:
78 return DUCKDB_TYPE_HUGEINT;
79 case LogicalTypeId::FLOAT:
80 return DUCKDB_TYPE_FLOAT;
81 case LogicalTypeId::DOUBLE:
82 return DUCKDB_TYPE_DOUBLE;
83 case LogicalTypeId::TIMESTAMP:
84 case LogicalTypeId::TIMESTAMP_TZ:
85 return DUCKDB_TYPE_TIMESTAMP;
86 case LogicalTypeId::TIMESTAMP_SEC:
87 return DUCKDB_TYPE_TIMESTAMP_S;
88 case LogicalTypeId::TIMESTAMP_MS:
89 return DUCKDB_TYPE_TIMESTAMP_MS;
90 case LogicalTypeId::TIMESTAMP_NS:
91 return DUCKDB_TYPE_TIMESTAMP_NS;
92 case LogicalTypeId::DATE:
93 return DUCKDB_TYPE_DATE;
94 case LogicalTypeId::TIME:
95 case LogicalTypeId::TIME_TZ:
96 return DUCKDB_TYPE_TIME;
97 case LogicalTypeId::VARCHAR:
98 return DUCKDB_TYPE_VARCHAR;
99 case LogicalTypeId::BLOB:
100 return DUCKDB_TYPE_BLOB;
101 case LogicalTypeId::BIT:
102 return DUCKDB_TYPE_BIT;
103 case LogicalTypeId::INTERVAL:
104 return DUCKDB_TYPE_INTERVAL;
105 case LogicalTypeId::DECIMAL:
106 return DUCKDB_TYPE_DECIMAL;
107 case LogicalTypeId::ENUM:
108 return DUCKDB_TYPE_ENUM;
109 case LogicalTypeId::LIST:
110 return DUCKDB_TYPE_LIST;
111 case LogicalTypeId::STRUCT:
112 return DUCKDB_TYPE_STRUCT;
113 case LogicalTypeId::MAP:
114 return DUCKDB_TYPE_MAP;
115 case LogicalTypeId::UNION:
116 return DUCKDB_TYPE_UNION;
117 case LogicalTypeId::UUID:
118 return DUCKDB_TYPE_UUID;
119 default: // LCOV_EXCL_START
120 D_ASSERT(0);
121 return DUCKDB_TYPE_INVALID;
122 } // LCOV_EXCL_STOP
123}
124
125idx_t GetCTypeSize(duckdb_type type) {
126 switch (type) {
127 case DUCKDB_TYPE_BOOLEAN:
128 return sizeof(bool);
129 case DUCKDB_TYPE_TINYINT:
130 return sizeof(int8_t);
131 case DUCKDB_TYPE_SMALLINT:
132 return sizeof(int16_t);
133 case DUCKDB_TYPE_INTEGER:
134 return sizeof(int32_t);
135 case DUCKDB_TYPE_BIGINT:
136 return sizeof(int64_t);
137 case DUCKDB_TYPE_UTINYINT:
138 return sizeof(uint8_t);
139 case DUCKDB_TYPE_USMALLINT:
140 return sizeof(uint16_t);
141 case DUCKDB_TYPE_UINTEGER:
142 return sizeof(uint32_t);
143 case DUCKDB_TYPE_UBIGINT:
144 return sizeof(uint64_t);
145 case DUCKDB_TYPE_HUGEINT:
146 case DUCKDB_TYPE_UUID:
147 return sizeof(duckdb_hugeint);
148 case DUCKDB_TYPE_FLOAT:
149 return sizeof(float);
150 case DUCKDB_TYPE_DOUBLE:
151 return sizeof(double);
152 case DUCKDB_TYPE_DATE:
153 return sizeof(duckdb_date);
154 case DUCKDB_TYPE_TIME:
155 return sizeof(duckdb_time);
156 case DUCKDB_TYPE_TIMESTAMP:
157 case DUCKDB_TYPE_TIMESTAMP_S:
158 case DUCKDB_TYPE_TIMESTAMP_MS:
159 case DUCKDB_TYPE_TIMESTAMP_NS:
160 return sizeof(duckdb_timestamp);
161 case DUCKDB_TYPE_VARCHAR:
162 return sizeof(const char *);
163 case DUCKDB_TYPE_BLOB:
164 return sizeof(duckdb_blob);
165 case DUCKDB_TYPE_INTERVAL:
166 return sizeof(duckdb_interval);
167 case DUCKDB_TYPE_DECIMAL:
168 return sizeof(duckdb_hugeint);
169 default: // LCOV_EXCL_START
170 // unsupported type
171 D_ASSERT(0);
172 return sizeof(const char *);
173 } // LCOV_EXCL_STOP
174}
175
176} // namespace duckdb
177
178void *duckdb_malloc(size_t size) {
179 return malloc(size: size);
180}
181
182void duckdb_free(void *ptr) {
183 free(ptr: ptr);
184}
185
186idx_t duckdb_vector_size() {
187 return STANDARD_VECTOR_SIZE;
188}
189
190bool duckdb_string_is_inlined(duckdb_string_t string_p) {
191 static_assert(sizeof(duckdb_string_t) == sizeof(duckdb::string_t),
192 "duckdb_string_t should have the same memory layout as duckdb::string_t");
193 auto &string = *(duckdb::string_t *)(&string_p);
194 return string.IsInlined();
195}
196