Skip to main content

datafusion_physical_expr/window/
standard_window_function_expr.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::{PhysicalExpr, PhysicalSortExpr};
6
7use arrow::array::ArrayRef;
8use arrow::datatypes::{FieldRef, SchemaRef};
9use arrow::record_batch::RecordBatch;
10use datafusion_common::Result;
11use datafusion_expr::{LimitEffect, PartitionEvaluator};
12
13use datafusion_physical_expr_common::utils::evaluate_expressions_to_arrays;
14use std::any::Any;
15use std::sync::Arc;
16
17/// Evaluates a window function by instantiating a
18/// [`PartitionEvaluator`] for calculating the function's output in
19/// that partition.
20///
21/// Note that unlike aggregation based window functions, some window
22/// functions such as `rank` ignore the values in the window frame,
23/// but others such as `first_value`, `last_value`, and
24/// `nth_value` need the value.
25pub trait StandardWindowFunctionExpr: Send + Sync + std::fmt::Debug {
26    /// Returns the aggregate expression as [`Any`] so that it can be
27    /// downcast to a specific implementation.
28    fn as_any(&self) -> &dyn Any;
29
30    /// The field of the final result of evaluating this window function.
31    fn field(&self) -> Result<FieldRef>;
32
33    /// Expressions that are passed to the [`PartitionEvaluator`].
34    fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>>;
35
36    /// Human readable name such as `"MIN(c2)"` or `"RANK()"`. The default
37    /// implementation returns placeholder text.
38    fn name(&self) -> &str {
39        "StandardWindowFunctionExpr: default name"
40    }
41
42    /// Evaluate window function's arguments against the input window
43    /// batch and return an [`ArrayRef`].
44    ///
45    /// Typically, the resulting vector is a single element vector.
46    fn evaluate_args(&self, batch: &RecordBatch) -> Result<Vec<ArrayRef>> {
47        evaluate_expressions_to_arrays(&self.expressions(), batch)
48    }
49
50    /// Create a [`PartitionEvaluator`] for evaluating the function on
51    /// a particular partition.
52    fn create_evaluator(&self) -> Result<Box<dyn PartitionEvaluator>>;
53
54    /// Construct a new [`StandardWindowFunctionExpr`] that produces
55    /// the same result as this function on a window with reverse
56    /// order. The return value of this function is used by the
57    /// DataFusion optimizer to avoid re-sorting the data when
58    /// possible.
59    ///
60    /// Returns `None` (the default) if no reverse is known (or possible).
61    ///
62    /// For example, the reverse of `lead(10)` is `lag(10)`.
63    fn reverse_expr(&self) -> Option<Arc<dyn StandardWindowFunctionExpr>> {
64        None
65    }
66
67    /// Returns the ordering introduced by the window function, if applicable.
68    /// Most window functions don't introduce an ordering, hence the default
69    /// value is `None`. Note that this information is used to update ordering
70    /// equivalences.
71    fn get_result_ordering(&self, _schema: &SchemaRef) -> Option<PhysicalSortExpr> {
72        None
73    }
74
75    fn limit_effect(&self) -> LimitEffect;
76}