Skip to content

[CALCITE-7668] MongoDB adapter should escape embedded quotes in field references - #5109

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
Samin061:mongo-escape-quotes
Jul 29, 2026
Merged

[CALCITE-7668] MongoDB adapter should escape embedded quotes in field references#5109
mihaibudiu merged 1 commit into
apache:mainfrom
Samin061:mongo-escape-quotes

Conversation

@Samin061

Copy link
Copy Markdown
Contributor

Changes Proposed

MongoDB pipeline injection through unescaped quotes in pushed-down field references

The Mongo adapter renders $project/$group stages as strings that are later parsed with BsonDocument.parse. MongoRules.quote wrapped a value in single quotes but left embedded single quotes and backslashes untouched (the old // TODO: handle embedded quotes), and the ITEM path that handles _MAP['key'] built '$key' the same way. Since the item key and field names come straight from the SQL statement, a value carrying a single quote closes the quoted token early and the rest is parsed as further pipeline syntax.

Reproduction: a projection such as SELECT _MAP['x'', evil: {$literal: 1}, y: ''z'] FROM mongo generates {$project: {..., evil: {$literal: 1}, ...}}, so an extra stage field lands in the pipeline sent to MongoDB. After the fix the key stays a single string.

The escaping lives in quote because that is the one place every field reference passes through; the item-key branch now routes through it, and the aggregate paths already call it. Added a unit test that fails on the old code.

@xiedeyantu

Copy link
Copy Markdown
Member

We do not oppose using AI tools to contribute code. Multiple reviewers have repeatedly advised creating a Jira ticket before submitting contributions—you acknowledged this but have yet to follow through. Moving forward, non-compliant PRs will no longer be reviewed.

@Samin061

Copy link
Copy Markdown
Contributor Author

That's on me, sorry. I'll get a CALCITE Jira filed for this one and re-title the PR to reference it, and do the same for my other open ones (#5075, #5106) before asking for review again.

import static org.hamcrest.MatcherAssert.assertThat;

/** Tests for {@link MongoRules}. */
class MongoRulesTest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not crazy about these tests, but a complete query covering these new rules whose output is validated on Mongo would be nice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair. Dropped MongoRulesTest and replaced it with an end-to-end test in MongoAdapterTest: the datatypes collection now carries two fields whose names are x', injected: {$literal: 1}, y: 'z and a\, and the test selects them through _MAP, checks the generated $project stage, and checks the values Mongo returns.

On the old code the first query parses as {$project: {C: '$x', injected: {$literal: 1}, y: 'z'}}, so the injected field lands in the pipeline and the query fails; with the escaping it returns C=quoted. :mongodb:test is green (53 passed, 6 pre-existing skips).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this query was validated against a real mongodb installation, please add a comment saying so.
We have had incorrect "expected" test results in the past.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new test has been placed in MongoAdapterTest, so I guess this was correctly validated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the expected values come from running the query against the Mongo instance the test spins up, not hand-written. Added a comment on the test method spelling that out.

@Samin061

Copy link
Copy Markdown
Contributor Author

Also still on the Jira: my ASF account request hasn't come through yet, so the PR title isn't prefixed with a CALCITE- id. I'll file the ticket and retitle this and my other open PRs as soon as it does.

@mihaibudiu

Copy link
Copy Markdown
Contributor

You don't need an ASF account, just a JIRA account to file issues.
We usually approve JIRA account requests instantly.

@Samin061

Copy link
Copy Markdown
Contributor Author

Got it, I had the two mixed up. Requesting the JIRA account now and I'll file the ticket for this one, then retitle this PR (and #5075, #5106) with the CALCITE- ids.

Nothing else pending on the code side: the unit test you weren't keen on is gone, replaced by an end-to-end case in MongoAdapterTest that runs the query against Mongo and checks both the generated $project stage and the returned values.

@rubenada rubenada changed the title MongoDB adapter should escape embedded quotes in field references [CALCITE-7668] MongoDB adapter should escape embedded quotes in field references Jul 24, 2026
@rubenada

Copy link
Copy Markdown
Contributor

I have created https://issues.apache.org/jira/browse/CALCITE-7668 to track this issue.

@rubenada

Copy link
Copy Markdown
Contributor

Fix LGTM.
Wdyt @xiedeyantu , @mihaibudiu ? Should it be squashed and merged?

@mihaibudiu

Copy link
Copy Markdown
Contributor

I have approved, but left a comment.

@mihaibudiu

Copy link
Copy Markdown
Contributor

Thanks for filing the issue.

@xiedeyantu xiedeyantu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@xiedeyantu

Copy link
Copy Markdown
Member

Friendly reminder: Please don't forget the comments on mihai.

@rubenada

Copy link
Copy Markdown
Contributor

We have several approvals, so this should be ready for merge.
@Samin061 could you squash commits into a single one, keeping as final commit message the Jira/PR title [CALCITE-7668] MongoDB adapter should escape embedded quotes in field references , please?

@rubenada rubenada added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jul 29, 2026
@Samin061
Samin061 force-pushed the mongo-escape-quotes branch from c014cf7 to 3a496ce Compare July 29, 2026 08:45
@Samin061

Copy link
Copy Markdown
Contributor Author

Done, squashed into a single commit with that message.

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu
mihaibudiu merged commit 2862a8d into apache:main Jul 29, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants