1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! # 🛰️ `ClickHouse` *Native Protocol* Rust Client w/ Arrow Compatibility
//!
//! `ClickHouse` access in rust over `ClickHouse`'s native protocol.
//!
//! A high-performance, async Rust client for `ClickHouse` with native Arrow integration. Designed
//! to be faster and more memory-efficient than existing alternatives.
//!
//! ## Why clickhouse-arrow?
//!
//! - **🚀 Performance**: Optimized for speed with zero-copy deserialization where possible
//! - **🎯 Arrow Native**: First-class Apache Arrow support for efficient data interchange
//! - **📊 90%+ Test Coverage**: Comprehensive test suite ensuring reliability
//! - **🔄 Async/Await**: Modern async API built on Tokio
//! - **🗜️ Compression**: LZ4 and ZSTD support for efficient data transfer
//! - **☁️ Cloud Ready**: Full `ClickHouse` Cloud compatibility
//! - **🛡️ Type Safe**: Compile-time type checking with the `#[derive(Row)]` macro
//!
//! ## Details
//!
//! The crate supports two "modes" of operation:
//!
//! ### `ArrowFormat`
//!
//! Support allowing interoperability with [arrow](https://docs.rs/arrow/latest/arrow/).
//!
//! ### `NativeFormat`
//!
//! Uses internal types and custom traits if a dependency on arrow is not required.
//!
//! ### `CreateOptions`, `SchemaConversions`, and Schemas
//!
//! #### Creating Tables from Arrow Schemas
//!
//! `clickhouse-arrow` provides powerful DDL capabilities through `CreateOptions`, allowing you to
//! create `ClickHouse` tables directly from Arrow schemas:
//!
//! ```rust,ignore
//! use clickhouse_arrow::{Client, ArrowFormat, CreateOptions};
//! use arrow::datatypes::{Schema, Field, DataType};
//!
//! // Define your Arrow schema
//! let schema = Schema::new(vec![
//! Field::new("id", DataType::UInt64, false),
//! Field::new("name", DataType::Utf8, false),
//! Field::new("status", DataType::Dictionary(Box::new(DataType::Int8), Box::new(DataType::Utf8)), false),
//! ]);
//!
//! // Configure table creation
//! let options = CreateOptions::new("MergeTree")
//! .with_order_by(&["id".to_string()])
//! .with_partition_by("toYYYYMM(created_at)")
//! .with_setting("index_granularity", 8192);
//!
//! // Create the table
//! client.create_table(None, "my_table", &schema, &options, None).await?;
//! ```
//!
//! #### Schema Conversions for Type Control
//!
//! `SchemaConversions` (type alias for `HashMap<String, Type>`) provides fine-grained control over
//! Arrow-to-ClickHouse type mappings. This is especially important for:
//!
//! 1. **Converting Dictionary → Enum**: By default, Arrow Dictionary types map to
//! `LowCardinality(String)`. Use `SchemaConversions` to map them to `Enum8` or `Enum16` instead:
//!
//! ```rust,ignore
//! use clickhouse_arrow::{Type, CreateOptions};
//! use std::collections::HashMap;
//!
//! let schema_conversions = HashMap::from([
//! // Convert status column from Dictionary to Enum8
//! ("status".to_string(), Type::Enum8(vec![
//! ("active".to_string(), 0),
//! ("inactive".to_string(), 1),
//! ("pending".to_string(), 2),
//! ])),
//! // Convert category to Enum16 for larger enums
//! ("category".to_string(), Type::Enum16(vec![
//! ("electronics".to_string(), 0),
//! ("clothing".to_string(), 1),
//! // ... up to 65k values
//! ])),
//! ]);
//!
//! let options = CreateOptions::new("MergeTree")
//! .with_order_by(&["id".to_string()])
//! .with_schema_conversions(schema_conversions);
//! ```
//!
//! 2. **Geo Types**: Preserve geographic types during conversion
//! 3. **Date Types**: Choose between `Date` and `Date32`
//! 4. **Custom Type Mappings**: Override any default type conversion
//!
//! #### Field Naming Constants
//!
//! When working with complex Arrow types, use these constants to ensure compatibility:
//!
//! ```rust,ignore
//! use clickhouse_arrow::arrow::types::*;
//!
//! // For List types - inner field is named "item"
//! let list_field = Field::new("data", DataType::List(
//! Arc::new(Field::new(LIST_ITEM_FIELD_NAME, DataType::Int32, true))
//! ), true);
//!
//! // For Struct/Tuple types - fields are named "field_0", "field_1", etc.
//! let tuple_fields = vec![
//! Field::new(format!("{}{}", TUPLE_FIELD_NAME_PREFIX, 0), DataType::Int32, false),
//! Field::new(format!("{}{}", TUPLE_FIELD_NAME_PREFIX, 1), DataType::Utf8, false),
//! ];
//!
//! // For Map types - uses specific field names
//! let map_type = DataType::Map(
//! Arc::new(Field::new(MAP_FIELD_NAME, DataType::Struct(
//! vec![
//! Field::new(STRUCT_KEY_FIELD_NAME, DataType::Utf8, false),
//! Field::new(STRUCT_VALUE_FIELD_NAME, DataType::Int32, true),
//! ].into()
//! ), false)),
//! false
//! );
//! ```
//!
//! These constants ensure your Arrow schemas align with `ClickHouse`'s expectations and maintain
//! compatibility with arrow-rs conventions.
//!
//! ## Queries
//!
//! ### Query Settings
//!
//! The `clickhouse_arrow::Settings` type allows configuring `ClickHouse` query settings. You can
//! import it directly:
//!
//! ```rust,ignore
//! use clickhouse_arrow::Settings;
//! // or via prelude
//! use clickhouse_arrow::prelude::*;
//! ```
//!
//! Refer to the settings module documentation for details and examples.
//!
//! ## Arrow Round-Trip
//!
//! There are cases where a round trip may deserialize a different type by schema or array than the
//! schema and array you used to create the table.
//!
//! will try to maintain an accurate and updated list as they occur. In addition, when possible, I
//! will provide options or other functionality to alter this behavior.
//!
//! #### `(String|Binary)View`/`Large(List|String|Binary)` variations are normalized.
//! - **Behavior**: `ClickHouse` does not make the same distinction between `Utf8`, `Utf8View`, or
//! `LargeUtf8`. All of these are mapped to either `Type::Binary` (the default, see above) or
//! `Type::String`
//! - **Option**: None
//! - **Default**: Unsupported
//! - **Impact**: When deserializing from `ClickHouse`, manual modification will be necessary to use
//! these data types.
//!
//! #### `Utf8` -> `Binary`
//! - **Behavior**: By default, `Type::String`/`DataType::Utf8` will be represented as Binary.
//! - **Option**: `strings_as_strings` (default: `false`).
//! - **Default**: Disabled (`false`).
//! - **Impact**: Set to `true` to strip map `Type::String` -> `DataType::Utf8`. Binary tends to be
//! more efficient to work with in high throughput scenarios
//!
//! #### Nullable `Array`s
//! - **Behavior**: `ClickHouse` does not allow `Nullable(Array(...))`, but insertion with non-null
//! data is allowed by default. To modify this behavior, set `array_nullable_error` to `true`.
//! - **Option**: `array_nullable_error` (default: `false`).
//! - **Default**: Disabled (`false`).
//! - **Impact**: Enables flexible insertion but may cause schema mismatches if nulls are present.
//!
//! #### `LowCardinality(Nullable(...))` vs `Nullable(LowCardinality(...))`
//! - **Behavior**: Like arrays mentioned above, `ClickHouse` does not allow nullable low
//! cardinality. The default behavior is to push down the nullability.
//! - **Option**: `low_cardinality_nullable_error` (default: `false`).
//! - **Default**: Disabled (`false`).
//! - **Impact**: Enables flexible insertion but may cause schema mismatches if nulls are present.
//!
//! #### `Enum8`/`Enum16` vs. `LowCardinality`
//! - **Behavior**: Arrow `Dictionary` types map to `LowCardinality`, but `ClickHouse` `Enum` types
//! may also map to `Dictionary`, altering the type on round-trip.
//! - **Option**: No options available rather provide hash maps for either `enum_i8` and/or
//! `enum_i16` for `CreateOptions` during schema creation.
//! - **Impact**: The default behavior will ignore enums when starting from arrow.
/// Derive macro for the [Row] trait.
///
/// This is similar in usage and implementation to the [`serde::Serialize`] and
/// [`serde::Deserialize`] derive macros.
///
/// ## serde attributes
/// The following [serde attributes](https://serde.rs/attributes.html) are supported, using `#[clickhouse_arrow(...)]` instead of `#[serde(...)]`:
/// - `with`
/// - `from` and `into`
/// - `try_from`
/// - `skip`
/// - `default`
/// - `deny_unknown_fields`
/// - `rename`
/// - `rename_all`
/// - `serialize_with`, `deserialize_with`
/// - `skip_deserializing`, `skip_serializing`
/// - `flatten`
/// - Index-based matching is disabled (the column names must match exactly).
/// - Due to the current interface of the [Row] trait, performance might not be optimal, as
/// a value map must be reconstitued for each flattened subfield.
///
/// ## ClickHouse-specific attributes
/// - The `nested` attribute allows handling [ClickHouse nested data structures](https://clickhouse.com/docs/en/sql-reference/data-types/nested-data-structures/nested).
/// See an example in the `tests` folder.
///
/// ## Known issues
/// - For serialization, the ordering of fields in the struct declaration must match the order in the `INSERT` statement, respectively in the table declaration. See issue [#34](https://github.com/Protryon/clickhouse_arrow/issues/34).
pub use Row;
pub use *;
/// Set this environment to enable additional debugs around arrow (de)serialization.
pub use ;
pub use *;
pub use ;
/// Contains useful top-level traits to interface with [`crate::prelude::NativeFormat`]
pub use *;
pub use Progress;
pub use ;
/// Represents the types that `ClickHouse` supports internally.
pub use *;
/// Contains useful top-level structures to interface with [`crate::prelude::NativeFormat`]
pub use *;
pub use ;
pub use *;
pub use ;
pub use CreateOptions;
pub use ;
// Type aliases used throughout the library
pub use *;
// External libraries
/// Re-exports
///
/// Exporting different external modules used by the library.
pub use *;