datafusion-match-recognize 0.56.1

SQL MATCH_RECOGNIZE row-pattern matching for Apache DataFusion
Documentation
// Licensed under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2025-Present Datadog, Inc.

//! Apply the shared navigation writers to a packed General frame.

use super::*;
use crate::core::SymbolId;
use crate::core::vm::{NavigationUpdate, NavigationValueDomain};
use crate::execution::common::progress::CoarseWorkCounter;

#[allow(clippy::too_many_arguments)]
pub(super) fn execute_vm_navigation_stores<Report>(
    agg_engine: &mut AggregateEngine<'_, '_>,
    matcher: &PatternMatcher,
    memory: &datafusion::execution::memory_pool::MemoryReservation,
    next_state: &mut ActiveThread,
    sym_id: SymbolId,
    ops: &[NavigationUpdate],
    work: &mut CoarseWorkCounter,
    report_completed_work: &mut Report,
) -> Result<()>
where
    Report: FnMut(usize) -> Result<()>,
{
    execute_vm_navigation_stores_into(
        agg_engine,
        matcher,
        sym_id,
        |writer, words, is_null| {
            if writer.kind == crate::core::vm::NavigationKind::First
                && next_state
                    .nav(matcher.program.vm_program().packed_navigation_layout())
                    .is_first_set(usize::from(writer.storage_idx))
            {
                return Ok(());
            }
            next_state
                .nav_mut(
                    matcher.program.vm_program().packed_navigation_layout(),
                    memory,
                )?
                .apply_update(writer, words, is_null)?;
            Ok(())
        },
        ops,
        work,
        report_completed_work,
    )
}

#[allow(clippy::too_many_arguments)]
pub(in crate::execution::matching::row_loop) fn execute_vm_navigation_stores_into<
    Report,
>(
    agg_engine: &mut AggregateEngine<'_, '_>,
    matcher: &PatternMatcher,
    sym_id: SymbolId,
    mut store: impl FnMut(NavigationUpdate, [u64; 2], bool) -> Result<()>,
    ops: &[NavigationUpdate],
    work: &mut CoarseWorkCounter,
    report_completed_work: &mut Report,
) -> Result<()>
where
    Report: FnMut(usize) -> Result<()>,
{
    if ops.is_empty() {
        return Ok(());
    }

    for &writer in ops {
        let (words, is_null) = if writer.domain == NavigationValueDomain::Classifier {
            ([sym_id.index() as u64, 0], false)
        } else {
            let bindings = matcher.row_bindings();
            let input = usize::from(writer.value_idx);
            let column = match writer.domain {
                NavigationValueDomain::Primitive => bindings.nav_columns[input].index(),
                NavigationValueDomain::Decimal128 => {
                    bindings.nav_d128_columns[input].index()
                }
                NavigationValueDomain::Classifier => {
                    unreachable!("classifier needs no row")
                }
            };
            let is_null = agg_engine.input_is_null(column)?;
            let words = if is_null {
                [0; 2]
            } else if writer.domain == NavigationValueDomain::Primitive {
                [agg_engine.input_word(column, false)?, 0]
            } else {
                let (low, high) = agg_engine.input_decimal(column)?;
                [low, high]
            };
            (words, is_null)
        };
        let op_result = store(writer, words, is_null);
        let accounting = work.complete_one(report_completed_work);
        op_result?;
        accounting?;
    }
    Ok(())
}