|
| 1 | +// Copyright (c) 2016-2025 Association of Universities for Research in Astronomy, Inc. (AURA) |
| 2 | +// Copyright (c) 2016-2025 Grackle Contributors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package grackle.sql.test |
| 17 | + |
| 18 | +import grackle._ |
| 19 | +import grackle.Predicate._ |
| 20 | +import grackle.Query.{Binding, Limit, OrderBy, OrderSelection, OrderSelections} |
| 21 | +import grackle.QueryCompiler.{Elab, SelectElaborator} |
| 22 | +import grackle.Value.{AbsentValue, EnumValue, IntValue, NullValue} |
| 23 | +import grackle.syntax._ |
| 24 | + |
| 25 | +// Covers a gap that predates issue #342 entirely: no fixture anywhere combines a union-typed |
| 26 | +// field with both `order` and `limit`, which is the only condition under which |
| 27 | +// SqlUnion.addFilterOrderByOffsetLimit pushes ordering into individual union branches |
| 28 | +// (SqlMapping.scala: `branchOrderBy = limit.flatMap(_ => orderBy)`). On backends where |
| 29 | +// encapsulateUnionBranch does real work (MSSQL), this is also the only path that exercises it. |
| 30 | +// The table is schema-qualified on every backend, so union-branch encapsulation is exercised |
| 31 | +// against a qualified name rather than only on MSSQL. The name is "union_order_entities", not |
| 32 | +// "entities" - that name is already taken by testdata/{pg,mssql}/interfaces.sql on both backends. |
| 33 | +trait SqlUnionOrderMapping[F[_]] extends SqlTestMapping[F] { |
| 34 | + |
| 35 | + object entities extends TableDef("qualified.union_order_entities") { |
| 36 | + val id = col("id", text) |
| 37 | + val entityType = col("entity_type", text) |
| 38 | + val name = col("name", text) |
| 39 | + } |
| 40 | + |
| 41 | + val schema = |
| 42 | + schema""" |
| 43 | + type Query { |
| 44 | + entities(order: Order, limit: Int): [Entity!]! |
| 45 | + } |
| 46 | + type ItemA { |
| 47 | + id: String! |
| 48 | + name: String! |
| 49 | + } |
| 50 | + type ItemB { |
| 51 | + id: String! |
| 52 | + name: String! |
| 53 | + } |
| 54 | + union Entity = ItemA | ItemB |
| 55 | + enum Order { |
| 56 | + ASC |
| 57 | + DESC |
| 58 | + } |
| 59 | + """ |
| 60 | + |
| 61 | + val QueryType = schema.ref("Query") |
| 62 | + val ItemAType = schema.ref("ItemA") |
| 63 | + val ItemBType = schema.ref("ItemB") |
| 64 | + val EntityType = schema.ref("Entity") |
| 65 | + |
| 66 | + val typeMappings = |
| 67 | + List( |
| 68 | + ObjectMapping( |
| 69 | + tpe = QueryType, |
| 70 | + fieldMappings = List( |
| 71 | + SqlObject("entities") |
| 72 | + ) |
| 73 | + ), |
| 74 | + SqlUnionMapping( |
| 75 | + tpe = EntityType, |
| 76 | + discriminator = entityTypeDiscriminator, |
| 77 | + fieldMappings = List( |
| 78 | + SqlField("id", entities.id, key = true, hidden = true), |
| 79 | + SqlField("name", entities.name, hidden = true), |
| 80 | + SqlField("entityType", entities.entityType, discriminator = true, hidden = true) |
| 81 | + ) |
| 82 | + ), |
| 83 | + ObjectMapping( |
| 84 | + tpe = ItemAType, |
| 85 | + fieldMappings = List( |
| 86 | + SqlField("id", entities.id, key = true), |
| 87 | + SqlField("name", entities.name) |
| 88 | + ) |
| 89 | + ), |
| 90 | + ObjectMapping( |
| 91 | + tpe = ItemBType, |
| 92 | + fieldMappings = List( |
| 93 | + SqlField("id", entities.id, key = true), |
| 94 | + SqlField("name", entities.name) |
| 95 | + ) |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + object entityTypeDiscriminator extends SqlDiscriminator { |
| 100 | + def discriminate(c: Cursor): Result[Type] = |
| 101 | + for { |
| 102 | + et <- c.fieldAs[String]("entityType") |
| 103 | + } yield et match { |
| 104 | + case "ItemA" => ItemAType |
| 105 | + case "ItemB" => ItemBType |
| 106 | + } |
| 107 | + |
| 108 | + def narrowPredicate(subtpe: Type): Result[Predicate] = { |
| 109 | + def mkPredicate(tpe: String): Result[Predicate] = |
| 110 | + Eql(EntityType / "entityType", Const(tpe)).success |
| 111 | + |
| 112 | + subtpe match { |
| 113 | + case ItemAType => mkPredicate("ItemA") |
| 114 | + case ItemBType => mkPredicate("ItemB") |
| 115 | + case _ => Result.internalError(s"Invalid discriminator: $subtpe") |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + sealed trait ListOrder { |
| 121 | + def ascending: Boolean |
| 122 | + } |
| 123 | + object ListOrder { |
| 124 | + case object Ascending extends ListOrder { def ascending = true } |
| 125 | + case object Descending extends ListOrder { def ascending = false } |
| 126 | + |
| 127 | + def fromGraphQLString(s: String): Option[ListOrder] = |
| 128 | + s.trim.toUpperCase match { |
| 129 | + case "ASC" => Some(Ascending) |
| 130 | + case "DESC" => Some(Descending) |
| 131 | + case _ => None |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + object OrderValue { |
| 136 | + def unapply(ev: EnumValue): Option[ListOrder] = |
| 137 | + ListOrder.fromGraphQLString(ev.name) |
| 138 | + } |
| 139 | + |
| 140 | + def mkLimit(query: Query, limit: Value): Result[Query] = |
| 141 | + limit match { |
| 142 | + case AbsentValue | NullValue => query.success |
| 143 | + case IntValue(num) if num > 0 => Limit(num, query).success |
| 144 | + case IntValue(num) => Result.failure(s"Expected limit > 0, found $num") |
| 145 | + case other => Result.failure(s"Expected limit > 0, found $other") |
| 146 | + } |
| 147 | + |
| 148 | + def mkOrderBy(query: Query, order: Value): Result[Query] = |
| 149 | + order match { |
| 150 | + case AbsentValue | NullValue => query.success |
| 151 | + case OrderValue(o) => |
| 152 | + OrderBy( |
| 153 | + OrderSelections( |
| 154 | + List(OrderSelection[String](EntityType / "name", ascending = o.ascending))), |
| 155 | + query |
| 156 | + ).success |
| 157 | + case _ => Result.failure(s"Expected order value, found $order") |
| 158 | + } |
| 159 | + |
| 160 | + override val selectElaborator = SelectElaborator { |
| 161 | + case (QueryType, "entities", List(Binding("order", order), Binding("limit", limit))) => |
| 162 | + Elab.transformChild(child => |
| 163 | + for { |
| 164 | + oc <- mkOrderBy(child, order) |
| 165 | + lc <- mkLimit(oc, limit) |
| 166 | + } yield lc) |
| 167 | + } |
| 168 | +} |
0 commit comments