After failed experiment:

SELECT ...
FROM parent
JOIN (SELECT * FROM child LIMIT 1) child ON child.parentId = child.id

The problem is that the join condition can't go inside the subquery, because SQLite complains about unknown table "parent". And when the join condition is outside the subquery, the LIMIT clause has been applied too early.

So we're still stuck with:

SELECT ...
FROM parent
JOIN child ON child.id = (SELECT id FROM child WHERE child.parentId = child.id LIMIT 1)

For some reason SQLite accepts the reference to the parent table in this case.
This commit is contained in:
Gwendal Roué committed 2020-04-19 14:14:13 +02:00
1 parent 278c18adfc
commit a7b8a73dba
2 files changed
+38 -23

No files matched your search

+11 -4
View File
@@ -224,6 +224,15 @@ extension SQLRelation: Refinable {
}
}
// Prepending filters for aesthetic reasons
func prependingFilters(_ filters: [SQLExpression]) -> Self {
map(\.filtersPromise) { filtersPromise in
filtersPromise.flatMap { existingFilters in
DatabasePromise { _ in filters + existingFilters }
}
}
}
func unfiltered() -> Self {
with(\.filtersPromise, DatabasePromise(value: []))
}
@@ -577,13 +586,11 @@ struct SQLAssociationCondition: Equatable {
/// - parameter db: A database connection.
/// - parameter leftAlias: A TableAlias for the table on the left of the
/// JOIN operator.
/// - parameter rightAlias: A TableAlias for the table on the right of the
/// JOIN operator.
/// - Returns: An array of SQL expression that should be joined with
/// the AND operator.
func expressions(_ db: Database, leftAlias: TableAlias, rightAlias: TableAlias) throws -> [SQLExpression] {
func expressions(_ db: Database, leftAlias: TableAlias) throws -> [SQLExpression] {
try columnMappings(db).map {
QualifiedColumn($0.right, alias: rightAlias) == QualifiedColumn($0.left, alias: leftAlias)
Column($0.right) == QualifiedColumn($0.left, alias: leftAlias)
}
}
@@ -1,4 +1,3 @@
/// SQLQueryGenerator is able to generate an SQL SELECT query.
struct SQLQueryGenerator: Refinable {
fileprivate private(set) var relation: SQLQualifiedRelation
@@ -627,7 +626,7 @@ private struct SQLQualifiedJoin: Refinable {
enum Target {
case all
case subRelation(SQLRelation)
case first(SQLRelation)
}
var kind: Kind
@@ -667,10 +666,8 @@ private struct SQLQualifiedJoin: Refinable {
.unfiltered()
.unordered()
let subRelation = relation
self.relation = SQLQualifiedRelation(outerRelation)
self.target = .subRelation(subRelation)
self.target = .first(relation)
} else {
self.relation = SQLQualifiedRelation(relation)
self.target = .all
@@ -709,32 +706,43 @@ private struct SQLQualifiedJoin: Refinable {
case .leftJoin:
allowsInnerJoin = false
}
// JOIN table...
sql += try "\(kind.rawValue) \(relation.source.sql(db, &context))"
let rightAlias = relation.sourceAlias
switch target {
case .all:
let filters = try condition.expressions(db, leftAlias: leftAlias, rightAlias: rightAlias)
+ relation.filtersPromise.resolve(db)
// ... ON <join conditions> AND <other filters>
var filters = try condition.expressions(db, leftAlias: leftAlias)
filters += try relation.filtersPromise.resolve(db)
if filters.isEmpty == false {
sql += " ON \(filters.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false))"
let filterSQL = filters
.joined(operator: .and)
.qualifiedExpression(with: rightAlias)
.expressionSQL(&context, wrappedInParenthesis: false)
sql += " ON \(filterSQL)"
}
case let .subRelation(subRelation):
case var .first(subRelation):
// Subquery: SELECT id ...
guard let primaryKey = try subRelation.primaryKeyExpression(db) else {
fatalError("Not implemented")
fatalError("Not implemented: support for WITHOUT ROWID optimization")
}
let subAlias = TableAlias()
let filters = try condition.expressions(db, leftAlias: leftAlias, rightAlias: subAlias)
let subRelation = subRelation
.qualified(with: subAlias)
.selectOnly([primaryKey])
.map(\.filtersPromise) { $0.flatMap { DatabasePromise(value: filters + $0) } }
let subQuery = SQLQuery(relation: subRelation, limit: SQLLimit(limit: 1, offset: nil))
let subQueryGenerator = SQLQueryGenerator(subQuery)
subRelation = subRelation.select([primaryKey])
// Subquery: ... WHERE <condition filters> AND <other filters>
let filters = try condition.expressions(db, leftAlias: leftAlias)
subRelation = subRelation.prependingFilters(filters)
// Subquery: ... LIMIT 1
let subQuery = SQLQuery(relation: subRelation, limit: SQLLimit(limit: 1, offset: nil))
// ... ON id = (SELECT id FROM table WHERE <condition filters> AND <other filters> LIMIT 1)
sql += " ON "
sql += rightAlias[primaryKey].expressionSQL(&context, wrappedInParenthesis: false)
sql += try " = (" + subQueryGenerator.sql(db, &context) + ")"
sql += " = ("
sql += try SQLQueryGenerator(subQuery).sql(db, &context)
sql += ")"
}
for (_, join) in relation.joins {