proof_of_sql/base/database/owned_table_utility.rs
1//! Utility functions for creating [`OwnedTable`]s and [`OwnedColumn`]s.
2//! These functions are primarily intended for use in tests.
3//!
4//! # Example
5//! ```
6//! use proof_of_sql::base::{database::owned_table_utility::*};
7//! # use proof_of_sql::base::scalar::MontScalar;
8//! # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
9//! let result = owned_table::<MyScalar>([
10//! bigint("a", [1, 2, 3]),
11//! boolean("b", [true, false, true]),
12//! int128("c", [1, 2, 3]),
13//! scalar("d", [1, 2, 3]),
14//! varchar("e", ["a", "b", "c"]),
15//! decimal75("f", 12, 1, [1, 2, 3]),
16//! ]);
17//! ```
18use super::{OwnedColumn, OwnedTable};
19use crate::base::{
20 posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
21 scalar::Scalar,
22};
23use alloc::{string::String, vec::Vec};
24use sqlparser::ast::Ident;
25
26/// Creates an [`OwnedTable`] from a list of `(Ident, OwnedColumn)` pairs.
27/// This is a convenience wrapper around [`OwnedTable::try_from_iter`] primarily for use in tests and
28/// intended to be used along with the other methods in this module (e.g. [bigint], [boolean], etc).
29/// The function will panic under a variety of conditions. See [`OwnedTable::try_from_iter`] for more details.
30///
31/// # Example
32/// ```
33/// use proof_of_sql::base::{database::owned_table_utility::*};
34/// # use proof_of_sql::base::scalar::MontScalar;
35/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
36/// let result = owned_table::<MyScalar>([
37/// bigint("a", [1, 2, 3]),
38/// boolean("b", [true, false, true]),
39/// int128("c", [1, 2, 3]),
40/// scalar("d", [1, 2, 3]),
41/// varchar("e", ["a", "b", "c"]),
42/// decimal75("f", 12, 1, [1, 2, 3]),
43/// ]);
44/// ```
45/// ///
46/// # Panics
47/// - Panics if converting the iterator into an `OwnedTable<S>` fails.
48pub fn owned_table<S: Scalar>(
49 iter: impl IntoIterator<Item = (Ident, OwnedColumn<S>)>,
50) -> OwnedTable<S> {
51 OwnedTable::try_from_iter(iter).unwrap()
52}
53
54/// Creates a (Ident, `OwnedColumn`) pair for a uint8 column.
55/// This is primarily intended for use in conjunction with [`owned_table`].
56/// # Example
57/// ```
58/// use proof_of_sql::base::{database::owned_table_utility::*};
59/// # use proof_of_sql::base::scalar::MontScalar;
60/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
61/// let result = owned_table::<MyScalar>([
62/// uint8("a", [1_u8, 2, 3]),
63/// ]);
64///```
65pub fn uint8<S: Scalar>(
66 name: impl Into<Ident>,
67 data: impl IntoIterator<Item = impl Into<u8>>,
68) -> (Ident, OwnedColumn<S>) {
69 (
70 name.into(),
71 OwnedColumn::Uint8(data.into_iter().map(Into::into).collect()),
72 )
73}
74
75/// Creates a (Ident, `OwnedColumn`) pair for a tinyint column.
76/// This is primarily intended for use in conjunction with [`owned_table`].
77/// # Example
78/// ```
79/// use proof_of_sql::base::{database::owned_table_utility::*};
80/// # use proof_of_sql::base::scalar::MontScalar;
81/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
82/// let result = owned_table::<MyScalar>([
83/// tinyint("a", [1_i8, 2, 3]),
84/// ]);
85///```
86pub fn tinyint<S: Scalar>(
87 name: impl Into<Ident>,
88 data: impl IntoIterator<Item = impl Into<i8>>,
89) -> (Ident, OwnedColumn<S>) {
90 (
91 name.into(),
92 OwnedColumn::TinyInt(data.into_iter().map(Into::into).collect()),
93 )
94}
95
96/// Creates a `(Ident, OwnedColumn)` pair for a smallint column.
97/// This is primarily intended for use in conjunction with [`owned_table`].
98/// # Example
99/// ```rust
100/// use proof_of_sql::base::{database::owned_table_utility::*};
101/// # use proof_of_sql::base::scalar::MontScalar;
102/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
103/// let result = owned_table::<MyScalar>([
104/// smallint("a", [1_i16, 2, 3]),
105/// ]);
106/// ```
107pub fn smallint<S: Scalar>(
108 name: impl Into<Ident>,
109 data: impl IntoIterator<Item = impl Into<i16>>,
110) -> (Ident, OwnedColumn<S>) {
111 (
112 name.into(),
113 OwnedColumn::SmallInt(data.into_iter().map(Into::into).collect()),
114 )
115}
116
117/// Creates a `(Ident, OwnedColumn)` pair for an int column.
118/// This is primarily intended for use in conjunction with [`owned_table`].
119/// # Example
120/// ```rust
121/// use proof_of_sql::base::{database::owned_table_utility::*};
122/// # use proof_of_sql::base::scalar::MontScalar;
123/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
124/// let result = owned_table::<MyScalar>([
125/// int("a", [1, 2, 3]),
126/// ]);
127/// ```
128pub fn int<S: Scalar>(
129 name: impl Into<Ident>,
130 data: impl IntoIterator<Item = impl Into<i32>>,
131) -> (Ident, OwnedColumn<S>) {
132 (
133 name.into(),
134 OwnedColumn::Int(data.into_iter().map(Into::into).collect()),
135 )
136}
137
138/// Creates a `(Ident, OwnedColumn)` pair for a bigint column.
139/// This is primarily intended for use in conjunction with [`owned_table`].
140/// # Example
141/// ```rust
142/// use proof_of_sql::base::{database::owned_table_utility::*};
143/// # use proof_of_sql::base::scalar::MontScalar;
144/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
145/// let result = owned_table::<MyScalar>([
146/// bigint("a", [1, 2, 3]),
147/// ]);
148/// ```
149pub fn bigint<S: Scalar>(
150 name: impl Into<Ident>,
151 data: impl IntoIterator<Item = impl Into<i64>>,
152) -> (Ident, OwnedColumn<S>) {
153 (
154 name.into(),
155 OwnedColumn::BigInt(data.into_iter().map(Into::into).collect()),
156 )
157}
158
159/// Creates a `(Ident, OwnedColumn)` pair for a boolean column.
160/// This is primarily intended for use in conjunction with [`owned_table`].
161/// # Example
162/// ```
163/// use proof_of_sql::base::{database::owned_table_utility::*};
164/// # use proof_of_sql::base::scalar::MontScalar;
165/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
166/// let result = owned_table::<MyScalar>([
167/// boolean("a", [true, false, true]),
168/// ]);
169/// ```
170pub fn boolean<S: Scalar>(
171 name: impl Into<Ident>,
172 data: impl IntoIterator<Item = impl Into<bool>>,
173) -> (Ident, OwnedColumn<S>) {
174 (
175 name.into(),
176 OwnedColumn::Boolean(data.into_iter().map(Into::into).collect()),
177 )
178}
179
180/// Creates a `(Ident, OwnedColumn)` pair for a int128 column.
181/// This is primarily intended for use in conjunction with [`owned_table`].
182/// # Example
183/// ```
184/// use proof_of_sql::base::{database::owned_table_utility::*};
185/// # use proof_of_sql::base::scalar::MontScalar;
186/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
187/// let result = owned_table::<MyScalar>([
188/// int128("a", [1, 2, 3]),
189/// ]);
190/// ```
191pub fn int128<S: Scalar>(
192 name: impl Into<Ident>,
193 data: impl IntoIterator<Item = impl Into<i128>>,
194) -> (Ident, OwnedColumn<S>) {
195 (
196 name.into(),
197 OwnedColumn::Int128(data.into_iter().map(Into::into).collect()),
198 )
199}
200
201/// Creates a `(Ident, OwnedColumn)` pair for a scalar column.
202/// This is primarily intended for use in conjunction with [`owned_table`].
203/// # Example
204/// ```
205/// use proof_of_sql::base::{database::owned_table_utility::*};
206/// # use proof_of_sql::base::scalar::MontScalar;
207/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
208/// let result = owned_table::<MyScalar>([
209/// scalar("a", [1, 2, 3]),
210/// ]);
211/// ```
212pub fn scalar<S: Scalar>(
213 name: impl Into<Ident>,
214 data: impl IntoIterator<Item = impl Into<S>>,
215) -> (Ident, OwnedColumn<S>) {
216 (
217 name.into(),
218 OwnedColumn::Scalar(data.into_iter().map(Into::into).collect()),
219 )
220}
221
222/// Creates a `(Ident, OwnedColumn)` pair for a varchar column.
223/// This is primarily intended for use in conjunction with [`owned_table`].
224/// # Example
225/// ```
226/// use proof_of_sql::base::{database::owned_table_utility::*};
227/// # use proof_of_sql::base::scalar::MontScalar;
228/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
229/// let result = owned_table::<MyScalar>([
230/// varchar("a", ["a", "b", "c"]),
231/// ]);
232/// ```
233pub fn varchar<S: Scalar>(
234 name: impl Into<Ident>,
235 data: impl IntoIterator<Item = impl Into<String>>,
236) -> (Ident, OwnedColumn<S>) {
237 (
238 name.into(),
239 OwnedColumn::VarChar(data.into_iter().map(Into::into).collect()),
240 )
241}
242
243/// Creates a `(Ident, OwnedColumn)` pair for a varbinary column.
244/// This is primarily intended for use in conjunction with [`owned_table`].
245/// # Example
246/// ```
247/// use proof_of_sql::base::{database::owned_table_utility::*};
248/// # use proof_of_sql::base::scalar::MontScalar;
249/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
250/// let result = owned_table::<MyScalar>([
251/// varbinary("a", [[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
252/// ]);
253/// ```
254pub fn varbinary<S: Scalar>(
255 name: impl Into<Ident>,
256 data: impl IntoIterator<Item = impl Into<Vec<u8>>>,
257) -> (Ident, OwnedColumn<S>) {
258 (
259 name.into(),
260 OwnedColumn::VarBinary(data.into_iter().map(Into::into).collect()),
261 )
262}
263
264/// Creates a `(Ident, OwnedColumn)` pair for a decimal75 column.
265/// This is primarily intended for use in conjunction with [`owned_table`].
266/// # Example
267/// ```
268/// use proof_of_sql::base::{database::owned_table_utility::*};
269/// # use proof_of_sql::base::scalar::MontScalar;
270/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
271/// let result = owned_table::<MyScalar>([
272/// decimal75("a", 12, 1, [1, 2, 3]),
273/// ]);
274/// ```
275///
276/// # Panics
277/// - Panics if creating the `Precision` from the specified precision value fails.
278pub fn decimal75<S: Scalar>(
279 name: impl Into<Ident>,
280 precision: u8,
281 scale: i8,
282 data: impl IntoIterator<Item = impl Into<S>>,
283) -> (Ident, OwnedColumn<S>) {
284 (
285 name.into(),
286 OwnedColumn::Decimal75(
287 crate::base::math::decimal::Precision::new(precision).unwrap(),
288 scale,
289 data.into_iter().map(Into::into).collect(),
290 ),
291 )
292}
293
294/// Creates a `(Ident, OwnedColumn)` pair for a timestamp column.
295/// This is primarily intended for use in conjunction with [`owned_table`].
296///
297/// # Parameters
298/// - `name`: The name of the column.
299/// - `time_unit`: The time unit of the timestamps.
300/// - `timezone`: The timezone for the timestamps.
301/// - `data`: The data for the column, provided as an iterator over `i64` values representing time since the unix epoch.
302///
303/// # Example
304/// ```
305/// use proof_of_sql::base::{database::owned_table_utility::*, posql_time::{PoSQLTimeZone, PoSQLTimeUnit}};
306/// # use proof_of_sql::base::scalar::MontScalar;
307/// # pub type MyScalar = MontScalar<ark_curve25519::FrConfig>;
308/// let result = owned_table::<MyScalar>([
309/// timestamptz("event_time", PoSQLTimeUnit::Second, PoSQLTimeZone::utc(), vec![1625072400, 1625076000, 1625079600]),
310/// ]);
311/// ```
312pub fn timestamptz<S: Scalar>(
313 name: impl Into<Ident>,
314 time_unit: PoSQLTimeUnit,
315 timezone: PoSQLTimeZone,
316 data: impl IntoIterator<Item = i64>,
317) -> (Ident, OwnedColumn<S>) {
318 (
319 name.into(),
320 OwnedColumn::TimestampTZ(time_unit, timezone, data.into_iter().collect()),
321 )
322}