Skip to content

Commit

Permalink
feat: log
Browse files Browse the repository at this point in the history
  • Loading branch information
Banyc committed Dec 13, 2023
1 parent a49ede0 commit 70e97af
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ polars = { version = "0.35.4", features = [
"json",
"lazy",
"lazy_regex",
"log",
"strings",
] }
rustyline = { version = "13.0.0", features = ["derive"] }
Expand Down
4 changes: 4 additions & 0 deletions src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ fn convert_expr(expr: &sql::expr::Expr) -> polars::lazy::dsl::Expr {
let expr = convert_expr(&cast.expr);
expr.cast(ty)
}
sql::expr::Expr::Log(log) => {
let expr = convert_expr(&log.expr);
expr.log(log.base)
}
sql::expr::Expr::Str(str) => match str.as_ref() {
sql::expr::StrExpr::Contains(contains) => {
let str = convert_expr(&contains.str);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl SqlHelper {
("all", color_functor()),
("any", color_functor()),
("pow", color_functor()),
("log", color_functor()),
("if", color_control_flow()),
("then", color_control_flow()),
("else", color_control_flow()),
Expand Down
22 changes: 22 additions & 0 deletions src/sql/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Expr {
Alias(Box<AliasExpr>),
Conditional(Box<ConditionalExpr>),
Cast(Box<CastExpr>),
Log(Box<LogExpr>),
Str(Box<StrExpr>),
Standalone(Box<StandaloneExpr>),
SortBy(Box<SortByExpr>),
Expand All @@ -40,6 +41,7 @@ pub fn expr<'a>() -> impl Parser<'a, &'a [Token], Expr, extra::Err<Rich<'a, Toke
.map(Box::new)
.map(Expr::Conditional);
let cast = cast_expr(expr.clone()).map(Box::new).map(Expr::Cast);
let log = log_expr(expr.clone()).map(Box::new).map(Expr::Log);
let str = str_expr(expr.clone()).map(Box::new).map(Expr::Str);
let atom = choice((
atom,
Expand All @@ -50,6 +52,7 @@ pub fn expr<'a>() -> impl Parser<'a, &'a [Token], Expr, extra::Err<Rich<'a, Toke
sort,
conditional,
cast,
log,
str,
))
.boxed();
Expand Down Expand Up @@ -368,6 +371,25 @@ fn cast_expr<'a>(
.map(|(ty, expr)| CastExpr { expr, ty })
}

#[derive(Debug, Clone, PartialEq)]
pub struct LogExpr {
pub expr: Expr,
pub base: f64,
}

fn log_expr<'a>(
expr: impl Parser<'a, &'a [Token], Expr, extra::Err<Rich<'a, Token>>> + Clone,
) -> impl Parser<'a, &'a [Token], LogExpr, extra::Err<Rich<'a, Token>>> + Clone {
let float = select_ref! { Token::Literal(Literal::Float(float)) => float };
let int = select_ref! { Token::Literal(Literal::Int(int)) => int };
let base = choice((float, int)).map(|s| s.parse::<f64>().unwrap());

just(Token::ExprKeyword(ExprKeyword::Log))
.ignore_then(base)
.then(expr.clone())
.map(|(base, expr)| LogExpr { expr, base })
}

#[derive(Debug, Clone, PartialEq)]
pub enum StrExpr {
Contains(Contains),
Expand Down
4 changes: 3 additions & 1 deletion src/sql/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub enum ExprKeyword {
All,
Any,
Pow,
Log,
}

fn expr_keyword<'a>() -> impl Parser<'a, &'a str, ExprKeyword, extra::Err<Rich<'a, char>>> + Clone {
Expand Down Expand Up @@ -185,11 +186,12 @@ fn expr_keyword<'a>() -> impl Parser<'a, &'a str, ExprKeyword, extra::Err<Rich<'
let all = text::keyword("all").to(ExprKeyword::All);
let any = text::keyword("any").to(ExprKeyword::Any);
let pow = text::keyword("pow").to(ExprKeyword::Pow);
let log = text::keyword("log").to(ExprKeyword::Log);
let a = choice((
sum, sqrt, count, sort, asc, desc, reverse, first, last, mean, median, max, min, var, std,
abs, unique, by, is, alias, col, exclude, cast, nan, all, any,
));
let b = choice((pow,));
let b = choice((pow, log));
choice((a, b)).boxed()
}

Expand Down

0 comments on commit 70e97af

Please sign in to comment.