Skip to content

Match Spark semantics for endsWith filters on values with a trailing newline - #157

Open
RaphaelFakhri wants to merge 1 commit into
mongodb:mainfrom
RaphaelFakhri:fix-endswith-trailing-newline
Open

RaphaelFakhri wants to merge 1 commit into
mongodb:mainfrom
RaphaelFakhri:fix-endswith-trailing-newline

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Summary

ExpressionConverter translates StringEndsWith into the regular expression .*\Q<value>\E$. In MongoDB regular expressions, $ also matches before a final newline, so a value such as "abc\n" matches endsWith("abc"). Spark treats pushed filters as fully applied and does not filter those rows again, so the result differs from Spark semantics.

This change anchors the pattern with \z, which matches only at the absolute end of the string.

Changes

  • ExpressionConverter: use \z instead of $ for StringEndsWith.
  • MongoBatchTest#testReadsFiltersMatchLiterals: add line_end and line_end\n documents and an endsWith("line_end") case that expects only line_end.

Testing

./gradlew -Dorg.mongodb.test.uri=mongodb://127.0.0.1:27999 integrationTest \
  --tests "com.mongodb.spark.sql.connector.read.MongoBatchTest"
  • Without the fix, the new case fails (line_end\n is returned).
  • With the fix, all 33 tests in MongoBatchTest pass against MongoDB 7.0.14.
  • spotlessCheck, checkstyleMain and checkstyleIntegrationTest pass.

…newline

The pushed down StringEndsWith regex ended with $, which in MongoDB
regular expressions also matches before a final newline. Values such as
"abc\n" were returned for endsWith("abc") even though Spark does not
reapply pushed filters. Anchor the pattern with \z instead.
@RaphaelFakhri
RaphaelFakhri requested a review from a team as a code owner November 4, 2026 01:59
@RaphaelFakhri
RaphaelFakhri requested review from nhachicha and removed request for a team November 4, 2026 01:59
@strogiyotec

Copy link
Copy Markdown
Contributor

hi @RaphaelFakhri thanks for opening the PR we will take a look at it next week

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.

2 participants