Visorgood opened a new pull request, #60564:
URL: https://github.com/apache/spark/pull/60564
### What changes were proposed in this pull request?
`V2ExpressionSQLBuilder` builds the LIKE pattern for `STARTS_WITH`,
`ENDS_WITH` and `CONTAINS` by stripping the first and last character of the
rendered pattern argument, which assumes a quoted string literal. This PR
builds the LIKE predicate only when the pattern is a non-null string literal,
and declines any other pattern with `visitUnexpectedExpr`:
- A source that compiles predicates with `V2ExpressionSQLBuilder`, such as a
JDBC dialect (its `compileExpression` catches the error), no longer pushes the
predicate down, so Spark evaluates it.
- `ToStringSQLBuilder` renders such a predicate as a function call, e.g.
`STARTS_WITH(email, a)`.
The literal check matches the one in #56558 (SPARK-57500), which fixes the
built-in JDBC dialects. This PR fixes the base builder that other DSv2 sources
and custom dialects use.
### Why are the changes needed?
With a column pattern, the builder renders LIKE against a mangled copy of
the column name instead of the column's value: `startswith(email, email)`
becomes `email LIKE 'mai%' ESCAPE '\'`. A DSv2 source that pushes this
predicate returns wrong rows without an error, and `toString` of the predicate
throws `StringIndexOutOfBoundsException` when the pattern column's name has one
character.
### Does this PR introduce _any_ user-facing change?
Yes. `startsWith`, `endsWith` and `contains` with a non-literal pattern are
no longer pushed down to DSv2 sources that compile predicates with
`V2ExpressionSQLBuilder`, so Spark evaluates them and returns the correct rows.
On master this includes the built-in JDBC dialects. For example, before this
change
spark.table("h2.test.address").filter($"email".startsWith($"email"))
was planned with `PushedFilters: [EMAIL IS NOT NULL, EMAIL LIKE 'MAI%'
ESCAPE '\']` and returned 0 of the table's 6 rows; with it, the predicate stays
in Spark and all 6 rows are returned. Pushdown with a string literal pattern is
unchanged.
### How was this patch tested?
New tests, both of which fail without the fix:
- `V2ExpressionUtilsSuite`: the builder still renders LIKE for a string
literal pattern; for a column, a one-character column and a NULL literal it
fails with `UNEXPECTED_V2_EXPRESSION`, and `toString` renders a function call.
Without the fix, the builder returns `email LIKE 'mai%' ESCAPE '\'`.
- `JDBCV2Suite`: on H2, `startsWith`, `endsWith` and `contains` with a
column pattern return all rows and keep the filter in Spark. Without the fix,
the query returns 0 of 6 rows.
Ran `V2ExpressionUtilsSuite`, `JDBCV2Suite` and `JDBCSuite` locally.
### Was this patch authored or co-authored using generative AI tooling?
Co-authored-by: Claude Opus 5.5
cc @jiwen624
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]