1use std::fmt::Debug;
12use crate::value_types::duck_value_type::{DuckValueReader, DuckValueType, DuckValueWriter};
13use quack_rs::interval::DuckInterval;
14use quack_rs::prelude::{LogicalType, TypeId, Value, VectorReader, VectorWriter};
15use crate::DuckResult;
16
17#[derive(Default,Debug, Clone, Copy, PartialEq, Eq)]
22pub struct DuckTimestamp {
23 pub micros_since_epoch: i64,
27}
28
29impl DuckValueType for DuckTimestamp {
33 fn type_id() -> TypeId {
34 TypeId::Timestamp
35 }
36
37 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
38 Self {
39 micros_since_epoch: unsafe { reader.read_timestamp(row) },
40 }
41 }
42
43 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
44 unsafe { writer.write_timestamp(idx, v.micros_since_epoch) }
45 }
46 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
47 Self{
48 micros_since_epoch:value.as_timestamp()
49 }
50 }
51}
52
53#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
63pub struct DuckTimestampTz {
64 pub micros_since_epoch: i64,
68}
69
70impl DuckValueType for DuckTimestampTz {
76 fn type_id() -> TypeId {
77 TypeId::TimestampTz
78 }
79 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
80 Self {
81 micros_since_epoch: unsafe { reader.read_timestamp_tz(row) },
82 }
83 }
84 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
85 unsafe { writer.write_timestamp_tz(idx, v.micros_since_epoch) }
86 }
87 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
88 Self{
89 micros_since_epoch:value.as_timestamp_tz()
90 }
91 }
92
93}
94
95#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
101pub struct DuckTimestampS {
102 pub seconds_since_epoch: i64,
106}
107
108impl DuckValueType for DuckTimestampS {
112 fn type_id() -> TypeId {
113 TypeId::TimestampS
114 }
115 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
116 Self {
117 seconds_since_epoch: unsafe { reader.read_timestamp_s(row) },
118 }
119 }
120 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
121 unsafe { writer.write_timestamp_s(idx, v.seconds_since_epoch) }
122 }
123 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
124 Self{
125 seconds_since_epoch:value.as_timestamp_s()
126 }
127 }
128}
129
130#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
136pub struct DuckTimestampMs {
137 pub millis_since_epoch: i64,
141}
142
143impl DuckValueType for DuckTimestampMs {
147 fn type_id() -> TypeId {
148 TypeId::TimestampMs
149 }
150 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
151 Self {
152 millis_since_epoch: unsafe { reader.read_timestamp_ms(row) },
153 }
154 }
155 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
156 unsafe { writer.write_timestamp_ms(idx, v.millis_since_epoch) }
157 }
158 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
159 Self{
160 millis_since_epoch:value.as_timestamp_ms()
161 }
162 }
163}
164
165#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
169pub struct DuckTimestampNs {
170 pub nanos_since_epoch: i64,
174}
175
176impl DuckValueType for DuckTimestampNs {
180 fn type_id() -> TypeId {
181 TypeId::TimestampNs
182 }
183 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
184 Self {
185 nanos_since_epoch: unsafe { reader.read_timestamp_ns(row) },
186 }
187 }
188 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
189 unsafe { writer.write_timestamp_ns(idx, v.nanos_since_epoch) }
190 }
191 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
192 Self{
193 nanos_since_epoch:value.as_timestamp_ns()
194 }
195 }
196}
197
198#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
204pub struct DuckTimeTz {
205 pub bits: u64,
210}
211
212impl DuckValueType for DuckTimeTz {
216 fn type_id() -> TypeId {
217 TypeId::TimeTz
218 }
219 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
220 Self {
221 bits: unsafe { reader.read_time_tz(row) },
222 }
223 }
224 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
225 unsafe { writer.write_time_tz(idx, v.bits) }
226 }
227 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
228 Self{
229 bits:value.as_time_tz()
230 }
231 }
232}
233
234#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
243pub struct DuckDecimal<const WIDTH: u8, const SCALE: u8> {
244 pub unscaled: i128,
248}
249
250impl<const WIDTH: u8, const SCALE: u8> DuckValueType for DuckDecimal<WIDTH, SCALE> {
254 fn type_id() -> TypeId {
255 TypeId::Decimal
256 }
257 fn logical_type() -> LogicalType {
258 LogicalType::decimal(WIDTH, SCALE)
259 }
260 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
261 Self {
262 unscaled: value.as_decimal().value,
263 }
264 }
265 fn read_valid(reader: &DuckValueReader, row: usize) -> Option<Self> {
270 Some(Self {
271 unscaled: unsafe { reader.vector_reader.read_decimal(row, WIDTH) },
272 })
273 }
274 fn write_valid(writer: &mut DuckValueWriter, idx: usize, vo: &Self) {
278 unsafe { writer.vector_writer.write_decimal(idx, WIDTH, vo.unscaled) }
279 }
280}
281
282#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
286pub struct DuckDate {
287 pub days_since_epoch: i32,
291}
292
293impl DuckValueType for DuckDate {
297 fn type_id() -> TypeId {
298 TypeId::Date
299 }
300 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
301 Self {
302 days_since_epoch: unsafe { reader.read_date(row) },
303 }
304 }
305 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
306 unsafe { writer.write_date(idx, v.days_since_epoch) }
307 }
308 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
309 Self{
310 days_since_epoch:value.as_date()
311 }
312 }
313}
314
315#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
320pub struct DuckTime {
321 pub micros_since_midnight: i64,
325}
326
327impl DuckValueType for DuckTime {
331 fn type_id() -> TypeId {
332 TypeId::Time
333 }
334 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
335 Self {
336 micros_since_midnight: unsafe { reader.read_time(row) },
337 }
338 }
339 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
340 unsafe { writer.write_time(idx, v.micros_since_midnight) }
341 }
342 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
343 Self{
344 micros_since_midnight:value.as_time()
345 }
346 }
347}
348
349#[cfg(feature = "duckdb-1-5")]
357#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
358pub struct DuckTimeNs {
359 pub nanos_since_midnight: i64,
363}
364
365#[cfg(feature = "duckdb-1-5")]
369impl DuckValueType for DuckTimeNs {
370 fn type_id() -> TypeId {
371 TypeId::TimeNs
372 }
373 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
374 Self {
380 nanos_since_midnight: unsafe { reader.read_i64(row) },
381 }
382 }
383 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
384 unsafe { writer.write_i64(idx, v.nanos_since_midnight) }
385 }
386 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
387 Self {
388 nanos_since_midnight: value.as_time_ns(),
389 }
390 }
391}
392
393#[derive(Default, Debug, Clone, PartialEq, Eq)]
399pub struct DuckBlob {
400 pub value: Vec<u8>,
404}
405
406impl DuckValueType for DuckBlob {
410 fn type_id() -> TypeId {
411 TypeId::Blob
412 }
413 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
414 Self {
415 value: unsafe { reader.read_blob(row).to_vec() },
416 }
417 }
418 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
419 unsafe { writer.write_blob(idx, v.value.as_slice()) }
420
421 }
422 fn read_by_duck_value_valid(value: &Value) -> DuckResult<Self> {
426 Ok(Self{
427 value:value.as_blob()?
428 })
429 }
430}
431
432#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
436pub struct DuckUuid {
437 pub value: u128,
441}
442
443impl DuckValueType for DuckUuid {
447 fn type_id() -> TypeId {
448 TypeId::Uuid
449 }
450 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
451 Self {
452 value: unsafe { reader.read_uuid(row) },
453 }
454 }
455 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
456 unsafe { writer.write_uuid(idx, v.value) }
457 }
458 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
459 Self{
460 value:value.as_uuid()
461 }
462 }
463}
464
465impl DuckValueType for DuckInterval {
474 fn type_id() -> TypeId {
475 TypeId::Interval
476 }
477 fn read_valid_by_vector_reader(reader: &VectorReader, row: usize) -> Self {
478 unsafe { reader.read_interval(row) }
479 }
480 fn write_valid_to_vector_writer(writer: &mut VectorWriter, idx: usize, v: &Self) {
481 unsafe { writer.write_interval(idx, *v) }
482 }
483 fn read_by_duck_value_valid_simple(value: &Value) -> Self {
484 value.as_interval()
485 }
486}