From c883acb86f8dbf9e24c6faa2664c5d7235de19c0 Mon Sep 17 00:00:00 2001 From: Knut Forkalsrud Date: Thu, 30 Dec 2021 15:40:54 -0800 Subject: [PATCH] [CALCITE-4970] Support type conversion when no conversion necessary --- .../calcite/avatica/util/AbstractCursor.java | 4 ++++ .../avatica/util/NumberAccessorTest.java | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java b/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java index f05cbcc6d3..f0988f0ca6 100644 --- a/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java +++ b/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java @@ -423,6 +423,10 @@ public Reader getNCharacterStream() throws SQLException { } public T getObject(Class type) throws SQLException { + Object val = getObject(); + if (val == null || type == null || type.isAssignableFrom(val.getClass())) { + return (T) val; + } throw cannotConvert("Object (with type)"); } } diff --git a/core/src/test/java/org/apache/calcite/avatica/util/NumberAccessorTest.java b/core/src/test/java/org/apache/calcite/avatica/util/NumberAccessorTest.java index f1b2ef1fcb..ea969d5b1b 100644 --- a/core/src/test/java/org/apache/calcite/avatica/util/NumberAccessorTest.java +++ b/core/src/test/java/org/apache/calcite/avatica/util/NumberAccessorTest.java @@ -50,6 +50,26 @@ public void testBigDecimalZeroScale() throws SQLException { assertEquals(orig, accessor.getBigDecimal(0)); } + + @Test + public void testGetLongObjectWithType() throws SQLException { + final Long orig = 1337L; + NumberAccessor accessor = new AbstractCursor.NumberAccessor( + new Getter() { + @Override public Object getObject() { + return orig; + } + + @Override public boolean wasNull() { + return false; + } + }, + 0); + + assertEquals(orig, accessor.getObject(Long.class)); + } + + } // End NumberAccessorTest.java