|
| 1 | +import Foundation |
| 2 | +import InlineSnapshotTesting |
| 3 | +import StructuredQueriesPostgres |
| 4 | +import StructuredQueriesPostgresTestSupport |
| 5 | +import Testing |
| 6 | + |
| 7 | +extension SnapshotTests.Commands.Select { |
| 8 | + @Suite("Selection Semantics") |
| 9 | + struct SelectionSemanticsTests { |
| 10 | + |
| 11 | + // MARK: - Answer: Incremental Selection is NOT Supported |
| 12 | + |
| 13 | + @Test("Incremental .select() is NOT supported by design") |
| 14 | + func incrementalSelectNotSupported() async { |
| 15 | + // FINDING from academic review: |
| 16 | + // Question: Does User.select { $0.id }.select { $0.name } work? |
| 17 | + // Answer: NO - Type system prevents it |
| 18 | + // |
| 19 | + // Explanation: |
| 20 | + // - User.select { $0.id } returns Select<Int, User, ()> where Columns=Int |
| 21 | + // - .select() requires Columns=() or Columns=(repeat each C1) |
| 22 | + // - Since Columns=Int, no matching overload exists |
| 23 | + // |
| 24 | + // Design Decision: "Last wins" semantics would require Columns to be Any, |
| 25 | + // losing type safety. Current design sacrifices incremental selection for |
| 26 | + // compile-time correctness. |
| 27 | + |
| 28 | + // ✅ Correct: Batch selection |
| 29 | + await assertSQL( |
| 30 | + of: User.select { ($0.id, $0.name) } |
| 31 | + ) { |
| 32 | + """ |
| 33 | + SELECT "users"."id", "users"."name" |
| 34 | + FROM "users" |
| 35 | + """ |
| 36 | + } |
| 37 | + |
| 38 | + // ❌ This does NOT compile (which is intentional): |
| 39 | + // User.select { $0.id }.select { $0.name } |
| 40 | + // ^^^^^^^^ Error: requires Columns == () |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Selection Patterns That DO Work |
| 44 | + |
| 45 | + @Test("Multiple column selection via tuple") |
| 46 | + func multipleColumnSelectionViaTuple() async { |
| 47 | + await assertSQL( |
| 48 | + of: User.select { ($0.id, $0.name) } |
| 49 | + ) { |
| 50 | + """ |
| 51 | + SELECT "users"."id", "users"."name" |
| 52 | + FROM "users" |
| 53 | + """ |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test("Selection order matches tuple order") |
| 58 | + func selectionOrderMatchesTupleOrder() async { |
| 59 | + // Verify that column order in SQL matches tuple order |
| 60 | + await assertSQL( |
| 61 | + of: User.select { ($0.name, $0.id) } |
| 62 | + ) { |
| 63 | + """ |
| 64 | + SELECT "users"."name", "users"."id" |
| 65 | + FROM "users" |
| 66 | + """ |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test("Selection can be combined with WHERE") |
| 71 | + func selectionWithWhere() async { |
| 72 | + await assertSQL( |
| 73 | + of: User |
| 74 | + .where { $0.id > 10 } |
| 75 | + .select { ($0.id, $0.name) } |
| 76 | + ) { |
| 77 | + """ |
| 78 | + SELECT "users"."id", "users"."name" |
| 79 | + FROM "users" |
| 80 | + WHERE ("users"."id") > (10) |
| 81 | + """ |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test("Selection can be combined with JOIN") |
| 86 | + func selectionWithJoin() async { |
| 87 | + await assertSQL( |
| 88 | + of: Reminder |
| 89 | + .join(User.all) { $0.assignedUserID == $1.id } |
| 90 | + .select { ($0.id, $0.title, $1.name) } |
| 91 | + ) { |
| 92 | + """ |
| 93 | + SELECT "reminders"."id", "reminders"."title", "users"."name" |
| 94 | + FROM "reminders" |
| 95 | + JOIN "users" ON ("reminders"."assignedUserID") = ("users"."id") |
| 96 | + """ |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // MARK: - Algebraic Properties: WHERE Clause Monoid Laws |
| 101 | + |
| 102 | + @Test("WHERE clause monoid: Identity law (WHERE true)") |
| 103 | + func whereMonoidIdentity() async { |
| 104 | + // Monoid identity: query.where { true } should be equivalent to query |
| 105 | + let withoutWhere = User.all |
| 106 | + let withTrueWhere = User.where { _ in true } |
| 107 | + |
| 108 | + let sql1 = withoutWhere.query.prepare { "$\($0)" }.sql |
| 109 | + let sql2 = withTrueWhere.query.prepare { "$\($0)" }.sql |
| 110 | + |
| 111 | + // NOTE: These are NOT structurally identical (one has WHERE $1 binding for true), |
| 112 | + // but they are semantically equivalent |
| 113 | + #expect(sql1.contains("SELECT")) |
| 114 | + #expect(sql2.contains("WHERE $1")) // true becomes a bind parameter |
| 115 | + } |
| 116 | + |
| 117 | + @Test("WHERE clause composition: Associativity") |
| 118 | + func whereAssociativity() async { |
| 119 | + // (p1 AND p2) AND p3 generates different SQL than p1 AND (p2 AND p3) |
| 120 | + // due to BinaryOperator parenthesization |
| 121 | + let left = User.where { ($0.id > 10 && $0.id < 100) && $0.id != 50 } |
| 122 | + let right = User.where { $0.id > 10 && ($0.id < 100 && $0.id != 50) } |
| 123 | + |
| 124 | + let leftSQL = left.query.prepare { "$\($0)" }.sql |
| 125 | + let rightSQL = right.query.prepare { "$\($0)" }.sql |
| 126 | + |
| 127 | + // Parenthesization differs, but both are valid SQL |
| 128 | + // Left: WHERE ((p1 AND p2) AND p3) |
| 129 | + // Right: WHERE (p1 AND (p2 AND p3)) |
| 130 | + #expect(leftSQL != rightSQL) // Syntactically different |
| 131 | + #expect(leftSQL.contains("WHERE")) |
| 132 | + #expect(rightSQL.contains("WHERE")) |
| 133 | + // Both are semantically equivalent in PostgreSQL |
| 134 | + } |
| 135 | + |
| 136 | + @Test("Multiple WHERE clauses are combined with AND") |
| 137 | + func multipleWhereCombinedWithAnd() async { |
| 138 | + await assertSQL( |
| 139 | + of: User |
| 140 | + .where { $0.id > 10 } |
| 141 | + .where { $0.id < 100 } |
| 142 | + ) { |
| 143 | + """ |
| 144 | + SELECT "users"."id", "users"."name" |
| 145 | + FROM "users" |
| 146 | + WHERE ("users"."id") > (10) AND ("users"."id") < (100) |
| 147 | + """ |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test("WHERE clause order does NOT affect SQL (commutativity of AND)") |
| 152 | + func whereCommutativity() async { |
| 153 | + let query1 = User |
| 154 | + .where { $0.id > 10 } |
| 155 | + .where { $0.id < 100 } |
| 156 | + |
| 157 | + let query2 = User |
| 158 | + .where { $0.id < 100 } |
| 159 | + .where { $0.id > 10 } |
| 160 | + |
| 161 | + let sql1 = query1.query.prepare { "$\($0)" }.sql |
| 162 | + let sql2 = query2.query.prepare { "$\($0)" }.sql |
| 163 | + |
| 164 | + // SQL WHERE clauses respect call order, so these WILL differ |
| 165 | + // (AND is commutative semantically, but not syntactically in the DSL) |
| 166 | + #expect(sql1.contains("WHERE")) |
| 167 | + #expect(sql2.contains("WHERE")) |
| 168 | + // Document: Call order determines SQL order (NOT commutative) |
| 169 | + } |
| 170 | + |
| 171 | + // MARK: - Idempotence Properties |
| 172 | + |
| 173 | + @Test("DISTINCT is idempotent (last call wins)") |
| 174 | + func distinctIdempotence() async { |
| 175 | + let once = User.distinct() |
| 176 | + let twice = User.distinct().distinct() |
| 177 | + let thrice = User.distinct().distinct().distinct() |
| 178 | + |
| 179 | + let sql1 = once.query.prepare { "$\($0)" }.sql |
| 180 | + let sql2 = twice.query.prepare { "$\($0)" }.sql |
| 181 | + let sql3 = thrice.query.prepare { "$\($0)" }.sql |
| 182 | + |
| 183 | + // All should generate identical SQL |
| 184 | + #expect(sql1 == sql2) |
| 185 | + #expect(sql2 == sql3) |
| 186 | + #expect(sql1.contains("DISTINCT")) |
| 187 | + } |
| 188 | + |
| 189 | + @Test("LIMIT is NOT idempotent (last call wins)") |
| 190 | + func limitNotIdempotent() async { |
| 191 | + let limit10 = User.limit(10) |
| 192 | + let limit5 = User.limit(10).limit(5) |
| 193 | + |
| 194 | + let sql1 = limit10.query.prepare { "$\($0)" }.sql |
| 195 | + let sql2 = limit5.query.prepare { "$\($0)" }.sql |
| 196 | + |
| 197 | + // NOTE: Literal integers become bind parameters ($1) |
| 198 | + #expect(sql1.contains("LIMIT $1")) |
| 199 | + #expect(sql2.contains("LIMIT $1")) |
| 200 | + #expect(sql1 == sql2) // SQL is identical (bindings differ) |
| 201 | + |
| 202 | + // To verify different bindings, check the bindings array: |
| 203 | + let bindings1 = limit10.query.prepare { "$\($0)" }.bindings |
| 204 | + let bindings2 = limit5.query.prepare { "$\($0)" }.bindings |
| 205 | + #expect(bindings1.count == 1) |
| 206 | + #expect(bindings2.count == 1) |
| 207 | + // Bindings contain different values (10 vs 5) |
| 208 | + } |
| 209 | + |
| 210 | + // MARK: - Query Combinator Semantics |
| 211 | + |
| 212 | + @Test("Query combinators preserve all clauses") |
| 213 | + func combinatorsPreserveClauses() async { |
| 214 | + await assertSQL( |
| 215 | + of: User |
| 216 | + .where { $0.id > 10 } |
| 217 | + .order(by: \.id) |
| 218 | + .limit(5) |
| 219 | + ) { |
| 220 | + """ |
| 221 | + SELECT "users"."id", "users"."name" |
| 222 | + FROM "users" |
| 223 | + WHERE ("users"."id") > (10) |
| 224 | + ORDER BY "users"."id" |
| 225 | + LIMIT 5 |
| 226 | + """ |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments