This is an automated email from the ASF dual-hosted git repository.

yuqi1129 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 2043e62ea8 [#14848] improvement(spark-connector): Let JDBC catalog 
subclasses opt in to preferTimestampNTZ (#14849)
2043e62ea8 is described below

commit 2043e62ea8757052bd4e25ab5bf0c40fb7db3161
Author: Qi Yu <[email protected]>
AuthorDate: Wed Nov 11 10:41:19 2026 +0800

    [#14848] improvement(spark-connector): Let JDBC catalog subclasses opt in 
to preferTimestampNTZ (#14849)
    
    ### What changes were proposed in this pull request?
    
    - `GravitinoJdbcCatalog#supportsPreferTimestampNtz()` (protected)
    replaces the hard-coded `jdbc-postgresql` check. The default still
    returns true only for PostgreSQL.
    - `GravitinoJdbcCatalog#useTimestampNtz()` (protected) returns the mode
    selected at initialization, for subclasses that override
    `getSparkTypeConverter()`.
    
    ### Why are the changes needed?
    
    A Spark catalog for another JDBC provider cannot opt in to
    `preferTimestampNTZ`, and cannot keep its own type converter consistent
    with the JDBC reader, because the decision is hard-coded and the mode is
    private.
    
    Fix #14848
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. Existing catalogs behave as before.
    
    ### How was this patch tested?
    
    - New tests in `TestGravitinoJdbcCatalog`: a subclass for another
    provider that opts in honors `true`/`false`/unset, its converter follows
    the reader, and invalid values are still rejected.
    - JDBC unit tests on Spark 3.5 and 4.0 (90 each) passed.
---
 .../spark/connector/jdbc/GravitinoJdbcCatalog.java | 26 +++++++++-
 .../connector/jdbc/TestGravitinoJdbcCatalog.java   | 58 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 2 deletions(-)

diff --git 
a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/GravitinoJdbcCatalog.java
 
b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/GravitinoJdbcCatalog.java
index a5e259d5ea..3fd2b78c23 100644
--- 
a/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/GravitinoJdbcCatalog.java
+++ 
b/spark-connector/spark-common/src/main/java/org/apache/gravitino/spark/connector/jdbc/GravitinoJdbcCatalog.java
@@ -77,7 +77,7 @@ public class GravitinoJdbcCatalog extends BaseCatalog {
         getPropertiesConverter().toSparkCatalogProperties(options, properties);
     applyJdbcCredential(gravitinoCatalogClient, all);
     useTimestampNtz = false;
-    if ("jdbc-postgresql".equalsIgnoreCase(gravitinoCatalogClient.provider())) 
{
+    if (supportsPreferTimestampNtz()) {
       String value = preferTimestampNtz(options, properties);
       Preconditions.checkArgument(
           "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value),
@@ -90,7 +90,8 @@ public class GravitinoJdbcCatalog extends BaseCatalog {
       useTimestampNtz = Boolean.parseBoolean(value);
     }
     // One value drives both the reader and converter for every provider. 
Explicitly disable NTZ
-    // outside PostgreSQL, including when Spark's session timestamp type 
defaults to NTZ.
+    // for catalogs that do not support the option, including when Spark's 
session timestamp type
+    // defaults to NTZ.
     setPreferTimestampNtz(all, useTimestampNtz);
     jdbcTableCatalog.initialize(name, new CaseInsensitiveStringMap(all));
     jdbcOptions = new HashMap<>(all);
@@ -238,6 +239,27 @@ public class GravitinoJdbcCatalog extends BaseCatalog {
     return new SparkJdbcTypeConverter(useTimestampNtz);
   }
 
+  /**
+   * Returns whether this catalog honors the {@code preferTimestampNTZ} 
option. PostgreSQL catalogs
+   * do. A subclass whose source and Spark JDBC dialect keep zone-free 
timestamps as wall-clock
+   * values can opt in; every other catalog reads with the option off.
+   *
+   * @return true if the catalog reads {@code preferTimestampNTZ} from its 
options
+   */
+  protected boolean supportsPreferTimestampNtz() {
+    return 
"jdbc-postgresql".equalsIgnoreCase(gravitinoCatalogClient.provider());
+  }
+
+  /**
+   * Returns the timestamp mode selected when the catalog was initialized. A 
subclass that overrides
+   * {@link #getSparkTypeConverter()} must use it so its converter agrees with 
the JDBC reader.
+   *
+   * @return whether zone-free timestamps use Spark TimestampNTZType
+   */
+  protected boolean useTimestampNtz() {
+    return useTimestampNtz;
+  }
+
   @Override
   public void createNamespace(String[] namespace, Map<String, String> metadata)
       throws NamespaceAlreadyExistsException {
diff --git 
a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestGravitinoJdbcCatalog.java
 
b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestGravitinoJdbcCatalog.java
index 2512a0fb13..d76d90d611 100644
--- 
a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestGravitinoJdbcCatalog.java
+++ 
b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/jdbc/TestGravitinoJdbcCatalog.java
@@ -35,6 +35,7 @@ import org.apache.gravitino.credential.Credential;
 import org.apache.gravitino.credential.JdbcCredential;
 import org.apache.gravitino.credential.SupportsCredentials;
 import org.apache.gravitino.rel.types.Types;
+import org.apache.gravitino.spark.connector.SparkTypeConverter;
 import org.apache.gravitino.spark.connector.catalog.GravitinoCatalogManager;
 import org.apache.spark.sql.connector.catalog.TableCatalog;
 import org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions;
@@ -233,6 +234,63 @@ public class TestGravitinoJdbcCatalog {
     }
   }
 
+  /** A catalog for another provider that opts in to preferTimestampNTZ. */
+  private static class OptInCatalog extends GravitinoJdbcCatalog {
+    @Override
+    protected boolean supportsPreferTimestampNtz() {
+      return true;
+    }
+
+    @Override
+    protected SparkTypeConverter getSparkTypeConverter() {
+      return new SparkJdbcTypeConverter(useTimestampNtz());
+    }
+  }
+
+  @ParameterizedTest
+  @CsvSource({"true, true", "false, false", "'', false"})
+  void testSubclassCanOptInToNtzOption(String option, boolean expected) throws 
Exception {
+    try (MockedStatic<GravitinoCatalogManager> global = 
mockStatic(GravitinoCatalogManager.class)) {
+      
global.when(GravitinoCatalogManager::get).thenReturn(mock(GravitinoCatalogManager.class));
+      SupportsCredentials credentials = mock(SupportsCredentials.class);
+      when(credentials.getCredentials()).thenReturn(new Credential[0]);
+      Catalog source = mock(Catalog.class);
+      when(source.provider()).thenReturn("jdbc-mysql");
+      when(source.supportsCredentials()).thenReturn(credentials);
+      OptInCatalog catalog = new OptInCatalog();
+      FieldUtils.writeField(catalog, "gravitinoCatalogClient", source, true);
+
+      JDBCOptions options =
+          initialize(
+              catalog,
+              option.isEmpty() ? Map.of() : Map.of("preferTimestampNTZ", 
option),
+              Map.of());
+      Assertions.assertEquals(expected, options.preferTimestampNTZ());
+      Assertions.assertEquals(expected, catalog.useTimestampNtz());
+      Assertions.assertEquals(
+          expected ? DataTypes.TimestampNTZType : DataTypes.TimestampType,
+          
catalog.getSparkTypeConverter().toSparkType(Types.TimestampType.withoutTimeZone()));
+    }
+  }
+
+  @Test
+  void testOptedInCatalogStillRejectsInvalidNtzValue() throws Exception {
+    try (MockedStatic<GravitinoCatalogManager> global = 
mockStatic(GravitinoCatalogManager.class)) {
+      
global.when(GravitinoCatalogManager::get).thenReturn(mock(GravitinoCatalogManager.class));
+      SupportsCredentials credentials = mock(SupportsCredentials.class);
+      when(credentials.getCredentials()).thenReturn(new Credential[0]);
+      Catalog source = mock(Catalog.class);
+      when(source.provider()).thenReturn("jdbc-mysql");
+      when(source.supportsCredentials()).thenReturn(credentials);
+      OptInCatalog catalog = new OptInCatalog();
+      FieldUtils.writeField(catalog, "gravitinoCatalogClient", source, true);
+
+      Assertions.assertThrows(
+          IllegalArgumentException.class,
+          () -> initialize(catalog, Map.of("preferTimestampNTZ", "yes"), 
Map.of()));
+    }
+  }
+
   @Test
   void testNtzPropertyNeedsTheBypassPrefix() throws Exception {
     try (MockedStatic<GravitinoCatalogManager> global = 
mockStatic(GravitinoCatalogManager.class)) {

Reply via email to