Skip to main content

vortex_scan/plan/builtins/row_idx/
expr.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::fmt::Formatter;
5
6use vortex_array::ArrayRef;
7use vortex_array::dtype::DType;
8use vortex_array::dtype::Nullability;
9use vortex_array::dtype::PType;
10use vortex_array::scalar_fn::Arity;
11use vortex_array::scalar_fn::ChildName;
12use vortex_array::scalar_fn::EmptyOptions;
13use vortex_array::scalar_fn::ExecutionArgs;
14use vortex_array::scalar_fn::ScalarFnId;
15use vortex_array::scalar_fn::ScalarFnVTable;
16use vortex_array::scalar_fn::display::ExprDisplay;
17use vortex_error::VortexResult;
18use vortex_error::vortex_bail;
19use vortex_expr::Expression;
20use vortex_expr::ScalarFnExprExt;
21use vortex_session::registry::CachedId;
22
23#[derive(Clone)]
24/// A reference to the global row index supplied by a scan.
25pub struct RowIdx;
26
27impl ScalarFnVTable for RowIdx {
28    type Options = EmptyOptions;
29
30    fn id(&self) -> ScalarFnId {
31        static ID: CachedId = CachedId::new("vortex.row_idx");
32        *ID
33    }
34
35    fn arity(&self, _options: &Self::Options) -> Arity {
36        Arity::Exact(0)
37    }
38
39    fn child_name(&self, _instance: &Self::Options, _child_idx: usize) -> ChildName {
40        unreachable!()
41    }
42
43    fn fmt_sql(
44        &self,
45        _options: &Self::Options,
46        _expr: &dyn ExprDisplay,
47        f: &mut Formatter<'_>,
48    ) -> std::fmt::Result {
49        write!(f, "#row_idx")
50    }
51
52    fn return_dtype(&self, _options: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
53        Ok(DType::Primitive(PType::U64, Nullability::NonNullable))
54    }
55
56    fn execute(
57        &self,
58        _options: &Self::Options,
59        _args: &dyn ExecutionArgs,
60        _ctx: &mut vortex_array::ExecutionCtx,
61    ) -> VortexResult<ArrayRef> {
62        vortex_bail!(
63            "RowIdxExpr should not be executed directly, use it in the context of a Vortex scan and it will be substituted for a row index array"
64        );
65    }
66
67    fn is_strict(&self, _options: &Self::Options) -> bool {
68        true
69    }
70}
71
72/// Returns an expression for the global scan row index.
73pub fn row_idx() -> Expression {
74    RowIdx.new_expr(EmptyOptions, [])
75}
76
77#[cfg(test)]
78mod tests {
79    use vortex_expr::Lambda;
80    use vortex_expr::checked_add;
81    use vortex_expr::var;
82
83    use super::row_idx;
84
85    /// The scan replaces `row_idx()` before anything runs, but never looks inside a lambda's body,
86    /// so a lambda can't read it. `Lambda::new` recognizes it by its id.
87    #[test]
88    fn a_lambda_cannot_read_row_idx() {
89        let error = Lambda::new(["x"], checked_add(var("x"), row_idx()))
90            .expect_err("row_idx() is not a parameter");
91        assert!(
92            error
93                .to_string()
94                .contains("lambda (x) can only read its parameters, not #row_idx"),
95            "{error}"
96        );
97    }
98}