Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast_build): implement ifnull function #620

Merged
merged 9 commits into from
Jul 10, 2022

Conversation

bugoverdose
Copy link
Contributor

Hello, I implemented the ifnull function at ast_build.

I was going to name it if_null because it's snake_case,
but I found the already implemented ifnull function at executor/evaluate/function.rs, so I erased the _ part.

Please leave an RC if there's any problem, suggestion or conflict at main branch.

Thank you.

Closes #618

@devgony devgony added the enhancement New feature or request label Jul 9, 2022
Comment on lines 25 to 31
FunctionNode::IfNull(expr_node, then_node) => expr_node
.try_into()
.and_then(|expr| then_node
.try_into()
.map(|then| Function::IfNull { expr, then })
.map(Box::new)
.map(Expr::Function))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem like rustfmt is applied.
Would you like to run cargo fmt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ever0de Yes, I totally forgot to do so.

I ran the command and pushed the commit.

Thank you for the fast feedback.

@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2022

Codecov Report

Merging #620 (da8c2c9) into main (64efec5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #620   +/-   ##
=======================================
  Coverage   94.27%   94.27%           
=======================================
  Files         214      214           
  Lines       15193    15207   +14     
=======================================
+ Hits        14323    14337   +14     
  Misses        870      870           
Impacted Files Coverage Δ
core/src/ast_builder/mod.rs 100.00% <ø> (ø)
core/src/ast_builder/expr/function.rs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 64efec5...da8c2c9. Read the comment docs.

Copy link
Member

@ever0de ever0de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! thanks

Copy link
Contributor Author

@bugoverdose bugoverdose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fetched the upstream main branch, merged it, and resolved the conflict.

Comment on lines 9 to 14
#[derive(Clone)]
pub enum FunctionNode {
Abs(ExprNode),
IfNull(ExprNode, ExprNode),
Floor(ExprNode),
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I just ordered the Abs, IfNull, Floor alphabetically,
but I think that it makes more sense to match the order at Function enum (core/src/ast/function.rs).

All the code below also follows the same order.

Comment on lines -25 to 26
function::{abs, floor, FunctionNode},
function::{abs, floor, ifnull, FunctionNode},
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part gets ordered alphabetically when I run cargo fmt though.


#[test]
fn function_abs() {
fn abs_test() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we used the #[test] macro, it turns out that this is a test function.
It seems good to omit the test prefix.

example)
https://github.com/gluesql/gluesql/blob/main/core/src/ast_builder/select/having.rs#L73-L74
https://github.com/gluesql/gluesql/blob/main/core/src/plan/evaluable.rs#L175-L176

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I really wanted to just write it like abs, ifnull, floor.

But I get an error[E0255]: the name abs is defined multiple times message when I do so (because of line 68).

I'll rollback the changes to function_abs format for now.

Copy link
Member

@ever0de ever0de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines 83 to 89
let actual = col("updated_at").ifnull(col("created_at"));
let expected = "IFNULL(updated_at, created_at)";
test_expr(actual, expected);

let actual = text("HELLO").ifnull(text("WORLD"));
let expected = "IFNULL('HELLO', 'WORLD')";
test_expr(actual, expected);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current two unit tests are identical.
Both tests use

pub fn ifnull(self, another: ExprNode) -> ExprNode {
   ..
}

Let's replace one of those to use

pub fn ifnull<T: Into<ExprNode>, V: Into<ExprNode>>(expr: T, then: V) -> ExprNode {
   ..
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I'll fix one of them.

Copy link
Member

@panarch panarch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good, thanks a lot 👍

@panarch panarch merged commit 2f749ae into gluesql:main Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AST Builder] Implement ifnull function
5 participants