Skip to content

Commit

Permalink
CBL-6320: Test multi level array index - Low Level (#2163)
Browse files Browse the repository at this point in the history
* CBL-6320: Test multi level array index - Low Level

Added following tests:
- Deeply nested arrays: 10 levels deep with and without index.
- Triggers of unnest tables (for array indexes)
  * insertion triggers
  * deletion triggers
  * update triggers
- Edge case, missing property at the unnestPath.
  • Loading branch information
jianminzhao authored Oct 18, 2024
1 parent f5ac824 commit 87ce29b
Show file tree
Hide file tree
Showing 2 changed files with 457 additions and 17 deletions.
44 changes: 43 additions & 1 deletion C/tests/c4QueryTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ N_WAY_TEST_CASE_METHOD(NestedQueryTest, "C4Query Nested UNNEST", "[Query][C]") {
"Univ of Pennsylvania, student_112, 3, piano", "Univ of Pennsylvania, student_112, 3, swimming",
"Univ of Pennsylvania, student_189, 2, violin", "Univ of Pennsylvania, student_189, 2, movies"};

for ( int withIndex = 1; withIndex <= 1; ++withIndex ) {
for ( int withIndex = 0; withIndex <= 1; ++withIndex ) {
if ( withIndex ) {
C4Log("-------- Repeating with index --------");
C4IndexOptions indexOpts{};
Expand Down Expand Up @@ -985,6 +985,48 @@ N_WAY_TEST_CASE_METHOD(NestedQueryTest, "C4Query Nested UNNEST", "[Query][C]") {
}
}

N_WAY_TEST_CASE_METHOD(NestedQueryTest, "C4Query Nested UNNEST - Missing Array", "[Query][C]") {
deleteDatabase();
db = c4db_openNamed(kDatabaseName, &dbConfig(), ERROR_INFO());
std::stringstream documents{R"(
[
{"name": "Jonh Doe", "contacts": ["Contact1", "Contact2"]},
{"name": "Scott Tiger", "contacts": [] },
{"name": "Foo Bar" },
{"name": "Harry Potter", "contacts": "my contacts" }
]
)"};
importJSONFile(documents, c4db_getDefaultCollection(db, nullptr));
for ( int withIndex = 0; withIndex <= 1; ++withIndex ) {
if ( withIndex ) {
C4Log("-------- Repeating with index --------");
C4IndexOptions indexOpts{};
indexOpts.unnestPath = "contacts";
REQUIRE(c4db_createIndex2(db, C4STR("contacts"), C4STR(""), kC4N1QLQuery, kC4ArrayIndex, &indexOpts,
nullptr));
}

const char* queryStr = "SELECT doc.name, contact FROM _default AS doc UNNEST doc.contacts AS contact";

// For the above query, server returns
// results {
// "Jonh Doe, Contact1",
// "Jonh Doe, Contact2"
// }
// But we return the following. The third row comes from the 4th document, of which
// property "contacts" is a scalar, "my contacts". We treat it as an array of one element.
// Pasin has found the responsible code. In SQLiteFleeceEach.cc, line 251, if we change it 1 to 0,
// we will get the same result as what the server get.
vector<string> results{"Jonh Doe, Contact1", "Jonh Doe, Contact2", "Harry Potter, my contacts"};

C4Error error{};
c4query_release(query);
query = c4query_new2(db, kC4N1QLQuery, c4str(queryStr), nullptr, ERROR_INFO(error));
REQUIRE(query);
CHECK(run2(nullptr, 2) == results);
}
}

N_WAY_TEST_CASE_METHOD(C4QueryTest, "C4Query Seek", "[Query][C]") {
compile(json5("['=', ['.', 'contact', 'address', 'state'], 'CA']"));
C4Error error;
Expand Down
Loading

0 comments on commit 87ce29b

Please sign in to comment.