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

graph,graphql,store: nested sorting #3096

Merged
merged 1 commit into from
Feb 14, 2023
Merged

graph,graphql,store: nested sorting #3096

merged 1 commit into from
Feb 14, 2023

Conversation

kamilkisiela
Copy link
Contributor

@kamilkisiela kamilkisiela commented Dec 23, 2021

#3737

Given this schema:

type Country @entity {
  id: ID!
  capital: City!
  cities: [City!]!
}

type City @entity {
  id: ID!
  name: String
  population: Int
  landmarks: [String!]!
}

It adds fields of child entity to *_orderBy enum:

enum Country_orderBy {
   id
   capital
   cities
+  capital__id
+  capital__name
+  capital__population
}
  • sort by type Child @entity { name: String }
  • sort by type Child @entity { id: ID }
  • can't sort by type Child @entity { names: [String] }
  • can't sort by type Child @entity { secondLevelChild: AnotherEntity }
  • can't sort by type Child @entity { secondLevelChildren: [AnotherEntity] }
  • support @derivedFrom
  • sort by interface Child { name: String }
  • sort by interface Child { id: ID }
  • sort interface by type Child @entity { name: String } (done in Nested Sorting: interface by non-derived child entity #4058)

@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from e3e551a to ad81c2b Compare January 13, 2022 13:28
@kamilkisiela kamilkisiela force-pushed the kamil-sorting branch 2 times, most recently from 980d3a8 to 2c84c57 Compare January 13, 2022 13:35
@kamilkisiela kamilkisiela marked this pull request as ready for review January 14, 2022 12:14
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from ad81c2b to c288e18 Compare January 20, 2022 13:36
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from c288e18 to c02173a Compare January 24, 2022 11:31
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from c02173a to 9afb13b Compare January 25, 2022 09:00
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from 9afb13b to 46d2872 Compare March 3, 2022 17:02
@dotansimha dotansimha force-pushed the kamil-filter-by-child branch from 46d2872 to 771a995 Compare March 8, 2022 15:30
@dotansimha dotansimha force-pushed the kamil-filter-by-child branch 2 times, most recently from 809b6fd to f34918a Compare March 13, 2022 14:48
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from f34918a to 2a77b5d Compare April 5, 2022 10:51
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch 2 times, most recently from 1e9f27b to 5523cf7 Compare April 27, 2022 12:35
@dotansimha dotansimha force-pushed the kamil-filter-by-child branch 2 times, most recently from 6e04d05 to 6f9312f Compare May 3, 2022 06:28
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch 3 times, most recently from b863adb to 461a827 Compare May 4, 2022 09:38
@kamilkisiela kamilkisiela force-pushed the kamil-filter-by-child branch from 461a827 to 2c30a78 Compare May 23, 2022 12:37
@kamilkisiela kamilkisiela changed the title WIP: Sorting by child fields WIP: Nested Sorting Aug 31, 2022
@kamilkisiela kamilkisiela force-pushed the kamil-sorting branch 2 times, most recently from 5e8be64 to cce0b3b Compare September 26, 2022 10:55
@kamilkisiela kamilkisiela changed the title WIP: Nested Sorting Nested Sorting Sep 26, 2022
@kamilkisiela kamilkisiela force-pushed the kamil-sorting branch 2 times, most recently from 230c169 to 1d9d19d Compare October 5, 2022 13:12
Copy link
Collaborator

@lutter lutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started reviewing this, but then realized that it also deals with sorting by interface fields, and now I am not certain how it relates to PR #4058. Should I continue reviewing this or is this superseded by that PR?

graphql/src/schema/api.rs Show resolved Hide resolved
graphql/src/schema/api.rs Outdated Show resolved Hide resolved
graphql/src/schema/api.rs Outdated Show resolved Hide resolved
graphql/src/schema/api.rs Outdated Show resolved Hide resolved
graphql/src/store/query.rs Outdated Show resolved Hide resolved
graphql/src/store/query.rs Show resolved Hide resolved
graph/src/components/store/mod.rs Show resolved Hide resolved
@kamilkisiela
Copy link
Contributor Author

@lutter

I started reviewing this, but then realized that it also deals with sorting by interface fields, and now I am not certain how it relates to PR #4058. Should I continue reviewing this or is this superseded by that PR?

It deals with sorting by interface fields, #4058 adds sorting an interface by children (as it's a bit more complex).

Copy link
Collaborator

@lutter lutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still not completely through the PR, but figured these comments would be good to address to make some progress.

store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
graph/src/components/store/mod.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
store/postgres/src/relational_queries.rs Outdated Show resolved Hide resolved
@kamilkisiela
Copy link
Contributor Author

@lutter about the failing test, it works locally but I will try to use exact version on PostgreSQL docker image.

@kamilkisiela
Copy link
Contributor Author

kamilkisiela commented Nov 24, 2022

@lutter

Rows of musicians table are sorted by main_band, block_range columns. You can see that b1 is used by two musicians.

Sometimes the unit test is 🟢, but sometimes it's 🔴 .

Do you think that the order of row insertion affects the end result? These two rows are pretty much identical (block_range and main_band).

entity data
Musician {"id": "m2", "vid": 1, "name": "Lisa", "bands": ["b1"], "main_band": "b1", "block_range": "[0,)"}
Musician {"id": "m1", "vid": 2, "name": "John", "bands": ["b1", "b2"], "main_band": "b1", "block_range": "[0,)"}
Musician {"id": "m3", "vid": 3, "name": "Tom", "bands": ["b1", "b2"], "main_band": "b2", "block_range": "[1,)"}
Musician {"id": "m4", "vid": 4, "name": "Valerie", "bands": [], "main_band": null, "block_range": "[1,)"}

After running the unit tests, I run the query generated by graph-node:

select 'Musician' as entity, to_jsonb(c.*) as data from (select  c.*
 from "sgd21"."musician" c left join "sgd21"."band" as cc on (cc."id" = c."main_band" and cc."block_range" @> 1)
where c.block_range @> 1
order by cc."id", cc.block_range
limit 100) c

I always get consistent results.

@lutter
Copy link
Collaborator

lutter commented Dec 1, 2022

select 'Musician' as entity, to_jsonb(c.*) as data from (select  c.*
 from "sgd21"."musician" c left join "sgd21"."band" as cc on (cc."id" = c."main_band" and cc."block_range" @> 1)
where c.block_range @> 1
order by cc."id", cc.block_range
limit 100) c

I am not entirely sure what is going on here; for me locally, can_query_with_sorting_by_child_entity_id fails consistently. Is that the test you are talking about? If so, it should say order by cc.name, cc.id, cc.block_range based on the GraphQL for asc.

@kamilkisiela kamilkisiela force-pushed the kamil-sorting branch 2 times, most recently from 4b0a0f5 to 1d343e1 Compare January 12, 2023 12:24
@kamilkisiela kamilkisiela requested a review from lutter January 12, 2023 13:01
Copy link
Collaborator

@lutter lutter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Sorry it took so long to review.

One final thing: we should have an environment variable GRAPH_GRAPHQL_DISABLE_CHILD_SORTING which defaults to false that we can set to turn this off if it leads to performance issues. The flag should work just the same as GRAPH_GRAPHQL_DISABLE_BOOL_FILTERS does. I left comments where I think this needs to be checked to (a) suppress child sorting in the GraphQL schema and (b) return an error in queries.

Other than that, this should be good to go.

graphql/src/store/query.rs Outdated Show resolved Hide resolved
graphql/src/schema/api.rs Show resolved Hide resolved
@neysofu neysofu mentioned this pull request Feb 13, 2023
2 tasks
graphql: test orderBy enum

graphql: do not sort an interface by child-level entity

graphql: merge Object and Interface match case

graphql: Do not pass field type

store: avoid expect

Avoid format macro

Make ChildKeyDetails more expressive

Use constraint_violation macro

Sorting by child id

Require less data

Refactor EntityOrderByChild*

Support sorting by child id

Add GRAPH_GRAPHQL_DISABLE_CHILD_SORTING (false by default)
@dotansimha dotansimha changed the title Nested Sorting graph,graphql,store: nested sorting Feb 14, 2023
@dotansimha
Copy link
Contributor

@lutter merged with the integration tests failing, it seems like the same on master. Unrelated to this PR, and the only change was a rebase.

@dotansimha dotansimha merged commit b4d589a into master Feb 14, 2023
@dotansimha dotansimha deleted the kamil-sorting branch February 14, 2023 01:32
neysofu pushed a commit that referenced this pull request Feb 16, 2023
neysofu pushed a commit that referenced this pull request Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sorting by child entities
3 participants