-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Adds new filters to increase Postgrest parity #81
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
Open
filipecabaco
wants to merge
1
commit into
master
Choose a base branch
from
feat/add-filters-real-832
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some comments aren't visible on the classic Files Changed page.
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
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,218 @@ | ||
| -- Fix 1: Drop old 4-arg overload and its dependent before creating the 5-arg version | ||
| drop function if exists realtime.is_visible_through_filters(realtime.wal_column[], realtime.user_defined_filter[]); | ||
| drop function if exists realtime.check_equality_op(realtime.equality_op, regtype, text, text); | ||
|
|
||
| alter type realtime.equality_op add value 'like'; | ||
| alter type realtime.equality_op add value 'ilike'; | ||
| alter type realtime.equality_op add value 'is'; | ||
| alter type realtime.equality_op add value 'match'; | ||
| alter type realtime.equality_op add value 'imatch'; | ||
| alter type realtime.equality_op add value 'isdistinct'; | ||
|
|
||
| alter type realtime.user_defined_filter add attribute negate boolean; | ||
|
|
||
| drop function if exists realtime.check_equality_op(realtime.equality_op, regtype, text, text); | ||
|
|
||
| create or replace function realtime.check_equality_op( | ||
| op realtime.equality_op, | ||
| type_ regtype, | ||
| val_1 text, | ||
| val_2 text, | ||
| negate boolean DEFAULT false | ||
| ) | ||
| returns bool | ||
| stable -- Fix 2: was immutable, uses EXECUTE so must be stable | ||
| language plpgsql | ||
| as $$ | ||
| declare | ||
| op_symbol text; | ||
| res boolean; | ||
| begin | ||
| -- IS DISTINCT FROM / IS NOT DISTINCT FROM: infix, both sides typed literals | ||
| if op = 'isdistinct' then | ||
| execute format( | ||
| 'select %L::%s %s %L::%s', | ||
| val_1, | ||
| type_::text, | ||
| case when negate then 'IS NOT DISTINCT FROM' else 'IS DISTINCT FROM' end, | ||
| val_2, | ||
| type_::text | ||
| ) into res; | ||
| return res; | ||
| end if; | ||
|
|
||
| -- IS requires a keyword RHS (NULL, TRUE, FALSE, UNKNOWN), not a typed literal | ||
| if op = 'is' then | ||
| if val_2 not in ('null', 'true', 'false', 'unknown') then | ||
| raise exception 'invalid value for is filter: must be null, true, false, or unknown'; | ||
| end if; | ||
| execute format( | ||
| 'select %L::%s %s %s', | ||
| val_1, | ||
| type_::text, | ||
| case when negate then 'IS NOT' else 'IS' end, | ||
| upper(val_2) | ||
| ) into res; | ||
| return res; | ||
| end if; | ||
|
|
||
| op_symbol = case | ||
| when op = 'eq' then '=' | ||
| when op = 'neq' then '!=' | ||
| when op = 'lt' then '<' | ||
| when op = 'lte' then '<=' | ||
| when op = 'gt' then '>' | ||
| when op = 'gte' then '>=' | ||
| when op = 'in' then '= any' | ||
| when op = 'like' then 'LIKE' | ||
| when op = 'ilike' then 'ILIKE' | ||
| when op = 'match' then '~' | ||
| when op = 'imatch' then '~*' | ||
| else null | ||
| end; | ||
|
|
||
| if op_symbol is null then | ||
| raise exception 'unsupported equality operator: %', op::text; | ||
| end if; | ||
|
|
||
| execute format( | ||
| 'select %L::%s %s (%L::%s)', | ||
| val_1, | ||
| type_::text, | ||
| op_symbol, | ||
| val_2, | ||
| case when op = 'in' then type_::text || '[]' else type_::text end | ||
| ) into res; | ||
|
|
||
| return case when negate then not res else res end; | ||
| end; | ||
| $$; | ||
|
|
||
|
|
||
| create or replace function realtime.is_visible_through_filters(columns realtime.wal_column[], filters realtime.user_defined_filter[]) | ||
| returns bool | ||
| language sql | ||
| stable -- Fix 2: was immutable, calls stable function so must be stable | ||
| as $$ | ||
| select | ||
|
filipecabaco marked this conversation as resolved.
|
||
| filters is null | ||
| or array_length(filters, 1) is null | ||
| or coalesce( | ||
| count(col.name) = count(1) | ||
| and sum( | ||
| realtime.check_equality_op( | ||
| op:=f.op, | ||
| type_:=coalesce(col.type_oid::regtype, col.type_name::regtype), | ||
| val_1:=col.value #>> '{}', | ||
| val_2:=f.value, | ||
| negate:=coalesce(f.negate, false) | ||
| )::int | ||
| ) filter (where col.name is not null) = count(col.name), | ||
| false | ||
| ) | ||
| from | ||
| unnest(filters) f | ||
|
filipecabaco marked this conversation as resolved.
|
||
| left join unnest(columns) col | ||
| on f.column_name = col.name; | ||
| $$; | ||
|
|
||
|
|
||
| create or replace function realtime.subscription_check_filters() | ||
| returns trigger | ||
| language plpgsql | ||
| as $$ | ||
| declare | ||
| col_names text[] = coalesce( | ||
| array_agg(c.column_name order by c.ordinal_position), | ||
| '{}'::text[] | ||
| ) | ||
| from | ||
| information_schema.columns c | ||
| where | ||
| format('%I.%I', c.table_schema, c.table_name)::regclass = new.entity | ||
| and pg_catalog.has_column_privilege( | ||
| (new.claims ->> 'role'), | ||
| format('%I.%I', c.table_schema, c.table_name)::regclass, | ||
| c.column_name, | ||
| 'SELECT' | ||
| ); | ||
| -- Fix 3: removed unused table_col_names declaration | ||
| filter realtime.user_defined_filter; | ||
| col_type regtype; | ||
| in_val jsonb; | ||
| selected_col text; | ||
| begin | ||
| for filter in select * from unnest(new.filters) loop | ||
| if not filter.column_name = any(col_names) then | ||
| raise exception 'invalid column for filter %', filter.column_name; | ||
| end if; | ||
|
|
||
| col_type = ( | ||
| select atttypid::regtype | ||
| from pg_catalog.pg_attribute | ||
| where attrelid = new.entity | ||
| and attname = filter.column_name | ||
| ); | ||
| if col_type is null then | ||
| raise exception 'failed to lookup type for column %', filter.column_name; | ||
| end if; | ||
|
|
||
| if filter.op = 'in'::realtime.equality_op then | ||
| in_val = realtime.cast(filter.value, (col_type::text || '[]')::regtype); | ||
| if coalesce(jsonb_array_length(in_val), 0) > 100 then | ||
| raise exception 'too many values for `in` filter. Maximum 100'; | ||
| end if; | ||
| elsif filter.op = 'is'::realtime.equality_op then | ||
| if filter.value not in ('null', 'true', 'false', 'unknown') then | ||
| raise exception 'invalid value for is filter: must be null, true, false, or unknown'; | ||
| end if; | ||
| -- Fix 4: validate like/ilike only applies to text-compatible columns | ||
| elsif filter.op in ('like'::realtime.equality_op, 'ilike'::realtime.equality_op) then | ||
| if not exists ( | ||
| select 1 from pg_catalog.pg_operator | ||
| where oprname = '~~' and oprleft = col_type | ||
| ) then | ||
| raise exception 'operator % requires a text-compatible column type, got %', filter.op::text, col_type::text; | ||
| end if; | ||
| -- Fix 5: validate match/imatch regex patterns eagerly | ||
| elsif filter.op in ('match'::realtime.equality_op, 'imatch'::realtime.equality_op) then | ||
| begin | ||
| perform '' ~ filter.value; | ||
| exception when others then | ||
| raise exception 'invalid regular expression for % filter: %', filter.op::text, sqlerrm; | ||
| end; | ||
| else | ||
| perform realtime.cast(filter.value, col_type); | ||
| end if; | ||
| end loop; | ||
|
|
||
| if new.selected_columns = '{}' then | ||
| raise exception 'selected_columns cannot be empty. Remove the select parameter to capture all columns.'; | ||
| end if; | ||
|
|
||
| if new.selected_columns is not null and array_position(new.selected_columns, null::text) is not null then | ||
| raise exception 'selected_columns cannot contain null values.'; | ||
| end if; | ||
|
|
||
| if new.selected_columns is not null then | ||
| for selected_col in select * from unnest(new.selected_columns) loop | ||
| if not selected_col = any(col_names) then | ||
| raise exception 'invalid column for select %', selected_col; | ||
| end if; | ||
| end loop; | ||
| end if; | ||
|
|
||
| -- Fix 6: include negate in the ORDER BY for deterministic normalization | ||
| new.filters = coalesce( | ||
| array_agg(f order by f.column_name, f.op, f.value, f.negate), | ||
| '{}' | ||
| ) from unnest(new.filters) f; | ||
|
|
||
| new.selected_columns = ( | ||
| select array_agg(c order by c) | ||
| from unnest(new.selected_columns) c | ||
| ); | ||
|
|
||
| return new; | ||
| 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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.