|
23 | 23 | import static com.google.firebase.firestore.testutil.TestUtil.query;
|
24 | 24 | import static org.junit.Assert.assertFalse;
|
25 | 25 | import static org.junit.Assert.assertTrue;
|
26 |
| -import static org.junit.Assume.assumeTrue; |
27 | 26 |
|
28 | 27 | import com.google.firebase.firestore.core.Query;
|
| 28 | +import com.google.firebase.firestore.core.Target; |
29 | 29 | import java.util.Arrays;
|
30 | 30 | import java.util.Collections;
|
31 | 31 | import java.util.List;
|
@@ -58,6 +58,14 @@ public class TargetIndexMatcherTest {
|
58 | 58 | query("collId")
|
59 | 59 | .filter(filter("a", "array-contains-any", Collections.singletonList("a"))));
|
60 | 60 |
|
| 61 | + List<Query> queriesWithOrderBy = |
| 62 | + Arrays.asList( |
| 63 | + query("collId").orderBy(orderBy("a")), |
| 64 | + query("collId").orderBy(orderBy("a", "desc")), |
| 65 | + query("collId").orderBy(orderBy("a", "asc")), |
| 66 | + query("collId").orderBy(orderBy("a")).orderBy(orderBy("__name__")), |
| 67 | + query("collId").filter(filter("a", "array-contains", "a")).orderBy(orderBy("b"))); |
| 68 | + |
61 | 69 | @Test
|
62 | 70 | public void canUseMergeJoin() {
|
63 | 71 | Query q = query("collId").filter(filter("a", "==", 1)).filter(filter("b", "==", 2));
|
@@ -441,164 +449,6 @@ public void withMultipleNotIn() {
|
441 | 449 | validateServesTarget(q, "a", FieldIndex.Segment.Kind.ASCENDING);
|
442 | 450 | }
|
443 | 451 |
|
444 |
| - @Test |
445 |
| - public void buildTargetIndex() { |
446 |
| - Query q = |
447 |
| - query("collId") |
448 |
| - .filter(filter("a", "==", 1)) |
449 |
| - .filter(filter("b", "==", 2)) |
450 |
| - .orderBy(orderBy("__name__", "desc")); |
451 |
| - TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
452 |
| - FieldIndex expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
453 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
454 |
| - |
455 |
| - q = query("collId").orderBy(orderBy("a")); |
456 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
457 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
458 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
459 |
| - |
460 |
| - q = query("collId").orderBy(orderBy("a")).orderBy(orderBy("b")); |
461 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
462 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
463 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
464 |
| - |
465 |
| - q = query("collId").filter(filter("a", "array-contains", "a")).orderBy(orderBy("b")); |
466 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
467 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
468 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
469 |
| - |
470 |
| - q = query("collId").orderBy(orderBy("b")); |
471 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
472 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
473 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
474 |
| - |
475 |
| - q = query("collId").orderBy(orderBy("a")); |
476 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
477 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
478 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
479 |
| - |
480 |
| - q = query("collId").filter(filter("a", ">", 1)).filter(filter("a", "<", 10)); |
481 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
482 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
483 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
484 |
| - |
485 |
| - q = query("collId").filter(filter("a", "in", Arrays.asList(1, 2))).filter(filter("b", "==", 5)); |
486 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
487 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
488 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
489 |
| - |
490 |
| - q = query("collId").filter(filter("value", "array-contains", "foo")).orderBy(orderBy("value")); |
491 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
492 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
493 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
494 |
| - |
495 |
| - q = |
496 |
| - query("collId") |
497 |
| - .filter(filter("a", "array-contains", "a")) |
498 |
| - .filter(filter("a", ">", "b")) |
499 |
| - .orderBy(orderBy("a", "asc")); |
500 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
501 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
502 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
503 |
| - |
504 |
| - q = query("collId").filter(filter("a", "==", 1)).orderBy(orderBy("__name__", "desc")); |
505 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
506 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
507 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
508 |
| - |
509 |
| - q = query("collId").filter(filter("a1", "==", "a")).filter(filter("a2", "==", "b")); |
510 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
511 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
512 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
513 |
| - |
514 |
| - q = |
515 |
| - query("collId") |
516 |
| - .filter(filter("equality1", "==", "a")) |
517 |
| - .filter(filter("equality2", "==", "b")) |
518 |
| - .filter(filter("inequality", ">=", "c")); |
519 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
520 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
521 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
522 |
| - |
523 |
| - q = |
524 |
| - query("collId") |
525 |
| - .filter(filter("equality1", "==", "a")) |
526 |
| - .filter(filter("inequality", ">=", "c")) |
527 |
| - .filter(filter("equality2", "==", "b")); |
528 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
529 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
530 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
531 |
| - |
532 |
| - q = query("collId").orderBy(orderBy("a")); |
533 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
534 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
535 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
536 |
| - |
537 |
| - q = query("collId").orderBy(orderBy("a", "desc")); |
538 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
539 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
540 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
541 |
| - |
542 |
| - q = query("collId").orderBy(orderBy("a")).orderBy(orderBy("__name__")); |
543 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
544 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
545 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
546 |
| - |
547 |
| - q = query("collId").filter(filter("a", "!=", 1)); |
548 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
549 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
550 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
551 |
| - |
552 |
| - q = query("collId").filter(filter("a", "!=", 1)).orderBy(orderBy("a")).orderBy(orderBy("b")); |
553 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
554 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
555 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
556 |
| - |
557 |
| - q = query("collId").filter(filter("a", "==", "a")).filter(filter("b", ">", "b")); |
558 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
559 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
560 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
561 |
| - |
562 |
| - q = |
563 |
| - query("collId") |
564 |
| - .filter(filter("a1", "==", "a")) |
565 |
| - .filter(filter("a2", ">", "b")) |
566 |
| - .orderBy(orderBy("a2", "asc")); |
567 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
568 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
569 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
570 |
| - |
571 |
| - q = |
572 |
| - query("collId") |
573 |
| - .filter(filter("a", ">=", 1)) |
574 |
| - .filter(filter("a", "==", 5)) |
575 |
| - .filter(filter("a", "<=", 10)); |
576 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
577 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
578 |
| - // assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
579 |
| - |
580 |
| - q = |
581 |
| - query("collId") |
582 |
| - .filter(filter("a", "not-in", Arrays.asList(1, 2, 3))) |
583 |
| - .filter(filter("a", ">=", 2)); |
584 |
| - targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
585 |
| - expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
586 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
587 |
| - } |
588 |
| - |
589 |
| - @Test |
590 |
| - public void failedTest() { |
591 |
| - assumeTrue("Skip this test due to a bug in CSI.", false); |
592 |
| - Query q = |
593 |
| - query("collId") |
594 |
| - .filter(filter("a", ">=", 1)) |
595 |
| - .filter(filter("a", "==", 5)) |
596 |
| - .filter(filter("a", "<=", 10)); |
597 |
| - TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(q.toTarget()); |
598 |
| - FieldIndex expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
599 |
| - assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
600 |
| - } |
601 |
| - |
602 | 452 | @Test
|
603 | 453 | public void withMultipleOrderBys() {
|
604 | 454 | Query q =
|
@@ -737,4 +587,183 @@ private void validateDoesNotServeTarget(
|
737 | 587 | TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(query.toTarget());
|
738 | 588 | assertFalse(targetIndexMatcher.servedByIndex(expectedIndex));
|
739 | 589 | }
|
| 590 | + |
| 591 | + @Test |
| 592 | + public void testBuildTargetIndexWithQueriesWithEqualities() { |
| 593 | + for (Query query : queriesWithEqualities) { |
| 594 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 595 | + } |
| 596 | + } |
| 597 | + |
| 598 | + @Test |
| 599 | + public void testBuildTargetIndexWithQueriesWithInequalities() { |
| 600 | + for (Query query : queriesWithInequalities) { |
| 601 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 602 | + } |
| 603 | + } |
| 604 | + |
| 605 | + @Test |
| 606 | + public void testBuildTargetIndexWithQueriesWithArrayContains() { |
| 607 | + for (Query query : queriesWithArrayContains) { |
| 608 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 609 | + } |
| 610 | + } |
| 611 | + |
| 612 | + @Test |
| 613 | + public void testBuildTargetIndexWithQueriesWithOrderBy() { |
| 614 | + for (Query query : queriesWithOrderBy) { |
| 615 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 616 | + } |
| 617 | + } |
| 618 | + |
| 619 | + @Test |
| 620 | + public void testBuildTargetIndexWithInequalityUsesSingleFieldIndex() { |
| 621 | + Query query = query("collId").filter(filter("a", ">", 1)).filter(filter("a", "<", 10)); |
| 622 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 623 | + } |
| 624 | + |
| 625 | + @Test |
| 626 | + public void testBuildTargetIndexWithCollection() { |
| 627 | + Query query = query("collId"); |
| 628 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 629 | + } |
| 630 | + |
| 631 | + @Test |
| 632 | + public void testBuildTargetIndexWithArrayContainsAndOrderBy() { |
| 633 | + Query query = |
| 634 | + query("collId") |
| 635 | + .filter(filter("a", "array-contains", "a")) |
| 636 | + .filter(filter("a", ">", "b")) |
| 637 | + .orderBy(orderBy("a", "asc")); |
| 638 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 639 | + } |
| 640 | + |
| 641 | + @Test |
| 642 | + public void testBuildTargetIndexWithEqualityAndDescendingOrder() { |
| 643 | + Query query = query("collId").filter(filter("a", "==", 1)).orderBy(orderBy("__name__", "desc")); |
| 644 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 645 | + } |
| 646 | + |
| 647 | + @Test |
| 648 | + public void testBuildTargetIndexWithMultipleEqualities() { |
| 649 | + Query query = query("collId").filter(filter("a1", "==", "a")).filter(filter("a2", "==", "b")); |
| 650 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 651 | + } |
| 652 | + |
| 653 | + @Test |
| 654 | + public void testBuildTargetIndexWithMultipleEqualitiesAndInequality() { |
| 655 | + Query query = |
| 656 | + query("collId") |
| 657 | + .filter(filter("equality1", "==", "a")) |
| 658 | + .filter(filter("equality2", "==", "b")) |
| 659 | + .filter(filter("inequality", ">=", "c")); |
| 660 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 661 | + query = |
| 662 | + query("collId") |
| 663 | + .filter(filter("equality1", "==", "a")) |
| 664 | + .filter(filter("inequality", ">=", "c")) |
| 665 | + .filter(filter("equality2", "==", "b")); |
| 666 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 667 | + } |
| 668 | + |
| 669 | + @Test |
| 670 | + public void testBuildTargetIndexWithMultipleFilters() { |
| 671 | + Query query = query("collId").filter(filter("a", "==", "a")).filter(filter("b", ">", "b")); |
| 672 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 673 | + query = |
| 674 | + query("collId") |
| 675 | + .filter(filter("a1", "==", "a")) |
| 676 | + .filter(filter("a2", ">", "b")) |
| 677 | + .orderBy(orderBy("a2", "asc")); |
| 678 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 679 | + query = |
| 680 | + query("collId") |
| 681 | + .filter(filter("a", ">=", 1)) |
| 682 | + .filter(filter("a", "==", 5)) |
| 683 | + .filter(filter("a", "<=", 10)); |
| 684 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 685 | + query = |
| 686 | + query("collId") |
| 687 | + .filter(filter("a", "not-in", Arrays.asList(1, 2, 3))) |
| 688 | + .filter(filter("a", ">=", 2)); |
| 689 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 690 | + } |
| 691 | + |
| 692 | + @Test |
| 693 | + public void testBuildTargetIndexWithMultipleOrderBys() { |
| 694 | + Query query = |
| 695 | + query("collId") |
| 696 | + .orderBy(orderBy("fff")) |
| 697 | + .orderBy(orderBy("bar", "desc")) |
| 698 | + .orderBy(orderBy("__name__")); |
| 699 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 700 | + query = |
| 701 | + query("collId") |
| 702 | + .orderBy(orderBy("foo")) |
| 703 | + .orderBy(orderBy("bar")) |
| 704 | + .orderBy(orderBy("__name__", "desc")); |
| 705 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 706 | + } |
| 707 | + |
| 708 | + @Test |
| 709 | + public void testBuildTargetIndexWithInAndNotIn() { |
| 710 | + Query query = |
| 711 | + query("collId") |
| 712 | + .filter(filter("a", "not-in", Arrays.asList(1, 2, 3))) |
| 713 | + .filter(filter("b", "in", Arrays.asList(1, 2, 3))); |
| 714 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 715 | + } |
| 716 | + |
| 717 | + @Test |
| 718 | + public void testBuildTargetIndexWithEqualityAndDifferentOrderBy() { |
| 719 | + Query query = |
| 720 | + query("collId") |
| 721 | + .filter(filter("foo", "==", "")) |
| 722 | + .filter(filter("bar", "==", "")) |
| 723 | + .orderBy(orderBy("qux")); |
| 724 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 725 | + query = |
| 726 | + query("collId") |
| 727 | + .filter(filter("aaa", "==", "")) |
| 728 | + .filter(filter("qqq", "==", "")) |
| 729 | + .filter(filter("ccc", "==", "")) |
| 730 | + .orderBy(orderBy("fff", "desc")) |
| 731 | + .orderBy(orderBy("bbb")); |
| 732 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 733 | + } |
| 734 | + |
| 735 | + @Test |
| 736 | + public void testBuildTargetIndexWithEqualsAndNotIn() { |
| 737 | + Query query = |
| 738 | + query("collId") |
| 739 | + .filter(filter("a", "==", 1)) |
| 740 | + .filter(filter("b", "not-in", Arrays.asList(1, 2, 3))); |
| 741 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 742 | + } |
| 743 | + |
| 744 | + @Test |
| 745 | + public void testBuildTargetIndexWithInAndOrderBy() { |
| 746 | + Query query = |
| 747 | + query("collId") |
| 748 | + .filter(filter("a", "not-in", Arrays.asList(1, 2, 3))) |
| 749 | + .orderBy(orderBy("a")) |
| 750 | + .orderBy(orderBy("b")); |
| 751 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 752 | + } |
| 753 | + |
| 754 | + @Test |
| 755 | + public void testBuildTargetIndexWithInAndOrderBySameField() { |
| 756 | + Query query = |
| 757 | + query("collId").filter(filter("a", "in", Arrays.asList(1, 2, 3))).orderBy(orderBy("a")); |
| 758 | + validateBuildTargetIndexCreateFullMatchIndex(query); |
| 759 | + } |
| 760 | + |
| 761 | + private void validateBuildTargetIndexCreateFullMatchIndex(Query query) { |
| 762 | + Target target = query.toTarget(); |
| 763 | + TargetIndexMatcher targetIndexMatcher = new TargetIndexMatcher(target); |
| 764 | + FieldIndex expectedIndex = targetIndexMatcher.BuildTargetIndex(); |
| 765 | + assertTrue(targetIndexMatcher.servedByIndex(expectedIndex)); |
| 766 | + // Check the index created is a FULL MATCH index |
| 767 | + assertTrue(expectedIndex.getSegments().size() >= target.getSegmentCount()); |
| 768 | + } |
740 | 769 | }
|
0 commit comments