Skip to content

fix nested fields relationships #564

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
merged 9 commits into from
Aug 12, 2024
Merged

Conversation

soupi
Copy link
Contributor

@soupi soupi commented Aug 8, 2024

What

Selecting nested field relationships had a bug where it would try to look up a composite type field column in the parent collection. For example:

I have a collection institution with a field staff that is a composite type that contains the field favorite_artist_id. Trying to have a relationship from this field to the Artist table would attempt to look-up this field in institution, instead of in staff.

This PR fixes this.

How

Before, we had a type called TableNameAndReference that describe the metadata name of a table and held a (relationship alias) reference to it.

  • We modify this type to contain a Source instead of a Name, which can be a collection name or a nested field name. This is done so it is clearer whether we are tracking a collection or a type.
  • Instead of looking up a Collection everywhere, we lookup /anything/ that can have fields by replacing lookup_collection with lookup_fields_info.
  • When translating nested fields, instead of passing the joins accumulator for the parent table, we create another on the fly, and translate the joins using the table created for the nested field instead of the parent table.

We also:

  • Create nicer aliases for nested fields so they are easier to track
  • Add a test that is based on sql tables from the v3-engine, so we add a new sql file and update the configurations accordingly.

@soupi soupi changed the title Gil/fix nested fields relationships fix nested fields relationships Aug 8, 2024
env,
state,
fields,
&nested_field_table_reference,
nested_field_from,
&mut join_relationship_fields,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we handle and add joins here, instead in the parent.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like this change.

Comment on lines +73 to +80
-- ,(
-- 3,
-- 'University of Nowhere',
-- null,
-- null,
-- ARRAY ['nothing',null],
-- NULL
-- );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

commenting these out because of bugs but keeping them so it's easier to work on the bug.

https://linear.app/hasura/issue/APIPG-760/configuration-nullability-issues-in-arrays-and-composite-types

@soupi soupi marked this pull request as ready for review August 8, 2024 14:26
@soupi soupi requested a review from plcplc August 12, 2024 09:02
Copy link
Contributor

@plcplc plcplc left a comment

Choose a reason for hiding this comment

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

Left some comments - translatign nested field selection is a complex thing.

But if it works I'm happy.

sql.append_syntax("(");
column.to_sql(sql);
sql.append_syntax(")");
if !columns.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This change confused me a lot until I realized it is in a From clause.

Mulling it over a bit I think it's an alright change. But I wonder if it's ever necessary to give a column signature to FROM UNNEST(..). Maybe we could do without it entirely?

Copy link
Contributor Author

@soupi soupi Aug 12, 2024

Choose a reason for hiding this comment

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

Will have a look at it in the future. I don't remember the exact case right now, but I think we use it for variables or something where we need to specify the names and types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And sorry about this, I'm working on a similar ticket and kind of got some changes mixed together.

env,
state,
fields,
&nested_field_table_reference,
nested_field_from,
&mut join_relationship_fields,
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this change.

"%5_nested_field_binding"."address_line_1" AS "address_line_1",
"%5_nested_field_binding"."address_line_2" AS "address_line_2"
"%5_make_person.result.address"."address_line_1" AS "address_line_1",
"%5_make_person.result.address"."address_line_2" AS "address_line_2"
Copy link
Contributor

Choose a reason for hiding this comment

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

Urgh, this is difficult to read.

Information-wise it's much better than the arbitrary name, but the use of . suggests column projection happening in-line and it's only when you look closely that you see it's actually a quoted identifier.

Maybe we should change this scheme to e.g. -> or [<field>] in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have experimented with a few schemes and thought this was the best. I feel that once you understand what's going on, it becomes fine. We can revisit this is the future :)

let source = TableSource::NestedField {
collection_name: collection_name.clone(),
type_name: nested_field_type_name,
field_path: FieldPath(field_path),
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this lets us build up multiple nested field lookups in one go. Are there examples of this in tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The field_path and collection_name are only used for naming the alias, so even if this isn't perfect, it wouldn't effect the query.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think if they're only used as a convenience we should consider making that more obvious in the code. There is a lot of stuff that has happen, so the more we can direct the reader's attention the better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I understand. I did try to make this explicit in the type's comments:
https://github.com/hasura/ndc-postgres/pull/564/files#diff-d24352dcf20dd7c6e0b5e4f0c7dec878e49c311b976280b19008620bb936bb2eR80

But I guess there is a discoverability issue here.

@soupi soupi enabled auto-merge August 12, 2024 14:07
@soupi soupi disabled auto-merge August 12, 2024 14:10
@soupi soupi added this pull request to the merge queue Aug 12, 2024
Merged via the queue into main with commit 346d2db Aug 12, 2024
30 checks passed
@soupi soupi deleted the gil/fix-nested-fields-relationships branch August 12, 2024 14:51
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.

2 participants