|
| 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.doobie.mysql |
| 17 | + |
| 18 | +import cats.effect.Sync |
| 19 | +import cats.syntax.all._ |
| 20 | +import org.typelevel.doobie.Transactor |
| 21 | + |
| 22 | +import grackle.Mapping |
| 23 | +import grackle.Query.OrderSelection |
| 24 | +import grackle.doobie._ |
| 25 | +import grackle.sql._ |
| 26 | + |
| 27 | +abstract class DoobieMySqlMapping[F[_]]( |
| 28 | + val transactor: Transactor[F], |
| 29 | + val monitor: DoobieMonitor[F] |
| 30 | +)( |
| 31 | + implicit val M: Sync[F] |
| 32 | +) extends Mapping[F] |
| 33 | + with DoobieMySqlMappingLike[F] |
| 34 | + |
| 35 | +/** |
| 36 | + * MySQL 8.0.14+ dialect. LATERAL derived tables (8.0.14) and window functions (8.0) are native, |
| 37 | + * so the two deep structural hooks are pg-like; the differences are all local rendering: no |
| 38 | + * NULLS FIRST/LAST (CASE-WHEN emulation, nulls-low polarity), no DISTINCT ON (FirstValueColumn, |
| 39 | + * as MSSQL), no ILIKE (LIKE is already case-insensitive under the default *_ci collations - |
| 40 | + * it's the case-SENSITIVE branch that needs help), and a LIMIT-anchored offset/limit grammar |
| 41 | + * identical to SQLite's comma form, handled the same way. |
| 42 | + */ |
| 43 | +trait DoobieMySqlMappingLike[F[_]] extends DoobieMappingLike[F] with SqlMappingLike[F] { |
| 44 | + import SqlQuery.SqlSelect |
| 45 | + import TableExpr.Laterality |
| 46 | + |
| 47 | + // MySQL has no charset-agnostic binary collation: the collation named here must match the |
| 48 | + // character set of the columns it is applied to. utf8mb4 is MySQL 8's default, so that is |
| 49 | + // the default here; mappings over a legacy charset (latin1, utf8mb3) must override this, |
| 50 | + // otherwise ordering a text column raises "COLLATION 'utf8mb4_bin' is not valid for |
| 51 | + // CHARACTER SET ..." at query time. |
| 52 | + def binaryCollation: String = "utf8mb4_bin" |
| 53 | + |
| 54 | + // The default utf8mb4 collations are case/accent-insensitive; the collated rendering paths |
| 55 | + // want code-point order, which the binary collation supplies. |
| 56 | + def collateToFragment: Fragment = Fragments.const(s" COLLATE $binaryCollation") |
| 57 | + |
| 58 | + def aliasDefToFragment(alias: String): Fragment = |
| 59 | + Fragments.const(s" AS $alias") |
| 60 | + |
| 61 | + // MySQL's OFFSET is only legal inside a LIMIT clause, exactly like SQLite - and MySQL |
| 62 | + // supports the same comma form `LIMIT <offset>, <limit>`, so the same rendering trick |
| 63 | + // applies: offsetToFragment opens the clause, limitToFragment supplies the second operand, |
| 64 | + // and the two hooks below guarantee the pair is always complete. |
| 65 | + def offsetToFragment(offset: Fragment): Fragment = |
| 66 | + Fragments.const(" LIMIT ") |+| offset |+| Fragments.const(", ") |
| 67 | + |
| 68 | + def limitToFragment(limit: Fragment): Fragment = |
| 69 | + limit |
| 70 | + |
| 71 | + // MySQL has no `LIMIT -1`; its documented "no upper bound" idiom is LIMIT |
| 72 | + // 18446744073709551615, which doesn't fit the Option[Int] the query tree carries. |
| 73 | + // Int.MaxValue rows is unbounded for any practical purpose. |
| 74 | + def normalizeOffsetLimit(query: SqlQuery): SqlQuery = |
| 75 | + query match { |
| 76 | + case s: SqlSelect if s.offset.nonEmpty && s.limit.isEmpty => |
| 77 | + s.copy(limit = Int.MaxValue.some) |
| 78 | + case _ => query |
| 79 | + } |
| 80 | + |
| 81 | + def defaultOffsetForLimit(limit: Option[Int]): Option[Int] = |
| 82 | + limit.as(0) |
| 83 | + |
| 84 | + // Inverted from Postgres: under MySQL's default *_ci collations a plain LIKE is already |
| 85 | + // case-insensitive, so the case-SENSITIVE branch is the one needing an explicit collation. |
| 86 | + // The case-insensitive branch still normalizes through UPPER (as Oracle/MSSQL/SQLite do) |
| 87 | + // so it works under any column collation, not just the default. |
| 88 | + def likeToFragment(expr: Fragment, pattern: String, caseInsensitive: Boolean): Fragment = |
| 89 | + if (caseInsensitive) { |
| 90 | + Fragments.const("UPPER(") |+| expr |+| Fragments.const(") LIKE ") |+| |
| 91 | + Fragments.bind(stringEncoder, pattern.toUpperCase) |
| 92 | + } else { |
| 93 | + expr |+| Fragments.const(s" COLLATE $binaryCollation LIKE ") |+| |
| 94 | + Fragments.bind(stringEncoder, pattern) |
| 95 | + } |
| 96 | + |
| 97 | + // MySQL's CAST vocabulary is its own restricted set, not column type names. |
| 98 | + def ascribedNullToFragment(codec: Codec): Fragment = |
| 99 | + Fragments.sqlTypeName(codec) match { |
| 100 | + case Some(name) => |
| 101 | + val castName = name.toUpperCase match { |
| 102 | + case "TINYINT" | "SMALLINT" | "INTEGER" | "INT" | "BIGINT" | "BOOLEAN" | "BIT" => |
| 103 | + Some("SIGNED") |
| 104 | + case "CHAR" | "VARCHAR" | "NVARCHAR" | "TEXT" | "CLOB" => Some("CHAR") |
| 105 | + case "DECIMAL" | "NUMERIC" => Some("DECIMAL") |
| 106 | + // CAST(... AS DOUBLE/FLOAT) only exists since MySQL 8.0.17, above this dialect's |
| 107 | + // 8.0.14 floor - a bare NULL is legal everywhere and float-typed ascription is |
| 108 | + // only an inference hint, so it is the safe rendering here. |
| 109 | + case "FLOAT" | "REAL" | "DOUBLE" | "DOUBLE PRECISION" => None |
| 110 | + case "DATE" => Some("DATE") |
| 111 | + case "TIME" => Some("TIME") |
| 112 | + case "TIMESTAMP" | "DATETIME" => Some("DATETIME") |
| 113 | + // DoobieMapping.sqlTypeName reports a json codec's vendor type name as "JSONB". |
| 114 | + case "JSON" | "JSONB" => Some("JSON") |
| 115 | + case _ => None |
| 116 | + } |
| 117 | + castName match { |
| 118 | + case Some(cn) => Fragments.const(s"CAST(NULL AS $cn)") |
| 119 | + case None => Fragments.const("NULL") |
| 120 | + } |
| 121 | + case None => Fragments.const("NULL") |
| 122 | + } |
| 123 | + |
| 124 | + def collateSelected: Boolean = false |
| 125 | + |
| 126 | + // No DISTINCT ON - plain DISTINCT plus the FirstValueColumn window strategy, as MSSQL. |
| 127 | + def distinctOnToFragment(dcols: List[Fragment]): Fragment = |
| 128 | + Fragments.const("DISTINCT ") |
| 129 | + |
| 130 | + def distinctOrderColumn( |
| 131 | + owner: ColumnOwner, |
| 132 | + col: SqlColumn, |
| 133 | + predCols: List[SqlColumn], |
| 134 | + orders: List[OrderSelection[_]]): SqlColumn = |
| 135 | + SqlColumn.FirstValueColumn(owner, col, predCols, orders) |
| 136 | + |
| 137 | + // Parenthesized union branches are legal in MySQL and may carry inline ORDER BY/LIMIT/ |
| 138 | + // OFFSET. A branch-level ORDER BY without LIMIT may be discarded by the optimizer, which is |
| 139 | + // harmless: branch orders only determine row *selection* when paired with limit/offset (and |
| 140 | + // normalizeOffsetLimit guarantees any offset gets a limit); pure ordering is redone by the |
| 141 | + // outer query or in memory. So no derived-table encapsulation is needed. |
| 142 | + def encapsulateUnionBranch(s: SqlSelect): SqlSelect = s |
| 143 | + def unionBranchToFragment(branch: Fragment): Fragment = Fragments.parentheses(branch) |
| 144 | + |
| 145 | + // Native LATERAL derived tables, as Oracle - this is what sets the 8.0.14 version floor. |
| 146 | + def mkLateral(inner: Boolean): Laterality = Laterality.Lateral |
| 147 | + |
| 148 | + // MySQL has no NULLS FIRST/LAST. Its default is nulls-low (NULLs first in ASC, last in |
| 149 | + // DESC), so the two cases needing correction are the mirror image of Postgres's: a |
| 150 | + // sort-key prefix pushes NULLs to the requested end. Note the key polarity: NULL -> 1 |
| 151 | + // in BOTH branches (1 sorts last under ASC = NULLS LAST; 1 sorts first under DESC = |
| 152 | + // NULLS FIRST). MSSQL's dialect inverts the key in its DESC branch - a known latent bug |
| 153 | + // there, deliberately not replicated. Pinned by NullOrderingSuite. |
| 154 | + def orderToFragment(col: Fragment, ascending: Boolean, nullsLast: Boolean): Fragment = { |
| 155 | + val dir = if (ascending) Fragments.empty else Fragments.const(" DESC") |
| 156 | + val nulls = |
| 157 | + if (nullsLast && ascending) |
| 158 | + Fragments.const("CASE WHEN ") |+| col |+| |
| 159 | + Fragments.const(" IS NULL THEN 1 ELSE 0 END ASC, ") |
| 160 | + else if (!nullsLast && !ascending) |
| 161 | + Fragments.const("CASE WHEN ") |+| col |+| |
| 162 | + Fragments.const(" IS NULL THEN 1 ELSE 0 END DESC, ") |
| 163 | + else |
| 164 | + Fragments.empty |
| 165 | + |
| 166 | + nulls |+| col |+| dir |
| 167 | + } |
| 168 | + |
| 169 | + // MySQL sorts NULL below any non-NULL value (NULLs first in ASC), same as MSSQL/SQLite/H2. |
| 170 | + def nullsHigh: Boolean = false |
| 171 | +} |
0 commit comments