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
25 changes: 25 additions & 0 deletions engine/query-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ pub enum Expr {
In(InExpr),
Arithmetic(ArithmeticExpr),
UnaryArithmetic(UnaryArithmeticExpr),
FunctionCall(FunctionCallExpr),
}

/// Function call expression parsed from `name(args...)` syntax.
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionCallExpr {
name: String,
args: Vec<Expr>,
}

impl FunctionCallExpr {
pub fn new(name: impl Into<String>, args: Vec<Expr>) -> Self {
Self {
name: name.into(),
args,
}
}

pub fn name(&self) -> &str {
&self.name
}

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

/// Membership expression parsed from an `in` or `not in` filter clause.
Expand Down
23 changes: 21 additions & 2 deletions engine/query-ast/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
ArithmeticExpr, ArithmeticOp, CompareExpr, CompareOp, Expr, InExpr, InOp, Literal,
OrderDirection, OrderExpr, Path, PathStep, SelectQuery, Shape, ShapeItem,
ArithmeticExpr, ArithmeticOp, CompareExpr, CompareOp, Expr, FunctionCallExpr, InExpr, InOp,
Literal, OrderDirection, OrderExpr, Path, PathStep, SelectQuery, Shape, ShapeItem,
};
use alloc::string::ToString;
use alloc::vec;
Expand Down Expand Up @@ -170,6 +170,25 @@ fn compare_expr_can_store_non_equality_operator() {
assert_eq!(expr.op(), CompareOp::Ge);
}

#[test]
fn function_call_expr_can_store_name_and_arguments() {
let expr = Expr::FunctionCall(FunctionCallExpr::new(
"f64",
vec![Expr::Path(Path::new(vec![PathStep::new("view_count")]))],
));

let Expr::FunctionCall(function) = expr else {
panic!("expected expression to be a function call");
};

assert_eq!(function.name(), "f64");
assert_eq!(function.args().len(), 1);
let Expr::Path(path) = &function.args()[0] else {
panic!("expected function argument to be a path");
};
assert_eq!(path.steps()[0].field_name(), "view_count");
}

#[test]
fn order_expr_can_reference_a_path() {
let path = Expr::Path(Path::new(vec![PathStep::new("title")]));
Expand Down
25 changes: 25 additions & 0 deletions engine/query-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,31 @@ pub enum ValueExpr {
Literal(Literal),
Arithmetic(ArithmeticExpr),
UnaryArithmetic(UnaryArithmeticExpr),
Cast(CastExpr),
}

/// Resolved explicit scalar cast value expression.
#[derive(Debug, Clone, PartialEq)]
pub struct CastExpr {
operand: Box<ValueExpr>,
target_type: ScalarType,
}

impl CastExpr {
pub fn new(operand: ValueExpr, target_type: ScalarType) -> Self {
Self {
operand: Box::new(operand),
target_type,
}
}

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

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

/// Resolved arithmetic value expression.
Expand Down
27 changes: 26 additions & 1 deletion engine/query-ir/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod fixtures;

use crate::{
ArithmeticExpr, ArithmeticOp, CompareExpr, CompareOp, Expr, InExpr, InOp, Literal,
ArithmeticExpr, ArithmeticOp, CastExpr, CompareExpr, CompareOp, Expr, InExpr, InOp, Literal,
OrderDirection, OrderExpr, ResolvedComputedField, ResolvedPath, ResolvedPathError,
ResolvedPathStep, ResolvedPathStepKind, ResolvedShape, ResolvedShapeField, ResolvedShapeItem,
SelectQuery, UnaryArithmeticExpr, UnaryArithmeticOp, ValueExpr,
Expand Down Expand Up @@ -98,6 +98,21 @@ fn unary_arithmetic_expr_can_store_operand_and_operator() {
assert_eq!(unary.scalar_type(), ScalarType::Int64);
}

#[test]
fn cast_expr_can_store_operand_and_target_type() {
let expr = ValueExpr::Cast(CastExpr::new(
ValueExpr::Literal(Literal::Int64(1)),
ScalarType::Float64,
));

let ValueExpr::Cast(cast) = expr else {
panic!("value expression should store a cast expression");
};

assert_eq!(cast.operand(), &ValueExpr::Literal(Literal::Int64(1)));
assert_eq!(cast.target_type(), ScalarType::Float64);
}

#[test]
fn resolved_shape_can_contain_link_field_with_child_shape() {
let author_shape_field = ResolvedShapeField::new(
Expand Down Expand Up @@ -314,6 +329,7 @@ fn resolved_select_query_can_store_order_by_path() {
ValueExpr::Literal(_) => panic!("order by should reference a resolved 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"),
}
}

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

match compare.right() {
Expand All @@ -360,6 +377,7 @@ fn resolved_select_query_can_store_filter_compare_expr() {
ValueExpr::Path(_) => panic!("filter right side should store a 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"),
}
}

Expand Down Expand Up @@ -393,6 +411,7 @@ fn resolved_select_query_can_store_filter_compare_int_literal() {
ValueExpr::Path(_) => panic!("filter right side should store a 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"),
}
}

Expand Down Expand Up @@ -426,6 +445,7 @@ fn resolved_select_query_can_store_filter_compare_bool_literal() {
ValueExpr::Path(_) => panic!("filter right side should store a 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"),
}
}

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

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

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

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

Expand All @@ -631,6 +655,7 @@ fn value_expr_can_store_literal() {
ValueExpr::Path(_) => panic!("value expression should store a 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"),
}
}

Expand Down
35 changes: 35 additions & 0 deletions engine/query-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ impl<'a> Parser<'a> {
Ok(Expr::Path(self.parse_path(true)?))
}
Some(token) => match token.kind() {
TokenKind::Ident(_) if self.next_token_is_lparen() => {
self.parse_function_call_expr()
}
TokenKind::Ident(_) => Ok(Expr::Path(self.parse_path(false)?)),
TokenKind::Int(_)
| TokenKind::Float(_)
Expand All @@ -412,6 +415,32 @@ impl<'a> Parser<'a> {
}
}

fn parse_function_call_expr(&mut self) -> Result<Expr, ParseError> {
let name = self.expect_ident()?;
self.expect_token(TokenKind::LParen)?;

let mut args = vec![];
if self
.peek()
.is_some_and(|token| token.kind() == &TokenKind::RParen)
{
self.expect_rparen()?;
return Ok(Expr::FunctionCall(query_ast::FunctionCallExpr::new(
name, args,
)));
}

args.push(self.parse_expr()?);
while self.consume_comma_if_present() {
args.push(self.parse_expr()?);
}

self.expect_rparen()?;
Ok(Expr::FunctionCall(query_ast::FunctionCallExpr::new(
name, args,
)))
}

fn parse_order_clause(&mut self) -> Result<Vec<OrderExpr>, ParseError> {
if !self
.peek()
Expand Down Expand Up @@ -812,6 +841,12 @@ impl<'a> Parser<'a> {
}
}

fn next_token_is_lparen(&self) -> bool {
self.tokens
.get(self.cursor + 1)
.is_some_and(|token| token.kind() == &TokenKind::LParen)
}

fn consume_i64_min_literal_if_present(&mut self) -> bool {
match self.peek().map(Token::kind) {
Some(TokenKind::Int(value)) if value == "9223372036854775808" => {
Expand Down
75 changes: 75 additions & 0 deletions engine/query-parser/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ fn lexer_can_tokenize_arithmetic_operators() {
assert_eq!(tokens[14].kind(), &TokenKind::Int("100".to_string()));
}

#[test]
fn lexer_can_tokenize_function_call_expression() {
let tokens = lex("filter f64(.view_count) / 2.0 >= 10.5").expect("query should lex");

assert_eq!(tokens[0].kind(), &TokenKind::Keyword(Keyword::Filter));
assert_eq!(tokens[1].kind(), &TokenKind::Ident("f64".to_string()));
assert_eq!(tokens[2].kind(), &TokenKind::LParen);
assert_eq!(tokens[3].kind(), &TokenKind::Dot);
assert_eq!(
tokens[4].kind(),
&TokenKind::Ident("view_count".to_string())
);
assert_eq!(tokens[5].kind(), &TokenKind::RParen);
assert_eq!(tokens[6].kind(), &TokenKind::Slash);
assert_eq!(tokens[7].kind(), &TokenKind::Float("2.0".to_string()));
assert_eq!(tokens[8].kind(), &TokenKind::Ge);
assert_eq!(tokens[9].kind(), &TokenKind::Float("10.5".to_string()));
}

#[test]
fn lexer_can_tokenize_computed_shape_assignment() {
let tokens = lex("select Post { score := .likes + 1 }").expect("query should lex");
Expand Down Expand Up @@ -642,6 +661,62 @@ fn parser_can_parse_filter_arithmetic_addition() {
}
}

#[test]
fn parser_can_parse_function_call_as_arithmetic_operand() {
let query = parse_select("select Post { title } filter f64(.view_count) / 2.0 >= 10.5")
.expect("query should parse");

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

match filter {
Expr::Compare(compare) => {
let Expr::Arithmetic(arithmetic) = compare.left() else {
panic!("left side should be arithmetic expression");
};
assert_eq!(arithmetic.op(), ArithmeticOp::Div);

let Expr::FunctionCall(function) = arithmetic.left() else {
panic!("arithmetic left operand should be a function call");
};
assert_eq!(function.name(), "f64");
assert_eq!(function.args().len(), 1);
assert_path_expr(&function.args()[0], &["view_count"]);

assert_literal_expr(arithmetic.right(), &Literal::Float64(2.0));
assert_eq!(compare.op(), CompareOp::Ge);
assert_literal_expr(compare.right(), &Literal::Float64(10.5));
}
_ => panic!("filter should be compare expression"),
}
}

#[test]
fn parser_preserves_function_call_argument_count() {
let query = parse_select("select Post { title } filter concat() = concat(.title, \"!\")")
.expect("query should parse");

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

match filter {
Expr::Compare(compare) => {
let Expr::FunctionCall(left) = compare.left() else {
panic!("left side should be a function call");
};
assert_eq!(left.name(), "concat");
assert_eq!(left.args().len(), 0);

let Expr::FunctionCall(right) = compare.right() else {
panic!("right side should be a function call");
};
assert_eq!(right.name(), "concat");
assert_eq!(right.args().len(), 2);
assert_path_expr(&right.args()[0], &["title"]);
assert_literal_expr(&right.args()[1], &Literal::String("!".to_string()));
}
_ => panic!("filter should be compare expression"),
}
}

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