proof_of_sql/base/database/
literal_value.rs

1use crate::base::{
2    database::ColumnType,
3    math::{decimal::Precision, i256::I256},
4    posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
5    scalar::{Scalar, ScalarExt},
6};
7use alloc::{string::String, vec::Vec};
8use serde::{Deserialize, Serialize};
9
10/// Represents a literal value.
11///
12/// Note: The types here should correspond to native SQL database types.
13/// See `<https://ignite.apache.org/docs/latest/sql-reference/data-types>` for
14/// a description of the native types used by Apache Ignite.
15#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
16#[non_exhaustive]
17pub enum LiteralValue {
18    /// Boolean literals
19    Boolean(bool),
20    /// u8 literals
21    Uint8(u8),
22    /// i8 literals
23    TinyInt(i8),
24    /// i16 literals
25    SmallInt(i16),
26    /// i32 literals
27    Int(i32),
28    /// i64 literals
29    BigInt(i64),
30
31    /// String literals
32    ///  - the first element maps to the str value.
33    ///  - the second element maps to the str hash (see [`crate::base::scalar::Scalar`]).
34    VarChar(String),
35    /// Binary data literals
36    ///  - the backing store is a Vec<u8> for variable length binary data
37    VarBinary(Vec<u8>),
38    /// i128 literals
39    Int128(i128),
40    /// Decimal literals with a max width of 252 bits
41    ///  - the backing store maps to the type [`crate::base::scalar::Curve25519Scalar`]
42    Decimal75(Precision, i8, I256),
43    /// Scalar literals. The underlying `[u64; 4]` is the limbs of the canonical form of the literal
44    Scalar([u64; 4]),
45    /// `TimeStamp` defined over a unit (s, ms, ns, etc) and timezone with backing store
46    /// mapped to i64, which is time units since unix epoch
47    TimeStampTZ(PoSQLTimeUnit, PoSQLTimeZone, i64),
48}
49
50impl LiteralValue {
51    /// Provides the column type associated with the column
52    #[must_use]
53    pub fn column_type(&self) -> ColumnType {
54        match self {
55            Self::Boolean(_) => ColumnType::Boolean,
56            Self::Uint8(_) => ColumnType::Uint8,
57            Self::TinyInt(_) => ColumnType::TinyInt,
58            Self::SmallInt(_) => ColumnType::SmallInt,
59            Self::Int(_) => ColumnType::Int,
60            Self::BigInt(_) => ColumnType::BigInt,
61            Self::VarChar(_) => ColumnType::VarChar,
62            Self::VarBinary(_) => ColumnType::VarBinary,
63            Self::Int128(_) => ColumnType::Int128,
64            Self::Scalar(_) => ColumnType::Scalar,
65            Self::Decimal75(precision, scale, _) => ColumnType::Decimal75(*precision, *scale),
66            Self::TimeStampTZ(tu, tz, _) => ColumnType::TimestampTZ(*tu, *tz),
67        }
68    }
69
70    /// Converts the literal to a scalar
71    pub(crate) fn to_scalar<S: Scalar>(&self) -> S {
72        match self {
73            Self::Boolean(b) => b.into(),
74            Self::Uint8(i) => i.into(),
75            Self::TinyInt(i) => i.into(),
76            Self::SmallInt(i) => i.into(),
77            Self::Int(i) => i.into(),
78            Self::BigInt(i) => i.into(),
79            Self::VarChar(str) => str.into(),
80            Self::VarBinary(bytes) => S::from_byte_slice_via_hash(bytes),
81            Self::Decimal75(_, _, i) => i.into_scalar(),
82            Self::Int128(i) => i.into(),
83            Self::Scalar(limbs) => (*limbs).into(),
84            Self::TimeStampTZ(_, _, time) => time.into(),
85        }
86    }
87}