Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fstrings #566

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nemo-physical/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains data structures and implementations
//! for realizing the evaluation of functions on columnar data.

pub mod evaluation;
pub mod tree;

pub(crate) mod definitions;
pub(crate) mod evaluation;
8 changes: 4 additions & 4 deletions nemo-physical/src/function/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) enum StackOperation {

/// Representation of a [FunctionTree] as a stack program
#[derive(Debug, Clone)]
pub(crate) struct StackProgram {
pub struct StackProgram {
/// Maximmum size of the stack
size: usize,
/// List of instructions
Expand Down Expand Up @@ -115,7 +115,7 @@ impl StackProgram {
}

/// Construct a [StackProgram] from [FunctionTree].
pub(crate) fn from_function_tree<ReferenceType: Hash + Eq + Debug + Clone>(
pub fn from_function_tree<ReferenceType: Hash + Eq + Debug + Clone>(
tree: &FunctionTree<ReferenceType>,
reference_map: &HashMap<ReferenceType, usize>,
this: Option<ReferenceType>,
Expand Down Expand Up @@ -263,7 +263,7 @@ impl StackProgram {
///
/// # Panics
/// Panics if the [StackProgram] is not valid.
pub(crate) fn evaluate_data(&self, referenced_values: &[AnyDataValue]) -> Option<AnyDataValue> {
pub fn evaluate_data(&self, referenced_values: &[AnyDataValue]) -> Option<AnyDataValue> {
self.evaluate(referenced_values, None)
}

Expand All @@ -274,7 +274,7 @@ impl StackProgram {
///
/// # Panics
/// Panics if the [StackProgram] is not valid.
pub(crate) fn evaluate_bool(
pub fn evaluate_bool(
&self,
referenced_values: &[AnyDataValue],
this: Option<AnyDataValue>,
Expand Down
4 changes: 2 additions & 2 deletions nemo-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nemo::{
rule_model::{
components::{fact::Fact, tag::Tag, term::primitive::Primitive, ProgramComponent},
error::ValidationErrorBuilder,
term_map::PrimitiveTermMap,
substitution::Substitution,
},
};

Expand Down Expand Up @@ -269,7 +269,7 @@ impl NemoTrace {
}
}

fn assignement_to_dict(assignment: &PrimitiveTermMap, py: Python) -> PyResult<PyObject> {
fn assignement_to_dict(assignment: &Substitution, py: Python) -> PyResult<PyObject> {
let dict = PyDict::new_bound(py);
for (variable, term) in assignment {
if let Primitive::Ground(ground) = term {
Expand Down
8 changes: 4 additions & 4 deletions nemo/src/execution/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
term::primitive::{ground::GroundTerm, variable::Variable, Primitive},
},
program::Program,
term_map::PrimitiveTermMap,
substitution::Substitution,
},
table_manager::{MemoryUsage, SubtableExecutionPlan, TableManager},
};
Expand Down Expand Up @@ -452,9 +452,9 @@ impl<Strategy: RuleSelectionStrategy> ExecutionEngine<Strategy> {

let rule_application = TraceRuleApplication::new(
rule_index,
PrimitiveTermMap::new(variable_assignment.into_iter().map(
|(variable, value)| (Primitive::from(variable), Primitive::from(value)),
)),
Substitution::new(variable_assignment.into_iter().map(|(variable, value)| {
(Primitive::from(variable), Primitive::from(value))
})),
head_index,
);

Expand Down
2 changes: 1 addition & 1 deletion nemo/src/execution/planning/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub(crate) mod filter;
pub(crate) mod functions;
pub(crate) mod join;
pub(crate) mod negation;
pub(super) mod operation;
pub(crate) mod operation;
pub(crate) mod union;
4 changes: 2 additions & 2 deletions nemo/src/execution/planning/operations/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
};

/// Helper function to translate a [OperationTerm] into a [FunctionTree].
pub(super) fn operation_term_to_function_tree(
pub(crate) fn operation_term_to_function_tree(
translation: &VariableTranslation,
operation_term: &OperationTerm,
) -> FunctionTree<OperationColumnMarker> {
Expand Down Expand Up @@ -56,7 +56,7 @@ macro_rules! unary {
}

/// Helper function to translate a [Operation] into a [FunctionTree].
pub(super) fn operation_to_function_tree(
pub(crate) fn operation_to_function_tree(
translation: &VariableTranslation,
operation: &Operation,
) -> FunctionTree<OperationColumnMarker> {
Expand Down
29 changes: 11 additions & 18 deletions nemo/src/execution/tracing/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
rule_model::{
components::{fact::Fact, rule::Rule},
program::Program,
term_map::PrimitiveTermMap,
substitution::Substitution,
},
};

Expand Down Expand Up @@ -49,14 +49,14 @@ pub(crate) struct TraceRuleApplication {
/// Index of the rule that was applied
rule_index: RuleIndex,
/// Variable assignment used during the rule application
assignment: PrimitiveTermMap,
assignment: Substitution,
/// Index of the head atom which produced the fact under consideration
_position: usize,
}

impl TraceRuleApplication {
/// Create new [TraceRuleApplication].
pub fn new(rule_index: RuleIndex, assignment: PrimitiveTermMap, _position: usize) -> Self {
pub fn new(rule_index: RuleIndex, assignment: Substitution, _position: usize) -> Self {
Self {
rule_index,
assignment,
Expand Down Expand Up @@ -201,30 +201,23 @@ pub struct TraceTreeRuleApplication {
/// Rule that was applied
pub rule: Rule,
/// Variable assignment used during the rule application
pub assignment: PrimitiveTermMap,
pub assignment: Substitution,
/// Index of the head atom which produced the fact under consideration
_position: usize,
}

impl TraceTreeRuleApplication {
/// Instantiate the given rule with its assignment producing a [Rule] with only ground terms.
fn to_instantiated_rule(&self) -> Rule {
let mut rule = self.rule.clone();
self.assignment.apply(&mut rule);

rule
}

/// Get the [Fact] that was produced by this rule application.
fn to_derived_atom(&self) -> Fact {
let rule = self.to_instantiated_rule();
let derived_atom = rule.head()[self._position].clone();
Fact::from(derived_atom)
let mut fact = self.rule.head()[self._position].clone();
self.assignment.apply(&mut fact);

Fact::from(fact)
}

/// Get a string representation of the Instantiated rule.
fn to_instantiated_string(&self) -> String {
self.to_instantiated_rule().to_string()
self.rule.display_instantiated(&self.assignment)
}
}

Expand Down Expand Up @@ -562,7 +555,7 @@ mod test {
ProgramComponent,
},
program::ProgramBuilder,
term_map::PrimitiveTermMap,
substitution::Substitution,
},
};

Expand All @@ -579,7 +572,7 @@ mod test {
)
});

PrimitiveTermMap::new(terms)
Substitution::new(terms)
}};
}

Expand Down
11 changes: 9 additions & 2 deletions nemo/src/parser/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use basic::{
string::StringLiteral, variable::Variable,
};
use complex::{
aggregation::Aggregation, arithmetic::Arithmetic, atom::Atom, map::Map, negation::Negation,
operation::Operation, parenthesized::ParenthesizedExpression, tuple::Tuple,
aggregation::Aggregation, arithmetic::Arithmetic, atom::Atom, fstring::FormatString, map::Map,
negation::Negation, operation::Operation, parenthesized::ParenthesizedExpression, tuple::Tuple,
};
use nom::{branch::alt, combinator::map};

Expand Down Expand Up @@ -37,6 +37,8 @@ pub enum Expression<'a> {
Boolean(Boolean<'a>),
/// Constant
Constant(Constant<'a>),
/// Format String
FormatString(FormatString<'a>),
/// Map
Map(Map<'a>),
/// Negation
Expand Down Expand Up @@ -67,6 +69,7 @@ impl<'a> Expression<'a> {
Expression::Blank(expression) => expression.context(),
Expression::Boolean(expression) => expression.context(),
Expression::Constant(expression) => expression.context(),
Expression::FormatString(expression) => expression.context(),
Expression::Map(expression) => expression.context(),
Expression::Number(expression) => expression.context(),
Expression::Negation(expression) => expression.context(),
Expand Down Expand Up @@ -101,6 +104,7 @@ impl<'a> Expression<'a> {
map(Map::parse, Self::Map),
map(Negation::parse, Self::Negation),
map(Tuple::parse, Self::Tuple),
map(FormatString::parse, Self::FormatString),
))(input)
}
}
Expand All @@ -116,6 +120,7 @@ impl<'a> ProgramAST<'a> for Expression<'a> {
Expression::Blank(expression) => expression,
Expression::Boolean(expression) => expression,
Expression::Constant(expression) => expression,
Expression::FormatString(expression) => expression,
Expression::Map(expression) => expression,
Expression::Number(expression) => expression,
Expression::Negation(expression) => expression,
Expand All @@ -136,6 +141,7 @@ impl<'a> ProgramAST<'a> for Expression<'a> {
Expression::Blank(expression) => expression.span(),
Expression::Boolean(expression) => expression.span(),
Expression::Constant(expression) => expression.span(),
Expression::FormatString(expression) => expression.span(),
Expression::Map(expression) => expression.span(),
Expression::Number(expression) => expression.span(),
Expression::Negation(expression) => expression.span(),
Expand Down Expand Up @@ -201,6 +207,7 @@ mod test {
("\"\"", ParserContext::String),
("(1,)", ParserContext::Tuple),
("?variable", ParserContext::Variable),
("f\"{?x + ?y}\"", ParserContext::FormatString),
];

for (input, expect) in test {
Expand Down
7 changes: 6 additions & 1 deletion nemo/src/parser/ast/expression/basic/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(missing_docs)]

use nom::{
branch::alt,
combinator::opt,
sequence::{delimited, pair},
};
Expand Down Expand Up @@ -39,7 +40,11 @@ impl<'a> StringLiteral<'a> {

/// Parse the main part of the string.
pub fn parse_string(input: ParserInput<'a>) -> ParserResult<'a, Token<'a>> {
delimited(Token::quote, Token::string, Token::quote)(input)
delimited(
Token::quote,
alt((Token::string, Token::empty)),
Token::quote,
)(input)
}

/// Parse the language tag of the string.
Expand Down
1 change: 1 addition & 0 deletions nemo/src/parser/ast/expression/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod aggregation;
pub mod arithmetic;
pub mod atom;
pub mod fstring;
pub mod infix;
pub mod map;
pub mod negation;
Expand Down
Loading
Loading