Skip to content
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
63 changes: 63 additions & 0 deletions engine/query-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,69 @@ pub enum ValueExpr {
Arithmetic(ArithmeticExpr),
UnaryArithmetic(UnaryArithmeticExpr),
Cast(CastExpr),
StringFunction(StringFunctionExpr),
}

/// Resolved built-in string value function.
#[derive(Debug, Clone, PartialEq)]
pub struct StringFunctionExpr {
kind: StringFunctionKind,
args: Vec<StringFunctionArg>,
cardinality: Cardinality,
}

impl StringFunctionExpr {
pub fn new(
kind: StringFunctionKind,
args: Vec<StringFunctionArg>,
cardinality: Cardinality,
) -> Self {
Self {
kind,
args,
cardinality,
}
}

pub fn kind(&self) -> StringFunctionKind {
self.kind
}

pub fn args(&self) -> &[StringFunctionArg] {
&self.args
}

pub fn cardinality(&self) -> Cardinality {
self.cardinality
}
}

/// Built-in string value functions accepted by Semantic IR.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StringFunctionKind {
Concat,
Str,
}

/// One resolved string function argument.
#[derive(Debug, Clone, PartialEq)]
pub struct StringFunctionArg {
value: ValueExpr,
scalar_type: ScalarType,
}

impl StringFunctionArg {
pub fn new(value: ValueExpr, scalar_type: ScalarType) -> Self {
Self { value, scalar_type }
}

pub fn value(&self) -> &ValueExpr {
&self.value
}

pub fn scalar_type(&self) -> ScalarType {
self.scalar_type
}
}

/// Resolved explicit scalar cast value expression.
Expand Down
42 changes: 41 additions & 1 deletion engine/query-ir/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
ArithmeticExpr, ArithmeticOp, CastExpr, CompareExpr, CompareOp, Expr, InExpr, InOp, Literal,
OrderDirection, OrderExpr, ResolvedComputedField, ResolvedPath, ResolvedPathError,
ResolvedPathStep, ResolvedPathStepKind, ResolvedShape, ResolvedShapeField, ResolvedShapeItem,
SelectQuery, UnaryArithmeticExpr, UnaryArithmeticOp, ValueExpr,
SelectQuery, StringFunctionArg, StringFunctionExpr, StringFunctionKind, UnaryArithmeticExpr,
UnaryArithmeticOp, ValueExpr,
};
use alloc::boxed::Box;
use alloc::string::ToString;
Expand Down Expand Up @@ -113,6 +114,35 @@ fn cast_expr_can_store_operand_and_target_type() {
assert_eq!(cast.target_type(), ScalarType::Float64);
}

#[test]
fn string_function_expr_can_store_kind_arguments_types_and_cardinality() {
let expr = ValueExpr::StringFunction(StringFunctionExpr::new(
StringFunctionKind::Concat,
vec![
StringFunctionArg::new(
ValueExpr::Literal(Literal::String("Hello".to_string())),
ScalarType::Str,
),
StringFunctionArg::new(post_title_path_value(), ScalarType::Str),
],
Cardinality::Required,
));

let ValueExpr::StringFunction(function) = expr else {
panic!("value expression should store a string function");
};

assert_eq!(function.kind(), StringFunctionKind::Concat);
assert_eq!(function.cardinality(), Cardinality::Required);
assert_eq!(function.args().len(), 2);
assert_eq!(function.args()[0].scalar_type(), ScalarType::Str);
assert_eq!(
function.args()[0].value(),
&ValueExpr::Literal(Literal::String("Hello".to_string()))
);
assert_eq!(function.args()[1].scalar_type(), ScalarType::Str);
}

#[test]
fn resolved_shape_can_contain_link_field_with_child_shape() {
let author_shape_field = ResolvedShapeField::new(
Expand Down Expand Up @@ -330,6 +360,7 @@ fn resolved_select_query_can_store_order_by_path() {
ValueExpr::Arithmetic(_) => panic!("order by should reference a resolved path"),
ValueExpr::UnaryArithmetic(_) => panic!("order by should reference a resolved path"),
ValueExpr::Cast(_) => panic!("order by should reference a resolved path"),
ValueExpr::StringFunction(_) => panic!("order by should reference a resolved path"),
}
}

Expand Down Expand Up @@ -367,6 +398,7 @@ fn resolved_select_query_can_store_filter_compare_expr() {
panic!("filter left side should reference a resolved path")
}
ValueExpr::Cast(_) => panic!("filter left side should reference a resolved path"),
ValueExpr::StringFunction(_) => panic!("filter left side should reference a resolved path"),
}

match compare.right() {
Expand All @@ -378,6 +410,7 @@ fn resolved_select_query_can_store_filter_compare_expr() {
ValueExpr::Arithmetic(_) => panic!("filter right side should store a literal"),
ValueExpr::UnaryArithmetic(_) => panic!("filter right side should store a literal"),
ValueExpr::Cast(_) => panic!("filter right side should store a literal"),
ValueExpr::StringFunction(_) => panic!("filter right side should store a literal"),
}
}

Expand Down Expand Up @@ -412,6 +445,7 @@ fn resolved_select_query_can_store_filter_compare_int_literal() {
ValueExpr::Arithmetic(_) => panic!("filter right side should store a literal"),
ValueExpr::UnaryArithmetic(_) => panic!("filter right side should store a literal"),
ValueExpr::Cast(_) => panic!("filter right side should store a literal"),
ValueExpr::StringFunction(_) => panic!("filter right side should store a literal"),
}
}

Expand Down Expand Up @@ -446,6 +480,7 @@ fn resolved_select_query_can_store_filter_compare_bool_literal() {
ValueExpr::Arithmetic(_) => panic!("filter right side should store a literal"),
ValueExpr::UnaryArithmetic(_) => panic!("filter right side should store a literal"),
ValueExpr::Cast(_) => panic!("filter right side should store a literal"),
ValueExpr::StringFunction(_) => panic!("filter right side should store a literal"),
}
}

Expand Down Expand Up @@ -504,6 +539,7 @@ fn resolved_select_query_can_store_filter_is_null_expr() {
panic!("filter left side should reference a resolved path")
}
ValueExpr::Cast(_) => panic!("filter left side should reference a resolved path"),
ValueExpr::StringFunction(_) => panic!("filter left side should reference a resolved path"),
}
}

Expand Down Expand Up @@ -538,6 +574,7 @@ fn resolved_select_query_can_store_filter_is_not_null_expr() {
panic!("filter left side should reference a resolved path")
}
ValueExpr::Cast(_) => panic!("filter left side should reference a resolved path"),
ValueExpr::StringFunction(_) => panic!("filter left side should reference a resolved path"),
}
}

Expand Down Expand Up @@ -577,6 +614,7 @@ fn resolved_select_query_can_store_filter_in_expr() {
panic!("filter left side should reference a resolved path")
}
ValueExpr::Cast(_) => panic!("filter left side should reference a resolved path"),
ValueExpr::StringFunction(_) => panic!("filter left side should reference a resolved path"),
}

assert_eq!(in_expr.op(), InOp::In);
Expand Down Expand Up @@ -640,6 +678,7 @@ fn value_expr_can_reference_resolved_path() {
panic!("value expression should reference a resolved path")
}
ValueExpr::Cast(_) => panic!("value expression should reference a resolved path"),
ValueExpr::StringFunction(_) => panic!("value expression should reference a resolved path"),
}
}

Expand All @@ -656,6 +695,7 @@ fn value_expr_can_store_literal() {
ValueExpr::Arithmetic(_) => panic!("value expression should store a literal"),
ValueExpr::UnaryArithmetic(_) => panic!("value expression should store a literal"),
ValueExpr::Cast(_) => panic!("value expression should store a literal"),
ValueExpr::StringFunction(_) => panic!("value expression should store a literal"),
}
}

Expand Down
46 changes: 46 additions & 0 deletions engine/query-parser/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,52 @@ fn parser_preserves_function_call_argument_count() {
}
}

#[test]
fn parser_preserves_variadic_concat_arguments() {
let query = parse_select(
"select Post { title } filter concat(.title, \" - \", .subtitle) = \"Hello - Draft\"",
)
.expect("query should parse");

let filter = query.filter().expect("query should have filter");

match filter {
Expr::Compare(compare) => {
let Expr::FunctionCall(function) = compare.left() else {
panic!("left side should be a function call");
};

assert_eq!(function.name(), "concat");
assert_eq!(function.args().len(), 3);
assert_path_expr(&function.args()[0], &["title"]);
assert_literal_expr(&function.args()[1], &Literal::String(" - ".to_string()));
assert_path_expr(&function.args()[2], &["subtitle"]);
}
_ => panic!("filter should be compare expression"),
}
}

#[test]
fn parser_preserves_str_function_call_argument() {
let query = parse_select("select Post { title } filter str(.published) = \"true\"")
.expect("query should parse");

let filter = query.filter().expect("query should have filter");

match filter {
Expr::Compare(compare) => {
let Expr::FunctionCall(function) = compare.left() else {
panic!("left side should be a function call");
};

assert_eq!(function.name(), "str");
assert_eq!(function.args().len(), 1);
assert_path_expr(&function.args()[0], &["published"]);
}
_ => panic!("filter should be compare expression"),
}
}

#[test]
fn parser_parses_unary_minus_integer_literal() {
let query =
Expand Down
Loading