Skip to content

Commit

Permalink
I think now working + with docs
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Jun 22, 2024
1 parent d0b0f87 commit 7e28ccc
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 103 deletions.
22 changes: 18 additions & 4 deletions prqlc/prqlc-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,24 @@ where
end: right.1.end,
source_id: left.1.source_id,
};
let kind = ExprKind::Binary(BinaryExpr {
left: Box::new(left.0),
op,
right: Box::new(right.0),
let kind = ExprKind::Binary(match op {
// For the power operator, we need to reverse the order, since
// `math.pow a b` is equivalent to `b ** a`. (but for example
// `sub a b` is equivalent to `a - b`).
// (I think this is the most globally consistent approach, since
// final arguments should be the "data", which in the case of
// `pow` would be the base; but it's not perfect, we could
// change it...)
BinOp::Pow => BinaryExpr {
left: Box::new(right.0),
op,
right: Box::new(left.0),
},
_ => BinaryExpr {
left: Box::new(left.0),
op,
right: Box::new(right.0),
},
});
(into_expr(kind, span), span)
})
Expand Down
50 changes: 50 additions & 0 deletions prqlc/prqlc-parser/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,56 @@ fn test_func_call() {
"###);
}

#[test]
fn test_right_assoc() {
assert_yaml_snapshot!(parse_expr(r#"2 ** 3 ** 4"#).unwrap(), @r###"
---
Binary:
left:
Literal:
Integer: 2
op: Pow
right:
Binary:
left:
Literal:
Integer: 3
op: Pow
right:
Literal:
Integer: 4
"###);
assert_yaml_snapshot!(parse_expr(r#"1 + 2 ** (3 + 4) ** 4"#).unwrap(), @r###"
---
Binary:
left:
Literal:
Integer: 1
op: Add
right:
Binary:
left:
Literal:
Integer: 2
op: Pow
right:
Binary:
left:
Binary:
left:
Literal:
Integer: 3
op: Add
right:
Literal:
Integer: 4
op: Pow
right:
Literal:
Integer: 4
"###);
}

#[test]
fn test_op_precedence() {
assert_yaml_snapshot!(parse_expr(r#"1 + 2 - 3 - 4"#).unwrap(), @r###"
Expand Down
5 changes: 2 additions & 3 deletions prqlc/prqlc/src/codegen/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,8 @@ mod test {
assert_is_formatted(r#"let a = 5 / 2 / 2"#);
assert_is_formatted(r#"let a = 5 / (2 / 2)"#);

// TODO: parsing for pow operator
// assert_is_formatted(r#"let a = (5 ** 2) ** 2"#);
// assert_is_formatted(r#"let a = 5 ** 2 ** 2"#);
assert_is_formatted(r#"let a = (5 ** 2) ** 2"#);
assert_is_formatted(r#"let a = 5 ** 2 ** 2"#);
}

#[test]
Expand Down
36 changes: 18 additions & 18 deletions prqlc/prqlc/src/semantic/ast_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,24 @@ fn expand_binary(ast::BinaryExpr { op, left, right }: ast::BinaryExpr) -> Result
let left = expand_expr(*left)?;
let right = expand_expr(*right)?;

let func_name = match op {
ast::BinOp::Mul => ["std", "mul"],
ast::BinOp::DivInt => ["std", "div_i"],
ast::BinOp::DivFloat => ["std", "div_f"],
ast::BinOp::Mod => ["std", "mod"],
ast::BinOp::Pow => ["std", "pow"],
ast::BinOp::Add => ["std", "add"],
ast::BinOp::Sub => ["std", "sub"],
ast::BinOp::Eq => ["std", "eq"],
ast::BinOp::Ne => ["std", "ne"],
ast::BinOp::Gt => ["std", "gt"],
ast::BinOp::Lt => ["std", "lt"],
ast::BinOp::Gte => ["std", "gte"],
ast::BinOp::Lte => ["std", "lte"],
ast::BinOp::RegexSearch => ["std", "regex_search"],
ast::BinOp::And => ["std", "and"],
ast::BinOp::Or => ["std", "or"],
ast::BinOp::Coalesce => ["std", "coalesce"],
let func_name: Vec<&str> = match op {
ast::BinOp::Mul => vec!["std", "mul"],
ast::BinOp::DivInt => vec!["std", "div_i"],
ast::BinOp::DivFloat => vec!["std", "div_f"],
ast::BinOp::Mod => vec!["std", "mod"],
ast::BinOp::Pow => vec!["std", "math", "pow"],
ast::BinOp::Add => vec!["std", "add"],
ast::BinOp::Sub => vec!["std", "sub"],
ast::BinOp::Eq => vec!["std", "eq"],
ast::BinOp::Ne => vec!["std", "ne"],
ast::BinOp::Gt => vec!["std", "gt"],
ast::BinOp::Lt => vec!["std", "lt"],
ast::BinOp::Gte => vec!["std", "gte"],
ast::BinOp::Lte => vec!["std", "lte"],
ast::BinOp::RegexSearch => vec!["std", "regex_search"],
ast::BinOp::And => vec!["std", "and"],
ast::BinOp::Or => vec!["std", "or"],
ast::BinOp::Coalesce => vec!["std", "coalesce"],
};
Ok(new_binop(left, &func_name, right).kind)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ inputs:
table:
- default_db
- customers

Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ inputs:
table:
- default_db
- salaries

Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ inputs:
table:
- default_db
- orders

2 changes: 1 addition & 1 deletion prqlc/prqlc/src/sql/std.sql.prql
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module math {
let asin = column -> s"ASIN({column:0})"
let tan = column -> s"TAN({column:0})"
let atan = column -> s"ATAN({column:0})"
let pow = exponent column -> s"POW({column:0}, {exponent:0})"
let pow = exponent column -> s"POW({exponent:0}, {column:0})"
let round = n_digits column -> s"ROUND({column:0}, {n_digits:0})"
}

Expand Down
3 changes: 2 additions & 1 deletion prqlc/prqlc/tests/integration/queries/math_module.prql
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ select {
total_sin = math.sin total | math.asin | math.round 2,
total_tan = math.tan total | math.atan | math.round 2,
total_deg = total | math.degrees | math.radians | math.round 2,
total_square = total | math.pow 2| math.round 2,
total_square = total | math.pow 2 | math.round 2,
total_square_op = (total ** 2) | math.round 2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@ FROM
table_0
ORDER BY
id

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: prqlc/prqlc/tests/integration/queries.rs
expression: "# mssql:test\n# sqlite:skip (see https://github.com/rusqlite/rusqlite/issues/1211)\nfrom invoices\ntake 5\nselect {\n total_original = total | math.round 2,\n total_x = math.pi - total | math.round 2 | math.abs,\n total_floor = math.floor total,\n total_ceil = math.ceil total,\n total_log10 = math.log10 total | math.round 3,\n total_log2 = math.log 2 total | math.round 3,\n total_sqrt = math.sqrt total | math.round 3,\n total_ln = math.ln total | math.exp | math.round 2,\n total_cos = math.cos total | math.acos | math.round 2,\n total_sin = math.sin total | math.asin | math.round 2,\n total_tan = math.tan total | math.atan | math.round 2,\n total_deg = total | math.degrees | math.radians | math.round 2,\n total_square = total | math.pow 2| math.round 2,\n}\n"
expression: "# mssql:test\n# sqlite:skip (see https://github.com/rusqlite/rusqlite/issues/1211)\nfrom invoices\ntake 5\nselect {\n total_original = total | math.round 2,\n total_x = math.pi - total | math.round 2 | math.abs,\n total_floor = math.floor total,\n total_ceil = math.ceil total,\n total_log10 = math.log10 total | math.round 3,\n total_log2 = math.log 2 total | math.round 3,\n total_sqrt = math.sqrt total | math.round 3,\n total_ln = math.ln total | math.exp | math.round 2,\n total_cos = math.cos total | math.acos | math.round 2,\n total_sin = math.sin total | math.asin | math.round 2,\n total_tan = math.tan total | math.atan | math.round 2,\n total_deg = total | math.degrees | math.radians | math.round 2,\n total_square = (total ** 2) | math.round 2,\n}\n"
input_file: prqlc/prqlc/tests/integration/queries/math_module.prql
---
SELECT
Expand All @@ -21,4 +21,3 @@ FROM
invoices
LIMIT
5

Loading

0 comments on commit 7e28ccc

Please sign in to comment.