Skip to content

Commit e3bfade

Browse files
authored
Update LiteCore to 3.3.0-112 (#3426)
* CBL-7196: Predictive Queries test failing when running with CBL 3.3.0 * CBL-7200: LITECORE_VERSION_STRING should be set to 0.0.0 in Project.xcconfig * CBL-7195: Upgrade SQLite to the latest release, 3.50.3 * SQLite 3.50.3 (since v3.48.0) improved EXPLAIN QUERY PLAN output for covering indexes. When a covering index is used - meaning the query can retrieve all required data (including output columns) directly from the index without accessing the underlying table - the plan now shows USING COVERING INDEX instead of USING INDEX. Some predictive query tests were previously checking only for USING INDEX and need to be updated. Since the tests do not differentiate between covering and non-covering indexes, added a helper function to check for either form to simplify the assertions in tests.
1 parent cff250d commit e3bfade

File tree

10 files changed

+86
-75
lines changed

10 files changed

+86
-75
lines changed

Objective-C/Tests/CBLTestCase.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,13 @@ extern const NSTimeInterval kExpTimeout;
204204
randomAccess: (BOOL)randomAccess
205205
test: (void (^)(uint64_t n, CBLQueryResult *result))block;
206206

207+
/** Checks whether the explain plan contains either USING INDEX or USING COVERING INDEX for the given index name. */
208+
- (BOOL) isUsingIndexNamed: (NSString*)indexName forQuery: (CBLQuery*)query;
209+
207210
- (NSString*) getRickAndMortyJSON;
208211

209212
/**
210-
/// This expectation will allow overfill expectation.
213+
This expectation will allow overfill expectation.
211214
CBL-2363: Replicator might send extra idle status when its being stopped, which is not a bug
212215
*/
213216
- (XCTestExpectation*) allowOverfillExpectationWithDescription:(NSString *)description;

Objective-C/Tests/CBLTestCase.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,19 @@ - (uint64_t) verifyQuery: (CBLQuery*)q
453453
return n;
454454
}
455455

456+
- (BOOL) isUsingIndexNamed: (NSString*)indexName forQuery: (CBLQuery*)query {
457+
NSError* error;
458+
NSString* plan = [query explain: &error];
459+
AssertNil(error);
460+
AssertNotNil(plan);
461+
462+
NSString *usingIndex = [NSString stringWithFormat:@"USING INDEX %@", indexName];
463+
NSString *usingCoveringIndex = [NSString stringWithFormat:@"USING COVERING INDEX %@", indexName];
464+
465+
return ([plan rangeOfString:usingIndex].location != NSNotFound ||
466+
[plan rangeOfString:usingCoveringIndex].location != NSNotFound);
467+
}
468+
456469
- (BOOL) isProfiling {
457470
return NSProcessInfo.processInfo.environment[@"PROFILING"] != nil;
458471
}

Objective-C/Tests/DatabaseTest.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,17 +2038,16 @@ - (void) testPerformMaintenanceReindex {
20382038
CBLQuery* q = [CBLQueryBuilder select: @[[CBLQuerySelectResult expression: key]]
20392039
from: [CBLQueryDataSource database: self.db]
20402040
where: [key greaterThan: [CBLQueryExpression integer: 9]]];
2041-
NSString* explain = [q explain: &error];
2042-
Assert([explain rangeOfString: @"USING INDEX KeyIndex"].location != NSNotFound);
2041+
2042+
Assert([self isUsingIndexNamed: @"KeyIndex" forQuery: q]);
20432043

20442044
// Reindex:
20452045
Assert([_db performMaintenance: kCBLMaintenanceTypeReindex error: &error],
20462046
@"Error when reindexing the database: %@", error);
20472047

20482048
// Check if the index is still there and used:
20492049
AssertEqual(self.db.indexes.count, 1u);
2050-
explain = [q explain: &error];
2051-
Assert([explain rangeOfString: @"USING INDEX KeyIndex"].location != NSNotFound);
2050+
Assert([self isUsingIndexNamed: @"KeyIndex" forQuery: q]);
20522051
}
20532052

20542053
- (void) testPerformMaintenanceIntegrityCheck {

Objective-C/Tests/PartialIndexTest.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,11 @@ - (void) testCreatePartialValueIndex {
6161
NSString* sql = @"SELECT * FROM _ WHERE type = 'number' AND num > 1000";
6262
CBLQuery* q = [_db createQuery: sql error: &error];
6363
AssertNotNil(q);
64-
NSString* explain = [q explain: &error];
65-
Assert([explain rangeOfString: @"USING INDEX numIndex"].location != NSNotFound);
64+
Assert([self isUsingIndexNamed: @"numIndex" forQuery: q]);
6665

6766
sql = @"SELECT * FROM _ WHERE type = 'foo' AND num > 1000";
6867
q = [_db createQuery: sql error: &error];
69-
explain = [q explain: &error];
70-
AssertFalse([explain rangeOfString: @"USING INDEX numIndex"].location != NSNotFound);
68+
AssertFalse([self isUsingIndexNamed: @"numIndex" forQuery: q]);
7169
}
7270

7371
/**

Objective-C/Tests/PredictiveQueryTest.m

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ - (void) testIndexPredictionValueUsingValueIndex {
538538
from: kDATA_SRC_DB
539539
where: [sumPrediction equalTo: EXPR_VAL(@15)]];
540540

541-
Assert([[q explain: nil] rangeOfString: @"USING INDEX SumIndex"].location != NSNotFound);
541+
Assert([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
542542

543543
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
544544
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -577,9 +577,9 @@ - (void) testIndexMultiplePredictionValuesUsingValueIndex {
577577
from: kDATA_SRC_DB
578578
where: [[sumPrediction lessThanOrEqualTo: EXPR_VAL(@15)] orExpression:
579579
[avgPrediction equalTo: EXPR_VAL(@8)]]];
580-
NSString* explain = [q explain: nil];
581-
Assert([explain rangeOfString: @"USING INDEX SumIndex"].location != NSNotFound);
582-
Assert([explain rangeOfString: @"USING INDEX AvgIndex"].location != NSNotFound);
580+
581+
Assert([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
582+
Assert([self isUsingIndexNamed: @"AvgIndex" forQuery: q]);
583583

584584
int64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
585585
Assert([r integerAtIndex: 0] == 15 || [r integerAtIndex: 1] == 8);
@@ -611,7 +611,7 @@ - (void) testIndexCompoundPredictiveValuesUsingValueIndex {
611611
where: [[sumPrediction equalTo: EXPR_VAL(@15)] andExpression:
612612
[avgPrediction equalTo: EXPR_VAL(@3)]]];
613613

614-
Assert([[q explain: nil] rangeOfString: @"USING INDEX SumAvgIndex"].location != NSNotFound);
614+
Assert([self isUsingIndexNamed: @"SumAvgIndex" forQuery: q]);
615615

616616
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
617617
AssertEqual([r integerAtIndex: 0], 15);
@@ -644,7 +644,7 @@ - (void) testIndexPredictionResultUsingPredictiveIndex {
644644
from: kDATA_SRC_DB
645645
where: [sumPrediction equalTo: EXPR_VAL(@15)]];
646646

647-
Assert([[q explain: nil] rangeOfString: @"USING INDEX AggCache"].location == NSNotFound);
647+
AssertFalse([self isUsingIndexNamed: @"AggCache" forQuery: q]);
648648

649649
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
650650
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -677,12 +677,12 @@ - (void) testIndexPredictionValueUsingPredictiveIndex {
677677
properties: @[@"sum"]];
678678
Assert([self.db createIndex: index withName: @"SumIndex" error: &error]);
679679

680-
CBLQuery *q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers"),
680+
CBLQuery* q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers"),
681681
SEL_EXPR(sumPrediction)]
682682
from: kDATA_SRC_DB
683683
where: [sumPrediction equalTo: EXPR_VAL(@15)]];
684684

685-
Assert([[q explain: nil] rangeOfString: @"USING INDEX SumIndex"].location != NSNotFound);
685+
Assert([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
686686

687687
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
688688
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -725,9 +725,9 @@ - (void) testIndexMuliplePredictionValuesUsingPredictiveIndex {
725725
from: kDATA_SRC_DB
726726
where: [[sumPrediction lessThanOrEqualTo: EXPR_VAL(@15)] orExpression:
727727
[avgPrediction equalTo: EXPR_VAL(@8)]]];
728-
NSString* explain = [q explain: nil];
729-
Assert([explain rangeOfString: @"USING INDEX SumIndex"].location != NSNotFound);
730-
Assert([explain rangeOfString: @"USING INDEX AvgIndex"].location != NSNotFound);
728+
729+
Assert([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
730+
Assert([self isUsingIndexNamed: @"AvgIndex" forQuery: q]);
731731

732732
int64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
733733
Assert([r integerAtIndex: 0] == 15 || [r integerAtIndex: 1] == 8);
@@ -762,7 +762,7 @@ - (void) testIndexCompoundPredictionValuesUsingPredictiveIndex {
762762
where: [[sumPrediction equalTo: EXPR_VAL(@15)] andExpression:
763763
[avgPrediction equalTo: EXPR_VAL(@3)]]];
764764

765-
Assert([[q explain: nil] rangeOfString: @"USING INDEX SumAvgIndex"].location != NSNotFound);
765+
Assert([self isUsingIndexNamed: @"SumAvgIndex" forQuery: q]);
766766

767767
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
768768
AssertEqual([r integerAtIndex: 0], 15);
@@ -795,7 +795,8 @@ - (void) testDeletePredictiveIndex {
795795
CBLQuery *q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers")]
796796
from: kDATA_SRC_DB
797797
where: [sumPrediction equalTo: EXPR_VAL(@15)]];
798-
Assert([[q explain: nil] rangeOfString: @"USING INDEX SumIndex"].location != NSNotFound);
798+
799+
Assert([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
799800

800801
// Query with index:
801802
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
@@ -813,7 +814,8 @@ - (void) testDeletePredictiveIndex {
813814
q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers")]
814815
from: kDATA_SRC_DB
815816
where: [sumPrediction equalTo: EXPR_VAL(@15)]];
816-
Assert([[q explain: nil] rangeOfString: @"USING INDEX SumIndex"].location == NSNotFound);
817+
818+
AssertFalse([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
817819

818820
numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
819821
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -861,9 +863,9 @@ - (void) testDeletePredictiveIndexesSharingSameCacheTable {
861863
from: kDATA_SRC_DB
862864
where: [[sumPrediction lessThanOrEqualTo: EXPR_VAL(@15)] orExpression:
863865
[avgPrediction equalTo: EXPR_VAL(@8)]]];
864-
NSString* explain = [q explain: nil];
865-
Assert([explain rangeOfString: @"USING INDEX SumIndex"].location != NSNotFound);
866-
Assert([explain rangeOfString: @"USING INDEX AvgIndex"].location != NSNotFound);
866+
867+
Assert([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
868+
Assert([self isUsingIndexNamed: @"AvgIndex" forQuery: q]);
867869

868870
uint64_t numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
869871
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -881,8 +883,8 @@ - (void) testDeletePredictiveIndexesSharingSameCacheTable {
881883
q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers")]
882884
from: kDATA_SRC_DB
883885
where: [sumPrediction equalTo: EXPR_VAL(@15)]];
884-
explain = [q explain: nil];
885-
Assert([explain rangeOfString: @"USING INDEX SumIndex"].location == NSNotFound);
886+
887+
AssertFalse([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
886888

887889
numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
888890
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -894,8 +896,8 @@ - (void) testDeletePredictiveIndexesSharingSameCacheTable {
894896
q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers")]
895897
from: kDATA_SRC_DB
896898
where: [avgPrediction equalTo: EXPR_VAL(@8)]];
897-
explain = [q explain: nil];
898-
Assert([explain rangeOfString: @"USING INDEX AvgIndex"].location != NSNotFound);
899+
900+
Assert([self isUsingIndexNamed: @"AvgIndex" forQuery: q]);
899901

900902
numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
901903
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -911,8 +913,8 @@ - (void) testDeletePredictiveIndexesSharingSameCacheTable {
911913
q = [CBLQueryBuilder select: @[SEL_PROP(@"numbers")]
912914
from: kDATA_SRC_DB
913915
where: [avgPrediction equalTo: EXPR_VAL(@8)]];
914-
explain = [q explain: nil];
915-
Assert([explain rangeOfString: @"USING INDEX AvgIndex"].location == NSNotFound);
916+
917+
AssertFalse([self isUsingIndexNamed: @"AvgIndex" forQuery: q]);
916918

917919
numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
918920
NSArray* numbers = [[r arrayAtIndex:0] toArray];
@@ -929,9 +931,10 @@ - (void) testDeletePredictiveIndexesSharingSameCacheTable {
929931
from: kDATA_SRC_DB
930932
where: [[sumPrediction lessThanOrEqualTo: EXPR_VAL(@15)] orExpression:
931933
[avgPrediction equalTo: EXPR_VAL(@8)]]];
932-
explain = [q explain: nil];
933-
Assert([explain rangeOfString: @"USING INDEX SumIndex"].location == NSNotFound);
934-
Assert([explain rangeOfString: @"USING INDEX AvgIndex"].location == NSNotFound);
934+
935+
AssertFalse([self isUsingIndexNamed: @"SumIndex" forQuery: q]);
936+
AssertFalse([self isUsingIndexNamed: @"AvgIndex" forQuery: q]);
937+
935938
numRows = [self verifyQuery: q randomAccess: NO test: ^(uint64_t n, CBLQueryResult *r) {
936939
NSArray* numbers = [[r arrayAtIndex:0] toArray];
937940
Assert(numbers.count > 0);

Swift/Tests/CBLTestCase.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ class CBLTestCase: XCTestCase {
334334
return n
335335
}
336336

337+
func isUsingIndex(named indexName: String, for query: Query) throws -> Bool {
338+
let plan = try query.explain()
339+
let usingIndex = "USING INDEX \(indexName)"
340+
let usingCoveringIndex = "USING COVERING INDEX \(indexName)"
341+
return plan.contains(usingIndex) || plan.contains(usingCoveringIndex)
342+
}
343+
337344
func getRickAndMortyJSON() throws -> String {
338345
var content = "Earth(C-137)".data(using: .utf8)!
339346
var blob = Blob(contentType: "text/plain", data: content)

Swift/Tests/DatabaseTest.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,14 @@ class DatabaseTest: CBLTestCase {
177177
.from(DataSource.collection(defaultCollection!))
178178
.where(key.greaterThan(Expression.int(9)))
179179

180-
var explain = try q.explain() as NSString
181-
XCTAssertNotEqual(explain.range(of: "USING INDEX KeyIndex").location, NSNotFound)
180+
XCTAssert(try isUsingIndex(named: "KeyIndex", for: q))
182181

183182
// Reindex:
184183
try db.performMaintenance(type: .reindex)
185184

186185
// Check if the index is still there and used:
187186
XCTAssertEqual(try defaultCollection!.indexes().count, 1)
188-
explain = try q.explain() as NSString
189-
XCTAssertNotEqual(explain.range(of: "USING INDEX KeyIndex").location, NSNotFound)
187+
XCTAssert(try isUsingIndex(named: "KeyIndex", for: q))
190188
}
191189

192190
func testPerformMaintenanceIntegrityCheck() throws {

Swift/Tests/PartialIndexTest.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ class PartialIndexTest: CBLTestCase {
5050

5151
var sql = "SELECT * FROM _ WHERE type = 'number' AND num > 1000"
5252
var q = try db.createQuery(sql)
53-
var explain = try q.explain()
54-
XCTAssert(explain.contains("USING INDEX numIndex"))
53+
XCTAssert(try isUsingIndex(named: "numIndex", for: q))
5554

5655
sql = "SELECT * FROM _ WHERE type = 'foo' AND num > 1000"
5756
q = try db.createQuery(sql)
58-
explain = try q.explain()
59-
XCTAssertFalse(explain.contains("USING INDEX numIndex"))
57+
XCTAssertFalse(try isUsingIndex(named: "numIndex", for: q))
6058
}
6159

6260
/// 2. TestCreatePartialFullTextIndex

0 commit comments

Comments
 (0)