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

Additional Fixes for Embedded Entities in Nested Queries #1628

Merged
merged 1 commit into from
Sep 19, 2019

Conversation

deusaquilus
Copy link
Collaborator

@deusaquilus deusaquilus commented Sep 18, 2019

Fixes #1627

In many ways, this represents a continuation of #1613. In #1613, schema protraction was introduced in order to solve the situation where a querySchema needs to be propagated for an entity that is wrapped into a tuple or case class e.g:

case class Emb(name:String) extends Embedded
case class Parent(id: Int, emb: Embedded)
implicit val embSchema = schemaMeta[Emb]("EmbTable", _.name -> "theName")
run { query[Emb].map(e => Parent(1, e)).distinct.map(p => (p.id, p.emb.name)) }

This PR represents a fix for the opposite case, i.e. where an entity from a super-class is sub-selected:

implicit val parenttSchema = schemaMeta[Parent]("ParentTable", _.id -> "theParentId", _.emb.name -> "theName")
run { query[Parent].map(p => p.emb).distinct.map(e => (e.name))

Several additional changes to Schema Protraction needed to be made for this purpose including:

  1. A way to sub-select data out of an Entity representing an entity whose embedded element is being extracted. This transformation is roughly:
`Entity(name, 
      PropertyAlias(List("id"), "theParentId"), 
      PropertyAlias(List("emb", "name"), "theName")...PropertyAlias(List("emb", xyz), "theName"))` ->
  `Entity(name, PropertyAlias(List("name"), "theName"))`

Or more sussinctly:

Entity(name, _.id -> "theParnetId", _.emb.name -> "theName"... _.emb.xyz) ->     // before .map(p => p.emb)
  Entity(name, _.name -> "theName"... _.xyz) // afterward

That is to say, every PropertyAlias path to emb inside of the host entity needs to be extracted from te Entity.

An additional change that needed to be made was the introduction of invisible identities. This was specifically needed for certain queries where one embedded entity was mapped into another. For instance:

case class Emb(id: Int, name: String) extends Embedded
case class Parent(id: Int, name: String, emb: Emb) extends Embedded
case class GrandParent(id: Int, par: Parent)

query[GrandParent]
    .map(g => g.par).distinct
    .map(p => (p.name, p.emb)).distinct
    .map(tup => (tup._1, tup._2)).distinct
}

In this kind of situation p.emb (in the second map clause) needs to be directly sub-selected and it's properties are then appended to it in the ExpandNestedQueries phase. Since the default-case of ExpandNestedQueries (in ExpandSelect) is to take the selected property directly i.e:

case other =>
                  trace"Reference is unidentified: $other returning:" andReturn
                    OrderedSelect(Integer.MAX_VALUE, SelectValue(Ident(name), Some(expandColumn(name, renameable)), false))

We needed to enhance Ident to be able to be invisible and then propagate this property:

case other =>
                  trace"Reference is unidentified: $other returning:" andReturn
                    OrderedSelect(Integer.MAX_VALUE, SelectValue(Ident.Opinionated(name, visible), Some(expandColumn(name, renameable)), false))

Many tests of this functionality were introduced that unwrap as well as unwrap and rewrap entities.

These kinds of changes are not necessarily the most "savory" form of AST manipulation and a Typed-AST would forego many of these issues. For example, if we knew that the emb identity in the query above is a nested entity, we could just expand it into it's component properties on the spot (i.e. inside of ExpandDistinct as opposed to having to make the property invisible inside of recursive calls of ExpandNestedQueries.

  • Unit test all changes
  • Update README.md if applicable
  • Add [WIP] to the pull request title if it's work in progress
  • Squash commits that aren't meaningful changes
  • Run sbt scalariformFormat test:scalariformFormat to make sure that the source files are formatted

@getquill/maintainers

@deusaquilus deusaquilus force-pushed the additional_nesting_fixes branch 3 times, most recently from 98e4c73 to 416fbbb Compare September 19, 2019 02:04
@deusaquilus deusaquilus changed the title [WIP] Additional Fixes for Embedded Entities in Nested Queries Additional Fixes for Embedded Entities in Nested Queries Sep 19, 2019
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.

Schema Protraction Does not work when a sub-entity is selected
1 participant