datafusion-common 56.0.0

Common functionality for DataFusion query engine
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! FunctionalDependencies keeps track of functional dependencies
//! inside DFSchema.

use std::fmt::{Display, Formatter};
use std::ops::Deref;
use std::vec::IntoIter;

use crate::utils::{merge_and_order_indices, set_difference};
use crate::{DFSchema, HashSet, JoinType, NullEquality};

/// This object defines a constraint on a table.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum Constraint {
    /// Columns with the given indices form a composite primary key (they are
    /// jointly unique and not nullable):
    PrimaryKey(Vec<usize>),
    /// Columns with the given indices form a composite unique key:
    Unique(Vec<usize>),
}

/// This object encapsulates a list of functional constraints:
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd)]
pub struct Constraints {
    inner: Vec<Constraint>,
}

impl Constraints {
    /// Create a new [`Constraints`] object from the given `constraints`.
    /// Users should use the [`Constraints::default`] or [`SqlToRel::new_constraint_from_table_constraints`]
    /// functions for constructing [`Constraints`] instances. This constructor
    /// is for internal purposes only and does not check whether the argument
    /// is valid. The user is responsible for supplying a valid vector of
    /// [`Constraint`] objects.
    ///
    /// [`SqlToRel::new_constraint_from_table_constraints`]: https://docs.rs/datafusion/latest/datafusion/sql/planner/struct.SqlToRel.html#method.new_constraint_from_table_constraints
    pub fn new_unverified(constraints: Vec<Constraint>) -> Self {
        Self { inner: constraints }
    }

    /// Extends the current constraints with the given `other` constraints.
    pub fn extend(&mut self, other: Constraints) {
        self.inner.extend(other.inner);
    }

    /// Projects constraints using the given projection indices. Returns `None`
    /// if any of the constraint columns are not included in the projection.
    pub fn project(&self, proj_indices: &[usize]) -> Option<Self> {
        let projected = self
            .inner
            .iter()
            .filter_map(|constraint| {
                match constraint {
                    Constraint::PrimaryKey(indices) => {
                        let new_indices =
                            update_elements_with_matching_indices(indices, proj_indices);
                        // Only keep the constraint if all columns are preserved:
                        (new_indices.len() == indices.len())
                            .then_some(Constraint::PrimaryKey(new_indices))
                    }
                    Constraint::Unique(indices) => {
                        let new_indices =
                            update_elements_with_matching_indices(indices, proj_indices);
                        // Only keep the constraint if all columns are preserved:
                        (new_indices.len() == indices.len())
                            .then_some(Constraint::Unique(new_indices))
                    }
                }
            })
            .collect::<Vec<_>>();

        (!projected.is_empty()).then_some(Constraints::new_unverified(projected))
    }
}

impl IntoIterator for Constraints {
    type Item = Constraint;
    type IntoIter = IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.inner.into_iter()
    }
}

impl Display for Constraints {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let pk = self
            .inner
            .iter()
            .map(|c| format!("{c:?}"))
            .collect::<Vec<_>>();
        let pk = pk.join(", ");
        write!(f, "constraints=[{pk}]")
    }
}

impl Deref for Constraints {
    type Target = [Constraint];

    fn deref(&self) -> &Self::Target {
        self.inner.as_slice()
    }
}

/// This object defines a functional dependence in the schema. A functional
/// dependence defines a relationship between determinant keys and dependent
/// columns. A determinant key is a column, or a set of columns, whose value
/// uniquely determines values of some other (dependent) columns. If two rows
/// have the same determinant key, dependent columns in these rows are
/// necessarily the same. If the determinant key is unique, the set of
/// dependent columns is equal to the entire schema and the determinant key can
/// serve as a primary key. Note that a primary key may "downgrade" into a
/// determinant key due to an operation such as a join, and this object is
/// used to track dependence relationships in such cases. For more information
/// on functional dependencies, see:
/// <https://www.scaler.com/topics/dbms/functional-dependency-in-dbms/>
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionalDependence {
    // Column indices of the (possibly composite) determinant key:
    pub source_indices: Vec<usize>,
    // Column indices of dependent column(s):
    pub target_indices: Vec<usize>,
    /// Flag indicating whether one of the `source_indices` can receive NULL values.
    /// For a data source, if the constraint in question is `Constraint::Unique`,
    /// this flag is `true`. If the constraint in question is `Constraint::PrimaryKey`,
    /// this flag is `false`.
    /// Note that as the schema changes between different stages in a plan,
    /// such as after LEFT JOIN or RIGHT JOIN operations, this property may
    /// change.
    pub nullable: bool,
    /// The NULL-comparison semantics under which this dependency holds. The
    /// conservative default, [`NullEquality::NullEqualsNothing`], means it
    /// holds only across rows whose determinant contains no NULLs; e.g. a
    /// nullable `UNIQUE` constraint permits multiple NULL rows that may differ.
    /// [`NullEquality::NullEqualsNull`] means it also holds when NULL
    /// determinant values are treated as equal; e.g. a `GROUP BY` key.
    pub null_equality: NullEquality,
    // The functional dependency mode:
    pub mode: Dependency,
}

/// Describes functional dependency mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dependency {
    /// A determinant key may occur only once.
    Single,
    /// A determinant key may occur multiple times (in multiple rows).
    Multi,
}

impl FunctionalDependence {
    // Creates a new functional dependence.
    pub fn new(
        source_indices: Vec<usize>,
        target_indices: Vec<usize>,
        nullable: bool,
    ) -> Self {
        Self {
            source_indices,
            target_indices,
            nullable,
            // Assume the dependency does not hold across NULL rows by default:
            null_equality: NullEquality::NullEqualsNothing,
            // Start with the least restrictive mode by default:
            mode: Dependency::Multi,
        }
    }

    pub fn with_mode(mut self, mode: Dependency) -> Self {
        self.mode = mode;
        self
    }

    pub fn with_null_equality(mut self, null_equality: NullEquality) -> Self {
        self.null_equality = null_equality;
        self
    }

    /// Returns `true` if this dependency remains usable for operations that
    /// treat NULL determinant values as equal (`GROUP BY`, `DISTINCT` and
    /// sorting, which place all NULL keys together): it must hold under
    /// NULLs-are-equal semantics, have a non-nullable determinant (e.g. a
    /// `PRIMARY KEY`), or have no nullable source field in the given `schema`.
    pub fn is_valid_across_nulls(&self, schema: &DFSchema) -> bool {
        self.null_equality == NullEquality::NullEqualsNull
            || !self.nullable
            || self
                .source_indices
                .iter()
                .all(|&source_idx| !schema.field(source_idx).is_nullable())
    }
}

/// This object encapsulates all functional dependencies in a given relation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionalDependencies {
    deps: Vec<FunctionalDependence>,
}

impl FunctionalDependencies {
    /// Creates an empty `FunctionalDependencies` object.
    pub fn empty() -> Self {
        Self { deps: vec![] }
    }

    /// Creates a new `FunctionalDependencies` object from a vector of
    /// `FunctionalDependence` objects.
    pub fn new(dependencies: Vec<FunctionalDependence>) -> Self {
        Self { deps: dependencies }
    }

    /// Creates a new `FunctionalDependencies` object from the given constraints.
    pub fn new_from_constraints(
        constraints: Option<&Constraints>,
        n_field: usize,
    ) -> Self {
        if let Some(Constraints { inner: constraints }) = constraints {
            // Construct dependency objects based on each individual constraint:
            let dependencies = constraints
                .iter()
                .map(|constraint| {
                    // All the field indices are associated with the whole table
                    // since we are dealing with table level constraints:
                    let dependency = match constraint {
                        Constraint::PrimaryKey(indices) => FunctionalDependence::new(
                            indices.to_vec(),
                            (0..n_field).collect::<Vec<_>>(),
                            false,
                        ),
                        Constraint::Unique(indices) => FunctionalDependence::new(
                            indices.to_vec(),
                            (0..n_field).collect::<Vec<_>>(),
                            true,
                        ),
                    };
                    // As primary keys are guaranteed to be unique, set the
                    // functional dependency mode to `Dependency::Single`:
                    dependency.with_mode(Dependency::Single)
                })
                .collect::<Vec<_>>();
            Self::new(dependencies)
        } else {
            // There is no constraint, return an empty object:
            Self::empty()
        }
    }

    pub fn with_dependency(mut self, mode: Dependency) -> Self {
        self.deps.iter_mut().for_each(|item| item.mode = mode);
        self
    }

    /// Merges the given functional dependencies with these.
    pub fn extend(&mut self, other: FunctionalDependencies) {
        self.deps.extend(other.deps);
    }

    /// Sanity checks if functional dependencies are valid. For example, if
    /// there are 10 fields, we cannot receive any index further than 9.
    pub fn is_valid(&self, n_field: usize) -> bool {
        self.deps.iter().all(
            |FunctionalDependence {
                 source_indices,
                 target_indices,
                 ..
             }| {
                source_indices
                    .iter()
                    .max()
                    .map(|&max_index| max_index < n_field)
                    .unwrap_or(true)
                    && target_indices
                        .iter()
                        .max()
                        .map(|&max_index| max_index < n_field)
                        .unwrap_or(true)
            },
        )
    }

    /// Adds the `offset` value to `source_indices` and `target_indices` for
    /// each functional dependency.
    pub fn add_offset(&mut self, offset: usize) {
        self.deps.iter_mut().for_each(
            |FunctionalDependence {
                 source_indices,
                 target_indices,
                 ..
             }| {
                *source_indices = add_offset_to_vec(source_indices, offset);
                *target_indices = add_offset_to_vec(target_indices, offset);
            },
        )
    }

    /// Updates `source_indices` and `target_indices` of each functional
    /// dependence using the index mapping given in `proj_indices`.
    ///
    /// Assume that `proj_indices` is \[2, 5, 8\] and we have a functional
    /// dependence \[5\] (`source_indices`) -> \[5, 8\] (`target_indices`).
    /// In the updated schema, fields at indices \[2, 5, 8\] will transform
    /// to \[0, 1, 2\]. Therefore, the resulting functional dependence will
    /// be \[1\] -> \[1, 2\].
    pub fn project_functional_dependencies(
        &self,
        proj_indices: &[usize],
        // The argument `n_out` denotes the schema field length, which is needed
        // to correctly associate a `Single`-mode dependence with the whole table.
        n_out: usize,
    ) -> FunctionalDependencies {
        let mut projected_func_dependencies = Vec::with_capacity(self.deps.len());
        for FunctionalDependence {
            source_indices,
            target_indices,
            nullable,
            null_equality,
            mode,
        } in &self.deps
        {
            let new_source_indices =
                update_elements_with_matching_indices(source_indices, proj_indices);
            let new_target_indices = if *mode == Dependency::Single {
                // Associate with all of the fields in the schema:
                (0..n_out).collect()
            } else {
                // Update associations according to projection:
                update_elements_with_matching_indices(target_indices, proj_indices)
            };
            // All of the composite indices should still be valid after projection;
            // otherwise, functional dependency cannot be propagated.
            if new_source_indices.len() == source_indices.len() {
                let new_func_dependence = FunctionalDependence::new(
                    new_source_indices,
                    new_target_indices,
                    *nullable,
                )
                .with_mode(*mode)
                .with_null_equality(*null_equality);
                projected_func_dependencies.push(new_func_dependence);
            }
        }
        FunctionalDependencies::new(projected_func_dependencies)
    }

    /// This function joins this set of functional dependencies with the `other`
    /// according to the given `join_type`.
    pub fn join(
        &self,
        other: &FunctionalDependencies,
        join_type: &JoinType,
        left_cols_len: usize,
    ) -> FunctionalDependencies {
        // Get mutable copies of left and right side dependencies:
        let mut right_func_dependencies = other.clone();
        let mut left_func_dependencies = self.clone();

        match join_type {
            JoinType::Inner | JoinType::Left | JoinType::Right => {
                // Add offset to right schema:
                right_func_dependencies.add_offset(left_cols_len);

                // Result may have multiple values, update the dependency mode:
                left_func_dependencies =
                    left_func_dependencies.with_dependency(Dependency::Multi);
                right_func_dependencies =
                    right_func_dependencies.with_dependency(Dependency::Multi);

                if *join_type == JoinType::Left {
                    // Downgrade the right side, since it may have additional NULL values:
                    right_func_dependencies.downgrade_dependencies();
                } else if *join_type == JoinType::Right {
                    // Downgrade the left side, since it may have additional NULL values:
                    left_func_dependencies.downgrade_dependencies();
                }
                // Combine left and right functional dependencies:
                left_func_dependencies.extend(right_func_dependencies);
                left_func_dependencies
            }
            JoinType::LeftSemi | JoinType::LeftAnti | JoinType::LeftMark => {
                // These joins preserve functional dependencies of the left side:
                left_func_dependencies
            }
            JoinType::RightSemi | JoinType::RightAnti | JoinType::RightMark => {
                // These joins preserve functional dependencies of the right side:
                right_func_dependencies
            }
            JoinType::Full => {
                // All of the functional dependencies are lost in a FULL join:
                FunctionalDependencies::empty()
            }
        }
    }

    /// This function downgrades a functional dependency when nullability becomes
    /// a possibility:
    /// - If the dependency in question is UNIQUE (i.e. nullable), a new null value
    ///   invalidates the dependency.
    /// - If the dependency in question is PRIMARY KEY (i.e. not nullable), a new
    ///   null value turns it into UNIQUE mode.
    fn downgrade_dependencies(&mut self) {
        // Delete nullable dependencies, since they are no longer valid:
        self.deps.retain(|item| !item.nullable);
        // Survivors had a non-null determinant, so every new NULL comes from a
        // padded row whose targets are all NULL too:
        self.deps.iter_mut().for_each(|item| {
            item.nullable = true;
            item.null_equality = NullEquality::NullEqualsNull;
        });
    }

    /// This function ensures that functional dependencies involving uniquely
    /// occurring determinant keys cover their entire table in terms of
    /// dependent columns.
    pub fn extend_target_indices(&mut self, n_out: usize) {
        self.deps.iter_mut().for_each(
            |FunctionalDependence {
                 mode,
                 target_indices,
                 ..
             }| {
                // If unique, cover the whole table:
                if *mode == Dependency::Single {
                    *target_indices = (0..n_out).collect::<Vec<_>>();
                }
            },
        )
    }
}

impl Deref for FunctionalDependencies {
    type Target = [FunctionalDependence];

    fn deref(&self) -> &Self::Target {
        self.deps.as_slice()
    }
}

/// Calculates functional dependencies for aggregate output, when there is a GROUP BY expression.
///
/// `group_by_input_indices[i]` is the index of the input field that GROUP BY
/// expression `i` passes through unchanged (a column reference), or `None` if
/// the expression computes a new value. Only a column reference can carry an
/// input dependency; a computed expression such as `CAST(pk AS INT)` cannot,
/// even though its output name may equal the column's name.
pub fn aggregate_functional_dependencies(
    aggr_input_schema: &DFSchema,
    group_by_input_indices: &[Option<usize>],
    aggr_schema: &DFSchema,
) -> FunctionalDependencies {
    let mut aggregate_func_dependencies = vec![];
    let aggr_fields = aggr_schema.fields();
    // Association covers the whole table:
    let target_indices = (0..aggr_schema.fields().len()).collect::<Vec<_>>();
    // Get functional dependencies of the schema:
    let func_dependencies = aggr_input_schema.functional_dependencies();
    // The loop below only re-expresses input dependencies. Skip it when the
    // input has none. The GROUP BY-key dependency below always runs.
    if !func_dependencies.is_empty() {
        // Compare the columns covered by every dependency, whatever its NULL
        // semantics: leaving out the ones that do not hold across NULLs would
        // hide GROUP BY expressions this determinant does not cover and
        // upgrade its mode to `Single` wrongly.
        let existing_target_indices = get_target_indices_from_dependencies(
            func_dependencies.iter(),
            group_by_input_indices,
        );
        for FunctionalDependence {
            source_indices,
            nullable,
            null_equality,
            mode,
            ..
        } in &func_dependencies.deps
        {
            // Indices into the GROUP BY list for this determinant:
            let mut new_source_indices = vec![];
            let mut new_source_input_indices = vec![];
            for (idx, input_idx) in group_by_input_indices.iter().enumerate() {
                // When one of the input determinant columns is a GROUP BY
                // expression, add the index of the GROUP BY expression as a new
                // determinant key:
                if let Some(input_idx) = input_idx
                    && source_indices.contains(input_idx)
                    // A key column can be in the GROUP BY list more than one
                    // time (`x, x AS k`). Count it one time.
                    && !new_source_input_indices.contains(&Some(*input_idx))
                {
                    new_source_indices.push(idx);
                    new_source_input_indices.push(Some(*input_idx));
                }
            }
            let new_target_indices = get_target_indices_from_dependencies(
                func_dependencies.iter(),
                &new_source_input_indices,
            );
            let mode = if existing_target_indices == new_target_indices
                && new_target_indices.is_some()
            {
                // If dependency covers all GROUP BY expressions, mode will be `Single`:
                Dependency::Single
            } else {
                // Otherwise, existing mode is preserved:
                *mode
            };
            // All of the composite indices occur in the GROUP BY expression:
            if new_source_indices.len() == source_indices.len() {
                // GROUP BY treats NULLs as equal: a determinant covering the
                // complete grouping key gets at most one output row per NULL too.
                let output_null_equality =
                    if new_source_indices.len() == group_by_input_indices.len() {
                        NullEquality::NullEqualsNull
                    } else {
                        *null_equality
                    };
                aggregate_func_dependencies.push(
                    FunctionalDependence::new(
                        new_source_indices,
                        target_indices.clone(),
                        *nullable,
                    )
                    .with_mode(mode)
                    .with_null_equality(output_null_equality),
                );
            }
        }
    }

    // When we have a GROUP BY key, we can guarantee uniqueness after
    // aggregation:
    if !group_by_input_indices.is_empty() {
        let count = group_by_input_indices.len();
        let source_indices = (0..count).collect::<Vec<_>>();
        let nullable = source_indices
            .iter()
            .any(|idx| aggr_fields[*idx].is_nullable());
        // If GROUP BY expressions do not already act as a determinant:
        if !aggregate_func_dependencies.iter().any(|item| {
            // If `item.source_indices` is a subset of GROUP BY expressions, we shouldn't add
            // them since `item.source_indices` defines this relation already.

            // The following simple comparison is working well because
            // GROUP BY expressions come here as a prefix.
            item.source_indices.iter().all(|idx| idx < &count)
                // A dependency that is not valid across NULLs cannot replace
                // the whole GROUP BY key dependency:
                && item.is_valid_across_nulls(aggr_schema)
        }) {
            // Add a new functional dependency associated with the whole table:
            // Use nullable property of the GROUP BY expression:
            aggregate_func_dependencies.push(
                FunctionalDependence::new(source_indices, target_indices, nullable)
                    .with_mode(Dependency::Single)
                    // Grouping collapses NULL keys into a single group:
                    .with_null_equality(NullEquality::NullEqualsNull),
            );
        }
    }
    FunctionalDependencies::new(aggregate_func_dependencies)
}

/// Returns target indices, for the determinant keys that are inside
/// group by expressions.
///
/// Only dependencies that hold when NULL determinant values are grouped
/// together are considered (see
/// [`FunctionalDependence::is_valid_across_nulls`]): a nullable `UNIQUE`
/// column permits multiple NULL rows that may differ in the other columns,
/// so it does not determine them across the single NULL group.
///
/// `group_by_input_indices` holds, for each GROUP BY expression, the index of
/// the `schema` field it references, or `None` for a computed expression.
pub fn get_target_functional_dependencies(
    schema: &DFSchema,
    group_by_input_indices: &[Option<usize>],
) -> Option<Vec<usize>> {
    let dependencies = schema.functional_dependencies();
    if dependencies.is_empty() {
        return None;
    }
    let dependencies = dependencies
        .iter()
        .filter(|dependency| dependency.is_valid_across_nulls(schema));
    get_target_indices_from_dependencies(dependencies, group_by_input_indices)
}

fn get_target_indices_from_dependencies<'a>(
    dependencies: impl IntoIterator<Item = &'a FunctionalDependence>,
    group_by_input_indices: &[Option<usize>],
) -> Option<Vec<usize>> {
    let mut combined_target_indices = HashSet::new();
    for dependency in dependencies {
        // If the GROUP BY expression contains a determinant key, we can use
        // the associated fields after aggregation even if they are not part
        // of the GROUP BY expression.
        if dependency
            .source_indices
            .iter()
            .all(|source_idx| group_by_input_indices.contains(&Some(*source_idx)))
        {
            combined_target_indices.extend(dependency.target_indices.iter());
        }
    }
    (!combined_target_indices.is_empty()).then_some({
        let mut result = combined_target_indices.into_iter().collect::<Vec<_>>();
        result.sort_unstable();
        result
    })
}

/// Returns indices for the minimal subset of GROUP BY expressions that are
/// functionally equivalent to the original set of GROUP BY expressions.
///
/// `group_by_input_indices` holds, for each GROUP BY expression, the index of
/// the `schema` field it references, or `None` for a computed expression. If
/// any GROUP BY expression is computed, returns `None`.
pub fn get_required_group_by_exprs_indices(
    schema: &DFSchema,
    group_by_input_indices: &[Option<usize>],
) -> Option<Vec<usize>> {
    let dependencies = schema.functional_dependencies();
    let mut groupby_expr_indices = group_by_input_indices
        .iter()
        .copied()
        .collect::<Option<Vec<_>>>()?;

    groupby_expr_indices.sort_unstable();
    for dependency in &dependencies.deps {
        // If all source indices are among GROUP BY expression indices, we
        // can remove target indices from GROUP BY expression indices and
        // use source indices instead. The dependency must also hold across
        // NULL keys, which GROUP BY collapses into a single group.
        if dependency
            .source_indices
            .iter()
            .all(|source_idx| groupby_expr_indices.contains(source_idx))
            && dependency.is_valid_across_nulls(schema)
        {
            groupby_expr_indices =
                set_difference(&groupby_expr_indices, &dependency.target_indices);
            groupby_expr_indices =
                merge_and_order_indices(groupby_expr_indices, &dependency.source_indices);
        }
    }
    groupby_expr_indices
        .iter()
        .map(|idx| {
            group_by_input_indices
                .iter()
                .position(|input_idx| *input_idx == Some(*idx))
        })
        .collect()
}

/// Returns indices for the minimal subset of ORDER BY expressions that are
/// functionally equivalent to the original set of ORDER BY expressions.
///
/// `sort_input_indices` holds, for each ORDER BY expression, the index of the
/// `schema` field it references, or `None` for a computed expression.
pub fn get_required_sort_exprs_indices(
    schema: &DFSchema,
    sort_input_indices: &[Option<usize>],
) -> Vec<usize> {
    let dependencies = schema.functional_dependencies();

    let mut known_field_indices = HashSet::with_capacity(sort_input_indices.len());
    let mut required_sort_expr_indices = Vec::with_capacity(sort_input_indices.len());

    for (sort_expr_idx, field_idx) in sort_input_indices.iter().enumerate() {
        // If the sort expression doesn't reference a schema field (e.g. a
        // computed expression), we can't reason about it via functional
        // dependencies, so conservatively keep it.
        let Some(field_idx) = *field_idx else {
            required_sort_expr_indices.push(sort_expr_idx);
            continue;
        };

        // A sort expression is removable if its value is functionally determined
        // by fields that already appear earlier in the sort order (and the
        // dependency remains valid across NULL rows).
        let removable = dependencies.deps.iter().any(|dependency| {
            dependency.target_indices.contains(&field_idx)
                && dependency
                    .source_indices
                    .iter()
                    .all(|source_idx| known_field_indices.contains(source_idx))
                && dependency.is_valid_across_nulls(schema)
        });

        if removable {
            continue;
        }

        known_field_indices.insert(field_idx);
        required_sort_expr_indices.push(sort_expr_idx);
    }

    required_sort_expr_indices
}

/// Updates entries inside the `entries` vector with their corresponding
/// indices inside the `proj_indices` vector.
fn update_elements_with_matching_indices(
    entries: &[usize],
    proj_indices: &[usize],
) -> Vec<usize> {
    entries
        .iter()
        .filter_map(|val| proj_indices.iter().position(|proj_idx| proj_idx == val))
        .collect()
}

/// Adds `offset` value to each entry inside `in_data`.
fn add_offset_to_vec<T: Copy + std::ops::Add<Output = T>>(
    in_data: &[T],
    offset: T,
) -> Vec<T> {
    in_data.iter().map(|&item| item + offset).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn constraints_iter() {
        let constraints = Constraints::new_unverified(vec![
            Constraint::PrimaryKey(vec![10]),
            Constraint::Unique(vec![20]),
        ]);
        let mut iter = constraints.iter();
        assert_eq!(iter.next(), Some(&Constraint::PrimaryKey(vec![10])));
        assert_eq!(iter.next(), Some(&Constraint::Unique(vec![20])));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_project_constraints() {
        let constraints = Constraints::new_unverified(vec![
            Constraint::PrimaryKey(vec![1, 2]),
            Constraint::Unique(vec![0, 3]),
        ]);

        // Project keeping columns 1,2,3
        let projected = constraints.project(&[1, 2, 3]).unwrap();
        assert_eq!(
            projected,
            Constraints::new_unverified(vec![Constraint::PrimaryKey(vec![0, 1])])
        );

        // Project keeping only column 0 - should return None as no constraints are preserved
        assert!(constraints.project(&[0]).is_none());
    }

    #[test]
    fn test_get_updated_id_keys() {
        let fund_dependencies =
            FunctionalDependencies::new(vec![FunctionalDependence::new(
                vec![1],
                vec![0, 1, 2],
                true,
            )]);
        let res = fund_dependencies.project_functional_dependencies(&[1, 2], 2);
        let expected = FunctionalDependencies::new(vec![FunctionalDependence::new(
            vec![0],
            vec![0, 1],
            true,
        )]);
        assert_eq!(res, expected);
    }
}