//! Factorized filter operator for filtering without flattening.
//!
//! This module provides operators that filter factorized data using selection
//! vectors, avoiding the O(n) data copying of traditional filtering.
//!
//! # Performance
//!
//! For a 2-hop query with 100 sources, 10 neighbors each hop filtering to 10%:
//!
//! - **Regular filter (copy)**: Copy 1,000 rows → 100 rows
//! - **Factorized filter (selection)**: Set selection bits, O(n_physical)
//!
//! This gives 10-100x speedups on low-selectivity filters.

use std::sync::Arc;

use super::{FactorizedOperator, FactorizedResult, LazyFactorizedChainOperator, Operator};
use crate::execution::chunk_state::{FactorizedSelection, LevelSelection};
use crate::execution::factorized_chunk::FactorizedChunk;
use crate::graph::GraphStore;
use grafeo_common::types::{EpochId, PropertyKey, Value};

/// A predicate that can be evaluated on factorized data at a specific level.
///
/// Unlike regular predicates that evaluate on flat DataChunks, factorized
/// predicates work directly on factorized levels for O(physical) evaluation.
pub trait FactorizedPredicate: Send + Sync {
    /// Evaluates the predicate for a single physical index at a level.
    ///
    /// # Arguments
    ///
    /// * `chunk` - The factorized chunk
    /// * `level` - The level to evaluate at
    /// * `physical_idx` - The physical index within the level
    ///
    /// # Returns
    ///
    /// `true` if the row passes the predicate
    fn evaluate(&self, chunk: &FactorizedChunk, level: usize, physical_idx: usize) -> bool;

    /// Evaluates the predicate for all physical indices at a level.
    ///
    /// Returns a `LevelSelection` representing which indices pass.
    /// Default implementation calls `evaluate` for each index.
    fn evaluate_batch(&self, chunk: &FactorizedChunk, level: usize) -> LevelSelection {
        let Some(level_data) = chunk.level(level) else {
            return LevelSelection::all(0);
        };

        let count = level_data.physical_value_count();
        LevelSelection::from_predicate(count, |idx| self.evaluate(chunk, level, idx))
    }

    /// Returns the level this predicate operates on.
    ///
    /// Returns `None` for predicates that span multiple levels.
    fn target_level(&self) -> Option<usize>;
}

/// A simple column value predicate for factorized data.
///
/// Evaluates a condition on a specific column at a specific level.
#[derive(Debug, Clone)]
pub struct ColumnPredicate {
    /// The level to evaluate at.
    level: usize,
    /// The column index within the level.
    column: usize,
    /// The comparison operator.
    op: CompareOp,
    /// The value to compare against.
    value: Value,
}

/// Comparison operators for column predicates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareOp {
    /// Equal.
    Eq,
    /// Not equal.
    Ne,
    /// Less than.
    Lt,
    /// Less than or equal.
    Le,
    /// Greater than.
    Gt,
    /// Greater than or equal.
    Ge,
}

impl ColumnPredicate {
    /// Creates a new column predicate.
    #[must_use]
    pub fn new(level: usize, column: usize, op: CompareOp, value: Value) -> Self {
        Self {
            level,
            column,
            op,
            value,
        }
    }

    /// Creates an equality predicate.
    #[must_use]
    pub fn eq(level: usize, column: usize, value: Value) -> Self {
        Self::new(level, column, CompareOp::Eq, value)
    }

    /// Creates an inequality predicate.
    #[must_use]
    pub fn ne(level: usize, column: usize, value: Value) -> Self {
        Self::new(level, column, CompareOp::Ne, value)
    }

    /// Creates a less-than predicate.
    #[must_use]
    pub fn lt(level: usize, column: usize, value: Value) -> Self {
        Self::new(level, column, CompareOp::Lt, value)
    }

    /// Creates a greater-than predicate.
    #[must_use]
    pub fn gt(level: usize, column: usize, value: Value) -> Self {
        Self::new(level, column, CompareOp::Gt, value)
    }

    fn compare_values(&self, left: &Value) -> bool {
        match (&left, &self.value) {
            (Value::Int64(a), Value::Int64(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Float64(a), Value::Float64(b)) => match self.op {
                CompareOp::Eq => (a - b).abs() < f64::EPSILON,
                CompareOp::Ne => (a - b).abs() >= f64::EPSILON,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::String(a), Value::String(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Bool(a), Value::Bool(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                _ => false,
            },
            (Value::Int64(a), Value::Float64(b)) | (Value::Float64(b), Value::Int64(a)) => {
                let af = *a as f64;
                match self.op {
                    CompareOp::Eq => (af - b).abs() < f64::EPSILON,
                    CompareOp::Ne => (af - b).abs() >= f64::EPSILON,
                    CompareOp::Lt => af < *b,
                    CompareOp::Le => af <= *b,
                    CompareOp::Gt => af > *b,
                    CompareOp::Ge => af >= *b,
                }
            }
            (Value::Timestamp(a), Value::Timestamp(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Date(a), Value::Date(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Time(a), Value::Time(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            _ => false, // Type mismatch
        }
    }
}

impl FactorizedPredicate for ColumnPredicate {
    fn evaluate(&self, chunk: &FactorizedChunk, level: usize, physical_idx: usize) -> bool {
        if level != self.level {
            return true; // Predicate doesn't apply to this level
        }

        let Some(level_data) = chunk.level(level) else {
            return false;
        };

        let Some(column) = level_data.column(self.column) else {
            return false;
        };

        let Some(value) = column.get_physical(physical_idx) else {
            return false;
        };

        self.compare_values(&value)
    }

    fn target_level(&self) -> Option<usize> {
        Some(self.level)
    }
}

/// A property-based predicate for factorized data.
///
/// Evaluates a condition on an entity's property (node or edge).
///
/// # Performance
///
/// Uses direct property lookup via `LpgStore::get_node_property()` which is
/// O(1) per entity. This avoids the O(properties) overhead of loading all
/// properties when only one is needed.
pub struct PropertyPredicate {
    /// The level containing the entity.
    level: usize,
    /// The column index of the entity (NodeId or EdgeId).
    column: usize,
    /// The property key to access.
    property: PropertyKey,
    /// The comparison operator.
    op: CompareOp,
    /// The value to compare against.
    value: Value,
    /// The graph store for property lookups.
    store: Arc<dyn GraphStore>,
    /// Optional epoch for time-travel property reads.
    viewing_epoch: Option<EpochId>,
}

impl PropertyPredicate {
    /// Creates a new property predicate.
    pub fn new(
        level: usize,
        column: usize,
        property: impl Into<PropertyKey>,
        op: CompareOp,
        value: Value,
        store: Arc<dyn GraphStore>,
    ) -> Self {
        Self {
            level,
            column,
            property: property.into(),
            op,
            value,
            store,
            viewing_epoch: None,
        }
    }

    /// Creates an equality predicate on a property.
    pub fn eq(
        level: usize,
        column: usize,
        property: impl Into<PropertyKey>,
        value: Value,
        store: Arc<dyn GraphStore>,
    ) -> Self {
        Self::new(level, column, property, CompareOp::Eq, value, store)
    }

    fn compare_values(&self, left: &Value) -> bool {
        match (left, &self.value) {
            (Value::Int64(a), Value::Int64(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Float64(a), Value::Float64(b)) => match self.op {
                CompareOp::Eq => (a - b).abs() < f64::EPSILON,
                CompareOp::Ne => (a - b).abs() >= f64::EPSILON,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::String(a), Value::String(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Bool(a), Value::Bool(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                _ => false,
            },
            (Value::Timestamp(a), Value::Timestamp(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Date(a), Value::Date(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            (Value::Time(a), Value::Time(b)) => match self.op {
                CompareOp::Eq => a == b,
                CompareOp::Ne => a != b,
                CompareOp::Lt => a < b,
                CompareOp::Le => a <= b,
                CompareOp::Gt => a > b,
                CompareOp::Ge => a >= b,
            },
            _ => false,
        }
    }
}

impl FactorizedPredicate for PropertyPredicate {
    fn evaluate(&self, chunk: &FactorizedChunk, level: usize, physical_idx: usize) -> bool {
        if level != self.level {
            return true;
        }

        let Some(level_data) = chunk.level(level) else {
            return false;
        };

        let Some(column) = level_data.column(self.column) else {
            return false;
        };

        // Try as node first - use direct property lookup (O(1) vs O(properties))
        if let Some(node_id) = column.get_node_id_physical(physical_idx) {
            let prop_val = if let Some(epoch) = self.viewing_epoch {
                self.store
                    .get_node_at_epoch(node_id, epoch)
                    .and_then(|n| n.get_property(self.property.as_str()).cloned())
            } else {
                self.store.get_node_property(node_id, &self.property)
            };
            if let Some(val) = prop_val {
                return self.compare_values(&val);
            }
        }

        // Try as edge - use direct property lookup
        if let Some(edge_id) = column.get_edge_id_physical(physical_idx) {
            let prop_val = if let Some(epoch) = self.viewing_epoch {
                self.store
                    .get_edge_at_epoch(edge_id, epoch)
                    .and_then(|e| e.get_property(self.property.as_str()).cloned())
            } else {
                self.store.get_edge_property(edge_id, &self.property)
            };
            if let Some(val) = prop_val {
                return self.compare_values(&val);
            }
        }

        false
    }

    /// Batch evaluates the predicate for all physical indices at a level.
    ///
    /// This is more cache-friendly than individual evaluations for large batches.
    fn evaluate_batch(&self, chunk: &FactorizedChunk, level: usize) -> LevelSelection {
        // If this predicate doesn't target this level, all rows pass
        if level != self.level {
            let count = chunk.level(level).map_or(0, |l| l.physical_value_count());
            return LevelSelection::all(count);
        }

        let Some(level_data) = chunk.level(level) else {
            return LevelSelection::all(0);
        };

        let Some(column) = level_data.column(self.column) else {
            // No column found means no matches - return empty selection
            return LevelSelection::from_predicate(level_data.physical_value_count(), |_| false);
        };

        let count = level_data.physical_value_count();

        // Evaluate all at once using direct property lookups
        LevelSelection::from_predicate(count, |idx| {
            // Try as node first
            if let Some(node_id) = column.get_node_id_physical(idx) {
                let val = if let Some(epoch) = self.viewing_epoch {
                    self.store
                        .get_node_at_epoch(node_id, epoch)
                        .and_then(|n| n.get_property(self.property.as_str()).cloned())
                } else {
                    self.store.get_node_property(node_id, &self.property)
                };
                if let Some(v) = val {
                    return self.compare_values(&v);
                }
            }
            // Try as edge
            if let Some(edge_id) = column.get_edge_id_physical(idx) {
                let val = if let Some(epoch) = self.viewing_epoch {
                    self.store
                        .get_edge_at_epoch(edge_id, epoch)
                        .and_then(|e| e.get_property(self.property.as_str()).cloned())
                } else {
                    self.store.get_edge_property(edge_id, &self.property)
                };
                if let Some(v) = val {
                    return self.compare_values(&v);
                }
            }
            false
        })
    }

    fn target_level(&self) -> Option<usize> {
        Some(self.level)
    }
}

/// Composite predicate combining multiple predicates with AND.
pub struct AndPredicate {
    predicates: Vec<Box<dyn FactorizedPredicate>>,
}

impl AndPredicate {
    /// Creates a new AND predicate.
    pub fn new(predicates: Vec<Box<dyn FactorizedPredicate>>) -> Self {
        Self { predicates }
    }
}

impl FactorizedPredicate for AndPredicate {
    fn evaluate(&self, chunk: &FactorizedChunk, level: usize, physical_idx: usize) -> bool {
        self.predicates
            .iter()
            .all(|p| p.evaluate(chunk, level, physical_idx))
    }

    fn target_level(&self) -> Option<usize> {
        // If all predicates target the same level, return that level
        let mut target = None;
        for pred in &self.predicates {
            match (target, pred.target_level()) {
                (None, Some(l)) => target = Some(l),
                (Some(t), Some(l)) if t != l => return None, // Multiple levels
                _ => {}
            }
        }
        target
    }
}

/// Composite predicate combining multiple predicates with OR.
pub struct OrPredicate {
    predicates: Vec<Box<dyn FactorizedPredicate>>,
}

impl OrPredicate {
    /// Creates a new OR predicate.
    pub fn new(predicates: Vec<Box<dyn FactorizedPredicate>>) -> Self {
        Self { predicates }
    }
}

impl FactorizedPredicate for OrPredicate {
    fn evaluate(&self, chunk: &FactorizedChunk, level: usize, physical_idx: usize) -> bool {
        self.predicates
            .iter()
            .any(|p| p.evaluate(chunk, level, physical_idx))
    }

    fn target_level(&self) -> Option<usize> {
        // Same logic as AND
        let mut target = None;
        for pred in &self.predicates {
            match (target, pred.target_level()) {
                (None, Some(l)) => target = Some(l),
                (Some(t), Some(l)) if t != l => return None,
                _ => {}
            }
        }
        target
    }
}

/// A filter operator that applies predicates to factorized data without flattening.
///
/// This operator uses selection vectors to mark filtered rows, avoiding the
/// O(n) data copying of traditional filtering. The selection is applied lazily
/// and can be materialized only when needed.
///
/// # Example
///
/// ```no_run
/// # use grafeo_core::execution::operators::{
/// #     FactorizedFilterOperator, PropertyPredicate, FactorizedCompareOp,
/// #     LazyFactorizedChainOperator,
/// # };
/// # use grafeo_core::graph::GraphStore;
/// # use grafeo_common::types::Value;
/// # use std::sync::Arc;
/// # fn example(expand_chain: LazyFactorizedChainOperator, store: Arc<dyn GraphStore>) {
/// // Query: MATCH (a)->(b)->(c) WHERE c.age > 30
/// let predicate = PropertyPredicate::new(
///     2, 0, "age", FactorizedCompareOp::Gt, Value::Int64(30), store,
/// );
/// let filter = FactorizedFilterOperator::new(expand_chain, Box::new(predicate));
/// # }
/// ```
pub struct FactorizedFilterOperator {
    /// The input operator providing factorized data.
    input: LazyFactorizedChainOperator,
    /// The predicate to apply.
    predicate: Box<dyn FactorizedPredicate>,
    /// Whether to materialize the selection or keep it lazy.
    materialize: bool,
}

impl FactorizedFilterOperator {
    /// Creates a new factorized filter operator.
    pub fn new(
        input: LazyFactorizedChainOperator,
        predicate: Box<dyn FactorizedPredicate>,
    ) -> Self {
        Self {
            input,
            predicate,
            materialize: false,
        }
    }

    /// Creates a filter operator with materialization enabled.
    pub fn with_materialize(
        input: LazyFactorizedChainOperator,
        predicate: Box<dyn FactorizedPredicate>,
    ) -> Self {
        Self {
            input,
            predicate,
            materialize: true,
        }
    }

    /// Sets whether to materialize the selection.
    ///
    /// If `true`, filtered data is copied. If `false` (default), only a
    /// selection vector is set.
    #[must_use]
    pub fn materialize(mut self, materialize: bool) -> Self {
        self.materialize = materialize;
        self
    }

    /// Applies the predicate to create a selection.
    fn apply_filter(&self, chunk: &FactorizedChunk) -> FactorizedSelection {
        let level_count = chunk.level_count();
        if level_count == 0 {
            return FactorizedSelection::all(&[]);
        }

        // Get level counts for creating the selection
        let level_counts: Vec<usize> = (0..level_count)
            .map(|i| chunk.level(i).map_or(0, |l| l.physical_value_count()))
            .collect();

        // Start with all selected
        let mut selection = FactorizedSelection::all(&level_counts);

        // Apply predicate at the target level
        if let Some(target_level) = self.predicate.target_level() {
            selection = selection.filter_level(target_level, |idx| {
                self.predicate.evaluate(chunk, target_level, idx)
            });
        } else {
            // Multi-level predicate: apply at each level
            for level in 0..level_count {
                selection =
                    selection.filter_level(level, |idx| self.predicate.evaluate(chunk, level, idx));
            }
        }

        selection
    }
}

impl FactorizedOperator for FactorizedFilterOperator {
    fn next_factorized(&mut self) -> FactorizedResult {
        // Get the factorized result from input
        let Some(mut chunk) = self.input.next_factorized()? else {
            return Ok(None);
        };

        // Apply the filter to create a selection
        let selection = self.apply_filter(&chunk);

        // Check if anything passes
        let any_selected = (0..chunk.level_count()).any(|level| {
            selection
                .level(level)
                .is_some_and(|sel| sel.selected_count() > 0)
        });

        if !any_selected {
            // Nothing passed the filter - try next chunk
            return self.next_factorized();
        }

        if self.materialize {
            // Materialize: create a new chunk with only selected rows
            chunk = self.materialize_selection(&chunk, &selection);
        } else {
            // Lazy: just set the selection on the chunk's state
            chunk.chunk_state_mut().set_selection(selection);
        }

        Ok(Some(chunk))
    }
}

impl FactorizedFilterOperator {
    /// Materializes a selection by copying only selected data.
    fn materialize_selection(
        &self,
        chunk: &FactorizedChunk,
        selection: &FactorizedSelection,
    ) -> FactorizedChunk {
        // For now, use the existing filter_deepest_multi for deepest level
        // A full implementation would handle all levels
        if let Some(target_level) = self.predicate.target_level()
            && target_level == chunk.level_count() - 1
        {
            // Filter at deepest level - use existing method
            if let Some(filtered) = chunk.filter_deepest_multi(|_values| {
                // This is a simplified approach - in a full implementation,
                // we'd map the values back to physical indices
                true
            }) {
                return filtered;
            }
        }

        // For other cases, clone the chunk with selection applied
        // Full materialization is complex - for now we keep the selection lazy
        let _ = selection;
        chunk.clone()
    }

    /// Resets the operator to its initial state.
    pub fn reset(&mut self) {
        Operator::reset(&mut self.input);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::execution::factorized_chunk::FactorizationLevel;
    use crate::execution::factorized_vector::FactorizedVector;
    use crate::execution::vector::ValueVector;
    use crate::graph::lpg::LpgStore;
    use grafeo_common::types::LogicalType;

    /// Creates a test factorized chunk for filtering tests.
    fn create_test_chunk() -> FactorizedChunk {
        // Level 0: 2 sources with values [10, 20]
        let mut source_data = ValueVector::with_type(LogicalType::Int64);
        source_data.push_int64(10);
        source_data.push_int64(20);
        let level0 = FactorizationLevel::flat(
            vec![FactorizedVector::flat(source_data)],
            vec!["source".to_string()],
        );

        // Level 1: 5 children (3 for source 0, 2 for source 1)
        // Values: [1, 2, 3, 4, 5]
        let mut child_data = ValueVector::with_type(LogicalType::Int64);
        child_data.push_int64(1);
        child_data.push_int64(2);
        child_data.push_int64(3);
        child_data.push_int64(4);
        child_data.push_int64(5);

        let offsets = vec![0u32, 3, 5];
        let child_vec = FactorizedVector::unflat(child_data, offsets, 2);
        let level1 =
            FactorizationLevel::unflat(vec![child_vec], vec!["child".to_string()], vec![3, 2]);

        let mut chunk = FactorizedChunk::empty();
        chunk.add_factorized_level(level0);
        chunk.add_factorized_level(level1);
        chunk
    }

    /// Creates a test chunk with float values.
    fn create_float_chunk() -> FactorizedChunk {
        let mut data = ValueVector::with_type(LogicalType::Float64);
        data.push_float64(1.5);
        data.push_float64(2.5);
        data.push_float64(3.5);
        let level0 = FactorizationLevel::flat(
            vec![FactorizedVector::flat(data)],
            vec!["value".to_string()],
        );
        let mut chunk = FactorizedChunk::empty();
        chunk.add_factorized_level(level0);
        chunk
    }

    /// Creates a test chunk with string values.
    fn create_string_chunk() -> FactorizedChunk {
        let mut data = ValueVector::with_type(LogicalType::String);
        data.push_string("apple");
        data.push_string("banana");
        data.push_string("cherry");
        let level0 = FactorizationLevel::flat(
            vec![FactorizedVector::flat(data)],
            vec!["fruit".to_string()],
        );
        let mut chunk = FactorizedChunk::empty();
        chunk.add_factorized_level(level0);
        chunk
    }

    /// Creates a test chunk with boolean values.
    fn create_bool_chunk() -> FactorizedChunk {
        let mut data = ValueVector::with_type(LogicalType::Bool);
        data.push_bool(true);
        data.push_bool(false);
        data.push_bool(true);
        let level0 =
            FactorizationLevel::flat(vec![FactorizedVector::flat(data)], vec!["flag".to_string()]);
        let mut chunk = FactorizedChunk::empty();
        chunk.add_factorized_level(level0);
        chunk
    }

    #[test]
    fn test_column_predicate_evaluate() {
        let chunk = create_test_chunk();

        // Predicate: child value > 2
        let pred = ColumnPredicate::gt(1, 0, Value::Int64(2));

        // Values at level 1: [1, 2, 3, 4, 5]
        assert!(!pred.evaluate(&chunk, 1, 0)); // 1 > 2 = false
        assert!(!pred.evaluate(&chunk, 1, 1)); // 2 > 2 = false
        assert!(pred.evaluate(&chunk, 1, 2)); // 3 > 2 = true
        assert!(pred.evaluate(&chunk, 1, 3)); // 4 > 2 = true
        assert!(pred.evaluate(&chunk, 1, 4)); // 5 > 2 = true
    }

    #[test]
    fn test_column_predicate_ne() {
        let chunk = create_test_chunk();

        // Predicate: child value != 3
        let pred = ColumnPredicate::ne(1, 0, Value::Int64(3));

        assert!(pred.evaluate(&chunk, 1, 0)); // 1 != 3
        assert!(pred.evaluate(&chunk, 1, 1)); // 2 != 3
        assert!(!pred.evaluate(&chunk, 1, 2)); // 3 != 3 = false
        assert!(pred.evaluate(&chunk, 1, 3)); // 4 != 3
        assert!(pred.evaluate(&chunk, 1, 4)); // 5 != 3
    }

    #[test]
    fn test_column_predicate_lt() {
        let chunk = create_test_chunk();

        // Predicate: child value < 3
        let pred = ColumnPredicate::lt(1, 0, Value::Int64(3));

        assert!(pred.evaluate(&chunk, 1, 0)); // 1 < 3
        assert!(pred.evaluate(&chunk, 1, 1)); // 2 < 3
        assert!(!pred.evaluate(&chunk, 1, 2)); // 3 < 3 = false
        assert!(!pred.evaluate(&chunk, 1, 3)); // 4 < 3 = false
    }

    #[test]
    fn test_column_predicate_le_ge() {
        let chunk = create_test_chunk();

        // Predicate: child value <= 3
        let pred_le = ColumnPredicate::new(1, 0, CompareOp::Le, Value::Int64(3));
        assert!(pred_le.evaluate(&chunk, 1, 0)); // 1 <= 3
        assert!(pred_le.evaluate(&chunk, 1, 2)); // 3 <= 3
        assert!(!pred_le.evaluate(&chunk, 1, 3)); // 4 <= 3 = false

        // Predicate: child value >= 3
        let pred_ge = ColumnPredicate::new(1, 0, CompareOp::Ge, Value::Int64(3));
        assert!(!pred_ge.evaluate(&chunk, 1, 0)); // 1 >= 3 = false
        assert!(pred_ge.evaluate(&chunk, 1, 2)); // 3 >= 3
        assert!(pred_ge.evaluate(&chunk, 1, 3)); // 4 >= 3
    }

    #[test]
    fn test_column_predicate_float() {
        let chunk = create_float_chunk();

        // Float comparisons
        let pred_eq = ColumnPredicate::eq(0, 0, Value::Float64(2.5));
        assert!(!pred_eq.evaluate(&chunk, 0, 0)); // 1.5 == 2.5 = false
        assert!(pred_eq.evaluate(&chunk, 0, 1)); // 2.5 == 2.5

        let pred_gt = ColumnPredicate::gt(0, 0, Value::Float64(2.0));
        assert!(!pred_gt.evaluate(&chunk, 0, 0)); // 1.5 > 2.0 = false
        assert!(pred_gt.evaluate(&chunk, 0, 1)); // 2.5 > 2.0
        assert!(pred_gt.evaluate(&chunk, 0, 2)); // 3.5 > 2.0

        // Float ne, lt, le, ge
        let pred_ne = ColumnPredicate::ne(0, 0, Value::Float64(2.5));
        assert!(pred_ne.evaluate(&chunk, 0, 0)); // 1.5 != 2.5
        assert!(!pred_ne.evaluate(&chunk, 0, 1)); // 2.5 != 2.5 = false

        let pred_lt = ColumnPredicate::new(0, 0, CompareOp::Lt, Value::Float64(2.5));
        assert!(pred_lt.evaluate(&chunk, 0, 0)); // 1.5 < 2.5

        let pred_le = ColumnPredicate::new(0, 0, CompareOp::Le, Value::Float64(2.5));
        assert!(pred_le.evaluate(&chunk, 0, 1)); // 2.5 <= 2.5

        let pred_ge = ColumnPredicate::new(0, 0, CompareOp::Ge, Value::Float64(2.5));
        assert!(pred_ge.evaluate(&chunk, 0, 1)); // 2.5 >= 2.5
    }

    #[test]
    fn test_column_predicate_string() {
        let chunk = create_string_chunk();

        // String comparisons
        let pred_eq = ColumnPredicate::eq(0, 0, Value::String("banana".into()));
        assert!(!pred_eq.evaluate(&chunk, 0, 0)); // "apple" == "banana" = false
        assert!(pred_eq.evaluate(&chunk, 0, 1)); // "banana" == "banana"

        let pred_lt = ColumnPredicate::lt(0, 0, Value::String("banana".into()));
        assert!(pred_lt.evaluate(&chunk, 0, 0)); // "apple" < "banana"
        assert!(!pred_lt.evaluate(&chunk, 0, 1)); // "banana" < "banana" = false
        assert!(!pred_lt.evaluate(&chunk, 0, 2)); // "cherry" < "banana" = false

        // String ne, le, gt, ge
        let pred_ne = ColumnPredicate::ne(0, 0, Value::String("banana".into()));
        assert!(pred_ne.evaluate(&chunk, 0, 0)); // "apple" != "banana"
        assert!(!pred_ne.evaluate(&chunk, 0, 1)); // "banana" != "banana" = false

        let pred_le = ColumnPredicate::new(0, 0, CompareOp::Le, Value::String("banana".into()));
        assert!(pred_le.evaluate(&chunk, 0, 1)); // "banana" <= "banana"

        let pred_gt = ColumnPredicate::new(0, 0, CompareOp::Gt, Value::String("banana".into()));
        assert!(pred_gt.evaluate(&chunk, 0, 2)); // "cherry" > "banana"

        let pred_ge = ColumnPredicate::new(0, 0, CompareOp::Ge, Value::String("banana".into()));
        assert!(pred_ge.evaluate(&chunk, 0, 1)); // "banana" >= "banana"
    }

    #[test]
    fn test_column_predicate_bool() {
        let chunk = create_bool_chunk();

        // Bool comparisons (only eq and ne make sense)
        let pred_eq = ColumnPredicate::eq(0, 0, Value::Bool(true));
        assert!(pred_eq.evaluate(&chunk, 0, 0)); // true == true
        assert!(!pred_eq.evaluate(&chunk, 0, 1)); // false == true = false
        assert!(pred_eq.evaluate(&chunk, 0, 2)); // true == true

        let pred_ne = ColumnPredicate::ne(0, 0, Value::Bool(true));
        assert!(!pred_ne.evaluate(&chunk, 0, 0)); // true != true = false
        assert!(pred_ne.evaluate(&chunk, 0, 1)); // false != true

        // Lt/Gt on bool should return false
        let pred_lt = ColumnPredicate::lt(0, 0, Value::Bool(true));
        assert!(!pred_lt.evaluate(&chunk, 0, 0));
    }

    #[test]
    fn test_column_predicate_mixed_int_float() {
        let chunk = create_test_chunk();

        // Compare Int64 column with Float64 value
        let pred = ColumnPredicate::gt(1, 0, Value::Float64(2.5));
        assert!(!pred.evaluate(&chunk, 1, 0)); // 1 > 2.5 = false
        assert!(!pred.evaluate(&chunk, 1, 1)); // 2 > 2.5 = false
        assert!(pred.evaluate(&chunk, 1, 2)); // 3 > 2.5 = true

        // All comparison ops with mixed types
        let pred_eq = ColumnPredicate::eq(1, 0, Value::Float64(3.0));
        assert!(pred_eq.evaluate(&chunk, 1, 2)); // 3 == 3.0

        let pred_ne = ColumnPredicate::ne(1, 0, Value::Float64(3.0));
        assert!(!pred_ne.evaluate(&chunk, 1, 2)); // 3 != 3.0 = false

        let pred_lt = ColumnPredicate::lt(1, 0, Value::Float64(3.0));
        assert!(pred_lt.evaluate(&chunk, 1, 1)); // 2 < 3.0

        let pred_le = ColumnPredicate::new(1, 0, CompareOp::Le, Value::Float64(3.0));
        assert!(pred_le.evaluate(&chunk, 1, 2)); // 3 <= 3.0

        let pred_ge = ColumnPredicate::new(1, 0, CompareOp::Ge, Value::Float64(3.0));
        assert!(pred_ge.evaluate(&chunk, 1, 2)); // 3 >= 3.0
    }

    #[test]
    fn test_column_predicate_type_mismatch() {
        let chunk = create_test_chunk();

        // Compare Int64 column with String value - should return false
        let pred = ColumnPredicate::eq(1, 0, Value::String("hello".into()));
        assert!(!pred.evaluate(&chunk, 1, 0));
    }

    #[test]
    fn test_column_predicate_wrong_level() {
        let chunk = create_test_chunk();

        // Predicate targets level 1, but we evaluate at level 0
        let pred = ColumnPredicate::gt(1, 0, Value::Int64(5));

        // Should return true when evaluated at wrong level (predicate doesn't apply)
        assert!(pred.evaluate(&chunk, 0, 0));
    }

    #[test]
    fn test_column_predicate_invalid_column() {
        let chunk = create_test_chunk();

        // Predicate targets column 5 which doesn't exist
        let pred = ColumnPredicate::eq(1, 5, Value::Int64(1));

        assert!(!pred.evaluate(&chunk, 1, 0));
    }

    #[test]
    fn test_column_predicate_invalid_level() {
        let chunk = create_test_chunk();

        // Predicate targets level 5 which doesn't exist
        let pred = ColumnPredicate::eq(5, 0, Value::Int64(1));

        assert!(!pred.evaluate(&chunk, 5, 0));
    }

    #[test]
    fn test_column_predicate_target_level() {
        let pred = ColumnPredicate::eq(2, 0, Value::Int64(1));
        assert_eq!(pred.target_level(), Some(2));
    }

    #[test]
    fn test_column_predicate_batch() {
        let chunk = create_test_chunk();

        // Predicate: child value > 2
        let pred = ColumnPredicate::gt(1, 0, Value::Int64(2));

        let selection = pred.evaluate_batch(&chunk, 1);

        // Should select indices 2, 3, 4 (values 3, 4, 5)
        assert_eq!(selection.selected_count(), 3);
        assert!(!selection.is_selected(0));
        assert!(!selection.is_selected(1));
        assert!(selection.is_selected(2));
        assert!(selection.is_selected(3));
        assert!(selection.is_selected(4));
    }

    #[test]
    fn test_column_predicate_batch_invalid_level() {
        let chunk = create_test_chunk();

        // Predicate targets level 5 which doesn't exist
        let pred = ColumnPredicate::eq(5, 0, Value::Int64(1));

        let selection = pred.evaluate_batch(&chunk, 5);
        assert_eq!(selection.selected_count(), 0);
    }

    #[test]
    fn test_and_predicate() {
        let chunk = create_test_chunk();

        // Predicate: child value > 1 AND child value < 5
        let pred = AndPredicate::new(vec![
            Box::new(ColumnPredicate::gt(1, 0, Value::Int64(1))),
            Box::new(ColumnPredicate::lt(1, 0, Value::Int64(5))),
        ]);

        // Should match 2, 3, 4 (indices 1, 2, 3)
        assert!(!pred.evaluate(&chunk, 1, 0)); // 1: false
        assert!(pred.evaluate(&chunk, 1, 1)); // 2: true
        assert!(pred.evaluate(&chunk, 1, 2)); // 3: true
        assert!(pred.evaluate(&chunk, 1, 3)); // 4: true
        assert!(!pred.evaluate(&chunk, 1, 4)); // 5: false
    }

    #[test]
    fn test_and_predicate_target_level() {
        // Same level - should return that level
        let pred1 = AndPredicate::new(vec![
            Box::new(ColumnPredicate::gt(1, 0, Value::Int64(1))),
            Box::new(ColumnPredicate::lt(1, 0, Value::Int64(5))),
        ]);
        assert_eq!(pred1.target_level(), Some(1));

        // Different levels - should return None
        let pred2 = AndPredicate::new(vec![
            Box::new(ColumnPredicate::gt(0, 0, Value::Int64(1))),
            Box::new(ColumnPredicate::lt(1, 0, Value::Int64(5))),
        ]);
        assert_eq!(pred2.target_level(), None);

        // Empty predicates
        let pred3 = AndPredicate::new(vec![]);
        assert_eq!(pred3.target_level(), None);
    }

    #[test]
    fn test_or_predicate() {
        let chunk = create_test_chunk();

        // Predicate: child value = 1 OR child value = 5
        let pred = OrPredicate::new(vec![
            Box::new(ColumnPredicate::eq(1, 0, Value::Int64(1))),
            Box::new(ColumnPredicate::eq(1, 0, Value::Int64(5))),
        ]);

        // Should match 1 and 5 (indices 0 and 4)
        assert!(pred.evaluate(&chunk, 1, 0)); // 1: true
        assert!(!pred.evaluate(&chunk, 1, 1)); // 2: false
        assert!(!pred.evaluate(&chunk, 1, 2)); // 3: false
        assert!(!pred.evaluate(&chunk, 1, 3)); // 4: false
        assert!(pred.evaluate(&chunk, 1, 4)); // 5: true
    }

    #[test]
    fn test_or_predicate_target_level() {
        // Same level - should return that level
        let pred1 = OrPredicate::new(vec![
            Box::new(ColumnPredicate::eq(1, 0, Value::Int64(1))),
            Box::new(ColumnPredicate::eq(1, 0, Value::Int64(5))),
        ]);
        assert_eq!(pred1.target_level(), Some(1));

        // Different levels - should return None
        let pred2 = OrPredicate::new(vec![
            Box::new(ColumnPredicate::eq(0, 0, Value::Int64(1))),
            Box::new(ColumnPredicate::eq(1, 0, Value::Int64(5))),
        ]);
        assert_eq!(pred2.target_level(), None);

        // Empty predicates
        let pred3 = OrPredicate::new(vec![]);
        assert_eq!(pred3.target_level(), None);
    }

    #[test]
    fn test_factorized_filter_selection() {
        let chunk = create_test_chunk();

        // Predicate: child value > 2
        let pred = ColumnPredicate::gt(1, 0, Value::Int64(2));

        // Create a selection using the filter logic
        let level_counts: Vec<usize> = (0..chunk.level_count())
            .map(|i| chunk.level(i).map_or(0, |l| l.physical_value_count()))
            .collect();

        let mut selection = FactorizedSelection::all(&level_counts);
        selection = selection.filter_level(1, |idx| pred.evaluate(&chunk, 1, idx));

        // Level 0 should still have all selected
        assert!(selection.is_selected(0, 0));
        assert!(selection.is_selected(0, 1));

        // Level 1 should have only 3, 4, 5 selected
        assert!(!selection.is_selected(1, 0));
        assert!(!selection.is_selected(1, 1));
        assert!(selection.is_selected(1, 2));
        assert!(selection.is_selected(1, 3));
        assert!(selection.is_selected(1, 4));
    }

    #[test]
    fn test_factorized_filter_operator_apply_filter() {
        let chunk = create_test_chunk();

        // Create a mock operator using the apply_filter method
        // We can test apply_filter indirectly by creating the selection
        let pred = ColumnPredicate::gt(1, 0, Value::Int64(2));

        let level_count = chunk.level_count();
        let level_counts: Vec<usize> = (0..level_count)
            .map(|i| chunk.level(i).map_or(0, |l| l.physical_value_count()))
            .collect();

        let mut selection = FactorizedSelection::all(&level_counts);

        if let Some(target_level) = pred.target_level() {
            selection = selection
                .filter_level(target_level, |idx| pred.evaluate(&chunk, target_level, idx));
        }

        // Verify selection
        assert_eq!(selection.level(1).unwrap().selected_count(), 3);
    }

    #[test]
    fn test_factorized_filter_operator_empty_chunk() {
        let chunk = FactorizedChunk::empty();

        let pred = ColumnPredicate::gt(0, 0, Value::Int64(2));
        let level_counts: Vec<usize> = Vec::new();
        let selection = FactorizedSelection::all(&level_counts);

        // Empty chunk should result in empty selection
        assert_eq!(selection.level_count(), 0);
        assert!(!pred.evaluate(&chunk, 0, 0));
    }

    #[test]
    fn test_factorized_filter_multi_level_predicate() {
        let chunk = create_test_chunk();

        // Create a predicate that spans multiple levels (no target level)
        let pred = AndPredicate::new(vec![
            Box::new(ColumnPredicate::gt(0, 0, Value::Int64(15))), // source > 15
            Box::new(ColumnPredicate::lt(1, 0, Value::Int64(5))),  // child < 5
        ]);

        assert_eq!(pred.target_level(), None);

        // Test evaluation - only source 20 (idx 1) passes level 0 filter
        // And children 4 (idx 3) passes level 1 filter
        // At level 0, idx 0 (value 10) should fail
        assert!(!pred.evaluate(&chunk, 0, 0)); // 10 > 15 = false
        assert!(pred.evaluate(&chunk, 0, 1)); // 20 > 15 = true

        // At level 1, all pass the level 0 check, then check level 1
        assert!(pred.evaluate(&chunk, 1, 3)); // 4 < 5 = true
        assert!(!pred.evaluate(&chunk, 1, 4)); // 5 < 5 = false
    }

    #[test]
    fn test_compare_op_debug() {
        // Ensure CompareOp derives Debug properly
        assert_eq!(format!("{:?}", CompareOp::Eq), "Eq");
        assert_eq!(format!("{:?}", CompareOp::Ne), "Ne");
        assert_eq!(format!("{:?}", CompareOp::Lt), "Lt");
        assert_eq!(format!("{:?}", CompareOp::Le), "Le");
        assert_eq!(format!("{:?}", CompareOp::Gt), "Gt");
        assert_eq!(format!("{:?}", CompareOp::Ge), "Ge");
    }

    #[test]
    fn test_compare_op_clone_eq() {
        let op1 = CompareOp::Eq;
        let op2 = op1;
        assert_eq!(op1, op2);
    }

    #[test]
    fn test_column_predicate_debug_clone() {
        let pred = ColumnPredicate::eq(1, 0, Value::Int64(5));
        let pred_clone = pred.clone();
        assert_eq!(format!("{:?}", pred), format!("{:?}", pred_clone));
    }

    // ==================== PropertyPredicate Tests ====================

    mod property_predicate_tests {
        use super::*;

        fn create_test_store() -> Arc<LpgStore> {
            Arc::new(LpgStore::new().unwrap())
        }

        fn create_chunk_with_node_ids(store: &Arc<LpgStore>) -> FactorizedChunk {
            // Create some nodes with properties
            let node1 = store.create_node(&["Person"]);
            let node2 = store.create_node(&["Person"]);
            let node3 = store.create_node(&["Person"]);

            store.set_node_property(node1, "age", Value::Int64(25));
            store.set_node_property(node2, "age", Value::Int64(35));
            store.set_node_property(node3, "age", Value::Int64(45));

            store.set_node_property(node1, "name", Value::String("Alix".into()));
            store.set_node_property(node2, "name", Value::String("Gus".into()));
            store.set_node_property(node3, "name", Value::String("Harm".into()));

            // Create a chunk with node IDs
            let mut node_data = ValueVector::with_type(LogicalType::Node);
            node_data.push_node_id(node1);
            node_data.push_node_id(node2);
            node_data.push_node_id(node3);

            let level0 = FactorizationLevel::flat(
                vec![FactorizedVector::flat(node_data)],
                vec!["n".to_string()],
            );

            let mut chunk = FactorizedChunk::empty();
            chunk.add_factorized_level(level0);
            chunk
        }

        #[test]
        fn test_property_predicate_eq_int() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Predicate: age = 35
            let pred = PropertyPredicate::eq(0, 0, "age", Value::Int64(35), store.clone());

            assert!(!pred.evaluate(&chunk, 0, 0)); // Alix age=25
            assert!(pred.evaluate(&chunk, 0, 1)); // Gus age=35
            assert!(!pred.evaluate(&chunk, 0, 2)); // Harm age=45
        }

        #[test]
        fn test_property_predicate_gt_int() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Predicate: age > 30
            let pred =
                PropertyPredicate::new(0, 0, "age", CompareOp::Gt, Value::Int64(30), store.clone());

            assert!(!pred.evaluate(&chunk, 0, 0)); // 25 > 30 = false
            assert!(pred.evaluate(&chunk, 0, 1)); // 35 > 30 = true
            assert!(pred.evaluate(&chunk, 0, 2)); // 45 > 30 = true
        }

        #[test]
        fn test_property_predicate_lt_le_ge() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // age < 35
            let pred_lt =
                PropertyPredicate::new(0, 0, "age", CompareOp::Lt, Value::Int64(35), store.clone());
            assert!(pred_lt.evaluate(&chunk, 0, 0)); // 25 < 35
            assert!(!pred_lt.evaluate(&chunk, 0, 1)); // 35 < 35 = false

            // age <= 35
            let pred_le =
                PropertyPredicate::new(0, 0, "age", CompareOp::Le, Value::Int64(35), store.clone());
            assert!(pred_le.evaluate(&chunk, 0, 0)); // 25 <= 35
            assert!(pred_le.evaluate(&chunk, 0, 1)); // 35 <= 35
            assert!(!pred_le.evaluate(&chunk, 0, 2)); // 45 <= 35 = false

            // age >= 35
            let pred_ge =
                PropertyPredicate::new(0, 0, "age", CompareOp::Ge, Value::Int64(35), store.clone());
            assert!(!pred_ge.evaluate(&chunk, 0, 0)); // 25 >= 35 = false
            assert!(pred_ge.evaluate(&chunk, 0, 1)); // 35 >= 35
            assert!(pred_ge.evaluate(&chunk, 0, 2)); // 45 >= 35
        }

        #[test]
        fn test_property_predicate_ne() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            let pred =
                PropertyPredicate::new(0, 0, "age", CompareOp::Ne, Value::Int64(35), store.clone());

            assert!(pred.evaluate(&chunk, 0, 0)); // 25 != 35
            assert!(!pred.evaluate(&chunk, 0, 1)); // 35 != 35 = false
            assert!(pred.evaluate(&chunk, 0, 2)); // 45 != 35
        }

        #[test]
        fn test_property_predicate_string() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // name = "Gus"
            let pred =
                PropertyPredicate::eq(0, 0, "name", Value::String("Gus".into()), store.clone());

            assert!(!pred.evaluate(&chunk, 0, 0)); // Alix
            assert!(pred.evaluate(&chunk, 0, 1)); // Gus
            assert!(!pred.evaluate(&chunk, 0, 2)); // Harm

            // name < "Gus"
            let pred_lt = PropertyPredicate::new(
                0,
                0,
                "name",
                CompareOp::Lt,
                Value::String("Gus".into()),
                store.clone(),
            );
            assert!(pred_lt.evaluate(&chunk, 0, 0)); // "Alix" < "Gus"
            assert!(!pred_lt.evaluate(&chunk, 0, 1)); // "Gus" < "Gus" = false

            // name > "Gus"
            let pred_gt = PropertyPredicate::new(
                0,
                0,
                "name",
                CompareOp::Gt,
                Value::String("Gus".into()),
                store.clone(),
            );
            assert!(pred_gt.evaluate(&chunk, 0, 2)); // "Harm" > "Gus"
            assert!(!pred_gt.evaluate(&chunk, 0, 0)); // "Alix" > "Gus" = false
        }

        #[test]
        fn test_property_predicate_float() {
            let store = create_test_store();

            // Add float properties
            let node1 = store.create_node(&["Thing"]);
            let node2 = store.create_node(&["Thing"]);
            store.set_node_property(node1, "score", Value::Float64(1.5));
            store.set_node_property(node2, "score", Value::Float64(2.5));

            let mut node_data = ValueVector::with_type(LogicalType::Node);
            node_data.push_node_id(node1);
            node_data.push_node_id(node2);

            let level0 = FactorizationLevel::flat(
                vec![FactorizedVector::flat(node_data)],
                vec!["n".to_string()],
            );
            let mut chunk = FactorizedChunk::empty();
            chunk.add_factorized_level(level0);

            // score = 2.5
            let pred_eq = PropertyPredicate::new(
                0,
                0,
                "score",
                CompareOp::Eq,
                Value::Float64(2.5),
                store.clone(),
            );
            assert!(!pred_eq.evaluate(&chunk, 0, 0));
            assert!(pred_eq.evaluate(&chunk, 0, 1));

            // score != 2.5
            let pred_ne = PropertyPredicate::new(
                0,
                0,
                "score",
                CompareOp::Ne,
                Value::Float64(2.5),
                store.clone(),
            );
            assert!(pred_ne.evaluate(&chunk, 0, 0));
            assert!(!pred_ne.evaluate(&chunk, 0, 1));

            // score > 2.0
            let pred_gt = PropertyPredicate::new(
                0,
                0,
                "score",
                CompareOp::Gt,
                Value::Float64(2.0),
                store.clone(),
            );
            assert!(!pred_gt.evaluate(&chunk, 0, 0)); // 1.5 > 2.0 = false
            assert!(pred_gt.evaluate(&chunk, 0, 1)); // 2.5 > 2.0

            // score < 2.0
            let pred_lt = PropertyPredicate::new(
                0,
                0,
                "score",
                CompareOp::Lt,
                Value::Float64(2.0),
                store.clone(),
            );
            assert!(pred_lt.evaluate(&chunk, 0, 0)); // 1.5 < 2.0

            // score <= 1.5
            let pred_le = PropertyPredicate::new(
                0,
                0,
                "score",
                CompareOp::Le,
                Value::Float64(1.5),
                store.clone(),
            );
            assert!(pred_le.evaluate(&chunk, 0, 0)); // 1.5 <= 1.5

            // score >= 2.5
            let pred_ge = PropertyPredicate::new(
                0,
                0,
                "score",
                CompareOp::Ge,
                Value::Float64(2.5),
                store.clone(),
            );
            assert!(pred_ge.evaluate(&chunk, 0, 1)); // 2.5 >= 2.5
        }

        #[test]
        fn test_property_predicate_bool() {
            let store = create_test_store();

            let node1 = store.create_node(&["Flag"]);
            let node2 = store.create_node(&["Flag"]);
            store.set_node_property(node1, "active", Value::Bool(true));
            store.set_node_property(node2, "active", Value::Bool(false));

            let mut node_data = ValueVector::with_type(LogicalType::Node);
            node_data.push_node_id(node1);
            node_data.push_node_id(node2);

            let level0 = FactorizationLevel::flat(
                vec![FactorizedVector::flat(node_data)],
                vec!["n".to_string()],
            );
            let mut chunk = FactorizedChunk::empty();
            chunk.add_factorized_level(level0);

            // active = true
            let pred = PropertyPredicate::eq(0, 0, "active", Value::Bool(true), store.clone());
            assert!(pred.evaluate(&chunk, 0, 0));
            assert!(!pred.evaluate(&chunk, 0, 1));

            // active != true
            let pred_ne = PropertyPredicate::new(
                0,
                0,
                "active",
                CompareOp::Ne,
                Value::Bool(true),
                store.clone(),
            );
            assert!(!pred_ne.evaluate(&chunk, 0, 0));
            assert!(pred_ne.evaluate(&chunk, 0, 1));
        }

        #[test]
        fn test_property_predicate_missing_property() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Property "nonexistent" doesn't exist
            let pred = PropertyPredicate::eq(0, 0, "nonexistent", Value::Int64(1), store.clone());

            // Should return false for missing property
            assert!(!pred.evaluate(&chunk, 0, 0));
            assert!(!pred.evaluate(&chunk, 0, 1));
        }

        #[test]
        fn test_property_predicate_wrong_level() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Predicate targets level 1, but chunk only has level 0
            let pred = PropertyPredicate::eq(1, 0, "age", Value::Int64(35), store.clone());

            // Should return true when evaluated at wrong level
            assert!(pred.evaluate(&chunk, 0, 0));
        }

        #[test]
        fn test_property_predicate_invalid_column() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Column 5 doesn't exist
            let pred = PropertyPredicate::eq(0, 5, "age", Value::Int64(35), store.clone());

            assert!(!pred.evaluate(&chunk, 0, 0));
        }

        #[test]
        fn test_property_predicate_target_level() {
            let store = create_test_store();
            let pred = PropertyPredicate::eq(2, 0, "age", Value::Int64(35), store);
            assert_eq!(pred.target_level(), Some(2));
        }

        #[test]
        fn test_property_predicate_batch() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Predicate: age > 30
            let pred =
                PropertyPredicate::new(0, 0, "age", CompareOp::Gt, Value::Int64(30), store.clone());

            let selection = pred.evaluate_batch(&chunk, 0);

            // Should select indices 1 and 2 (Gus=35, Harm=45)
            assert_eq!(selection.selected_count(), 2);
            assert!(!selection.is_selected(0)); // Alix=25
            assert!(selection.is_selected(1)); // Gus=35
            assert!(selection.is_selected(2)); // Harm=45
        }

        #[test]
        fn test_property_predicate_batch_wrong_level() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Predicate targets level 1
            let pred =
                PropertyPredicate::new(1, 0, "age", CompareOp::Gt, Value::Int64(30), store.clone());

            // Batch evaluate at level 0 - should return all selected
            let selection = pred.evaluate_batch(&chunk, 0);
            assert_eq!(selection.selected_count(), 3);
        }

        #[test]
        fn test_property_predicate_batch_invalid_level() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Predicate targets level 5 which doesn't exist
            let pred =
                PropertyPredicate::new(5, 0, "age", CompareOp::Gt, Value::Int64(30), store.clone());

            let selection = pred.evaluate_batch(&chunk, 5);
            assert_eq!(selection.selected_count(), 0);
        }

        #[test]
        fn test_property_predicate_batch_invalid_column() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // Column 5 doesn't exist
            let pred =
                PropertyPredicate::new(0, 5, "age", CompareOp::Gt, Value::Int64(30), store.clone());

            let selection = pred.evaluate_batch(&chunk, 0);
            // Should return all false (no matches)
            assert_eq!(selection.selected_count(), 0);
        }

        #[test]
        fn test_property_predicate_type_mismatch() {
            let store = create_test_store();
            let chunk = create_chunk_with_node_ids(&store);

            // age is Int64, but we compare with String
            let pred =
                PropertyPredicate::eq(0, 0, "age", Value::String("35".into()), store.clone());

            // Type mismatch should return false
            assert!(!pred.evaluate(&chunk, 0, 1));
        }
    }
}
