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
//! Schema Builder - Fluent API for building Arrow Schemas
//!
//! This module provides a type-safe, fluent API for building Arrow schemas
//! without manually constructing Field objects.
use arrow::datatypes::{DataType, Field, Schema, TimeUnit};
/// Schema Builder for constructing Arrow schemas using a fluent API
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
/// use arrow::datatypes::DataType;
///
/// let schema = SchemaBuilder::new()
/// .id("id")
/// .text("name")
/// .int32("age").nullable()
/// .build();
/// ```
#[derive(Debug, Clone)]
pub struct SchemaBuilder {
fields: Vec<Field>,
}
impl SchemaBuilder {
/// Create a new SchemaBuilder
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let builder = SchemaBuilder::new();
/// ```
pub fn new() -> Self {
Self { fields: Vec::new() }
}
/// Add a column with explicit type and nullability
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
/// use arrow::datatypes::DataType;
///
/// let schema = SchemaBuilder::new()
/// .column("id", DataType::Int64, false)
/// .column("name", DataType::Utf8, true)
/// .build();
/// ```
pub fn column(mut self, name: &str, data_type: DataType, nullable: bool) -> Self {
self.fields.push(Field::new(name, data_type, nullable));
self
}
/// Build the final Schema
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .id("id")
/// .text("name")
/// .build();
/// ```
pub fn build(self) -> Schema {
Schema::new(self.fields)
}
// ========== Type-specific convenience methods ==========
/// Add an ID column (Int64, NOT NULL)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .id("id")
/// .build();
/// ```
pub fn id(self, name: &str) -> Self {
self.column(name, DataType::Int64, false)
}
/// Add a text column (Utf8, nullable by default)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .text("name")
/// .build();
/// ```
pub fn text(self, name: &str) -> Self {
self.column(name, DataType::Utf8, true)
}
/// Add an Int32 column (nullable by default)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .int32("age")
/// .build();
/// ```
pub fn int32(self, name: &str) -> Self {
self.column(name, DataType::Int32, true)
}
/// Add an Int64 column (nullable by default)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .int64("user_id")
/// .build();
/// ```
pub fn int64(self, name: &str) -> Self {
self.column(name, DataType::Int64, true)
}
/// Add a Float64 column (nullable by default)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .float64("salary")
/// .build();
/// ```
pub fn float64(self, name: &str) -> Self {
self.column(name, DataType::Float64, true)
}
/// Add a Boolean column (nullable by default)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .boolean("is_active")
/// .build();
/// ```
pub fn boolean(self, name: &str) -> Self {
self.column(name, DataType::Boolean, true)
}
/// Add a Timestamp column (nullable by default)
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .timestamp("created_at")
/// .build();
/// ```
pub fn timestamp(self, name: &str) -> Self {
self.column(name, DataType::Timestamp(TimeUnit::Millisecond, None), true)
}
// ========== Nullability control methods ==========
/// Make the last added field nullable
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .int32("age").nullable()
/// .build();
/// ```
pub fn nullable(mut self) -> Self {
if let Some(field) = self.fields.last_mut() {
*field = Field::new(field.name(), field.data_type().clone(), true);
}
self
}
/// Make the last added field NOT NULL
///
/// # Example
///
/// ```rust
/// use dbx_core::SchemaBuilder;
///
/// let schema = SchemaBuilder::new()
/// .text("email").not_null()
/// .build();
/// ```
pub fn not_null(mut self) -> Self {
if let Some(field) = self.fields.last_mut() {
*field = Field::new(field.name(), field.data_type().clone(), false);
}
self
}
}
impl Default for SchemaBuilder {
fn default() -> Self {
Self::new()
}
}