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

Create select with comma macro to support negative number #663

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

24seconds
Copy link
Collaborator

@24seconds 24seconds commented Jul 23, 2022

Description

For the better readability, gluesql uses select! macro.
But there is a nit problem. It can not handle consecutive negative numbers. That is why there are some workaround code - for example, parse_i64

Below code is the one example of workaound.

test!(
    Ok(select!(
        field_one          | field_two
        I64                |    I64;
        1                  parse_i64("-1");
        parse_i64("-2")    2;
        3                  3;
        parse_i64("-4")    parse_i64("-4")
    )),
    "SELECT field_one, field_two FROM Item"
);

This is because macro rule can not handle consecutive negative numbers with whitespace separator. Here is the playground link

Suggestion

To solve this problem, It is good to use clear separator such as comma or bar instead of whitespace.
So I made a select_with_comma macro.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 2722269177

  • 27 of 27 (100.0%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 97.81%

Totals Coverage Status
Change from base Build 2698746514: 0.03%
Covered Lines: 28769
Relevant Lines: 29413

💛 - Coveralls

@24seconds 24seconds marked this pull request as ready for review July 23, 2022 04:48
@24seconds
Copy link
Collaborator Author

Oops. Thank you @ever0de . Let me change the separator as |

@24seconds
Copy link
Collaborator Author

It seems | is not good to be used as separator. I got this error.
image

@24seconds
Copy link
Collaborator Author

Let me try to find the workaround as much as possible I can. Until that, let me change this pr as draft

@24seconds 24seconds marked this pull request as draft July 23, 2022 06:15
@ever0de
Copy link
Member

ever0de commented Aug 6, 2022

We share some experimental results...

  1. If you change it to literal, the | delimiter works.
    1.1 Changing to literal disables ToOwned or closure.
  2. expr only works as a delimiter for =>, ,, and ;.

cc. @panarch

@24seconds
Copy link
Collaborator Author

24seconds commented Aug 6, 2022

conclusion

match expression: Change expr -> literal
separator: comma -> bar

Type converting: Implement trait - convert row value type(literal) to column value type
Reason of needing type converting trait implementation -> Date time, static lifetime str could be problematic. Literal is primitive type. We will pass our code as literal in macro. Therefore, in the macro we need to convert literals to gluesql types so that we can run (or execute) the code with gluesql types. Therefore we should implement converting logic - from literal to gluesql type

As-is
test!(
    Ok(select!(
        field_one          | field_two
        I64                |    I64;
        1                  parse_i64("-1");
        parse_i64("-2")    2;
        3                  3;
        parse_i64("-4")    parse_i64("-4")
    )),
    "SELECT field_one, field_two FROM Item"
);
To-be
test!(
    Ok(select!(
        field_one          | field_two
        I64                |    I64;
        1                  |   -1;
        2                  |    2;
        3                  |    3;
        -4                 |   -4
    )),
    "SELECT field_one, field_two FROM Item"
);

Note

.to_owned, Date could be removed in the macro usage side code base

@ever0de ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants