From 11e3adb4037104034bf69f5390ff332c22ba45a3 Mon Sep 17 00:00:00 2001 From: Jeremy Osterhoudt Date: Tue, 22 Jul 2025 14:06:23 -0700 Subject: [PATCH 1/3] Allow non-standards JDBC/SQL types to be returned as ANY --- core/src/main/java/org/apache/calcite/avatica/SqlType.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/avatica/SqlType.java b/core/src/main/java/org/apache/calcite/avatica/SqlType.java index 840e218e4c..3ed8e4393b 100644 --- a/core/src/main/java/org/apache/calcite/avatica/SqlType.java +++ b/core/src/main/java/org/apache/calcite/avatica/SqlType.java @@ -378,7 +378,11 @@ public enum SqlType { public static SqlType valueOf(int type) { final SqlType sqlType = BY_ID.get(type); if (sqlType == null) { - throw new IllegalArgumentException("Unknown SQL type " + type); + // In most cases the ANY type will allow non-standard types to be displayable in the query requests. + // However, I think we should add the ability to specify a non-standard type factory + // that can be referenced here to see if the type is supported. + // For now, we will just return ANY to unblock users of databases that support non-standard types. + return ANY; } return sqlType; } From 003e25be99d29dd0c2348945c1543d26bd4e99bd Mon Sep 17 00:00:00 2001 From: Jeremy Osterhoudt Date: Thu, 24 Jul 2025 12:42:51 -0700 Subject: [PATCH 2/3] Add unit tests and fix lint errors --- .../org/apache/calcite/avatica/SqlType.java | 9 +-- .../apache/calcite/avatica/SqlTypeTest.java | 60 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java diff --git a/core/src/main/java/org/apache/calcite/avatica/SqlType.java b/core/src/main/java/org/apache/calcite/avatica/SqlType.java index 3ed8e4393b..2ede01121a 100644 --- a/core/src/main/java/org/apache/calcite/avatica/SqlType.java +++ b/core/src/main/java/org/apache/calcite/avatica/SqlType.java @@ -378,10 +378,11 @@ public enum SqlType { public static SqlType valueOf(int type) { final SqlType sqlType = BY_ID.get(type); if (sqlType == null) { - // In most cases the ANY type will allow non-standard types to be displayable in the query requests. - // However, I think we should add the ability to specify a non-standard type factory - // that can be referenced here to see if the type is supported. - // For now, we will just return ANY to unblock users of databases that support non-standard types. + // In most cases the ANY type will allow non-standard types to be displayable in + // the query requests. However, I think we should add the ability to specify + // a non-standard type factory that can be referenced here to see if the type is supported. + // For now, we will just return ANY to unblock users of databases + // that support non-standard types. return ANY; } return sqlType; diff --git a/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java b/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java new file mode 100644 index 0000000000..425f3819a1 --- /dev/null +++ b/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.calcite.avatica; + +import org.junit.Test; + +import java.sql.Types; + +import static org.junit.Assert.assertEquals; + +/** + * Test class for {@link SqlType}. + */ +public class SqlTypeTest { + + @Test public void ensureAnyIsGivenForNonStandardSqlTypes() { + // Arrange + // Oracle TIMESTAMP WITH TIME ZONE + int timestampWithTimeZoneTypeValue = -101; + // SQL server DateTimeOffset + int dateTimeOffset = -155; + + // Act + SqlType tsSqlType = SqlType.valueOf(timestampWithTimeZoneTypeValue); + SqlType dtoSqlType = SqlType.valueOf(dateTimeOffset); + + // Assert + assertEquals(SqlType.ANY, tsSqlType); + assertEquals(SqlType.ANY, dtoSqlType); + } + + @Test public void ensureStandardSqlTypesAreFound() { + // Arrange + int doubleTypeValue = Types.DOUBLE; + int varcharTypeValue = Types.VARCHAR; + + // Act + SqlType doubleSqlType = SqlType.valueOf(doubleTypeValue); + SqlType varcharSqlType = SqlType.valueOf(varcharTypeValue); + + // Assert + assertEquals(SqlType.DOUBLE, doubleSqlType); + assertEquals(SqlType.VARCHAR, varcharSqlType); + } +} From d4db32250a9d86c7854390a0c339d2d26db053ef Mon Sep 17 00:00:00 2001 From: Jeremy Osterhoudt Date: Thu, 24 Jul 2025 13:47:49 -0700 Subject: [PATCH 3/3] Enhance the know types test --- .../apache/calcite/avatica/SqlTypeTest.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java b/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java index 425f3819a1..397af8490f 100644 --- a/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java +++ b/core/src/test/java/org/apache/calcite/avatica/SqlTypeTest.java @@ -20,6 +20,7 @@ import org.junit.Test; import java.sql.Types; +import java.util.*; import static org.junit.Assert.assertEquals; @@ -43,18 +44,20 @@ public class SqlTypeTest { assertEquals(SqlType.ANY, tsSqlType); assertEquals(SqlType.ANY, dtoSqlType); } - + @Test public void ensureStandardSqlTypesAreFound() { // Arrange - int doubleTypeValue = Types.DOUBLE; - int varcharTypeValue = Types.VARCHAR; + Map typeMap = new HashMap<>(); - // Act - SqlType doubleSqlType = SqlType.valueOf(doubleTypeValue); - SqlType varcharSqlType = SqlType.valueOf(varcharTypeValue); + for (SqlType sqlType : SqlType.values()) { + typeMap.put(sqlType.id, sqlType); + } - // Assert - assertEquals(SqlType.DOUBLE, doubleSqlType); - assertEquals(SqlType.VARCHAR, varcharSqlType); + Set typeValues = typeMap.keySet(); + + // Act and Assert + for (Integer typeValue : typeValues) { + assertEquals(typeMap.get(typeValue), SqlType.valueOf(typeValue)); + } } }