You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "shrirangmhalgi (via GitHub)" <gi...@apache.org> on 2026/06/10 08:12:38 UTC
[PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format stri… [spark]
shrirangmhalgi opened a new pull request, #56425:
URL: https://github.com/apache/spark/pull/56425
…ngs to Oracle format
OracleDialect.visitSQLFunction hardcodes 'IW' (ISO week) for all TRUNC pushdowns regardless of the requested truncation level. This causes trunc(col, 'MONTH') to silently return week-truncated dates instead of month-truncated dates on Oracle data sources.
Map Spark format strings to Oracle equivalents: WEEK->IW, MONTH->MM, QUARTER->Q, YEAR->YYYY.
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #56425:
URL: https://github.com/apache/spark/pull/56425#discussion_r3398956112
##########
sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala:
##########
@@ -1430,6 +1430,35 @@ class JDBCSuite extends SharedSparkSession {
assert(getJdbcType(oracleDialect, TimestampNTZType) == "TIMESTAMP")
}
+ test("Oracle TRUNC pushdown should map Spark format strings to Oracle format") {
+ val oracleDialect = JdbcDialects.get("jdbc:oracle://127.0.0.1/db")
+ val dateRef = FieldReference("d")
+
+ // LiteralValue for StringType must use UTF8String (Spark's internal string type)
+ // to match what V2ExpressionBuilder produces in the real pushdown path.
+ import org.apache.spark.unsafe.types.UTF8String
+ def truncExpr(fmt: String): GeneralScalarExpression = new GeneralScalarExpression("TRUNC",
+ Array[V2Expression](dateRef, LiteralValue(UTF8String.fromString(fmt), StringType)))
+
+ val monthSql = oracleDialect.compileExpression(truncExpr("MONTH")).get
+ assert(monthSql.contains("'MM'"),
+ s"trunc(d, 'MONTH') should produce Oracle 'MM', got: $monthSql")
+ assert(!monthSql.contains("'IW'"),
+ s"trunc(d, 'MONTH') should NOT produce 'IW', got: $monthSql")
+
+ val weekSql = oracleDialect.compileExpression(truncExpr("WEEK")).get
Review Comment:
Once the match handles case and rejects unmapped formats, it's worth covering both here: a lowercase format (e.g. `trunc(d, 'week')` — the exact shape OracleIntegrationSuite.scala:337 pushes) and an unmapped format asserting `compileExpression` returns `None`.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56425:
URL: https://github.com/apache/spark/pull/56425#issuecomment-4688014830
Thanks @cloud-fan for the thorough review! Addressed all points in the latest commit:
- Case-insensitive match via `toUpperCase(Locale.ROOT)`
- Unmapped formats now throw (compileExpression returns None, Spark evaluates locally)
- Added test coverage for lowercase format and unmapped format rejection
- Fixed the comment attribution
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56425:
URL: https://github.com/apache/spark/pull/56425#issuecomment-4677353968
@cloud-fan / @beliefer - Could you please review this PR which fixes an Oracle `TRUNC` pushdown bug where visitSQLFunction hardcodes `'IW'` for all truncation levels - causing `trunc(col, 'MONTH')`, `trunc(col, 'YEAR')`, and `trunc(col, 'QUARTER')` to silently return week-truncated dates instead of the correct truncation.
Thanks!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #56425:
URL: https://github.com/apache/spark/pull/56425#issuecomment-4738036779
thanks, merging to master/4.x/4.2 (bug fix)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #56425: [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format
URL: https://github.com/apache/spark/pull/56425
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56425:
URL: https://github.com/apache/spark/pull/56425#issuecomment-4736775513
Thanks @cloud-fan! Added `trunc(date1, 'MONTH')` integration test in the latest commit - the case that would have caught the regression since it was silently pushed as `TRUNC(d, 'IW')` before the fix.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56425:
URL: https://github.com/apache/spark/pull/56425#issuecomment-4734099956
@cloud-fan - i have addressed all the review comments in the latest commit. Let me know if anything else is needed.
Thanks!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56425:
URL: https://github.com/apache/spark/pull/56425#issuecomment-4704903921
Thanks @yadavay-amzn for the thorough review!
Added alias assertions for `'MM'` and `'YY'` in the latest commit. On the structured error - since the exception is internal-only (caught by `compileExpression` as `NonFatal -> None`, never surfaced to users), I kept the bare `IllegalArgumentException` to avoid adding an error class for a throw that's never displayed.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-57364][SQL] Fix Oracle TRUNC pushdown to map Spark format strings to Oracle format [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #56425:
URL: https://github.com/apache/spark/pull/56425#discussion_r3398956098
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/OracleDialect.scala:
##########
@@ -75,7 +75,16 @@ private case class OracleDialect() extends JdbcDialect with SQLConfHelper with N
override def visitSQLFunction(funcName: String, inputs: Array[String]): String = {
funcName match {
case "TRUNC" =>
- s"TRUNC(${inputs(0)}, 'IW')"
+ // Map Spark's trunc format strings to Oracle equivalents.
+ // inputs(1) arrives as e.g. "'MONTH'" (with quotes from LiteralValue.toString).
+ val oracleFormat = inputs(1) match {
Review Comment:
Spark's trunc format is case-insensitive (`parseTruncLevel` uppercases before matching, DateTimeUtils.scala:587), but this match is exact-case, so a valid format like `trunc(d, 'week')` falls through to the passthrough arm and compiles to `TRUNC(d, 'week')` — Oracle's TRUNC format model has no `WEEK` element, so the pushed query fails at runtime. The existing integration test pushes exactly this shape (`trunc(date1, 'week')`, OracleIntegrationSuite.scala:337); it worked before this PR because the hardcoded `'IW'` happened to be correct for week, and the docker integration suites don't run in PR CI, so this won't show up as a CI failure here. Match on `inputs(1).toUpperCase(Locale.ROOT)` instead.
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/OracleDialect.scala:
##########
@@ -75,7 +75,16 @@ private case class OracleDialect() extends JdbcDialect with SQLConfHelper with N
override def visitSQLFunction(funcName: String, inputs: Array[String]): String = {
funcName match {
case "TRUNC" =>
- s"TRUNC(${inputs(0)}, 'IW')"
+ // Map Spark's trunc format strings to Oracle equivalents.
+ // inputs(1) arrives as e.g. "'MONTH'" (with quotes from LiteralValue.toString).
Review Comment:
The quotes on this path come from `JDBCSQLBuilder.visitLiteral` -> `compileValue` (JdbcDialects.scala:384); `LiteralValue.toString` isn't invoked here.
```suggestion
// inputs(1) arrives quoted, e.g. "'MONTH'" (see JDBCSQLBuilder.visitLiteral).
```
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/OracleDialect.scala:
##########
@@ -75,7 +75,16 @@ private case class OracleDialect() extends JdbcDialect with SQLConfHelper with N
override def visitSQLFunction(funcName: String, inputs: Array[String]): String = {
funcName match {
case "TRUNC" =>
- s"TRUNC(${inputs(0)}, 'IW')"
+ // Map Spark's trunc format strings to Oracle equivalents.
+ // inputs(1) arrives as e.g. "'MONTH'" (with quotes from LiteralValue.toString).
+ val oracleFormat = inputs(1) match {
+ case "'WEEK'" => "'IW'"
+ case "'MONTH'" | "'MM'" | "'MON'" => "'MM'"
+ case "'QUARTER'" => "'Q'"
+ case "'YEAR'" | "'YYYY'" | "'YY'" => "'YYYY'"
+ case other => other
Review Comment:
Passing unmapped formats through diverges from Spark semantics: `trunc` returns NULL for formats it doesn't recognize or that are too fine for dates (`TruncDate` returns null below `MIN_LEVEL_OF_DATE_TRUNC`), but Oracle either errors or silently returns a real value — e.g. `'DD'` truncates to day, and `'DAY'` truncates to the start of the week. A non-literal format argument (`trunc(d, fmt_col)`) is also pushed down and lands here as a column reference. Throw in this arm instead, like `visitAggregateFunction` below (or `visitExtract`'s `visitUnexpectedExpr`): `compileExpression` catches it and returns None, so the expression simply isn't pushed and Spark evaluates `trunc` itself with the right semantics.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org