hawdb 0.33.0

An embedded Rust graph database for local data-plane workloads
Documentation
// Copyright 2026 Nowledge
// SPDX-License-Identifier: Apache-2.0

use crate::cypher;

pub(in crate::api) fn match_is_deterministic(
    patterns: &[cypher::MatchPattern],
    predicate: Option<&cypher::PredicateExpression>,
) -> bool {
    patterns.iter().all(|pattern| {
        pattern
            .first
            .properties
            .values()
            .all(value_is_deterministic)
            && pattern.steps.iter().all(|step| {
                step.relationship
                    .properties
                    .values()
                    .chain(step.target.properties.values())
                    .all(value_is_deterministic)
            })
    }) && predicate.is_none_or(|predicate| predicate_is_deterministic(&predicate.kind))
}

pub(in crate::api) fn projection_is_deterministic(projection: &cypher::ProjectionClause) -> bool {
    projection
        .items
        .iter()
        .all(|item| return_is_deterministic(&item.expression))
        && projection
            .order_by
            .iter()
            .all(|item| match &item.expression {
                cypher::OrderExpression::Value(value) => scalar_is_deterministic(value),
                cypher::OrderExpression::Projection(expression) => {
                    return_is_deterministic(expression)
                }
                cypher::OrderExpression::Property { .. }
                | cypher::OrderExpression::Id { .. }
                | cypher::OrderExpression::Column(_) => true,
            })
        && projection
            .predicate
            .as_ref()
            .is_none_or(|predicate| predicate_is_deterministic(&predicate.kind))
        && projection
            .offset
            .iter()
            .chain(projection.limit.iter())
            .all(value_is_deterministic)
}

pub(super) fn graph_options_are_deterministic(options: &cypher::GraphAlgorithmOptions) -> bool {
    [
        &options.damping,
        &options.max_iterations,
        &options.max_phases,
        &options.max_levels,
        &options.tolerance,
        &options.normalize_initial,
        &options.resolution,
    ]
    .into_iter()
    .flatten()
    .all(value_is_deterministic)
}

pub(in crate::api) fn value_is_deterministic(value: &cypher::ValueExpression) -> bool {
    match &value.kind {
        cypher::ValueExpressionKind::CurrentTimestamp => false,
        cypher::ValueExpressionKind::List(values) => values.iter().all(value_is_deterministic),
        cypher::ValueExpressionKind::Timestamp(value)
        | cypher::ValueExpressionKind::Conversion {
            argument: value, ..
        } => value_is_deterministic(value),
        // `date()`, `datetime()`, ... read the statement clock, and so does a
        // constructor whose map argument has only `timezone`. Both are bound
        // while planning. (A parameter map is checked against its value
        // before the plan cache is consulted.)
        cypher::ValueExpressionKind::Temporal {
            function,
            arguments,
        } => {
            !function.reads_clock(arguments.len())
                && !arguments.iter().any(value_is_clock_reading_map)
                && arguments.iter().all(value_is_deterministic)
        }
        cypher::ValueExpressionKind::Map(entries) => entries.values().all(value_is_deterministic),
        cypher::ValueExpressionKind::BindingIndex { .. }
        | cypher::ValueExpressionKind::Literal(_)
        | cypher::ValueExpressionKind::Parameter(_)
        | cypher::ValueExpressionKind::Binding(_)
        | cypher::ValueExpressionKind::BindingProperty { .. } => true,
    }
}

fn value_is_clock_reading_map(value: &cypher::ValueExpression) -> bool {
    match &value.kind {
        cypher::ValueExpressionKind::Map(entries) => entries
            .keys()
            .all(|key| key.eq_ignore_ascii_case("timezone")),
        cypher::ValueExpressionKind::Literal(value) => {
            hawdb_core::temporal::is_clock_reading_map(value)
        }
        _ => false,
    }
}

pub(in crate::api) fn scalar_is_deterministic(expression: &cypher::ScalarExpression) -> bool {
    use cypher::ScalarExpressionKind as Scalar;
    let values_are_deterministic = match &expression.kind {
        Scalar::Value(value) => value_is_deterministic(value),
        Scalar::JsonValue { path, .. } => value_is_deterministic(path),
        Scalar::Left { length, .. } => value_is_deterministic(length),
        Scalar::DefaultIfNullOrEq { empty, default, .. } => {
            value_is_deterministic(empty) && value_is_deterministic(default)
        }
        Scalar::DefaultIfNull { default, .. }
        | Scalar::CaseLowerPropertyDefault { default, .. } => value_is_deterministic(default),
        Scalar::CasePropertyNotNullOrEq {
            empty,
            non_empty,
            null_or_empty,
            ..
        } => [empty, non_empty, null_or_empty]
            .into_iter()
            .all(|value| value_is_deterministic(value)),
        Scalar::CasePropertyEqualsRank {
            branches, default, ..
        } => branches
            .iter()
            .flat_map(|(condition, result)| [condition, result])
            .chain([default])
            .all(value_is_deterministic),
        Scalar::CaseCoalesceDifferenceFloorZero { terms, .. } => terms
            .iter()
            .all(|term| value_is_deterministic(&term.default)),
        Scalar::PatternPredicate(pattern) | Scalar::PatternComprehension { pattern, .. } => {
            match_is_deterministic(std::slice::from_ref(pattern.as_ref()), None)
        }
        Scalar::Variable(_)
        | Scalar::Property { .. }
        | Scalar::Id(_)
        | Scalar::RelationshipType(_)
        | Scalar::Label { .. }
        | Scalar::Coalesce(_)
        | Scalar::Lower(_)
        | Scalar::DatePart { .. }
        | Scalar::Case { .. }
        | Scalar::VectorDistance { .. }
        | Scalar::Binary { .. }
        | Scalar::Not(_)
        | Scalar::IsNull { .. }
        | Scalar::ListFunction { .. }
        | Scalar::StringFunction { .. }
        | Scalar::ListComprehension { .. }
        | Scalar::List(_)
        | Scalar::ListIndex { .. }
        | Scalar::Conversion { .. }
        | Scalar::NumericFunction { .. }
        | Scalar::Reduce { .. }
        | Scalar::RelationshipEndpoint { .. }
        | Scalar::ListSlice { .. }
        | Scalar::Path(_)
        | Scalar::PropertyAccess { .. }
        | Scalar::MapLiteral(_) => true,
        // A clock read (`date()`) differs between executions; a map
        // argument with only `timezone` reads the clock at run time.
        Scalar::TemporalFunction {
            function,
            arguments,
        } => {
            !function.reads_clock(arguments.len())
                && !arguments.iter().any(|argument| {
                    matches!(&argument.kind, Scalar::MapLiteral(entries)
                        if entries.iter().all(|(key, _)| key.eq_ignore_ascii_case("timezone")))
                })
        }
    };
    values_are_deterministic && expression.kind.all_children(scalar_is_deterministic)
}

fn return_is_deterministic(expression: &cypher::ReturnExpression) -> bool {
    match &expression.kind {
        cypher::ReturnExpressionKind::Value(value) => scalar_is_deterministic(value),
        cypher::ReturnExpressionKind::Arithmetic { first, rest } => {
            return_is_deterministic(first)
                && rest.iter().all(|(_, value)| return_is_deterministic(value))
        }
        cypher::ReturnExpressionKind::Aggregate(cypher::AggregateExpression::Expression {
            argument,
            ..
        }) => return_is_deterministic(argument),
        cypher::ReturnExpressionKind::ListFunction { arguments, .. } => {
            arguments.iter().all(return_is_deterministic)
        }
        cypher::ReturnExpressionKind::Aggregate(_) | cypher::ReturnExpressionKind::Path(_) => true,
    }
}

fn predicate_is_deterministic(predicate: &cypher::PropertyPredicate) -> bool {
    use cypher::PropertyPredicate as Predicate;
    match predicate {
        Predicate::And(values) | Predicate::Or(values) => {
            values.iter().all(predicate_is_deterministic)
        }
        Predicate::Not(value) => predicate_is_deterministic(value),
        Predicate::IdEq { value, .. }
        | Predicate::IdNotEq { value, .. }
        | Predicate::IdCompare { value, .. }
        | Predicate::Eq { value, .. }
        | Predicate::NotEq { value, .. }
        | Predicate::Compare { value, .. }
        | Predicate::ListContains { value, .. }
        | Predicate::ListContainsLower { value, .. }
        | Predicate::Contains { value, .. }
        | Predicate::StartsWith { value, .. }
        | Predicate::EndsWith { value, .. }
        | Predicate::RegexMatch { pattern: value, .. }
        | Predicate::ParameterEq { right: value, .. }
        | Predicate::ParameterNotEq { right: value, .. }
        | Predicate::IdIn { values: value, .. }
        | Predicate::In { values: value, .. } => value_is_deterministic(value),
        Predicate::ExpressionIn { expression, values } => {
            scalar_is_deterministic(expression) && value_is_deterministic(values)
        }
        Predicate::ExpressionEq { expression, value }
        | Predicate::ExpressionNotEq { expression, value }
        | Predicate::ExpressionCompare {
            expression, value, ..
        }
        | Predicate::ExpressionContains { expression, value } => {
            scalar_is_deterministic(expression) && scalar_is_deterministic(value)
        }
        Predicate::ExpressionRegexMatch {
            expression,
            pattern,
        } => scalar_is_deterministic(expression) && value_is_deterministic(pattern),
        Predicate::RelationshipExists { .. }
        | Predicate::BoundRelationshipExists { .. }
        | Predicate::IsNull { .. }
        | Predicate::IsNotNull { .. }
        | Predicate::ParameterIsNull { .. }
        | Predicate::ParameterIsNotNull { .. } => true,
    }
}