velesdb_core/velesql/ast/
values.rs1use serde::{Deserialize, Serialize};
7
8#[non_exhaustive]
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11pub enum VectorExpr {
12 Literal(Vec<f32>),
14 Parameter(String),
16}
17
18#[non_exhaustive]
20#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
21pub enum Value {
22 Integer(i64),
24 UnsignedInteger(u64),
26 Float(f64),
28 String(String),
30 Boolean(bool),
32 Null,
34 Parameter(String),
36 Temporal(TemporalExpr),
38 Subquery(Box<Subquery>),
40}
41
42impl From<i64> for Value {
43 fn from(v: i64) -> Self {
44 Self::Integer(v)
45 }
46}
47
48impl From<u64> for Value {
49 fn from(v: u64) -> Self {
50 Self::UnsignedInteger(v)
51 }
52}
53
54impl From<f64> for Value {
55 fn from(v: f64) -> Self {
56 Self::Float(v)
57 }
58}
59
60impl From<&str> for Value {
61 fn from(v: &str) -> Self {
62 Self::String(v.to_string())
63 }
64}
65
66impl From<String> for Value {
67 fn from(v: String) -> Self {
68 Self::String(v)
69 }
70}
71
72impl From<bool> for Value {
73 fn from(v: bool) -> Self {
74 Self::Boolean(v)
75 }
76}
77
78#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80pub struct Subquery {
81 pub select: super::select::SelectStatement,
83 #[serde(default)]
85 pub correlations: Vec<CorrelatedColumn>,
86}
87
88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
90pub struct CorrelatedColumn {
91 pub outer_table: String,
93 pub outer_column: String,
95 pub inner_column: String,
97}
98
99#[non_exhaustive]
101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
102pub enum TemporalExpr {
103 Now,
105 Interval(IntervalValue),
107 Subtract(Box<TemporalExpr>, Box<TemporalExpr>),
109 Add(Box<TemporalExpr>, Box<TemporalExpr>),
111}
112
113impl TemporalExpr {
114 #[must_use]
116 pub fn to_epoch_seconds(&self) -> i64 {
117 use std::time::{SystemTime, UNIX_EPOCH};
118
119 let now = SystemTime::now()
122 .duration_since(UNIX_EPOCH)
123 .map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX));
124
125 match self {
126 Self::Now => now,
127 Self::Interval(iv) => iv.to_seconds(),
128 Self::Subtract(left, right) => left.to_epoch_seconds() - right.to_epoch_seconds(),
129 Self::Add(left, right) => left.to_epoch_seconds() + right.to_epoch_seconds(),
130 }
131 }
132}
133
134#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub struct IntervalValue {
137 pub magnitude: i64,
139 pub unit: IntervalUnit,
141}
142
143impl IntervalValue {
144 #[must_use]
146 pub fn to_seconds(&self) -> i64 {
147 match self.unit {
148 IntervalUnit::Seconds => self.magnitude,
149 IntervalUnit::Minutes => self.magnitude * 60,
150 IntervalUnit::Hours => self.magnitude * 3600,
151 IntervalUnit::Days => self.magnitude * 86400,
152 IntervalUnit::Weeks => self.magnitude * 604_800,
153 IntervalUnit::Months => self.magnitude * 2_592_000,
154 }
155 }
156}
157
158impl Value {
159 #[must_use]
164 pub fn is_subquery(&self) -> bool {
165 matches!(self, Self::Subquery(_))
166 }
167
168 #[must_use]
175 pub fn to_json(&self) -> serde_json::Value {
176 match self {
177 Self::Integer(i) => serde_json::json!(i),
178 Self::UnsignedInteger(u) => serde_json::json!(u),
179 Self::Float(f) => serde_json::json!(f),
180 Self::String(s) => serde_json::json!(s),
181 Self::Boolean(b) => serde_json::json!(b),
182 Self::Parameter(p) => serde_json::json!(format!("${p}")),
183 Self::Temporal(t) => serde_json::json!(t.to_epoch_seconds()),
184 Self::Null | Self::Subquery(_) => serde_json::Value::Null,
185 }
186 }
187}
188
189#[non_exhaustive]
191#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
192pub enum IntervalUnit {
193 Seconds,
195 Minutes,
197 Hours,
199 Days,
201 Weeks,
203 Months,
205}