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

[FEATURE]: Constant Condition Expression Optimization #4082

Open
1 task done
MineYuanlu opened this issue Feb 7, 2025 · 0 comments
Open
1 task done

[FEATURE]: Constant Condition Expression Optimization #4082

MineYuanlu opened this issue Feb 7, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@MineYuanlu
Copy link

MineYuanlu commented Feb 7, 2025

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

In the current version, expressions like inArray(field, []) generate SQL responses such as false. However, there is no advanced optimization for such cases. This can lead to situations where more complex condition expressions, like and(..., inArray(..., [])), only produce (... and false). Ideally, (... and false) can be optimized to false, and (... or true) can be optimized to true.

In extreme cases, this means the entire SQL query could be reduced to select ... from ... where false ...; or select ... from ...; (where true directly optimizes to removing the where clause).

I believe this can be implemented. Based on my initial review of the Drizzle ORM code, I see two potential approaches:

  1. Modify condition expressions to accept not only SQLWrapper | undefined but also boolean literals. This would allow for direct optimization. The downside is that it would require more widespread changes across the codebase.

  2. Detect constant conditions during the final SQL generation phase. While this method is more complex, it may be a more localized change than the first option.

Additionally, more complex queries could also be optimized. For instance, in subqueries like:

select ... from ... where 
cond1 or 
(exists (select ... from ... where false)) or 
(... in (select ... from ... where false))

This could be optimized to:

select ... from ... where cond1

Expected Outcome:
Better optimization of SQL queries by handling constant conditions (true/false) more efficiently, leading to potentially more performant queries in edge cases.

@MineYuanlu MineYuanlu added the enhancement New feature or request label Feb 7, 2025
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

No branches or pull requests

1 participant