-
Notifications
You must be signed in to change notification settings - Fork 30
Extract EqualityValueSet for filter value set re-use #1239
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
Merged
myronmarston
merged 2 commits into
block:main
from
marcdaniels-toast:mdaniels/equality-value-set-extraction
Jun 5, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
113 changes: 113 additions & 0 deletions
113
elasticgraph-graphql/lib/elastic_graph/graphql/filtering/equality_value_set.rb
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # Copyright 2024 - 2026 Block, Inc. | ||
| # | ||
| # Use of this source code is governed by an MIT-style | ||
| # license that can be found in the LICENSE file or at | ||
| # https://opensource.org/licenses/MIT. | ||
| # | ||
| # frozen_string_literal: true | ||
|
|
||
| module ElasticGraph | ||
| class GraphQL | ||
| module Filtering | ||
| # A set that can represent either a specific list of values or all values except a specific | ||
| # list, with support for common set operations (union, intersection, negation). In contrast | ||
| # to other set implementations that work with `FilterValueSetExtractor`, only only works with | ||
| # `equal_to_any_of` filtering (hence the `EqualityValueSet` name). | ||
| class EqualityValueSet < Data.define(:type, :values) | ||
| # `Data.define` provides the following methods: | ||
| # @dynamic initialize, type, values, with | ||
|
|
||
| def self.of(values) | ||
| new(:inclusive, values.to_set) | ||
| end | ||
|
|
||
| def self.of_all_except(values) | ||
| new(:exclusive, values.to_set) | ||
| end | ||
|
|
||
| ALL = of_all_except([]) | ||
| EMPTY = of([]) | ||
|
|
||
| def intersection(other) | ||
| if inclusive? && other.inclusive? | ||
| # Since both sets are inclusive, we can just delegate to `Set#intersection` here. | ||
| EqualityValueSet.of(values.intersection(other.values)) | ||
| elsif exclusive? && other.exclusive? | ||
| # Since both sets are exclusive, we need to return an exclusive set of the union of the | ||
| # excluded values. For example, when dealing with positive integers: | ||
| # | ||
| # s1 = EqualityValueSet.of_all_except([1, 2, 3]) # > 3 | ||
| # s2 = EqualityValueSet.of_all_except([3, 4, 5]) # 1, 2, > 5 | ||
| # | ||
| # s3 = s1.intersection(s2) | ||
| # | ||
| # Here s3 would be all values > 5 (the same as `EqualityValueSet.of_all_except([1, 2, 3, 4, 5])`) | ||
| EqualityValueSet.of_all_except(values.union(other.values)) | ||
| else | ||
| # Since one set is inclusive and one set is exclusive, we need to return an inclusive set of | ||
| # `included_values - excluded_values`. For example, when dealing with positive integers: | ||
| # | ||
| # s1 = EqualityValueSet.of([1, 2, 3]) # 1, 2, 3 | ||
| # s2 = EqualityValueSet.of_all_except([3, 4, 5]) # 1, 2, > 5 | ||
| # | ||
| # s3 = s1.intersection(s2) | ||
| # | ||
| # Here s3 would be just `1, 2`. | ||
| included_values, excluded_values = get_included_and_excluded_values(other) | ||
| EqualityValueSet.of(included_values - excluded_values) | ||
| end | ||
| end | ||
|
|
||
| def union(other) | ||
| if inclusive? && other.inclusive? | ||
| # Since both sets are inclusive, we can just delegate to `Set#union` here. | ||
| EqualityValueSet.of(values.union(other.values)) | ||
| elsif exclusive? && other.exclusive? | ||
| # Since both sets are exclusive, we need to return an exclusive set of the intersection of the | ||
| # excluded values. For example, when dealing with positive integers: | ||
| # | ||
| # s1 = EqualityValueSet.of_all_except([1, 2, 3]) # > 3 | ||
| # s2 = EqualityValueSet.of_all_except([3, 4, 5]) # 1, 2, > 5 | ||
| # | ||
| # s3 = s1.union(s2) | ||
| # | ||
| # Here s3 would be all 1, 2, > 3 (the same as `EqualityValueSet.of_all_except([3])`) | ||
| EqualityValueSet.of_all_except(values.intersection(other.values)) | ||
| else | ||
| # Since one set is inclusive and one set is exclusive, we need to return an exclusive set of | ||
| # `excluded_values - included_values`. For example, when dealing with positive integers: | ||
| # | ||
| # s1 = EqualityValueSet.of([1, 2, 3]) # 1, 2, 3 | ||
| # s2 = EqualityValueSet.of_all_except([3, 4, 5]) # 1, 2, > 5 | ||
| # | ||
| # s3 = s1.union(s2) | ||
| # | ||
| # Here s3 would be 1, 2, 3, > 5 (the same as `EqualityValueSet.of_all_except([4, 5])`) | ||
| included_values, excluded_values = get_included_and_excluded_values(other) | ||
| EqualityValueSet.of_all_except(excluded_values - included_values) | ||
| end | ||
| end | ||
|
|
||
| def negate | ||
| with(type: INVERTED_TYPES.fetch(type)) | ||
| end | ||
|
|
||
| INVERTED_TYPES = {inclusive: :exclusive, exclusive: :inclusive} | ||
|
|
||
| def inclusive? | ||
| type == :inclusive | ||
| end | ||
|
|
||
| def exclusive? | ||
| type == :exclusive | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def get_included_and_excluded_values(other) | ||
| inclusive? ? [values, other.values] : [other.values, values] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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
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
39 changes: 39 additions & 0 deletions
39
elasticgraph-graphql/sig/elastic_graph/graphql/filtering/equality_value_set.rbs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| module ElasticGraph | ||
| class GraphQL | ||
| module Filtering | ||
| type equalityValueSetType = :inclusive | :exclusive | ||
|
|
||
| class EqualityValueSet | ||
| include Support::_NegatableSet[EqualityValueSet] | ||
|
|
||
| attr_reader type: equalityValueSetType | ||
| attr_reader values: ::Set[untyped] | ||
|
|
||
| def initialize: (equalityValueSetType, ::Set[untyped]) -> void | ||
|
|
||
| def self.new: | ||
| (equalityValueSetType, ::Set[untyped]) -> instance | ||
| | (type: equalityValueSetType, values: ::Set[untyped]) -> instance | ||
|
|
||
| def with: ( | ||
| ?type: equalityValueSetType, | ||
| ?values: ::Set[untyped] | ||
| ) -> EqualityValueSet | ||
|
|
||
| def self.of: (::Enumerable[untyped]) -> EqualityValueSet | ||
| def self.of_all_except: (::Enumerable[untyped]) -> EqualityValueSet | ||
|
|
||
| ALL: EqualityValueSet | ||
| EMPTY: EqualityValueSet | ||
| INVERTED_TYPES: ::Hash[equalityValueSetType, equalityValueSetType] | ||
|
|
||
| def inclusive?: () -> bool | ||
| def exclusive?: () -> bool | ||
|
|
||
| private | ||
|
|
||
| def get_included_and_excluded_values: (EqualityValueSet) -> [::Set[untyped], ::Set[untyped]] | ||
| end | ||
| end | ||
| end | ||
| end |
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
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.
Uh oh!
There was an error while loading. Please reload this page.