Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(trino): normalize custom GMT-offset JVM zones before connecting

ericbyz3707760e43a2
diff --git a/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java b/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java
index c93d52ef42..c6c98e514e 100644
--- a/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java
+++ b/agents/drivers/trino/src/main/java/com/dbx/agent/trino/TrinoAgent.java
@@ -27,6 +27,8 @@ public final class TrinoAgent extends AbstractJdbcAgent {
private static final Pattern NUMERIC_PRECISION_PATTERN = Pattern.compile("(?i)^(decimal|numeric)\\((\\d+)(?:,\\s*\\d+)?\\)");
private static final Pattern NUMERIC_SCALE_PATTERN = Pattern.compile("(?i)^(decimal|numeric)\\(\\d+,\\s*(\\d+)\\)");
private static final Pattern CHARACTER_LENGTH_PATTERN = Pattern.compile("(?i)^(char|varchar)\\((\\d+)\\)");
+ // Custom "GMT+08:00" IDs reported by the JVM when the OS zone has no IANA mapping.
+ private static final Pattern GMT_OFFSET_ZONE_PATTERN = Pattern.compile("GMT(?<sign>[+-])(?<hours>\\d{1,2})(?::(?<minutes>\\d{2}))?", Pattern.CASE_INSENSITIVE);
@Override
protected String driverClass() {
@@ -395,7 +397,27 @@ public final class TrinoAgent extends AbstractJdbcAgent {
return value == null ? "" : value;
}
+ /**
+ * Trino derives the session time zone from the JVM default, but servers built on
+ * Joda-Time reject the custom "GMT+08:00" IDs the JVM reports when the OS zone has
+ * no IANA mapping (#13451). Rewriting whole-hour offsets to the equivalent IANA
+ * {@code Etc/GMT±N} form (note the inverted sign) keeps the same instant while
+ * staying accepted everywhere; java.util.TimeZone would normalize any bare-offset
+ * ID back to the rejected "GMT+08:00" spelling. Fractional offsets have no Etc
+ * equivalent and are left untouched.
+ */
+ static void normalizeDefaultTimeZone() {
+ Matcher customOffset = GMT_OFFSET_ZONE_PATTERN.matcher(java.util.TimeZone.getDefault().getID());
+ boolean wholeHourOnly = customOffset.matches() && (customOffset.group("minutes") == null || customOffset.group("minutes").equals("00"));
+ if (wholeHourOnly) {
+ // POSIX sign inversion: IANA's Etc/GMT-8 means UTC+8.
+ String etcZone = "Etc/GMT" + (customOffset.group("sign").equals("+") ? "-" : "+") + Integer.parseInt(customOffset.group("hours"));
+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(java.time.ZoneId.of(etcZone)));
+ }
+ }
+
public static void main(String[] args) {
+ normalizeDefaultTimeZone();
new MultiSessionJsonRpcServer(TrinoAgent::new).run();
}
}
diff --git a/agents/drivers/trino/src/test/java/com/dbx/agent/trino/TrinoAgentTimeZoneTest.java b/agents/drivers/trino/src/test/java/com/dbx/agent/trino/TrinoAgentTimeZoneTest.java
new file mode 100644
index 0000000000..11279c62e3
--- /dev/null
+++ b/agents/drivers/trino/src/test/java/com/dbx/agent/trino/TrinoAgentTimeZoneTest.java
@@ -0,0 +1,77 @@
+package com.dbx.agent.trino;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.time.ZoneId;
+import java.util.TimeZone;
+import org.junit.jupiter.api.Test;
+
+/**
+ * #13451: Trino derives the session zone from the JVM default, but servers built on
+ * Joda-Time reject the custom "GMT+08:00" IDs the JVM reports when the OS zone has no
+ * IANA mapping. The agent must rewrite such defaults to an equivalent IANA zone
+ * before the first connection.
+ */
+class TrinoAgentTimeZoneTest {
+ @Test
+ void rewritesCustomGmtOffsetDefaultToEquivalentIanaZone() {
+ TimeZone original = TimeZone.getDefault();
+ try {
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00"));
+ long before = TimeZone.getDefault().getOffset(System.currentTimeMillis());
+
+ TrinoAgent.normalizeDefaultTimeZone();
+
+ assertEquals("Etc/GMT-8", TimeZone.getDefault().getID());
+ assertEquals(before, TimeZone.getDefault().getOffset(System.currentTimeMillis()));
+ } finally {
+ TimeZone.setDefault(original);
+ }
+ }
+
+ @Test
+ void keepsIanaDefaultZonesUntouched() {
+ TimeZone original = TimeZone.getDefault();
+ try {
+ TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
+ TrinoAgent.normalizeDefaultTimeZone();
+ assertEquals("Asia/Shanghai", TimeZone.getDefault().toZoneId().getId());
+
+ TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
+ TrinoAgent.normalizeDefaultTimeZone();
+ assertEquals("UTC", TimeZone.getDefault().toZoneId().getId());
+ } finally {
+ TimeZone.setDefault(original);
+ }
+ }
+
+ @Test
+ void keepsFractionalGmtOffsetsUntouched() {
+ TimeZone original = TimeZone.getDefault();
+ try {
+ // Etc zones only cover whole hours; leave fractional offsets to the JVM.
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT+05:30"));
+ TrinoAgent.normalizeDefaultTimeZone();
+ assertEquals("GMT+05:30", TimeZone.getDefault().toZoneId().getId());
+ } finally {
+ TimeZone.setDefault(original);
+ }
+ }
+
+ @Test
+ void rewritesNegativeCustomGmtOffsets() {
+ TimeZone original = TimeZone.getDefault();
+ try {
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT-05:00"));
+ long before = TimeZone.getDefault().getOffset(System.currentTimeMillis());
+
+ TrinoAgent.normalizeDefaultTimeZone();
+
+ assertEquals("Etc/GMT+5", TimeZone.getDefault().getID());
+ assertEquals(before, TimeZone.getDefault().getOffset(System.currentTimeMillis()));
+ assertEquals(ZoneId.of("Etc/GMT+5").getRules().getOffset(java.time.Instant.now()).getTotalSeconds() * 1000L, before);
+ } finally {
+ TimeZone.setDefault(original);
+ }
+ }
+}