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

Multiple Filters #1451

Closed
kanekoshoyu opened this issue Jan 18, 2024 · 4 comments
Closed

Multiple Filters #1451

kanekoshoyu opened this issue Jan 18, 2024 · 4 comments
Assignees
Labels
improvement Improvements for existing features

Comments

@kanekoshoyu
Copy link

kanekoshoyu commented Jan 18, 2024

Hi,

I was wondering if there is any plan or a current workaround for using multiple filters (esp on select and update)

Originally I had a misunderstanding on AST builder syntax and I thought adding .filter() multiple times will keep adding nested filter, but turns out every new filter was just overriding another filter.

Then I tried adding AND within one filter expression, but I do not see the effect of the nested filter. From what I tested, it seems like only the first condition in the filter gets recognized, and the rest is discarded.

I saw online that we might be able to use subqueries (queries on top of queries) to get it solved. I am wondering if there is anything similar to such a concept in gluesql that I can use as a workaround.

Example of AND/OR within WHERE
https://www.techonthenet.com/sql/where.php

@devgony
Copy link
Collaborator

devgony commented Jan 20, 2024

To use AND or OR expression, you should do like this.

        .filter(
            col("name")
                .not_like(text("D%"))
                .and(col("name").not_like(text("M___"))),
        )

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/expr/pattern_matching.rs#L69
or

        .filter("c.name = 'Fruit' OR c.name = 'Meat'")

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/select.rs#L85

@panarch
Copy link
Member

panarch commented Jan 20, 2024

Originally I had a misunderstanding on AST builder syntax and I thought adding .filter() multiple times will keep adding nested filter, but turns out every new filter was just overriding another filter.

adding filter multiple times should work like below,
if it wasn't then could you provide the query you tested?

// filter node -> filter node -> build
let actual = table("Bar")
.select()
.filter("id IS NULL")
.filter("id > 10")
.filter("id < 20")
.build();
let expected = "SELECT * FROM Bar WHERE id IS NULL AND id > 10 AND id < 20";
test(actual, expected);

@kanekoshoyu
Copy link
Author

To use AND or OR expression, you should do like this.

        .filter(
            col("name")
                .not_like(text("D%"))
                .and(col("name").not_like(text("M___"))),
        )

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/expr/pattern_matching.rs#L69 or

        .filter("c.name = 'Fruit' OR c.name = 'Meat'")

https://github.com/gluesql/gluesql/blob/main/test-suite/src/ast_builder/select.rs#L85

Sure, this worked fine for select(). for update() I tried the other day and was not working.

@panarch
Copy link
Member

panarch commented Jan 27, 2024

Sure, this worked fine for select(). for update() I tried the other day and was not working.

thanks, yes it was only working for select().
I've just added concat filter support to ast builder update nodes.
this will be included in the next release.

ref.
Update AST builder UpdateFilterNode .filter(..) behavior, #1455
Update AST builder UpdateNode to enforce .filter(..) before .set(..), #1454

@panarch panarch added the improvement Improvements for existing features label Jan 27, 2024
@panarch panarch self-assigned this Jan 27, 2024
@panarch panarch closed this as completed Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improvements for existing features
Projects
None yet
Development

No branches or pull requests

3 participants