From 6ee7db48c8a0bbaa4988d4cb7fd8ac3673db3dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 26 Jul 2020 16:04:02 +0200 Subject: [PATCH] Take care of indexes on expressions as well sqlite> create table t(a,b); sqlite> create index i on t(a like '%a', a, b); sqlite> explain query plan select * from t where a like '%a'; QUERY PLAN `--SCAN TABLE t sqlite> explain query plan select * from t where a not like '%a'; QUERY PLAN `--SCAN TABLE t sqlite> explain query plan select * from t where (a like '%a') = 1; QUERY PLAN `--SEARCH TABLE t USING COVERING INDEX i (=?) sqlite> explain query plan select * from t where (a like '%a') = 0; QUERY PLAN `--SEARCH TABLE t USING COVERING INDEX i (=?) --- .../SQL/SQLExpression+QueryInterface.swift | 75 +++++++++---------- .../QueryInterfaceExpressionsTests.swift | 4 +- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift index df9f4582a..61be23e42 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift @@ -24,12 +24,11 @@ public struct _SQLExpressionNot: SQLExpression { public func _is(_ test: _SQLBooleanTest) -> SQLExpression { switch test { case .true: - return self + return _SQLExpressionEqual(.equal, self, true.sqlExpression) case .false: - // `(NOT expression) == 0` is equivalent to `expression` - return expression - + return _SQLExpressionEqual(.equal, self, false.sqlExpression) + case .falsey: // Support `NOT (NOT expression)` as a technique to build 0 or 1 return _SQLExpressionNot(self) @@ -103,17 +102,13 @@ struct SQLBinaryOperator: Hashable { /// The SQL for the negated operator, if any let negatedSQL: String? - /// Does the operator output a boolean value? - let isBoolean: Bool - /// Creates a binary operator /// - /// SQLBinaryOperator("-", isBoolean: false) - /// SQLBinaryOperator("LIKE", negated: "NOT LIKE", isBoolean: true) - init(_ sql: String, negated: String? = nil, isBoolean: Bool) { + /// SQLBinaryOperator("-") + /// SQLBinaryOperator("LIKE", negated: "NOT LIKE") + init(_ sql: String, negated: String? = nil) { self.sql = sql self.negatedSQL = negated - self.isBoolean = isBoolean } /// Returns the negated binary operator, if any @@ -124,32 +119,32 @@ struct SQLBinaryOperator: Hashable { guard let negatedSQL = negatedSQL else { return nil } - return SQLBinaryOperator(negatedSQL, negated: sql, isBoolean: isBoolean) + return SQLBinaryOperator(negatedSQL, negated: sql) } /// The `<` binary operator - static let lessThan = SQLBinaryOperator("<", isBoolean: true) + static let lessThan = SQLBinaryOperator("<") /// The `<=` binary operator - static let lessThanOrEqual = SQLBinaryOperator("<=", isBoolean: true) + static let lessThanOrEqual = SQLBinaryOperator("<=") /// The `>` binary operator - static let greaterThan = SQLBinaryOperator(">", isBoolean: true) + static let greaterThan = SQLBinaryOperator(">") /// The `>=` binary operator - static let greaterThanOrEqual = SQLBinaryOperator(">=", isBoolean: true) + static let greaterThanOrEqual = SQLBinaryOperator(">=") /// The `-` binary operator - static let subtract = SQLBinaryOperator("-", isBoolean: false) + static let subtract = SQLBinaryOperator("-") /// The `/` binary operator - static let divide = SQLBinaryOperator("/", isBoolean: false) + static let divide = SQLBinaryOperator("/") /// The `LIKE` binary operator - static let like = SQLBinaryOperator("LIKE", negated: "NOT LIKE", isBoolean: true) + static let like = SQLBinaryOperator("LIKE", negated: "NOT LIKE") /// The `MATCH` binary operator - static let match = SQLBinaryOperator("MATCH", isBoolean: true) + static let match = SQLBinaryOperator("MATCH") } /// _SQLExpressionBinary is an expression made of two expressions joined with a @@ -178,19 +173,11 @@ public struct _SQLExpressionBinary: SQLExpression { public func _is(_ test: _SQLBooleanTest) -> SQLExpression { switch test { case .true: - if op.isBoolean { - return self - } else { - return _SQLExpressionEqual(.equal, self, true.sqlExpression) - } + return _SQLExpressionEqual(.equal, self, true.sqlExpression) + case .false: - if let negatedOp = op.negated { - return _SQLExpressionBinary(negatedOp, lhs, rhs) - } else if op.isBoolean { - return _SQLExpressionNot(self) - } else { - return _SQLExpressionEqual(.equal, self, false.sqlExpression) - } + return _SQLExpressionEqual(.equal, self, false.sqlExpression) + case .falsey: if let negatedOp = op.negated { return _SQLExpressionBinary(negatedOp, lhs, rhs) @@ -408,8 +395,12 @@ public struct _SQLExpressionEqual: SQLExpression { public func _is(_ test: _SQLBooleanTest) -> SQLExpression { switch test { case .true: - return self - case .false, .falsey: + return _SQLExpressionEqual(.equal, self, true.sqlExpression) + + case .false: + return _SQLExpressionEqual(.equal, self, false.sqlExpression) + + case .falsey: return _SQLExpressionEqual(op.negated, lhs, rhs) } } @@ -449,8 +440,12 @@ public struct _SQLExpressionContains: SQLExpression { public func _is(_ test: _SQLBooleanTest) -> SQLExpression { switch test { case .true: - return self - case .false, .falsey: + return _SQLExpressionEqual(.equal, self, true.sqlExpression) + + case .false: + return _SQLExpressionEqual(.equal, self, false.sqlExpression) + + case .falsey: return _SQLExpressionContains(expression, collection, negated: !isNegated) } } @@ -495,8 +490,12 @@ public struct _SQLExpressionBetween: SQLExpression { public func _is(_ test: _SQLBooleanTest) -> SQLExpression { switch test { case .true: - return self - case .false, .falsey: + return _SQLExpressionEqual(.equal, self, true.sqlExpression) + + case .false: + return _SQLExpressionEqual(.equal, self, false.sqlExpression) + + case .falsey: return _SQLExpressionBetween(expression, lowerBound, upperBound, negated: !isNegated) } } diff --git a/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift b/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift index 10ccc1c35..e4e56d594 100644 --- a/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift +++ b/Tests/GRDBTests/QueryInterfaceExpressionsTests.swift @@ -1294,11 +1294,11 @@ class QueryInterfaceExpressionsTests: GRDBTestCase { XCTAssertEqual( sql(dbQueue, tableRequest.filter(Col.name.like("%foo") == true)), - "SELECT * FROM \"readers\" WHERE \"name\" LIKE '%foo'") + "SELECT * FROM \"readers\" WHERE (\"name\" LIKE '%foo') = 1") XCTAssertEqual( sql(dbQueue, tableRequest.filter(Col.name.like("%foo") == false)), - "SELECT * FROM \"readers\" WHERE \"name\" NOT LIKE '%foo'") + "SELECT * FROM \"readers\" WHERE (\"name\" LIKE '%foo') = 0") }