Skip to main content

reifydb_evaluate/expression/
access.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::sync::Arc;
5
6use arrow_array::RecordBatch;
7use reifydb_core::{
8	error::diagnostic::query::column_not_found, expression::AccessObjectExpression,
9	interface::identifier::ColumnObject,
10};
11use reifydb_value::{error, fragment::Fragment, value::system_columns::is_system_field};
12
13use crate::Result;
14
15pub(crate) fn access_position(batch: &RecordBatch, expr: &AccessObjectExpression) -> Result<usize> {
16	let source = match &expr.column.object {
17		ColumnObject::Qualified {
18			name,
19			..
20		} => name,
21		ColumnObject::Alias(alias) => alias,
22	};
23	let column = expr.column.name.text().to_string();
24
25	let qualified_name = format!("{}.{}", source.text(), &column);
26
27	let matching_col =
28		batch.schema_ref().fields().iter().enumerate().filter(|(_, field)| !is_system_field(field)).find(
29			|(_, field)| {
30				if field.name() == &qualified_name {
31					return true;
32				}
33
34				if matches!(&expr.column.object, ColumnObject::Qualified { .. })
35					&& field.name() == &column
36				{
37					return !field.name().contains('.');
38				}
39
40				false
41			},
42		);
43
44	if let Some((index, _)) = matching_col {
45		Ok(index)
46	} else {
47		Err(error!(column_not_found(Fragment::Statement {
48			column: expr.column.name.column(),
49			line: expr.column.name.line(),
50			text: Arc::from(format!("{}.{}", source.text(), &column)),
51		})))
52	}
53}