Skip to main content

reifydb_engine/expression/
access.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::sync::Arc;
5
6use reifydb_core::{
7	error::diagnostic::query::column_not_found, interface::identifier::ColumnShape, value::column::ColumnWithName,
8};
9use reifydb_rql::expression::AccessShapeExpression;
10use reifydb_type::{error, fragment::Fragment};
11
12use crate::{Result, expression::context::EvalContext};
13
14pub(crate) fn access_lookup(ctx: &EvalContext, expr: &AccessShapeExpression) -> Result<ColumnWithName> {
15	let source = match &expr.column.shape {
16		ColumnShape::Qualified {
17			name,
18			..
19		} => name,
20		ColumnShape::Alias(alias) => alias,
21	};
22	let column = expr.column.name.text().to_string();
23
24	let qualified_name = format!("{}.{}", source.text(), &column);
25
26	let matching_col = ctx.columns.iter().find(|col| {
27		if col.name().text() == qualified_name {
28			return true;
29		}
30
31		if matches!(&expr.column.shape, ColumnShape::Qualified { .. }) && col.name().text() == column {
32			return !col.name().text().contains('.');
33		}
34
35		false
36	});
37
38	if let Some(col) = matching_col {
39		Ok(ColumnWithName::new(col.name().clone(), col.data().clone()))
40	} else {
41		Err(error!(column_not_found(Fragment::Statement {
42			column: expr.column.name.column(),
43			line: expr.column.name.line(),
44			text: Arc::from(format!("{}.{}", source.text(), &column)),
45		})))
46	}
47}