-
Notifications
You must be signed in to change notification settings - Fork 615
Testing composite index queries against production #5352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ed2cdb5
add composite index test helper
milaGGL be22ebb
add terrafrom to CI
milaGGL e596678
format
milaGGL 2d6a1d7
Update ci_tests.yml
milaGGL 908ad73
run terrafrom only for firestore tests
milaGGL 307d634
Update ci_tests.yml
milaGGL b6ce791
Update CompositeIndexTestHelper.java
milaGGL 1d5ff95
Merge branch 'master' into mila/composite-index-testing
milaGGL 168c450
Merge branch 'master' into mila/composite-index-testing
milaGGL 2562401
keep consistent with Web
milaGGL 9f71b5c
Merge branch 'master' into mila/composite-index-testing
milaGGL 4f39e2d
Merge branch 'master' into mila/composite-index-testing
milaGGL b2e8188
add test class comments
milaGGL a8b167b
Merge branch 'master' into mila/composite-index-testing
milaGGL 504ffd7
Merge branch 'master' into mila/composite-index-testing
milaGGL 57c0ab4
add extra test helper, move terraform into firestore folder
milaGGL 539f548
remove pwd
milaGGL 61c69b3
rename getDoc and getDocs function
milaGGL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
locals { | ||
indexes = { | ||
index1 = [ | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "a" | ||
order = "ASCENDING" | ||
}, | ||
] | ||
index2 = [ | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "b" | ||
order = "ASCENDING" | ||
}, | ||
] | ||
index3 = [ | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "b" | ||
order = "DESCENDING" | ||
}, | ||
] | ||
index4 = [ | ||
{ | ||
field_path = "a" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "b" | ||
order = "ASCENDING" | ||
}, | ||
] | ||
index5 = [ | ||
{ | ||
field_path = "a" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "b" | ||
order = "DESCENDING" | ||
}, | ||
] | ||
index6 = [ | ||
{ | ||
field_path = "a" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "a" | ||
order = "DESCENDING" | ||
}, | ||
] | ||
index7 = [ | ||
{ | ||
field_path = "b" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "a" | ||
order = "ASCENDING" | ||
}, | ||
] | ||
index8 = [ | ||
{ | ||
field_path = "b" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "testId" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "a" | ||
order = "DESCENDING" | ||
}, | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
variable "project_info" {} | ||
|
||
provider "google" { | ||
project = var.project_info.project_id | ||
} | ||
|
||
resource "google_firestore_index" "default-db-index" { | ||
collection = "composite-index-test-collection" | ||
|
||
for_each = local.indexes | ||
dynamic "fields" { | ||
for_each = distinct(flatten([for k, v in local.indexes : [ | ||
for i in each.value : { | ||
field_path = i.field_path | ||
order = i.order | ||
}]])) | ||
content { | ||
field_path = lookup(fields.value, "field_path", null) | ||
order = lookup(fields.value, "order", null) | ||
} | ||
} | ||
|
||
} | ||
|
||
resource "google_firestore_index" "named-db-index" { | ||
collection = "composite-index-test-collection" | ||
database = "test-db" | ||
|
||
for_each = local.indexes | ||
dynamic "fields" { | ||
for_each = distinct(flatten([for k, v in local.indexes : [ | ||
for i in each.value : { | ||
field_path = i.field_path | ||
order = i.order | ||
}]])) | ||
content { | ||
field_path = lookup(fields.value, "field_path", null) | ||
order = lookup(fields.value, "order", null) | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...firestore/src/androidTest/java/com/google/firebase/firestore/CompositeIndexQueryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.firebase.firestore; | ||
|
||
import static com.google.firebase.firestore.Filter.equalTo; | ||
import static com.google.firebase.firestore.Filter.greaterThan; | ||
import static com.google.firebase.firestore.Filter.or; | ||
import static com.google.firebase.firestore.testutil.TestUtil.map; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.google.firebase.firestore.testutil.CompositeIndexTestHelper; | ||
import com.google.firebase.firestore.testutil.IntegrationTestUtil; | ||
import java.util.Map; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/* | ||
* Guidance for Creating Tests: | ||
* ---------------------------- | ||
* When creating tests that require composite indexes, it is recommended to utilize the | ||
* "CompositeIndexTestHelper" class. This utility class provides methods for creating | ||
* and setting test documents and running queries with ease, ensuring proper data | ||
* isolation and query construction. | ||
* | ||
* Please remember to update the main index configuration file (firestore_index_config.tf) | ||
* with any new composite indexes needed for the tests. This ensures synchronization with | ||
* other testing environments, including CI. You can generate the required index link by | ||
* clicking on the Firebase console link in the error message while running tests locally. | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
milaGGL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class CompositeIndexQueryTest { | ||
|
||
@After | ||
public void tearDown() { | ||
IntegrationTestUtil.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testOrQueriesWithCompositeIndexes() { | ||
CompositeIndexTestHelper testHelper = new CompositeIndexTestHelper(); | ||
Map<String, Map<String, Object>> testDocs = | ||
map( | ||
"doc1", map("a", 1, "b", 0), | ||
"doc2", map("a", 2, "b", 1), | ||
"doc3", map("a", 3, "b", 2), | ||
"doc4", map("a", 1, "b", 3), | ||
"doc5", map("a", 1, "b", 1)); | ||
CollectionReference collection = testHelper.withTestDocs(testDocs); | ||
|
||
Query query = collection.where(or(greaterThan("a", 2), equalTo("b", 1))); | ||
// with one inequality: a>2 || b==1. | ||
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc5", "doc2", "doc3"); | ||
|
||
// Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2 | ||
query = collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limit(2); | ||
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc1", "doc2"); | ||
|
||
// Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2 | ||
// Note: The public query API does not allow implicit ordering when limitToLast is used. | ||
query = collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limitToLast(2).orderBy("b"); | ||
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc3", "doc4"); | ||
|
||
// Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1 | ||
query = collection.where(or(equalTo("a", 2), equalTo("b", 1))).limit(1).orderBy("a"); | ||
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc5"); | ||
|
||
// Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1 | ||
query = collection.where(or(equalTo("a", 2), equalTo("b", 1))).limitToLast(1).orderBy("a"); | ||
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc2"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.