Skip to content

Conversation

suddendust
Copy link
Contributor

@suddendust suddendust commented Oct 6, 2025

Description

This PR adds support for un-nesting top-level array fields in flat PG collection. Currently, it assumes that the column defined in UnnestExpression is an array. Un-nesting JSON arrays in currently not support in flat collections.

Testing

Have added DocStoreQueryV1Test#testFlatPostgresCollectionUnnestTags to test the behaviour.

Checklist:

  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

Copy link

codecov bot commented Oct 6, 2025

Codecov Report

❌ Patch coverage is 33.33333% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.17%. Comparing base (f0e2bef) to head (2d016a3).

Files with missing lines Patch % Lines
...1/vistors/PostgresFilterTypeExpressionVisitor.java 0.00% 24 Missing ⚠️
...ors/PostgresUnnestFilterTypeExpressionVisitor.java 23.07% 8 Missing and 2 partials ⚠️
.../v1/vistors/PostgresFromTypeExpressionVisitor.java 66.66% 4 Missing and 4 partials ⚠️

❗ There is a different number of reports uploaded between BASE (f0e2bef) and HEAD (2d016a3). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (f0e2bef) HEAD (2d016a3)
integration 1 0
Additional details and impacted files
@@              Coverage Diff              @@
##               main     #239       +/-   ##
=============================================
- Coverage     79.69%   57.17%   -22.52%     
  Complexity     1097     1097               
=============================================
  Files           212      212               
  Lines          5328     5364       +36     
  Branches        450      463       +13     
=============================================
- Hits           4246     3067     -1179     
- Misses          762     2014     +1252     
+ Partials        320      283       -37     
Flag Coverage Δ
integration ?
unit 57.17% <33.33%> (-0.28%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@suddendust suddendust changed the title [Draft] Support for Unnest on Top-Level Array Fields in Flat Postgres Collection Support for Unnest on Top-Level Array Fields in Flat Postgres Collection Oct 8, 2025
// From looking at the data:
// - "hygiene" appears in docs 1, 5, 8 = 3 times
// - "personal-care" appears in docs 1, 3 = 2 times
// - "grooming" appears in docs 6, 7 = 2 times
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice. Thanks a lot for these inline comments. 🙂

return String.format(
"EXISTS (SELECT 1 FROM jsonb_array_elements(COALESCE(%s, '[]'::jsonb)) AS \"%s\" WHERE %s)",
parsedLhs, alias, parsedFilter);
if (isFlatCollection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if this comment is coming pretty late.
The more and more of these conditions make me feel that it would be better to separate the "FlatCollection Visitors" and "NestedCollection Visitors" in different classes backed by some factory, avoiding a lot of these checks. More than the checks, it'll be much cleaner to maintain/debug.

Having said that, I leave it upto you if the distinction really makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this makes sense. Let me see how much of this can be refactored. Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if this comment is coming pretty late. The more and more of these conditions make me feel that it would be better to separate the "FlatCollection Visitors" and "NestedCollection Visitors" in different classes backed by some factory, avoiding a lot of these checks. More than the checks, it'll be much cleaner to maintain/debug.

Having said that, I leave it upto you if the distinction really makes sense.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puneet-traceable @suresh-prakash I did a bit of refactoring and the changes are substantial. How about we merge this PR and take that up in a separate one? To keep things cleaner.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. That works.

postgresQueryParser.getQuery().getFilter(), postgresQueryParser);

if (StringUtils.isNotEmpty(unnestFilters) && mainFilter.isPresent()) {
return Optional.of(unnestFilters + " AND " + mainFilter.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Prefer StringBuilder, StringBuffer or String.format()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants