proof_of_sql/base/database/
literal_value.rs

1use crate::base::{
2    database::ColumnType,
3    math::{decimal::Precision, i256::I256},
4    scalar::Scalar,
5};
6use alloc::string::String;
7use proof_of_sql_parser::posql_time::{PoSQLTimeUnit, PoSQLTimeZone};
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    /// i128 literals
36    Int128(i128),
37    /// Decimal literals with a max width of 252 bits
38    ///  - the backing store maps to the type [`crate::base::scalar::Curve25519Scalar`]
39    Decimal75(Precision, i8, I256),
40    /// Scalar literals. The underlying `[u64; 4]` is the limbs of the canonical form of the literal
41    Scalar([u64; 4]),
42    /// `TimeStamp` defined over a unit (s, ms, ns, etc) and timezone with backing store
43    /// mapped to i64, which is time units since unix epoch
44    TimeStampTZ(PoSQLTimeUnit, PoSQLTimeZone, i64),
45}
46
47impl LiteralValue {
48    /// Provides the column type associated with the column
49    #[must_use]
50    pub fn column_type(&self) -> ColumnType {
51        match self {
52            Self::Boolean(_) => ColumnType::Boolean,
53            Self::Uint8(_) => ColumnType::Uint8,
54            Self::TinyInt(_) => ColumnType::TinyInt,
55            Self::SmallInt(_) => ColumnType::SmallInt,
56            Self::Int(_) => ColumnType::Int,
57            Self::BigInt(_) => ColumnType::BigInt,
58            Self::VarChar(_) => ColumnType::VarChar,
59            Self::Int128(_) => ColumnType::Int128,
60            Self::Scalar(_) => ColumnType::Scalar,
61            Self::Decimal75(precision, scale, _) => ColumnType::Decimal75(*precision, *scale),
62            Self::TimeStampTZ(tu, tz, _) => ColumnType::TimestampTZ(*tu, *tz),
63        }
64    }
65
66    /// Converts the literal to a scalar
67    pub(crate) fn to_scalar<S: Scalar>(&self) -> S {
68        match self {
69            Self::Boolean(b) => b.into(),
70            Self::Uint8(i) => i.into(),
71            Self::TinyInt(i) => i.into(),
72            Self::SmallInt(i) => i.into(),
73            Self::Int(i) => i.into(),
74            Self::BigInt(i) => i.into(),
75            Self::VarChar(str) => str.into(),
76            Self::Decimal75(_, _, i) => i.into_scalar(),
77            Self::Int128(i) => i.into(),
78            Self::Scalar(limbs) => (*limbs).into(),
79            Self::TimeStampTZ(_, _, time) => time.into(),
80        }
81    }
82}