typedb_driver/concept/
value.rs

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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

use std::{
    collections::HashMap,
    fmt,
    ops::{Add, Neg, Sub},
    str::FromStr,
};

use chrono::{DateTime, FixedOffset, MappedLocalTime, NaiveDate, NaiveDateTime};
use chrono_tz::Tz;

use crate::Error;

/// Represents the type of primitive value is held by a Value or Attribute.
#[derive(Clone, PartialEq, Eq)]
pub enum ValueType {
    Boolean,
    Integer,
    Double,
    Decimal,
    String,
    Date,
    Datetime,
    DatetimeTZ,
    Duration,
    Struct(String),
}

impl ValueType {
    pub(crate) const NONE_STR: &'static str = "none";
    pub(crate) const BOOLEAN_STR: &'static str = "boolean";
    pub(crate) const INTEGER_STR: &'static str = "integer";
    pub(crate) const DOUBLE_STR: &'static str = "double";
    pub(crate) const DECIMAL_STR: &'static str = "decimal";
    pub(crate) const STRING_STR: &'static str = "string";
    pub(crate) const DATE_STR: &'static str = "date";
    pub(crate) const DATETIME_STR: &'static str = "datetime";
    pub(crate) const DATETIME_TZ_STR: &'static str = "datetime-tz";
    pub(crate) const DURATION_STR: &'static str = "duration";

    pub fn name(&self) -> &str {
        match self {
            Self::Boolean => Self::BOOLEAN_STR,
            Self::Integer => Self::INTEGER_STR,
            Self::Double => Self::DOUBLE_STR,
            Self::Decimal => Self::DECIMAL_STR,
            Self::String => Self::STRING_STR,
            Self::Date => Self::DATE_STR,
            Self::Datetime => Self::DATETIME_STR,
            Self::DatetimeTZ => Self::DATETIME_TZ_STR,
            Self::Duration => Self::DURATION_STR,
            Self::Struct(name) => &name,
        }
    }
}

impl fmt::Display for ValueType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl fmt::Debug for ValueType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

#[derive(Clone, PartialEq)]
pub enum Value {
    Boolean(bool),
    Integer(i64),
    Double(f64),
    Decimal(Decimal),
    String(String),
    Date(NaiveDate),
    Datetime(NaiveDateTime),
    DatetimeTZ(DateTime<TimeZone>),
    Duration(Duration),
    Struct(Struct, String),
}

impl Value {
    /// Retrieves the `ValueType` of this value concept.
    ///
    /// # Examples
    ///
    /// ```rust
    /// value.get_type();
    /// ```
    pub fn get_type(&self) -> ValueType {
        match self {
            Self::Boolean(_) => ValueType::Boolean,
            Self::Integer(_) => ValueType::Integer,
            Self::Double(_) => ValueType::Double,
            Self::String(_) => ValueType::String,
            Self::Decimal(_) => ValueType::Decimal,
            Self::Date(_) => ValueType::Date,
            Self::Datetime(_) => ValueType::Datetime,
            Self::DatetimeTZ(_) => ValueType::DatetimeTZ,
            Self::Duration(_) => ValueType::Duration,
            Self::Struct(_, struct_type_name) => ValueType::Struct(struct_type_name.clone()),
        }
    }

    /// Retrieves the name of the `ValueType` of this value concept.
    ///
    /// # Examples
    ///
    /// ```rust
    /// value.get_type_name();
    /// ```
    pub fn get_type_name(&self) -> &str {
        match self {
            Self::Boolean(_) => ValueType::Boolean.name(),
            Self::Integer(_) => ValueType::Integer.name(),
            Self::Double(_) => ValueType::Double.name(),
            Self::String(_) => ValueType::String.name(),
            Self::Decimal(_) => ValueType::Decimal.name(),
            Self::Date(_) => ValueType::Date.name(),
            Self::Datetime(_) => ValueType::Datetime.name(),
            Self::DatetimeTZ(_) => ValueType::DatetimeTZ.name(),
            Self::Duration(_) => ValueType::Duration.name(),
            Self::Struct(_, struct_type_name) => struct_type_name,
        }
    }

    pub fn get_boolean(&self) -> Option<bool> {
        if let Value::Boolean(bool) = self {
            Some(*bool)
        } else {
            None
        }
    }

    pub fn get_integer(&self) -> Option<i64> {
        if let Value::Integer(integer) = self {
            Some(*integer)
        } else {
            None
        }
    }

    pub fn get_double(&self) -> Option<f64> {
        if let Value::Double(double) = self {
            Some(*double)
        } else {
            None
        }
    }

    pub fn get_string(&self) -> Option<&str> {
        if let Value::String(string) = self {
            Some(&**string)
        } else {
            None
        }
    }

    pub fn get_decimal(&self) -> Option<Decimal> {
        if let Value::Decimal(decimal) = self {
            Some(*decimal)
        } else {
            None
        }
    }

    pub fn get_date(&self) -> Option<NaiveDate> {
        if let Value::Date(naive_date) = self {
            Some(*naive_date)
        } else {
            None
        }
    }

    pub fn get_datetime(&self) -> Option<NaiveDateTime> {
        if let Value::Datetime(datetime) = self {
            Some(*datetime)
        } else {
            None
        }
    }

    pub fn get_datetime_tz(&self) -> Option<DateTime<TimeZone>> {
        if let Value::DatetimeTZ(datetime_tz) = self {
            Some(*datetime_tz)
        } else {
            None
        }
    }

    pub fn get_duration(&self) -> Option<Duration> {
        if let Value::Duration(duration) = self {
            Some(*duration)
        } else {
            None
        }
    }

    pub fn get_struct(&self) -> Option<&Struct> {
        if let Value::Struct(struct_, _) = self {
            Some(struct_)
        } else {
            None
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Boolean(bool) => write!(f, "{}", bool),
            Self::Integer(integer) => write!(f, "{}", integer),
            Self::Double(double) => write!(f, "{}", double),
            Self::String(string) => write!(f, "\"{}\"", string),
            Self::Decimal(decimal) => write!(f, "{}", decimal),
            Self::Date(date) => write!(f, "{}", date.format("%Y-%m-%d")),
            Self::Datetime(datetime) => write!(f, "{}", datetime.format("%FT%T%.9f")),
            Self::DatetimeTZ(datetime_tz) => match datetime_tz.timezone() {
                TimeZone::IANA(tz) => write!(f, "{} {}", datetime_tz.format("%FT%T%.9f"), tz.name()),
                TimeZone::Fixed(_) => write!(f, "{}", datetime_tz.format("%FT%T%.9f%:z")),
            },
            Self::Duration(duration) => write!(f, "{}", duration),
            Self::Struct(value, name) => write!(f, "{} {}", name, value),
        }
    }
}

impl fmt::Debug for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: ", self.get_type_name())?;
        match self {
            Value::Boolean(bool) => write!(f, "{}", bool),
            Value::Integer(integer) => write!(f, "{}", integer),
            Value::Double(double) => write!(f, "{}", double),
            Value::Decimal(decimal) => write!(f, "{}", decimal),
            Value::String(string) => write!(f, "\"{}\"", string),
            Value::Date(date) => write!(f, "{}", date),
            Value::Datetime(datetime) => write!(f, "{}", datetime),
            Value::DatetimeTZ(datetime_tz) => write!(f, "{}", datetime_tz),
            Value::Duration(duration) => write!(f, "{}", duration),
            Value::Struct(value, name) => write!(f, "{} {}", name, value),
        }
    }
}

/// A fixed-point decimal number.
/// Holds exactly 19 digits after the decimal point and a 64-bit value before the decimal point.
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Decimal {
    integer: i64,
    fractional: u64,
}

impl Decimal {
    const FRACTIONAL_PART_DENOMINATOR_LOG10: u32 = 19;
    pub const FRACTIONAL_PART_DENOMINATOR: u64 = 10u64.pow(Decimal::FRACTIONAL_PART_DENOMINATOR_LOG10);
    pub const MIN: Self = Self::new(i64::MIN, 0);
    pub const MAX: Self = Self::new(i64::MAX, Decimal::FRACTIONAL_PART_DENOMINATOR - 1);

    pub const fn new(integer: i64, fractional: u64) -> Self {
        assert!(fractional < Decimal::FRACTIONAL_PART_DENOMINATOR);
        Self { integer, fractional }
    }

    /// Get the integer part of the decimal as normal signed 64 bit number
    pub fn integer_part(&self) -> i64 {
        self.integer
    }

    /// Get the fractional part of the decimal, in multiples of 10^-19 (Decimal::FRACTIONAL_PART_DENOMINATOR)
    /// This means, the smallest decimal representable is 10^-19, and up to 19 decimal places are supported.
    pub fn fractional_part(&self) -> u64 {
        self.fractional
    }
}

impl Neg for Decimal {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::default() - self
    }
}

impl Add for Decimal {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let lhs = self;
        let (fractional, carry) = match lhs.fractional.overflowing_add(rhs.fractional) {
            (frac, false) if frac < Self::FRACTIONAL_PART_DENOMINATOR => (frac, 0),
            (frac, true) if frac < Self::FRACTIONAL_PART_DENOMINATOR => {
                (frac + 0u64.wrapping_sub(Self::FRACTIONAL_PART_DENOMINATOR), 1)
            }
            (frac, false) => (frac - Self::FRACTIONAL_PART_DENOMINATOR, 1),
            (_, true) => unreachable!(),
        };
        let integer = lhs.integer + rhs.integer + carry;

        Self::new(integer, fractional)
    }
}

impl Sub for Decimal {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let lhs = self;
        let (fractional, carry) = match lhs.fractional.overflowing_sub(rhs.fractional) {
            (frac, false) => (frac, 0),
            (frac, true) => (frac.wrapping_add(Self::FRACTIONAL_PART_DENOMINATOR), 1),
        };
        let integer = lhs.integer - rhs.integer - carry;

        Self::new(integer, fractional)
    }
}

impl fmt::Display for Decimal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl fmt::Debug for Decimal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.fractional == 0 {
            write!(f, "{}.0", self.integer_part())?;
        } else {
            // count number of tailing 0's that don't have to be represented
            let mut tail_0s = 0;
            let mut fractional = self.fractional;
            while fractional % 10 == 0 {
                tail_0s += 1;
                fractional /= 10;
            }

            let fractional_width = Self::FRACTIONAL_PART_DENOMINATOR_LOG10 - tail_0s;
            write!(f, "{}.{:0width$}dec", self.integer_part(), fractional, width = fractional_width as usize)?;
        }
        Ok(())
    }
}

/// Offset for datetime-tz. Can be retrieved from an IANA Tz or a FixedOffset.
#[derive(Copy, Clone, Debug)]
pub enum Offset {
    IANA(<Tz as chrono::TimeZone>::Offset),
    Fixed(FixedOffset),
}

impl chrono::Offset for Offset {
    fn fix(&self) -> FixedOffset {
        match self {
            Self::IANA(inner) => inner.fix(),
            Self::Fixed(inner) => inner.fix(),
        }
    }
}

impl fmt::Display for Offset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IANA(inner) => fmt::Display::fmt(inner, f),
            Self::Fixed(inner) => fmt::Display::fmt(inner, f),
        }
    }
}

/// TimeZone for datetime-tz. Can be represented as an IANA Tz or as a FixedOffset.
#[derive(Copy, Clone, Debug)]
pub enum TimeZone {
    IANA(Tz),
    Fixed(FixedOffset),
}

impl Default for TimeZone {
    fn default() -> Self {
        Self::IANA(Tz::default())
    }
}

impl chrono::TimeZone for TimeZone {
    type Offset = Offset;

    fn from_offset(offset: &Self::Offset) -> Self {
        match offset {
            Offset::IANA(offset) => Self::IANA(Tz::from_offset(offset)),
            Offset::Fixed(offset) => Self::Fixed(FixedOffset::from_offset(offset)),
        }
    }

    fn offset_from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<Self::Offset> {
        match self {
            Self::IANA(inner) => inner.offset_from_local_date(local).map(Offset::IANA),
            Self::Fixed(inner) => inner.offset_from_local_date(local).map(Offset::Fixed),
        }
    }

    fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<Self::Offset> {
        match self {
            Self::IANA(inner) => inner.offset_from_local_datetime(local).map(Offset::IANA),
            Self::Fixed(inner) => inner.offset_from_local_datetime(local).map(Offset::Fixed),
        }
    }

    fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset {
        match self {
            TimeZone::IANA(inner) => Offset::IANA(inner.offset_from_utc_date(utc)),
            TimeZone::Fixed(inner) => Offset::Fixed(inner.offset_from_utc_date(utc)),
        }
    }

    fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset {
        match self {
            TimeZone::IANA(inner) => Offset::IANA(inner.offset_from_utc_datetime(utc)),
            TimeZone::Fixed(inner) => Offset::Fixed(inner.offset_from_utc_datetime(utc)),
        }
    }
}

/// A relative duration, which contains months, days, and nanoseconds.
/// Can be used for calendar-relative durations (eg 7 days forward), or for absolute durations using the nanosecond component
/// When used as an absolute duration, convertible to chrono::Duration
#[repr(C)]
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct Duration {
    pub months: u32,
    pub days: u32,
    pub nanos: u64,
}

impl Duration {
    const NANOS_PER_SEC: u64 = 1_000_000_000;
    const NANOS_PER_MINUTE: u64 = 60 * Self::NANOS_PER_SEC;
    const NANOS_PER_HOUR: u64 = 60 * 60 * Self::NANOS_PER_SEC;
    const DAYS_PER_WEEK: u32 = 7;
    const MONTHS_PER_YEAR: u32 = 12;

    pub fn new(months: u32, days: u32, nanos: u64) -> Self {
        Self { months, days, nanos }
    }

    pub fn months(&self) -> u32 {
        self.months
    }

    pub fn days(&self) -> u32 {
        self.days
    }

    pub fn nanos(&self) -> u64 {
        self.nanos
    }

    fn is_empty(&self) -> bool {
        self.months == 0 && self.days == 0 && self.nanos == 0
    }
}

impl TryFrom<Duration> for chrono::Duration {
    type Error = crate::Error;

    fn try_from(duration: Duration) -> Result<Self, Self::Error> {
        if duration.months != 0 || duration.days != 0 {
            Err(Error::Other(String::from(
                "Converting TypeDB duration to chrono::Duration is only possible when months and days are not set.",
            )))
        } else {
            match i64::try_from(duration.nanos) {
                Ok(nanos) => Ok(chrono::Duration::nanoseconds(nanos)),
                Err(_) => {
                    Err(Error::Other(String::from("Duration u64 nanos exceeded i64 required for chrono::Duration")))
                }
            }
        }
    }
}

// TODO: Duration parsing is basically a copy of TypeDB server's code, can be made a dependency.
#[derive(Debug)]
pub struct DurationParseError;

struct Segment {
    number: u64,
    symbol: u8,
    number_len: usize,
}

fn read_u32(str: &str) -> Result<(Segment, &str), DurationParseError> {
    let mut i = 0;
    while i + 1 < str.len() && str.as_bytes()[i].is_ascii_digit() {
        i += 1;
    }
    if i == 0 {
        return Err(DurationParseError);
    }
    let value = str[..i].parse().map_err(|_| DurationParseError)?;
    Ok((Segment { number: value, symbol: str.as_bytes()[i], number_len: i }, &str[i + 1..]))
}

impl FromStr for Duration {
    type Err = DurationParseError;

    fn from_str(mut str: &str) -> Result<Self, Self::Err> {
        let mut months = 0;
        let mut days = 0;
        let mut nanos = 0;

        if str.as_bytes()[0] != b'P' {
            return Err(DurationParseError);
        }
        str = &str[1..];

        let mut parsing_time = false;
        let mut previous_symbol = None;
        while !str.is_empty() {
            if str.as_bytes()[0] == b'T' {
                parsing_time = true;
                str = &str[1..];
            }

            let (Segment { number, symbol, number_len }, tail) = read_u32(str)?;
            str = tail;

            if previous_symbol == Some(b'.') && symbol != b'S' {
                return Err(DurationParseError);
            }

            match symbol {
                b'Y' => months += number as u32 * Self::MONTHS_PER_YEAR,
                b'M' if !parsing_time => months += number as u32,

                b'W' => days += number as u32 * Self::DAYS_PER_WEEK,
                b'D' => days += number as u32,

                b'H' => nanos += number * Self::NANOS_PER_HOUR,
                b'M' if parsing_time => nanos += number * Self::NANOS_PER_MINUTE,
                b'.' => nanos += number * Self::NANOS_PER_SEC,
                b'S' if previous_symbol != Some(b'.') => nanos += number * Self::NANOS_PER_SEC,
                b'S' if previous_symbol == Some(b'.') => nanos += number * 10u64.pow(9 - number_len as u32),
                _ => return Err(DurationParseError),
            }
            previous_symbol = Some(symbol);
        }

        Ok(Self { months, days, nanos })
    }
}

/// ISO-8601 compliant representation of a duration
impl fmt::Display for Duration {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            f.write_str("PT0S")?;
            return Ok(());
        }

        write!(f, "P")?;

        if self.months > 0 || self.days > 0 {
            let years = self.months / Self::MONTHS_PER_YEAR;
            let months = self.months % Self::MONTHS_PER_YEAR;
            let days = self.days;
            if years > 0 {
                write!(f, "{years}Y")?;
            }
            if months > 0 {
                write!(f, "{months}M")?;
            }
            if days > 0 {
                write!(f, "{days}D")?;
            }
        }

        if self.nanos > 0 {
            write!(f, "T")?;

            let hours = self.nanos / Self::NANOS_PER_HOUR;
            let minutes = (self.nanos % Self::NANOS_PER_HOUR) / Self::NANOS_PER_MINUTE;
            let seconds = (self.nanos % Self::NANOS_PER_MINUTE) / Self::NANOS_PER_SEC;
            let nanos = self.nanos % Self::NANOS_PER_SEC;

            if hours > 0 {
                write!(f, "{hours}H")?;
            }
            if minutes > 0 {
                write!(f, "{minutes}M")?;
            }
            if seconds > 0 && nanos == 0 {
                write!(f, "{seconds}S")?;
            } else if nanos > 0 {
                write!(f, "{seconds}.{nanos:09}S")?;
            }
        }

        Ok(())
    }
}

impl fmt::Debug for Duration {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "months: {}, days: {}, nanos: {}", self.months, self.days, self.nanos)
    }
}

#[derive(Clone, PartialEq)]
pub struct Struct {
    pub(crate) fields: HashMap<String, Option<Value>>,
}

impl Struct {
    pub fn fields(&self) -> &HashMap<String, Option<Value>> {
        &self.fields
    }
}

impl fmt::Display for Struct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl fmt::Debug for Struct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.fields)
    }
}