vortex_layout/layouts/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::expr::{ChildName, ExprId, Expression, ExpressionView, VTable, VTableExt};
8use vortex_dtype::{DType, Nullability, PType};
9use vortex_error::{VortexResult, vortex_bail};
10
11pub struct RowIdx;
12
13impl VTable for RowIdx {
14    type Instance = ();
15
16    fn id(&self) -> ExprId {
17        ExprId::from("vortex.row_idx")
18    }
19
20    fn validate(&self, expr: &ExpressionView<Self>) -> VortexResult<()> {
21        if !expr.children().is_empty() {
22            vortex_bail!(
23                "RowIdx expression does not have children, got {}",
24                expr.children().len()
25            );
26        }
27        Ok(())
28    }
29
30    fn child_name(&self, _instance: &Self::Instance, _child_idx: usize) -> ChildName {
31        unreachable!()
32    }
33
34    fn fmt_sql(&self, _expr: &ExpressionView<Self>, f: &mut Formatter<'_>) -> std::fmt::Result {
35        write!(f, "#row_idx")
36    }
37
38    fn return_dtype(&self, _expr: &ExpressionView<Self>, _scope: &DType) -> VortexResult<DType> {
39        Ok(DType::Primitive(PType::U64, Nullability::NonNullable))
40    }
41
42    fn evaluate(&self, _expr: &ExpressionView<Self>, _scope: &ArrayRef) -> VortexResult<ArrayRef> {
43        vortex_bail!(
44            "RowIdxExpr should not be evaluated directly, use it in the context of a Vortex scan and it will be substituted for a row index array"
45        );
46    }
47}
48
49pub fn row_idx() -> Expression {
50    RowIdx.new_expr((), [])
51}