-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Extend supported types for first and last aggregations
#6225
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
isum
wants to merge
1
commit into
ion/amp-powered-subgraphs
Choose a base branch
from
ion/extend-first-and-last-aggregations
base: ion/amp-powered-subgraphs
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
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
14 changes: 14 additions & 0 deletions
14
graph/src/schema/test_schemas/ts_invalid_first_type_reference.graphql
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,14 @@ | ||
| # fail: FieldTypeUnknown("Stats", "firstBlockNumber", "BlockNumber") | ||
|
|
||
| type Data @entity(timeseries: true) { | ||
| id: Int8! | ||
| timestamp: Timestamp! | ||
| blockNumber: Int8! | ||
| } | ||
|
|
||
| type Stats @aggregation(intervals: ["hour", "day"], source: "Data") { | ||
| id: Int8! | ||
| timestamp: Timestamp! | ||
|
|
||
| firstBlockNumber: BlockNumber! @aggregate(fn: "first", arg: "blockNumber") | ||
| } |
14 changes: 14 additions & 0 deletions
14
graph/src/schema/test_schemas/ts_invalid_last_type_reference.graphql
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,14 @@ | ||
| # fail: FieldTypeUnknown("Stats", "lastBlockNumber", "BlockNumber") | ||
|
|
||
| type Data @entity(timeseries: true) { | ||
| id: Int8! | ||
| timestamp: Timestamp! | ||
| blockNumber: Int8! | ||
| } | ||
|
|
||
| type Stats @aggregation(intervals: ["hour", "day"], source: "Data") { | ||
| id: Int8! | ||
| timestamp: Timestamp! | ||
|
|
||
| lastBlockNumber: BlockNumber! @aggregate(fn: "last", arg: "blockNumber") | ||
| } |
43 changes: 43 additions & 0 deletions
43
graph/src/schema/test_schemas/ts_valid_non_numeric_first_last.graphql
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,43 @@ | ||
| # valid: Non-numeric first and last aggregations | ||
|
|
||
| type EntityA @entity(immutable: true) { | ||
| id: ID! | ||
| } | ||
|
|
||
| type EntityB @entity(immutable: true) { | ||
| id: Int8! | ||
| } | ||
|
|
||
| type EntityC @entity(immutable: true) { | ||
| id: Bytes! | ||
| } | ||
|
|
||
| type Data @entity(timeseries: true) { | ||
| id: Int8! | ||
| timestamp: Timestamp! | ||
| fieldA: EntityA! | ||
| fieldB: EntityB! | ||
| fieldC: EntityC! | ||
| fieldD: String! | ||
| fieldE: Bytes! | ||
| } | ||
|
|
||
| type Stats @aggregation(intervals: ["hour", "day"], source: "Data") { | ||
| id: Int8! | ||
| timestamp: Timestamp! | ||
|
|
||
| firstA: EntityA! @aggregate(fn: "first", arg: "fieldA") | ||
| lastA: EntityA! @aggregate(fn: "last", arg: "fieldA") | ||
|
|
||
| firstB: EntityB! @aggregate(fn: "first", arg: "fieldB") | ||
| lastB: EntityB! @aggregate(fn: "last", arg: "fieldB") | ||
|
|
||
| firstC: EntityC! @aggregate(fn: "first", arg: "fieldC") | ||
| lastC: EntityC! @aggregate(fn: "last", arg: "fieldC") | ||
|
|
||
| firstD: String! @aggregate(fn: "first", arg: "fieldD") | ||
| lastD: String! @aggregate(fn: "last", arg: "fieldD") | ||
|
|
||
| firstE: Bytes! @aggregate(fn: "first", arg: "fieldE") | ||
| lastE: Bytes! @aggregate(fn: "last", arg: "fieldE") | ||
| } |
14 changes: 14 additions & 0 deletions
14
store/postgres/migrations/2025-12-09-164812_extend_arg_min_max/down.sql
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,14 @@ | ||
| -- This file was generated by generate.sh in this directory | ||
| set search_path = public; | ||
| drop aggregate arg_min_text(text_and_value); | ||
| drop aggregate arg_max_text(text_and_value); | ||
| drop function arg_from_text_and_value(text_and_value); | ||
| drop function arg_max_agg_text(text_and_value, text_and_value); | ||
| drop function arg_min_agg_text(text_and_value, text_and_value); | ||
| drop type text_and_value; | ||
| drop aggregate arg_min_bytea(bytea_and_value); | ||
| drop aggregate arg_max_bytea(bytea_and_value); | ||
| drop function arg_from_bytea_and_value(bytea_and_value); | ||
| drop function arg_max_agg_bytea(bytea_and_value, bytea_and_value); | ||
| drop function arg_min_agg_bytea(bytea_and_value, bytea_and_value); | ||
| drop type bytea_and_value; |
98 changes: 98 additions & 0 deletions
98
store/postgres/migrations/2025-12-09-164812_extend_arg_min_max/generate.sh
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,98 @@ | ||
| #! /bin/bash | ||
|
|
||
| # Generate up and down migrations to define arg_min and arg_max functions | ||
| # for the types listed in `types`. | ||
| # | ||
| # The functions can all be used like | ||
| # | ||
| # select first_int4((arg, value)) from t | ||
| # | ||
| # and return the `arg int4` for the smallest value `value int8`. If there | ||
| # are several rows with the smallest value, we try hard to return the first | ||
| # one, but that also depends on how Postgres calculates these | ||
| # aggregations. Note that the relation over which we are aggregating does | ||
| # not need to be ordered. | ||
| # | ||
| # Unfortunately, it is not possible to do this generically, so we have to | ||
| # monomorphize and define an aggregate for each data type that we want to | ||
| # use. The `value` is always an `int8` | ||
| # | ||
| # If changes to these functions are needed, copy this script to a new | ||
| # migration, change it and regenerate the up and down migrations | ||
|
|
||
| types="text bytea" | ||
| dir=$(dirname $0) | ||
|
|
||
| read -d '' -r prelude <<'EOF' | ||
| -- This file was generated by generate.sh in this directory | ||
| set search_path = public; | ||
| EOF | ||
|
|
||
| read -d '' -r up_template <<'EOF' | ||
| create type public.@T@_and_value as ( | ||
| arg @T@, | ||
| value int8 | ||
| ); | ||
|
|
||
| create or replace function arg_min_agg_@T@ (a @T@_and_value, b @T@_and_value) | ||
| returns @T@_and_value | ||
| language sql immutable strict parallel safe as | ||
| 'select case when a.arg is null then b | ||
| when b.arg is null then a | ||
| when a.value <= b.value then a | ||
| else b end'; | ||
|
|
||
| create or replace function arg_max_agg_@T@ (a @T@_and_value, b @T@_and_value) | ||
| returns @T@_and_value | ||
| language sql immutable strict parallel safe as | ||
| 'select case when a.arg is null then b | ||
| when b.arg is null then a | ||
| when a.value > b.value then a | ||
| else b end'; | ||
|
|
||
| create function arg_from_@T@_and_value(a @T@_and_value) | ||
| returns @T@ | ||
| language sql immutable strict parallel safe as | ||
| 'select a.arg'; | ||
|
|
||
| create aggregate arg_min_@T@ (@T@_and_value) ( | ||
| sfunc = arg_min_agg_@T@, | ||
| stype = @T@_and_value, | ||
| finalfunc = arg_from_@T@_and_value, | ||
| parallel = safe | ||
| ); | ||
|
|
||
| comment on aggregate arg_min_@T@(@T@_and_value) is | ||
| 'For ''select arg_min_@T@((arg, value)) from ..'' return the arg for the smallest value'; | ||
|
|
||
| create aggregate arg_max_@T@ (@T@_and_value) ( | ||
| sfunc = arg_max_agg_@T@, | ||
| stype = @T@_and_value, | ||
| finalfunc = arg_from_@T@_and_value, | ||
| parallel = safe | ||
| ); | ||
|
|
||
| comment on aggregate arg_max_@T@(@T@_and_value) is | ||
| 'For ''select arg_max_@T@((arg, value)) from ..'' return the arg for the largest value'; | ||
| EOF | ||
|
|
||
| read -d '' -r down_template <<'EOF' | ||
| drop aggregate arg_min_@T@(@T@_and_value); | ||
| drop aggregate arg_max_@T@(@T@_and_value); | ||
| drop function arg_from_@T@_and_value(@T@_and_value); | ||
| drop function arg_max_agg_@T@(@T@_and_value, @T@_and_value); | ||
| drop function arg_min_agg_@T@(@T@_and_value, @T@_and_value); | ||
| drop type @T@_and_value; | ||
| EOF | ||
|
|
||
| echo "$prelude" > $dir/up.sql | ||
| for typ in $types | ||
| do | ||
| echo "${up_template//@T@/$typ}" >> $dir/up.sql | ||
| done | ||
|
|
||
| echo "$prelude" > $dir/down.sql | ||
| for typ in $types | ||
| do | ||
| echo "${down_template//@T@/$typ}" >> $dir/down.sql | ||
| done | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to check this in - just use the original generate.sh and locally change the
typesvariable. Having up/down checked in is enough. If you want to be really thorough, add a note to this directory that that's how the migrations were created