add support for complex boolean logic within addWhere and addHaving#2646
Open
albbird wants to merge 1 commit intomalloydata:mainfrom
Open
add support for complex boolean logic within addWhere and addHaving#2646albbird wants to merge 1 commit intomalloydata:mainfrom
albbird wants to merge 1 commit intomalloydata:mainfrom
Conversation
…unctions Signed-off-by: Vinicius A <vinicius.albuquerquebarros@bird.com>
Collaborator
|
OK, this is potentially awesome or awful. There are reasons for the limitations on filtering. DO NOT MERGE, until the conversations are complete. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
addWhereandaddHavingfunctions inmalloy-query-buildercouldn't express complex boolean logic (AND/OR) between filter conditions across different fields. All filters were joined with commas, limiting the ability to build queries with OR conditions or explicit AND groupings.Note: The existing
ClauseChaininmalloy-filteronly supports AND/OR operators within a single field's filter expression (e.g.,carrier ~ f'WN and AA'). It cannot combine conditions across different fields likeflight_count > 100 AND total_distance > 1000.Solution
Extended the
addWhereandaddHavingfunctions to accept an optionalconjunctionparameter ('and'|'or') that specifies how the current filter connects to the previous one, enabling boolean logic across multiple fields.Changes
packages/malloy-interfaces/src/types.tsConjunctiontype ('and'|'or')conjunctionproperty toFilterOperationtypepackages/malloy-interfaces/src/to_malloy.tsformatFilterBlockfunction to handle per-item conjunction operatorswhereandhavingclauses to outputand/orinstead of comma when conjunction is specifiedpackages/malloy-query-builder/src/query-ast.tsaddWherefunction overloads to accept optionalconjunctionparameteraddHavingfunction similarly for consistencyUsage Example
Generates:
ClauseChain vs Conjunction Parameter
carrier ~ f'WN and AA'carrier ~ f'WN'andorigin ~ f'TX'Combined Usage
The two approaches are complementary. You can use ClauseChain's complex within-field logic inside each
addWherecall, and then use the conjunction parameter to connect those filters across fields:Limitations
Cross-field grouped logic with parentheses is not supported:
This would require a filter tree builder, which is beyond the scope of this PR.
Key Design Decisions
where: field ~ f'value' and)Tests
Added comprehensive tests in
packages/malloy-query-builder/src/query-ast.spec.ts:All existing tests continue to pass.