Commit 4c25370
Support parent-referencing WHERE filters in includes child queries (#1307)
* Unit tests for filtering on parent fields in child query
* ci: apply automated fixes
* Support parent-referencing WHERE filters in includes child queries
Allow child queries to have additional WHERE clauses that reference
parent fields (e.g., eq(i.createdBy, p.createdBy)) beyond the single
correlation eq(). Parent-referencing WHEREs are detected in the builder,
parent fields are projected into the key stream, and filters are
re-injected into the child query where parent context is available.
When no parent-referencing filters exist, behavior is unchanged.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* ci: apply automated fixes
* Extract correlation condition from inside and() WHERE clauses
When users write a single .where() with and(eq(i.projectId, p.id), ...),
the correlation eq() is now found and extracted from inside the and().
The remaining args stay as WHERE clauses. This means users don't need
to know that the correlation must be a separate .where() call.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* ci: apply automated fixes
* Some more tests
* changeset
* Add failing test for shared correlation key with distinct parent filter values
Two parents share the same correlation key (groupId) but have different
values for a parent-referenced filter field (createdBy). The test verifies
that each parent receives its own filtered child set rather than a shared
union.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* ci: apply automated fixes
* Key child collections by composite routing key to fix shared correlation key collision
When multiple parents share the same correlation key but have different
parent-referenced filter values, child collections were incorrectly shared.
Fix by keying child collections by (correlationKey, parentFilterValues)
composite, and using composite child keys in the D2 stream to prevent
collisions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add failing test for shared correlation key with orderBy + limit
Reproduces the bug where grouped ordering for limit uses the raw
correlation key instead of the composite routing key, causing parents
that share a correlation key but differ on parent-referenced filters
to have their children merged before the limit is applied.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* ci: apply automated fixes
* Use composite routing key for grouped ordering with limit/offset
The includesGroupKeyFn for orderBy + limit/offset was grouping by raw
correlationKey, causing parents sharing a correlation key but differing
on parent-referenced filters to have their children merged before the
limit was applied. Use the same composite key as the routing layer.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add failing test for nested includes with parent-referencing filters at both levels
When both the child and grandchild includes use parent-referencing
filters, the grandchild collection comes back empty because the
nested routing index uses a different key than the nested buffer.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Use composite routing key in nested routing index to match nested buffer keys
The nested routing index was keyed by raw correlationKey while nested
buffers use computeRoutingKey(correlationKey, parentContext). This
mismatch caused drainNestedBuffers lookups to fail, leaving grandchild
collections empty when parent-referencing filters exist at both levels.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add test for three levels of nested includes with parent-referencing filters
Verifies that composite routing keys work at arbitrary nesting depth,
not just the first two levels.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* ci: apply automated fixes
* Add test for deleting one parent preserving sibling parent's child collection
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix shared correlation key: deduplicate parentKeyStream and defer child cleanup
Two fixes for when multiple parents share the same correlation key:
1. Add reduce operator on parentKeyStream to clamp multiplicities to 1,
preventing the inner join from producing duplicate child entries that
cause incorrect deletions when one parent is removed.
2. In Phase 5, only delete child registry entry when the last parent
referencing it is removed.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add test for spread select on child not leaking internal properties
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Strip internal __correlationKey and __parentContext from child results
These routing properties leak into user-visible results when the child
query uses a spread select (e.g. { ...i }).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* style: fix prettier formatting in compiler
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 99fb817 commit 4c25370
6 files changed
Lines changed: 1190 additions & 102 deletions
File tree
- .changeset
- packages/db
- src/query
- builder
- compiler
- live
- tests/query
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
894 | 895 | | |
895 | 896 | | |
896 | 897 | | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
897 | 931 | | |
898 | 932 | | |
899 | 933 | | |
| |||
915 | 949 | | |
916 | 950 | | |
917 | 951 | | |
918 | | - | |
| 952 | + | |
| 953 | + | |
919 | 954 | | |
920 | 955 | | |
921 | 956 | | |
| 957 | + | |
922 | 958 | | |
923 | 959 | | |
924 | 960 | | |
| |||
928 | 964 | | |
929 | 965 | | |
930 | 966 | | |
931 | | - | |
| 967 | + | |
932 | 968 | | |
933 | 969 | | |
934 | 970 | | |
935 | 971 | | |
936 | 972 | | |
937 | | - | |
938 | 973 | | |
939 | | - | |
940 | | - | |
| 974 | + | |
| 975 | + | |
941 | 976 | | |
942 | 977 | | |
943 | 978 | | |
| |||
948 | 983 | | |
949 | 984 | | |
950 | 985 | | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
951 | 1017 | | |
952 | 1018 | | |
953 | 1019 | | |
| |||
959 | 1025 | | |
960 | 1026 | | |
961 | 1027 | | |
962 | | - | |
| 1028 | + | |
| 1029 | + | |
963 | 1030 | | |
964 | | - | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
965 | 1091 | | |
966 | 1092 | | |
967 | | - | |
| 1093 | + | |
968 | 1094 | | |
969 | 1095 | | |
970 | 1096 | | |
971 | 1097 | | |
972 | 1098 | | |
973 | 1099 | | |
974 | 1100 | | |
| 1101 | + | |
| 1102 | + | |
975 | 1103 | | |
976 | 1104 | | |
977 | 1105 | | |
| |||
0 commit comments