|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::{VortexExpect, VortexResult}; |
| 5 | +use vortex_utils::aliases::hash_map::HashMap; |
| 6 | + |
| 7 | +use crate::expr::Expression; |
| 8 | +use crate::expr::exprs::is_null::IsNull; |
| 9 | +use crate::expr::traversal::{NodeExt, NodeVisitor, TraversalOrder}; |
| 10 | + |
| 11 | +/// Tracks whether an expression is null-sensitive. |
| 12 | +/// |
| 13 | +/// An expression is null-sensitive if it or any of its children is an `is_null` operation. |
| 14 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 15 | +pub enum NullSensitive { |
| 16 | + /// The expression or one of its children contains an `is_null` operation. |
| 17 | + Yes, |
| 18 | + /// The expression and all of its children do not contain an `is_null` operation. |
| 19 | + No, |
| 20 | +} |
| 21 | + |
| 22 | +impl NullSensitive { |
| 23 | + /// Combine two null sensitivity labels. |
| 24 | + /// |
| 25 | + /// Returns `Yes` if either label is `Yes`, otherwise `No`. |
| 26 | + pub fn combine(self, other: Self) -> Self { |
| 27 | + match (self, other) { |
| 28 | + (NullSensitive::Yes, _) | (_, NullSensitive::Yes) => NullSensitive::Yes, |
| 29 | + (NullSensitive::No, NullSensitive::No) => NullSensitive::No, |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +pub type NullSensitiveLabels<'a> = HashMap<&'a Expression, NullSensitive>; |
| 35 | + |
| 36 | +/// Label each expression in the tree with whether it is null-sensitive. |
| 37 | +/// |
| 38 | +/// An expression is null-sensitive if it or any of its descendants contain an `is_null` operation. |
| 39 | +pub fn label_null_sensitive(expr: &Expression) -> NullSensitiveLabels<'_> { |
| 40 | + let mut visitor = NullSensitiveVisitor { |
| 41 | + labels: Default::default(), |
| 42 | + }; |
| 43 | + expr.accept(&mut visitor) |
| 44 | + .vortex_expect("NullSensitiveVisitor is infallible"); |
| 45 | + visitor.labels |
| 46 | +} |
| 47 | + |
| 48 | +struct NullSensitiveVisitor<'a> { |
| 49 | + labels: NullSensitiveLabels<'a>, |
| 50 | +} |
| 51 | + |
| 52 | +impl<'a> NodeVisitor<'a> for NullSensitiveVisitor<'a> { |
| 53 | + type NodeTy = Expression; |
| 54 | + |
| 55 | + fn visit_down(&mut self, _node: &'a Self::NodeTy) -> VortexResult<TraversalOrder> { |
| 56 | + // Continue traversing down |
| 57 | + Ok(TraversalOrder::Continue) |
| 58 | + } |
| 59 | + |
| 60 | + fn visit_up(&mut self, node: &'a Expression) -> VortexResult<TraversalOrder> { |
| 61 | + // Check if this node is an is_null operation |
| 62 | + let is_null = node.is::<IsNull>(); |
| 63 | + |
| 64 | + // Combine labels from all children |
| 65 | + let children_sensitive = node |
| 66 | + .children() |
| 67 | + .iter() |
| 68 | + .filter_map(|child| self.labels.get(child)) |
| 69 | + .any(|&label| label == NullSensitive::Yes); |
| 70 | + |
| 71 | + // This node is sensitive if it's is_null or any child is sensitive |
| 72 | + let label = if is_null || children_sensitive { |
| 73 | + NullSensitive::Yes |
| 74 | + } else { |
| 75 | + NullSensitive::No |
| 76 | + }; |
| 77 | + |
| 78 | + self.labels.insert(node, label); |
| 79 | + |
| 80 | + Ok(TraversalOrder::Continue) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + use crate::expr::exprs::binary::eq; |
| 88 | + use crate::expr::exprs::get_item::col; |
| 89 | + use crate::expr::exprs::is_null::is_null; |
| 90 | + use crate::expr::exprs::literal::lit; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_null_sensitive_with_is_null() { |
| 94 | + // Expression: is_null($.col1) |
| 95 | + let expr = is_null(col("col1")); |
| 96 | + let labels = label_null_sensitive(&expr); |
| 97 | + |
| 98 | + // The root expression should be null-sensitive |
| 99 | + assert_eq!(labels.get(&expr), Some(&NullSensitive::Yes)); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_null_sensitive_without_is_null() { |
| 104 | + // Expression: $.col1 = 5 |
| 105 | + let expr = eq(col("col1"), lit(5)); |
| 106 | + let labels = label_null_sensitive(&expr); |
| 107 | + |
| 108 | + // The root expression should not be null-sensitive |
| 109 | + assert_eq!(labels.get(&expr), Some(&NullSensitive::No)); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_null_sensitive_nested() { |
| 114 | + // Expression: ($.col1 = 5) = is_null($.col2) |
| 115 | + let left = eq(col("col1"), lit(5)); |
| 116 | + let right = is_null(col("col2")); |
| 117 | + let expr = eq(left.clone(), right.clone()); |
| 118 | + |
| 119 | + let labels = label_null_sensitive(&expr); |
| 120 | + |
| 121 | + // The left side should not be sensitive |
| 122 | + assert_eq!(labels.get(&left), Some(&NullSensitive::No)); |
| 123 | + |
| 124 | + // The right side should be sensitive |
| 125 | + assert_eq!(labels.get(&right), Some(&NullSensitive::Yes)); |
| 126 | + |
| 127 | + // The root should be sensitive (because right child is sensitive) |
| 128 | + assert_eq!(labels.get(&expr), Some(&NullSensitive::Yes)); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_combine() { |
| 133 | + assert_eq!( |
| 134 | + NullSensitive::Yes.combine(NullSensitive::Yes), |
| 135 | + NullSensitive::Yes |
| 136 | + ); |
| 137 | + assert_eq!( |
| 138 | + NullSensitive::Yes.combine(NullSensitive::No), |
| 139 | + NullSensitive::Yes |
| 140 | + ); |
| 141 | + assert_eq!( |
| 142 | + NullSensitive::No.combine(NullSensitive::Yes), |
| 143 | + NullSensitive::Yes |
| 144 | + ); |
| 145 | + assert_eq!( |
| 146 | + NullSensitive::No.combine(NullSensitive::No), |
| 147 | + NullSensitive::No |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments