proof-of-sql 0.129.1

High performance zero knowledge (ZK) prover for SQL.
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use super::{
    Column, ColumnField, ColumnOperationError, ColumnOperationResult, ColumnType, Table,
    TableOperationError, TableOperationResult, TableOptions,
};
use crate::base::scalar::Scalar;
use alloc::vec::Vec;
use bumpalo::Bump;

/// Check if two schemas are compatible
/// Note that we can tolerate differences in column names but not in column types
fn are_schemas_compatible(left: &[ColumnField], right: &[ColumnField]) -> bool {
    left.len() == right.len()
        && left
            .iter()
            .zip(right)
            .all(|(field1, field2)| field1.data_type() == field2.data_type())
}

/// Union multiple columns of the same type into a single column
///
/// # Panics
/// This function should never panic as long as it is written correctly
#[expect(clippy::too_many_lines)]
pub fn column_union<'a, S: Scalar>(
    columns: &[&Column<'a, S>],
    alloc: &'a Bump,
    column_type: ColumnType,
) -> ColumnOperationResult<Column<'a, S>> {
    // Check for type mismatch
    let possible_bad_column_type = columns.iter().find_map(|col| {
        let found_column_type = col.column_type();
        (found_column_type != column_type).then_some(found_column_type)
    });
    if let Some(bad_column_type) = possible_bad_column_type {
        return Err(ColumnOperationError::UnionDifferentTypes {
            actual_type: bad_column_type,
            correct_type: column_type,
        });
    }
    // First, calculate the total length of the combined columns
    let len: usize = columns.iter().map(|col| col.len()).sum();

    Ok(match column_type {
        ColumnType::Boolean => {
            // Define a mutable iterator outside the closure
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_boolean().expect("Column types should match"))
                .copied();

            Column::Boolean(alloc.alloc_slice_fill_with(len, |_| {
                // Use iter.next() to get the next element
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::Uint8 => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_uint8().expect("Column types should match"))
                .copied();

            Column::Uint8(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::TinyInt => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_tinyint().expect("Column types should match"))
                .copied();

            Column::TinyInt(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::SmallInt => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_smallint().expect("Column types should match"))
                .copied();

            Column::SmallInt(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::Int => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_int().expect("Column types should match"))
                .copied();

            Column::Int(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::BigInt => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_bigint().expect("Column types should match"))
                .copied();

            Column::BigInt(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::Int128 => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_int128().expect("Column types should match"))
                .copied();

            Column::Int128(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::Scalar => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_scalar().expect("Column types should match"))
                .copied();

            Column::Scalar(alloc.alloc_slice_fill_with(len, |_| {
                iter.next().expect("Iterator should have enough elements")
            }) as &[_])
        }
        ColumnType::Decimal75(precision, scale) => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_decimal75().expect("Column types should match"))
                .copied();

            Column::Decimal75(
                precision,
                scale,
                alloc.alloc_slice_fill_with(len, |_| {
                    iter.next().expect("Iterator should have enough elements")
                }) as &[_],
            )
        }
        ColumnType::VarChar => {
            let (nested_results, nested_scalars): (Vec<_>, Vec<_>) = columns
                .iter()
                .map(|col| col.as_varchar().expect("Column types should match"))
                .unzip();

            // Create iterators for both results and scalars
            let mut result_iter = nested_results.into_iter().flatten().copied();
            let mut scalar_iter = nested_scalars.into_iter().flatten().copied();

            Column::VarChar((
                alloc.alloc_slice_fill_with(len, |_| {
                    result_iter
                        .next()
                        .expect("Iterator should have enough elements")
                }) as &[_],
                alloc.alloc_slice_fill_with(len, |_| {
                    scalar_iter
                        .next()
                        .expect("Iterator should have enough elements")
                }) as &[_],
            ))
        }
        ColumnType::VarBinary => {
            let (nested_results, nested_scalars): (Vec<_>, Vec<_>) = columns
                .iter()
                .map(|col| col.as_varbinary().expect("Column types should match"))
                .unzip();

            // Create iterators for both results and scalars
            let mut result_iter = nested_results.into_iter().flatten().copied();
            let mut scalar_iter = nested_scalars.into_iter().flatten().copied();

            Column::VarBinary((
                alloc.alloc_slice_fill_with(len, |_| {
                    result_iter
                        .next()
                        .expect("Iterator should have enough elements")
                }) as &[_],
                alloc.alloc_slice_fill_with(len, |_| {
                    scalar_iter
                        .next()
                        .expect("Iterator should have enough elements")
                }) as &[_],
            ))
        }
        ColumnType::TimestampTZ(tu, tz) => {
            let mut iter = columns
                .iter()
                .flat_map(|col| col.as_timestamptz().expect("Column types should match"))
                .copied();

            Column::TimestampTZ(
                tu,
                tz,
                alloc.alloc_slice_fill_with(len, |_| {
                    iter.next().expect("Iterator should have enough elements")
                }) as &[_],
            )
        }
    })
}

/// Union multiple tables with compatible schemas into a single table
#[expect(clippy::missing_panics_doc)]
pub fn table_union<'a, S: Scalar>(
    tables: &[Table<'a, S>],
    alloc: &'a Bump,
) -> TableOperationResult<Table<'a, S>> {
    // Check schema equality
    let mut tables_iter = tables.iter();
    let schema = tables_iter
        .next()
        .ok_or(TableOperationError::UnionNotEnoughTables)?
        .schema();
    let possible_bad_schema = tables_iter.find_map(|table| {
        let candidate_schema = table.schema();
        (!are_schemas_compatible(&schema, &candidate_schema)).then_some(candidate_schema)
    });
    if let Some(bad_schema) = possible_bad_schema {
        return Err(TableOperationError::UnionIncompatibleSchemas {
            actual_schema: bad_schema.clone(),
            correct_schema: schema,
        });
    }
    // Union the columns
    // Make sure to consider the case where the tables have no columns
    let num_rows = tables.iter().map(Table::num_rows).sum();
    let result = Table::<'a, S>::try_from_iter_with_options(
        schema.iter().enumerate().map(|(i, field)| {
            let columns: Vec<_> = tables
                .iter()
                .map(|table| table.column(i).expect("Schemas should be compatible"))
                .collect();
            (
                field.name(),
                column_union(&columns, alloc, field.data_type()).expect("Failed to union columns"),
            )
        }),
        TableOptions::new(Some(num_rows)),
    )
    .expect("Failed to create table from iterator");
    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::base::{map::IndexMap, scalar::test_scalar::TestScalar};

    #[test]
    fn we_can_union_no_columns() {
        let alloc = Bump::new();
        let result = column_union::<TestScalar>(&[], &alloc, ColumnType::BigInt).unwrap();
        assert_eq!(result, Column::BigInt(&[]));
    }

    #[test]
    fn we_can_union_columns_of_the_same_type() {
        let alloc = Bump::new();
        let col0: Column<TestScalar> = Column::BigInt(&[]);
        let col1: Column<TestScalar> = Column::BigInt(&[1, 2, 3]);
        let col2: Column<TestScalar> = Column::BigInt(&[4, 5, 6]);
        let col3: Column<TestScalar> = Column::BigInt(&[7, 8, 9]);
        let result =
            column_union(&[&col0, &col1, &col2, &col3], &alloc, ColumnType::BigInt).unwrap();
        assert_eq!(result, Column::BigInt(&[1, 2, 3, 4, 5, 6, 7, 8, 9]));

        let strings = vec!["a", "b", "c"];
        let scalars = strings
            .iter()
            .map(|s| TestScalar::from(*s))
            .collect::<Vec<_>>();
        let col0: Column<TestScalar> = Column::VarChar((&strings, &scalars));
        let col1: Column<TestScalar> = Column::VarChar((&strings, &scalars));
        let result = column_union(&[&col0, &col1], &alloc, ColumnType::VarChar).unwrap();
        let doubled_strings: Vec<_> = strings.iter().chain(strings.iter()).copied().collect();
        let doubled_scalars: Vec<_> = scalars.iter().chain(scalars.iter()).copied().collect();
        assert_eq!(
            result,
            Column::VarChar((&doubled_strings, &doubled_scalars))
        );
    }

    #[test]
    fn we_cannot_union_columns_with_wrong_types() {
        let alloc = Bump::new();
        let col0: Column<TestScalar> = Column::BigInt(&[]);
        let result = column_union(&[&col0], &alloc, ColumnType::Int);
        assert!(matches!(
            result,
            Err(ColumnOperationError::UnionDifferentTypes { .. })
        ));
    }

    #[test]
    fn we_cannot_union_no_tables() {
        let alloc = Bump::new();
        let result = table_union::<TestScalar>(&[], &alloc).unwrap_err();
        assert!(matches!(result, TableOperationError::UnionNotEnoughTables));
    }

    #[test]
    fn we_can_union_tables_without_columns() {
        let alloc = Bump::new();
        let table0 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::default(),
            TableOptions::new(Some(2)),
        )
        .unwrap();
        let table1 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::default(),
            TableOptions::new(Some(5)),
        )
        .unwrap();
        let table2 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::default(),
            TableOptions::new(Some(0)),
        )
        .unwrap();
        let result = table_union(&[table0, table1, table2], &alloc).unwrap();
        assert_eq!(
            result,
            Table::<'_, TestScalar>::try_new_with_options(
                IndexMap::default(),
                TableOptions::new(Some(7))
            )
            .unwrap()
        );
    }

    #[test]
    fn we_can_union_tables() {
        let alloc = Bump::new();
        // Column names don't matter
        let table0 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::from_iter(vec![
                ("a".into(), Column::BigInt(&[1, 2, 3])),
                ("b".into(), Column::BigInt(&[4, 5, 6])),
            ]),
            TableOptions::new(Some(3)),
        )
        .unwrap();
        let table1 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::from_iter(vec![
                ("c".into(), Column::BigInt(&[7, 8, 9])),
                ("d".into(), Column::BigInt(&[10, 11, 12])),
            ]),
            TableOptions::new(Some(3)),
        )
        .unwrap();
        let result = table_union(&[table0, table1], &alloc).unwrap();
        assert_eq!(
            result,
            Table::<'_, TestScalar>::try_new_with_options(
                IndexMap::from_iter(vec![
                    ("a".into(), Column::BigInt(&[1, 2, 3, 7, 8, 9])),
                    ("b".into(), Column::BigInt(&[4, 5, 6, 10, 11, 12])),
                ]),
                TableOptions::new(Some(6)),
            )
            .unwrap()
        );
    }

    #[test]
    fn we_cannot_union_tables_with_incompatible_schema() {
        let alloc = Bump::new();
        // Any difference in column types between a table and the result schema will do
        // regardless of whether the tables have the same schema
        let table0 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::from_iter(vec![
                ("a".into(), Column::BigInt(&[1, 2, 3])),
                ("b".into(), Column::BigInt(&[4, 5, 6])),
            ]),
            TableOptions::new(Some(3)),
        )
        .unwrap();
        let table1 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::from_iter(vec![
                ("c".into(), Column::BigInt(&[7, 8, 9])),
                ("d".into(), Column::Int(&[10, 11, 12])),
            ]),
            TableOptions::new(Some(3)),
        )
        .unwrap();
        let result = table_union(&[table0, table1], &alloc);
        assert!(matches!(
            result,
            Err(TableOperationError::UnionIncompatibleSchemas { .. })
        ));
    }

    #[test]
    fn we_can_union_varbinary_columns() {
        let alloc = Bump::new();

        let raw0 = [b"foo".as_ref(), b"bar".as_ref()];
        let scalars0: Vec<TestScalar> = raw0
            .iter()
            .map(|b| TestScalar::from_le_bytes_mod_order(b))
            .collect();
        let col0: Column<TestScalar> = Column::VarBinary((raw0.as_slice(), scalars0.as_slice()));

        let raw1 = [b"baz".as_ref(), b"qux".as_ref()];
        let scalars1: Vec<TestScalar> = raw1
            .iter()
            .map(|b| TestScalar::from_le_bytes_mod_order(b))
            .collect();
        let col1: Column<TestScalar> = Column::VarBinary((raw1.as_slice(), scalars1.as_slice()));

        let result = column_union(&[&col0, &col1], &alloc, ColumnType::VarBinary).unwrap();

        let expected_raw = [
            b"foo".as_ref(),
            b"bar".as_ref(),
            b"baz".as_ref(),
            b"qux".as_ref(),
        ];
        let expected_scalars: Vec<TestScalar> = expected_raw
            .iter()
            .map(|b| TestScalar::from_le_bytes_mod_order(b))
            .collect();
        assert_eq!(
            result,
            Column::VarBinary((expected_raw.as_slice(), expected_scalars.as_slice()))
        );
    }

    #[test]
    fn we_can_union_tables_with_varbinary_columns() {
        let alloc = Bump::new();
        let binary_binding = [b"foo".as_ref(), b"bar".as_ref()];
        let scalar_binding = [
            TestScalar::from_le_bytes_mod_order(b"foo"),
            TestScalar::from_le_bytes_mod_order(b"bar"),
        ];
        let table0 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::from_iter(vec![(
                "vb".into(),
                Column::VarBinary((binary_binding.as_slice(), scalar_binding.as_slice())),
            )]),
            TableOptions::new(Some(2)),
        )
        .unwrap();

        let binary_binding = [b"baz".as_ref(), b"qux".as_ref()];
        let scalar_binding2 = [
            TestScalar::from_le_bytes_mod_order(b"baz"),
            TestScalar::from_le_bytes_mod_order(b"qux"),
        ];
        let table1 = Table::<'_, TestScalar>::try_new_with_options(
            IndexMap::from_iter(vec![(
                "some_name".into(),
                Column::VarBinary((binary_binding.as_slice(), scalar_binding2.as_slice())),
            )]),
            TableOptions::new(Some(2)),
        )
        .unwrap();

        let result = table_union(&[table0, table1], &alloc).unwrap();

        let expected_raw = [
            b"foo".as_ref(),
            b"bar".as_ref(),
            b"baz".as_ref(),
            b"qux".as_ref(),
        ];
        let expected_scalars: Vec<TestScalar> = expected_raw
            .iter()
            .map(|b| TestScalar::from_le_bytes_mod_order(b))
            .collect();

        let expected = Table::try_new_with_options(
            IndexMap::from_iter(vec![(
                "vb".into(),
                Column::VarBinary((expected_raw.as_slice(), expected_scalars.as_slice())),
            )]),
            TableOptions::new(Some(4)),
        )
        .unwrap();

        assert_eq!(result, expected);
    }
}