vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
217 stars 116 forks source link

Problem with filter on NULL (probably a Swift 6.0 bug) #611

Closed Frizlab closed 4 months ago

Frizlab commented 4 months ago

Describe the issue

On Xcode 16 beta 1 and 2, .filter(\.$nullableField == nil) does not generate the proper query

Vapor version

4.102.0

Operating system and version

macOS 14.5 (23F79)

Swift version

Swift version 6.0 (swiftlang-6.0.0.4.52 clang-1600.0.21.1.3)

Steps to reproduce

  1. Create a model with at least one NULL-able field;
  2. Run a query filtering on NULL for the field.

Example:

private final class DbTestEntity : Model, @unchecked Sendable {

   static let schema = "test_entities"

   @ID
   var id: UUID?

   @Field(key: FieldKeys.nullableField)
   var nullableField: String?

   enum FieldKeys {
      static let nullableField: FieldKey = "nullable_field"
   }

}

func test() async throws {
   let countNulls = try await DbTestEntity.query(on: app.db)
      .filter(\.$nullableField == nil)
      .count()
}

Outcome

The following SQL should be generated and run:

SELECT COUNT("test_entities"."id") AS "aggregate" FROM "test_entities" WHERE "test_entities"."nullable_field" IS NULL

On Xcode 16 beta 1 and 2, the following is generated:

SELECT COUNT("test_entities"."id") AS "aggregate" FROM "test_entities" WHERE "test_entities"."nullable_field" = $1

with the following bindings ["nil"].

Interestingly using Swift version 6.0-dev (LLVM 5b37bd4c1c578e9, Swift 54898ce105c7185) in a Linux VM yields the correct query. Using the Swift 6.0 Development Snapshot 2024-06-22 (a) also yields the correct results.

Additional notes

No response

Frizlab commented 4 months ago

This looks fixed in the latest Xcode beta