From a181837a97a67211282e7e1f5daafe7fe1a99fb4 Mon Sep 17 00:00:00 2001 From: Banyc <36535895+Banyc@users.noreply.github.com> Date: Thu, 14 Dec 2023 00:11:23 +0800 Subject: [PATCH] feat: modulo --- src/df.rs | 1 + src/sql/expr.rs | 4 +++- src/sql/lexer.rs | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/df.rs b/src/df.rs index 2a5dc08..e5c24fd 100644 --- a/src/df.rs +++ b/src/df.rs @@ -108,6 +108,7 @@ fn convert_expr(expr: &sql::expr::Expr) -> polars::lazy::dsl::Expr { sql::expr::BinaryOperator::Sub => left - right, sql::expr::BinaryOperator::Mul => left * right, sql::expr::BinaryOperator::Div => left / right, + sql::expr::BinaryOperator::Modulo => left % right, sql::expr::BinaryOperator::Eq => left.eq(right), sql::expr::BinaryOperator::NotEq => left.neq(right), sql::expr::BinaryOperator::LtEq => left.lt_eq(right), diff --git a/src/sql/expr.rs b/src/sql/expr.rs index 7cdced3..7db2bb1 100644 --- a/src/sql/expr.rs +++ b/src/sql/expr.rs @@ -102,6 +102,7 @@ pub enum BinaryOperator { Sub, Mul, Div, + Modulo, Eq, NotEq, LtEq, @@ -126,7 +127,8 @@ fn term_expr<'a>( ) -> impl Parser<'a, &'a [Token], Expr, extra::Err>> + Clone { let mul = just(Token::Symbol(Symbol::Mul)).to(BinaryOperator::Mul); let div = just(Token::Symbol(Symbol::Div)).to(BinaryOperator::Div); - let operator = choice((mul, div)); + let modulo = just(Token::Symbol(Symbol::Percent)).to(BinaryOperator::Modulo); + let operator = choice((mul, div, modulo)); binary_expr(operator, expr) } diff --git a/src/sql/lexer.rs b/src/sql/lexer.rs index 77ad858..ff639a4 100644 --- a/src/sql/lexer.rs +++ b/src/sql/lexer.rs @@ -97,6 +97,7 @@ pub enum Symbol { Ampersand, Pipe, Comma, + Percent, } fn symbol<'a>() -> impl Parser<'a, &'a str, Symbol, extra::Err>> + Clone { @@ -111,6 +112,7 @@ fn symbol<'a>() -> impl Parser<'a, &'a str, Symbol, extra::Err>> let pipe = just('|').to(Symbol::Pipe); let comma = just(',').to(Symbol::Comma); let bang = just('!').to(Symbol::Bang); + let percent = just('%').to(Symbol::Percent); choice(( left_angle, right_angle, @@ -123,6 +125,7 @@ fn symbol<'a>() -> impl Parser<'a, &'a str, Symbol, extra::Err>> pipe, comma, bang, + percent, )) }