Skip to content

Commit

Permalink
addressing review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jianminzhao committed Jan 30, 2025
1 parent a083d7e commit 45b23cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
30 changes: 23 additions & 7 deletions C/tests/c4QueryTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,22 @@ N_WAY_TEST_CASE_METHOD(C4QueryTest, "C4Query FTS (Partial)", "[Query][C][FTS]")
// c.f. above test, "C4Query FTS". The query is the same.
// W/o "where" in options, there are five contacts whose street addresses contain "Hwy".

bool useJSON = GENERATE(false, true);

C4IndexOptions options{};
// With the following "where" option, the query picks 2 Californians.
options.where = "contact.address.state = 'CA'";
REQUIRE(c4coll_createIndex(defaultColl, C4STR("byStreet"), C4STR("contact.address.street"), kC4N1QLQuery,
kC4FullTextIndex, &options, WITH_ERROR(&err)));
if ( useJSON ) {
options.where = R"(["=", [".contact.address.state"], "CA"])";
REQUIRE(c4coll_createIndex(defaultColl, C4STR("byStreet"), C4STR("[[\".contact.address.street\"]]"),
kC4JSONQuery, kC4FullTextIndex, &options, WITH_ERROR(&err)));
compile(json5("['MATCH()', 'byStreet', 'Hwy']"));
} else {
options.where = "contact.address.state = 'CA'";
REQUIRE(c4coll_createIndex(defaultColl, C4STR("byStreet"), C4STR("contact.address.street"), kC4N1QLQuery,
kC4FullTextIndex, &options, WITH_ERROR(&err)));
compileSelect("SELECT META().id FROM _ WHERE MATCH(byStreet, 'Hwy')", kC4N1QLQuery);
}

compileSelect("SELECT META().id FROM _ WHERE MATCH(byStreet, 'Hwy')", kC4N1QLQuery);
auto results = runFTS();
CHECK(results == (vector<vector<C4FullTextMatch>>{{{15, 0, 0, 11, 3}}, {{43, 0, 0, 12, 3}}}));

Expand All @@ -498,9 +507,16 @@ N_WAY_TEST_CASE_METHOD(C4QueryTest, "C4Query FTS (Partial)", "[Query][C][FTS]")

// By creating the index with different options.where, the original index will be deleted,
// and the index table will be based soly on the new where clause. We get one Texan.
options.where = "contact.address.state = 'TX'";
REQUIRE(c4coll_createIndex(defaultColl, C4STR("byStreet"), C4STR("contact.address.street"), kC4N1QLQuery,
kC4FullTextIndex, &options, WITH_ERROR(&err)));
if ( useJSON ) {
options.where = R"(["=", [".contact.address.state"], "TX"])";
REQUIRE(c4coll_createIndex(defaultColl, C4STR("byStreet"), C4STR("[[\".contact.address.street\"]]"),
kC4JSONQuery, kC4FullTextIndex, &options, WITH_ERROR(&err)));
} else {
options.where = "contact.address.state = 'TX'";
REQUIRE(c4coll_createIndex(defaultColl, C4STR("byStreet"), C4STR("contact.address.street"), kC4N1QLQuery,
kC4FullTextIndex, &options, WITH_ERROR(&err)));
}

results = runFTS();
CHECK(results == (vector<vector<C4FullTextMatch>>{{{44, 0, 0, 12, 3}}}));
}
Expand Down
2 changes: 2 additions & 0 deletions LiteCore/Query/IndexSpec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ namespace litecore {

void setWhereClause(string_view whereClause_) const {
if ( !whereClause_.empty() ) whereClause = alloc_slice::nullPaddedString(whereClause_);
else
whereClause.reset();
}

/** The nested unnestPath from arrayOptions, as separated by "[]." is turned to an array. */
Expand Down
8 changes: 2 additions & 6 deletions LiteCore/Query/SQLiteDataFile+Indexes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,11 @@ namespace litecore {
if ( spec.type == IndexSpec::kFullText ) {
if ( same ) {
auto whatA = spec.what(), whatB = existingSpec->what();
if ( !whatA ? whatB != nullptr : !whatB ) same = false;
else if ( whatA )
same = whatA->toJSONString() == whatB->toJSONString();
same = whatA ? whatA->isEqual(whatB) : !whatB;
}
if ( same ) {
auto whereA = spec.where(), whereB = existingSpec->where();
if ( !whereA ? whereB != nullptr : !whereB ) same = false;
else if ( whereA )
same = whereA->toJSONString() == whereB->toJSONString();
same = whereA ? whereA->isEqual(whereB) : !whereB;
}
}
break;
Expand Down

0 comments on commit 45b23cb

Please sign in to comment.