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

Fix unnecessary nesting of infix queries #1131

Merged
merged 2 commits into from
Aug 7, 2018
Merged

Conversation

mosyp
Copy link
Collaborator

@mosyp mosyp commented Jul 30, 2018

Fixes #1058

Problem

InfixContext of SqlQuery always produces a nested query, sometimes this is unnecessary, see issue example.

Solution

Unnest queries.

implicit class ForUpdate[T](q: Query[T]) {
  def forUpdate = quote(infix"$q FOR UPDATE".as[Query[T]])
}
run(query[Person].forUpdate)

// before
// SELECT p.id, p.name, p.age FROM (SELECT * FROM Person p WHERE p.age < 18 FOR UPDATE) p	

// after
// SELECT p.name, p.age FROM person p WHERE p.age < 18 FOR UPDATE

Checklist

  • 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


import io.getquill.ast._

object ExpandMappedInfix {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am afraid this will break user defined infix.map(a => f(a)) call

Copy link
Collaborator Author

@mosyp mosyp Aug 1, 2018

Choose a reason for hiding this comment

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

@jilen Thanks for a review! Could you post an example? There's a test which handles that 6a94603#diff-e564af4387151d9877a17343b828b795R1050. ExpandMappedInfix is applied before the latest Normalize so user defined map will be normalized with expanded map in ExpandMappedInfix. However if there's any known case please share and I will include them in test

Copy link
Collaborator

@jilen jilen Aug 6, 2018

Choose a reason for hiding this comment

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

@mentegy For example, postgresql's Factorial (or any other postfix operator)

infix("$i !").map(x => f(x))       

Will this transformation break such operator ?

Copy link
Collaborator Author

@mosyp mosyp Aug 7, 2018

Choose a reason for hiding this comment

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

@jilen well that won't be possible since map is not available for Quoted[T], e.g:

    implicit class PostfixOps[T](v: T) {
      def fact = quote(infix"$v!".as[T])
    }
 run {
      q.map(e => e.id.fact) // no map available after fact
    }

So no known for me places where it could be broken. But I've added additional restriction, see updated pattern matching below, hence transformation will be applied only if q is a query in infix"$q ..."

@mosyp
Copy link
Collaborator Author

mosyp commented Aug 1, 2018

These changes completely has broken spark module, @deusaquilus any ideas why?

def apply(q: Ast): Ast = {
Transform(q) {
case Map(Infix("" :: parts, (q: Query) :: params), x, p) =>
Infix("" :: parts, Map(q, x, p) :: params)
Copy link
Collaborator Author

@mosyp mosyp Aug 7, 2018

Choose a reason for hiding this comment

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

Now, transformation is applied only if q is a query in infix"$q ..."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

wow seems like these changes fixed quill-spark errors, thank you @jilen !

@mosyp mosyp changed the title [wip] Fix Unnecessary nesting of infix queries Fix unnecessary nesting of infix queries Aug 7, 2018
@fwbrasil
Copy link
Collaborator

fwbrasil commented Aug 7, 2018

The solution introduces special handling for when the infix starts with a query, which might be confusing to users and break existing queries. I think it's a reasonable tradeoff, though. Could you update the readme and the changelog accordingly? Thank you!

@fwbrasil fwbrasil merged commit 679f6d6 into zio:master Aug 7, 2018
deusaquilus pushed a commit to deusaquilus2/quill that referenced this pull request Oct 25, 2018
* Fix unnecessary nesting of infix queries

* Update readme and changelog
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.

Unnecessary nesting of infix queries
4 participants