abir123rajbongshi-debug commented on code in PR #60609:
URL: https://github.com/apache/spark/pull/60609#discussion_r4629402486


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -448,6 +449,31 @@ object JdbcUtils extends Logging with SQLConfHelper {
     )
   }
 
+  /**
+   * Whether `expr` has a TimestampType or TimestampLTZNanosType literal, 
which would be rendered
+   * for the database without `timestampCalendarTimeZone`. Such expressions 
are not pushed down
+   * when the option is set, so Spark evaluates them on the values read in the 
option's time zone.
+   */
+  def containsCalendarTimeZoneTimestampLiteral(expr: Expression, options: 
JDBCOptions): Boolean =
+    options.timestampCalendarTimeZone.isDefined && {
+      def walk(e: Expression): Boolean = e match {
+        case l: LiteralValue[_] =>
+          l.dataType == TimestampType || 
l.dataType.isInstanceOf[TimestampLTZNanosType]

Review Comment:
   Done. With `timestampCalendarTimeZone` set, the guard (now 
`JdbcUtils.dependsOnTimeZoneWithCalendarOption`) also declines a cast from or 
to a timestamp type (or with an unknown input type), and an `EXTRACT` whose 
source is a TIMESTAMP column or of unknown type. `JDBCScanBuilder` resolves 
column references against the scan schema. All four V2 sites go through 
`cannotPushDown`, so predicates, aggregates and group-bys, joins and top-N are 
covered. Date functions like `month`/`year`/`trunc` take a DATE, so on a 
timestamp they go through the cast rule. Added a `JDBCV2Suite` test with the 
option set to UTC and the session in Asia/Kolkata: `hour(time1) = 5` returns 
the rows and is not pushed, `hour(time1) = 0` returns none, `time1 > date1` and 
a group-by over `CAST(time1 AS DATE)` are not pushed, and without the option 
`hour(time1) = 0` is still pushed as `EXTRACT`.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -480,14 +506,16 @@ object JdbcUtils extends Logging with SQLConfHelper {
     case _: StringType => JDBCValueGetter.StringGetter
     case TimestampType if metadata.contains("logical_time_type") =>
       JDBCValueGetter.LogicalTimeGetter
-    case TimestampType => JDBCValueGetter.TimestampGetter(dialect)
+    case TimestampType =>
+      JDBCValueGetter.TimestampGetter(dialect, options.flatMap(calendarFor))

Review Comment:
   Done. `ArrayGetter` now takes the optional calendar. With it set, TIMESTAMP 
array elements are read through `Array.getResultSet()` with `getTimestamp(2, 
calendar)`, recursing for nested arrays, so `timestamptz[]` elements are not 
reinterpreted. The array setter writes each TIMESTAMP element as its wall clock 
in the option's zone before `createArrayOf`. Added a `PostgresIntegrationSuite` 
test: with the option set to UTC and the JVM in Asia/Kolkata, a `TIMESTAMP` 
column and `TIMESTAMP[]`/`TIMESTAMPTZ[]` elements read as the same instant, and 
a written `TIMESTAMP[]` stores the UTC wall clock and reads back unchanged, 
with and without the Java 8 datetime API. Unset-option and TIMESTAMP_NTZ arrays 
keep the existing path.
   



##########
docs/sql-data-sources-jdbc.md:
##########
@@ -398,6 +398,14 @@ logging into the data sources.
     </td>
     <td>read/write</td>
   </tr>
+  <tr>
+    <td><code>timestampCalendarTimeZone</code></td>
+    <td>(none)</td>
+    <td>
+      When set, Spark reads and writes TIMESTAMP values in this time zone 
instead of the JVM default time zone. Set it to the time zone the database 
session uses for timestamps, for example <code>UTC</code> or 
<code>Asia/Kolkata</code>, when the two differ and values come out shifted. 
Only applies to TIMESTAMP columns, not TIMESTAMP_NTZ. When set, filters, 
aggregates, joins and sorts that contain timestamp values are evaluated by 
Spark instead of the database. An invalid time zone fails the query. If not 
set, the JVM default time zone is used.

Review Comment:
   Fixed. The doc row now names what the guard declines: expressions that use a 
timestamp literal, a cast to or from a timestamp, or a date or time part of a 
TIMESTAMP column (such as `hour(ts)`). It also says that comparisons between 
TIMESTAMP columns, and sorts, `MIN` and `MAX` on them, are still pushed down.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/jdbc/JDBCScanBuilder.scala:
##########
@@ -112,8 +116,8 @@ case class JDBCScanBuilder(
 
   override def pushAggregation(aggregation: Aggregation): Boolean = {
     if (!jdbcOptions.pushDownAggregate) return false
-    if (aggregation.aggregateExpressions.exists(containsUnsupportedCharPad) ||
-        aggregation.groupByExpressions.exists(containsUnsupportedCharPad)) {
+    if (aggregation.aggregateExpressions.exists(cannotPushDown) ||

Review Comment:
   Added. The new `JDBCV2Suite` test covers both sites with queries whose 
answers differ if the guard is dropped: `SUM(CASE WHEN time1 > TIMESTAMP 
'2022-05-18 03:00:00' THEN 1 ELSE 0 END)` is not pushed and returns 2 (H2 would 
return 1), and `ORDER BY time1 > TIMESTAMP '2022-05-18 03:00:00', name DESC 
LIMIT 1` is not pushed as a top-N and returns `amy` (H2 would return `alex`), 
checked with `checkAggregateRemoved`, `checkSortRemoved` and 
`checkLimitRemoved`.
   



-- 
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]

Reply via email to