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

Single path-element alias in top-level selects #2393

Merged
merged 1 commit into from
Jan 21, 2022

Conversation

deusaquilus
Copy link
Collaborator

@deusaquilus deusaquilus commented Jan 21, 2022

All top-level selects should have a alias but in the case of a nested (i.e. embedded object) only the last element will be used.
For example:

case class Name(first: String, last: String)
case class Person(name: Name, age: Int)

run(query[Person])
// SELECT p.first, p.last, p.age FROM Person p

In situations where things are re-wrapped in tuples, this will be resolved based on the name of the innermost property name of the thing in which each field is coming from:

run(query[Person].map(p => (p.name.first, p.name.last, p.age))
// SELECT p.first AS _1, p.last AS _2, p.age AS _3 FROM Person p

This applies with nested objects that are mapped as well. For example if Name is nested within an output tuple, the "last path element" of both p.first and p.last will be what they are in the Name object:

run(query[Person].map(p => (p.name, p.age))
// SELECT p.first AS first, p.last AS last, p.age AS _2 FROM Person p
// (age is AS _2 because it is a value-level thing that gets wrapped into a tuple)
// since aliases are redundant it gets simplified to:
// SELECT p.first, p.last, p.age AS _2 FROM Person p

In subselects this gets a bit more complex because subselect aliases have to be unique (e.g. imagine if the Name property appeared twice) so in order to know which one is which we propagate the whole path from in the nested object. That means that on the top level this must be re-aliased back into the last path element:

run(query[Person].map(p => (p.name, p.age).nested)
// SELECT p.namefirst AS FIRST, p.namelast as LAST, p.age AS _2 FROM (SELECT x.first AS namefirst, x.last AS namelast, x.age FROM Person p) AS p

@deusaquilus deusaquilus force-pushed the nested-toplevel branch 4 times, most recently from 6bcac3c to cecdc8d Compare January 21, 2022 18:25
@deusaquilus deusaquilus merged commit 4d75007 into zio:master Jan 21, 2022
@deusaquilus deusaquilus deleted the nested-toplevel branch January 21, 2022 19:38
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.

1 participant