Skip to content

Instantly share code, notes, and snippets.

@wolfendale
Created December 2, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfendale/1871e8f27768a74cc1c1742cd5aa593c to your computer and use it in GitHub Desktop.
Save wolfendale/1871e8f27768a74cc1c1742cd5aa593c to your computer and use it in GitHub Desktop.
Issues with unions in Quill

Unions

  val e = quote {
    querySchema[TestEntity]("test_entity", _.s -> "field_s", _.i -> "field_i")
  }

  val e2 = quote {
    querySchema[TestEntity]("test_entity_2", _.s -> "field_s_2", _.i -> "field_i_2")
  }

  val q = quote {
    e.union(e2)
  }

Should produce:

SELECT x.field_s, x.field_i, x.l, x.o FROM ((SELECT x.field_s, x.field_i, x.l, x.o FROM test_entity x) UNION (SELECT x.field_s_2, x.field_i_2, x.l, x.o FROM test_entity_2 x)) AS x

Actually produces:

SELECT x.field_s, x.field_i, x.l, x.o FROM ((SELECT x.field_s, x.field_i, x.l, x.o FROM test_entity x) UNION (SELECT x.field_s, x.field_i, x.l, x.o FROM test_entity_2 x)) AS x

The inner unions always get the same aliases applied to their field as the outer query. This probably always correct for the left hand side but is not necessarily so for the right hand side.

You can even see this starting in the "expanded sql" trace (note that the aliases shown on the entity are ignored and are replaced by those of the parent query):

FlattenSqlQuery(
  List(TableContext(Entity("test_entity_2", List(PropertyAlias(List("s"), "field_s_2"), PropertyAlias(List("i"), "field_i_2"))), "x")),
  None,
  None,
  List(),
  None,
  None,
  List(
    SelectValue(Property(Ident("x"), "field_s"), None, false),
    SelectValue(Property(Ident("x"), "field_i"), None, false),
    SelectValue(Property(Ident("x"), "l"), None, false),
    SelectValue(Property(Ident("x"), "o"), None, false)
  ),
  false
)

Second example showing the same idea:

  val e = quote {
    querySchema[TestEntity]("test_entity", _.s -> "field_s", _.i -> "field_i")
  }

  val q = quote {
    query[TestEntity].union(e2)
  }

Should produce:

SELECT x.s, x.i, x.l, x.o FROM ((SELECT x.s, x.i, x.l, x.o FROM test_entity x) UNION (SELECT x.field_s, x.field_i, x.l, x.o FROM test_entity x)) AS x

Produces:

SELECT x.s, x.i, x.l, x.o FROM ((SELECT x.s, x.i, x.l, x.o FROM test_entity x) UNION (SELECT x.s, x.i, x.l, x.o FROM test_entity x)) AS x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment