@@ -243,11 +243,7 @@ private SQLitePersistence.Query generateQuery(
243
243
// The number of total statements we union together. This is similar to a distributed normal
244
244
// form, but adapted for array values. We create a single statement per value in an
245
245
// ARRAY_CONTAINS or ARRAY_CONTAINS_ANY filter combined with the values from the query bounds.
246
- int statementCount =
247
- max (arrayValues .size (), 1 )
248
- * lowerBounds .length
249
- * (upperBounds == null ? 1 : upperBounds .length );
250
- // The number of "question marks" per single statement
246
+ int statementCount = max (arrayValues .size (), 1 ) * lowerBounds .length ;
251
247
int bindsPerStatement = 2 + (arrayValues .isEmpty () ? 0 : 1 ) + (upperBounds != null ? 1 : 0 );
252
248
Object [] bindArgs = new Object [statementCount * bindsPerStatement ];
253
249
@@ -256,7 +252,9 @@ private SQLitePersistence.Query generateQuery(
256
252
StringBuilder statement = new StringBuilder ();
257
253
statement .append (
258
254
"SELECT document_name, directional_value FROM index_entries WHERE index_id = ? " );
259
- statement .append (arrayValues .isEmpty () ? "AND array_value IS NULL " : "AND array_value = ? " );
255
+ if (!arrayValues .isEmpty ()) {
256
+ statement .append ("AND array_value = ? " );
257
+ }
260
258
statement .append ("AND directional_value " ).append (lowerBoundOp ).append (" ? " );
261
259
if (upperBounds != null ) {
262
260
statement .append ("AND directional_value " ).append (upperBoundOp ).append (" ? " );
@@ -289,17 +287,18 @@ private int fillBounds(
289
287
@ Nullable Object arrayValue ,
290
288
Object [] lowerBounds ,
291
289
@ Nullable Object [] upperBounds ) {
290
+ hardAssert (
291
+ upperBounds == null || upperBounds .length == lowerBounds .length ,
292
+ "Length of upper and lower bound should match" );
292
293
// Add bind variables for each combination of arrayValue, lowerBound and upperBound.
293
- for (Object lower : lowerBounds ) {
294
- for (int i = 0 ; i < (upperBounds != null ? upperBounds .length : 1 ); ++i ) {
295
- bindArgs [offset ++] = indexId ;
296
- if (arrayValue != null ) {
297
- bindArgs [offset ++] = arrayValue ;
298
- }
299
- bindArgs [offset ++] = lower ;
300
- if (upperBounds != null ) {
301
- bindArgs [offset ++] = upperBounds [i ];
302
- }
294
+ for (int i = 0 ; i < lowerBounds .length ; ++i ) {
295
+ bindArgs [offset ++] = indexId ;
296
+ if (arrayValue != null ) {
297
+ bindArgs [offset ++] = arrayValue ;
298
+ }
299
+ bindArgs [offset ++] = lowerBounds [i ];
300
+ if (upperBounds != null ) {
301
+ bindArgs [offset ++] = upperBounds [i ];
303
302
}
304
303
}
305
304
return offset ;
0 commit comments