datafusion-match-recognize 0.56.0

MATCH_RECOGNIZE extension for 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.

//! Resumable validation of an expanded winner before its private batch encoding.

use super::*;
use crate::execution::matching::ResolvedAfterMatchSkip;
use crate::execution::matching::path::{SelectedStepSources, SelectedSteps};

pub(crate) struct SelectedPathFill {
    pub(crate) steps: SelectedSteps,
    validation: SelectedPathValidation,
    pub(crate) start: AbsoluteRow,
    end: AbsoluteRow,
    acceptance: AbsoluteRow,
    skip_target: Option<AbsoluteRow>,
}

impl SelectedPathFill {
    pub(crate) fn new(
        steps: SelectedSteps,
        start: AbsoluteRow,
        end: AbsoluteRow,
        acceptance: AbsoluteRow,
    ) -> Self {
        Self {
            steps,
            validation: SelectedPathValidation::default(),
            start,
            end,
            acceptance,
            skip_target: None,
        }
    }

    /// Bind the frozen source anew for each slice. The validator and the restart
    /// policy see each step exactly once, in its actual reconstruction order.
    pub(crate) fn advance<'a, 's>(
        &mut self,
        out: &'a MatchAccumulator,
        automaton: &'a CompiledAutomaton,
        skip: &ResolvedAfterMatchSkip,
        emission: &MatchEmissionContext<'_>,
        source: impl FnOnce(&SelectedSteps) -> Result<(SelectedStepSources<'s>, usize)>,
        completed: &mut impl FnMut(usize) -> Result<()>,
    ) -> Result<Option<SelectedPathValidator<'a>>> {
        let order = if self.steps.in_reverse() {
            SelectedPathFillOrder::Reverse
        } else {
            SelectedPathFillOrder::Forward
        };
        let mut validator = out
            .selected_path_validator_for_automaton(
                automaton,
                &emission.partition,
                emission.global_offset,
                emission.batches.num_rows(),
                order,
            )?
            .with_progress(std::mem::take(&mut self.validation));
        let (source, chunk_rows) = source(&self.steps)?;
        if self.steps.advance(
            source,
            chunk_rows,
            &mut |step| {
                validator.observe(step)?;
                skip.observe_selected_step(step, &mut self.skip_target);
                Ok(())
            },
            completed,
        )? {
            return Ok(Some(validator));
        }
        self.validation = validator.into_progress();
        Ok(None)
    }

    pub(crate) fn finish(
        self,
        validator: SelectedPathValidator<'_>,
    ) -> Result<(CheckedSelectedPath, Option<AbsoluteRow>)> {
        let path = validator.finish(
            self.steps.finish()?,
            self.start,
            self.end,
            self.acceptance,
        )?;
        Ok((path, self.skip_target))
    }
}

/// These two values stay together across yields, so an encoding cursor cannot
/// be resumed against a different winner's rows.
pub(crate) struct ExpandedMatchPreparation {
    pub(crate) path: CheckedSelectedPath,
    progress: MatchEmissionProgress,
}

impl ExpandedMatchPreparation {
    pub(crate) fn begin(
        out: &mut MatchAccumulator,
        match_id: u64,
        path: CheckedSelectedPath,
        emission: &MatchEmissionContext<'_>,
    ) -> Result<Self> {
        let progress = out.begin_checked_match(match_id, &path, emission)?;
        Ok(Self { path, progress })
    }

    pub(crate) fn advance(
        &mut self,
        out: &mut MatchAccumulator,
        emission: MatchEmissionContext<'_>,
        completed: &mut impl FnMut(usize) -> Result<()>,
    ) -> Result<Option<PreparedMatchEmission>> {
        out.prepare_path_chunk(&mut self.progress, self.path.steps(), emission, completed)
    }
}