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
use super::{TableCommitment, VecCommitmentExt};
use crate::base::database::{
    ColumnField, ColumnRef, ColumnType, CommitmentAccessor, MetadataAccessor, SchemaAccessor,
    TableRef,
};
use proof_of_sql_parser::Identifier;
use std::collections::HashMap;

/// The commitments for all of the tables in a query.
///
/// Simply maps table refs to table commitments, and implements the following traits...
/// - [`MetadataAccessor`]
/// - [`CommitmentAccessor`]
/// - [`SchemaAccessor`]
pub type QueryCommitments<C> = HashMap<TableRef, TableCommitment<C>>;

/// A trait for extending the functionality of the [`QueryCommitments`] alias.
pub trait QueryCommitmentsExt<C>
where
    Vec<C>: VecCommitmentExt,
    Decompressed<C>: Into<C>,
{
    /// Create a new `QueryCommitments` from a collection of columns and an accessor.
    fn from_accessor_with_max_bounds(
        columns: impl IntoIterator<Item = ColumnRef>,
        accessor: &(impl CommitmentAccessor<Decompressed<C>> + SchemaAccessor),
    ) -> Self;
}

impl<C> QueryCommitmentsExt<C> for QueryCommitments<C>
where
    Vec<C>: VecCommitmentExt,
    Decompressed<C>: Into<C>,
{
    fn from_accessor_with_max_bounds(
        columns: impl IntoIterator<Item = ColumnRef>,
        accessor: &(impl CommitmentAccessor<Decompressed<C>> + SchemaAccessor),
    ) -> Self {
        columns
            .into_iter()
            .fold(HashMap::<_, Vec<_>>::new(), |mut table_columns, column| {
                table_columns
                    .entry(column.table_ref())
                    .or_default()
                    .push(ColumnField::new(column.column_id(), *column.column_type()));
                table_columns
            })
            .into_iter()
            .map(|(table_ref, columns)| {
                (
                    table_ref,
                    TableCommitment::from_accessor_with_max_bounds(
                        table_ref,
                        &Vec::from_iter(
                            accessor
                                .lookup_schema(table_ref)
                                .iter()
                                .filter_map(|c| columns.iter().find(|x| x.name() == c.0).copied()),
                        ),
                        accessor,
                    ),
                )
            })
            .collect()
    }
}

impl<C> MetadataAccessor for QueryCommitments<C>
where
    Vec<C>: VecCommitmentExt,
{
    fn get_length(&self, table_ref: crate::base::database::TableRef) -> usize {
        let table_commitment = self.get(&table_ref).unwrap();

        table_commitment.num_rows()
    }

    fn get_offset(&self, table_ref: crate::base::database::TableRef) -> usize {
        let table_commitment = self.get(&table_ref).unwrap();

        table_commitment.range().start
    }
}

/// Private convenience alias.
type Decompressed<C> = <Vec<C> as VecCommitmentExt>::DecompressedCommitment;

impl<C> CommitmentAccessor<Decompressed<C>> for QueryCommitments<C>
where
    Vec<C>: VecCommitmentExt,
{
    fn get_commitment(&self, column: ColumnRef) -> Decompressed<C> {
        let table_commitment = self.get(&column.table_ref()).unwrap();

        table_commitment
            .column_commitments()
            .get_commitment(&column.column_id())
            .unwrap()
    }
}

impl<C> SchemaAccessor for QueryCommitments<C>
where
    Vec<C>: VecCommitmentExt,
{
    fn lookup_column(
        &self,
        table_ref: crate::base::database::TableRef,
        column_id: Identifier,
    ) -> Option<ColumnType> {
        let table_commitment = self.get(&table_ref)?;

        table_commitment
            .column_commitments()
            .get_metadata(&column_id)
            .map(|column_metadata| *column_metadata.column_type())
    }

    fn lookup_schema(
        &self,
        table_ref: crate::base::database::TableRef,
    ) -> Vec<(Identifier, ColumnType)> {
        let table_commitment = self.get(&table_ref).unwrap();

        table_commitment
            .column_commitments()
            .column_metadata()
            .iter()
            .map(|(identifier, column_metadata)| (*identifier, *column_metadata.column_type()))
            .collect()
    }
}

#[cfg(all(test, feature = "blitzar"))]
mod tests {
    use super::*;
    use crate::{
        base::{
            commitment::{Bounds, ColumnBounds},
            database::{
                owned_table_utility::*, OwnedColumn, OwnedTable, OwnedTableTestAccessor,
                TestAccessor,
            },
            scalar::Curve25519Scalar,
        },
        proof_primitive::dory::{DoryCommitment, DoryEvaluationProof, DoryProverPublicSetup},
    };
    use ark_std::test_rng;
    use curve25519_dalek::RistrettoPoint;

    #[test]
    fn we_can_get_length_and_offset_of_tables() {
        let table_a: OwnedTable<Curve25519Scalar> = owned_table([
            bigint("column_a", [1, 2, 3, 4]),
            varchar("column_b", ["Lorem", "ipsum", "dolor", "sit"]),
        ]);

        let table_b: OwnedTable<Curve25519Scalar> = owned_table([scalar("column_c", [1, 2])]);

        let offset_commitment =
            TableCommitment::<RistrettoPoint>::from_owned_table_with_offset(&table_a, 2, &());
        let offset_table_id = "off.table".parse().unwrap();

        let no_offset_commitment = TableCommitment::from_owned_table_with_offset(&table_b, 0, &());
        let no_offset_id = "no.off".parse().unwrap();

        let no_columns_commitment = TableCommitment::try_from_columns_with_offset(
            Vec::<(&Identifier, &OwnedColumn<Curve25519Scalar>)>::new(),
            0,
            &(),
        )
        .unwrap();
        let no_columns_id = "no.columns".parse().unwrap();

        let no_rows_commitment = TableCommitment::try_from_columns_with_offset(
            [(
                &"column_c".parse().unwrap(),
                &OwnedColumn::<Curve25519Scalar>::BigInt(vec![]),
            )],
            3,
            &(),
        )
        .unwrap();
        let no_rows_id = "no.rows".parse().unwrap();

        let query_commitments = QueryCommitments::from_iter([
            (offset_table_id, offset_commitment),
            (no_offset_id, no_offset_commitment),
            (no_columns_id, no_columns_commitment),
            (no_rows_id, no_rows_commitment),
        ]);

        assert_eq!(query_commitments.get_offset(offset_table_id), 2);
        assert_eq!(query_commitments.get_length(offset_table_id), 4);

        assert_eq!(query_commitments.get_offset(no_offset_id), 0);
        assert_eq!(query_commitments.get_length(no_offset_id), 2);

        assert_eq!(query_commitments.get_offset(no_columns_id), 0);
        assert_eq!(query_commitments.get_length(no_columns_id), 0);

        assert_eq!(query_commitments.get_offset(no_rows_id), 3);
        assert_eq!(query_commitments.get_length(no_rows_id), 0);
    }

    #[test]
    fn we_can_get_commitment_of_a_column() {
        let column_a_id: Identifier = "column_a".parse().unwrap();
        let column_b_id: Identifier = "column_b".parse().unwrap();

        let table_a: OwnedTable<Curve25519Scalar> = owned_table([
            bigint(column_a_id, [1, 2, 3, 4]),
            varchar(column_b_id, ["Lorem", "ipsum", "dolor", "sit"]),
        ]);
        let table_b: OwnedTable<Curve25519Scalar> = owned_table([scalar(column_a_id, [1, 2])]);

        let table_a_commitment =
            TableCommitment::<RistrettoPoint>::from_owned_table_with_offset(&table_a, 2, &());
        let table_a_id = "table.a".parse().unwrap();

        let table_b_commitment = TableCommitment::from_owned_table_with_offset(&table_b, 0, &());
        let table_b_id = "table.b".parse().unwrap();

        let query_commitments = QueryCommitments::from_iter([
            (table_a_id, table_a_commitment.clone()),
            (table_b_id, table_b_commitment.clone()),
        ]);

        assert_eq!(
            query_commitments.get_commitment(ColumnRef::new(
                table_a_id,
                column_a_id,
                ColumnType::BigInt
            )),
            table_a_commitment.column_commitments().commitments()[0]
        );
        assert_eq!(
            query_commitments.get_commitment(ColumnRef::new(
                table_a_id,
                column_b_id,
                ColumnType::VarChar
            )),
            table_a_commitment.column_commitments().commitments()[1]
        );
        assert_eq!(
            query_commitments.get_commitment(ColumnRef::new(
                table_b_id,
                column_a_id,
                ColumnType::Scalar
            )),
            table_b_commitment.column_commitments().commitments()[0]
        );
    }

    #[test]
    fn we_can_get_schema_of_tables() {
        let column_a_id: Identifier = "column_a".parse().unwrap();
        let column_b_id: Identifier = "column_b".parse().unwrap();

        let table_a: OwnedTable<Curve25519Scalar> = owned_table([
            bigint(column_a_id, [1, 2, 3, 4]),
            varchar(column_b_id, ["Lorem", "ipsum", "dolor", "sit"]),
        ]);
        let table_b: OwnedTable<Curve25519Scalar> = owned_table([scalar(column_a_id, [1, 2])]);

        let table_a_commitment =
            TableCommitment::<RistrettoPoint>::from_owned_table_with_offset(&table_a, 2, &());
        let table_a_id = "table.a".parse().unwrap();

        let table_b_commitment = TableCommitment::from_owned_table_with_offset(&table_b, 0, &());
        let table_b_id = "table.b".parse().unwrap();

        let no_columns_commitment = TableCommitment::try_from_columns_with_offset(
            Vec::<(&Identifier, &OwnedColumn<Curve25519Scalar>)>::new(),
            0,
            &(),
        )
        .unwrap();
        let no_columns_id = "no.columns".parse().unwrap();

        let query_commitments = QueryCommitments::from_iter([
            (table_a_id, table_a_commitment),
            (table_b_id, table_b_commitment),
            (no_columns_id, no_columns_commitment),
        ]);

        assert_eq!(
            query_commitments
                .lookup_column(table_a_id, column_a_id)
                .unwrap(),
            ColumnType::BigInt
        );
        assert_eq!(
            query_commitments
                .lookup_column(table_a_id, column_b_id)
                .unwrap(),
            ColumnType::VarChar
        );
        assert_eq!(
            query_commitments.lookup_schema(table_a_id),
            vec![
                (column_a_id, ColumnType::BigInt),
                (column_b_id, ColumnType::VarChar)
            ]
        );

        assert_eq!(
            query_commitments
                .lookup_column(table_b_id, column_a_id)
                .unwrap(),
            ColumnType::Scalar
        );
        assert_eq!(
            query_commitments.lookup_column(table_b_id, column_b_id),
            None
        );
        assert_eq!(
            query_commitments.lookup_schema(table_b_id),
            vec![(column_a_id, ColumnType::Scalar),]
        );

        assert_eq!(
            query_commitments.lookup_column(no_columns_id, column_a_id),
            None
        );
        assert_eq!(query_commitments.lookup_schema(no_columns_id), vec![]);
    }

    #[test]
    fn we_can_get_query_commitments_from_accessor() {
        let setup = DoryProverPublicSetup::rand(4, 3, &mut test_rng());

        let column_a_id: Identifier = "column_a".parse().unwrap();
        let column_b_id: Identifier = "column_b".parse().unwrap();

        let table_a = owned_table([
            bigint(column_a_id, [1, 2, 3, 4]),
            varchar(column_b_id, ["Lorem", "ipsum", "dolor", "sit"]),
        ]);
        let table_b = owned_table([scalar(column_a_id, [1, 2]), int128(column_b_id, [1, 2])]);

        let mut table_a_commitment =
            TableCommitment::from_owned_table_with_offset(&table_a, 0, &setup);
        let table_a_id = "table.a".parse().unwrap();
        *table_a_commitment
            .column_commitments_mut()
            .column_metadata_mut()
            .get_mut(&column_a_id)
            .unwrap()
            .bounds_mut() = ColumnBounds::BigInt(Bounds::bounded(i64::MIN, i64::MAX).unwrap());

        let mut table_b_commitment =
            TableCommitment::from_owned_table_with_offset(&table_b, 0, &setup);
        let table_b_id = "table.b".parse().unwrap();
        *table_b_commitment
            .column_commitments_mut()
            .column_metadata_mut()
            .get_mut(&column_b_id)
            .unwrap()
            .bounds_mut() = ColumnBounds::Int128(Bounds::bounded(i128::MIN, i128::MAX).unwrap());

        let expected_query_commitments = QueryCommitments::from_iter([
            (table_a_id, table_a_commitment.clone()),
            (table_b_id, table_b_commitment.clone()),
        ]);

        let mut accessor =
            OwnedTableTestAccessor::<DoryEvaluationProof>::new_empty_with_setup(setup);
        accessor.add_table(table_a_id, table_a, 0);
        accessor.add_table(table_b_id, table_b, 0);

        let query_commitments = QueryCommitments::<DoryCommitment>::from_accessor_with_max_bounds(
            [
                ColumnRef::new(table_a_id, column_a_id, ColumnType::BigInt),
                ColumnRef::new(table_b_id, column_a_id, ColumnType::Scalar),
                ColumnRef::new(table_a_id, column_b_id, ColumnType::VarChar),
                ColumnRef::new(table_b_id, column_b_id, ColumnType::Int128),
            ],
            &accessor,
        );
        assert_eq!(query_commitments, expected_query_commitments);
    }
}