From f7b06af9a12f85c6d6bed1c2735df5ff388d97f5 Mon Sep 17 00:00:00 2001 From: Gabriel Escobar Date: Thu, 24 Feb 2022 17:41:29 -0300 Subject: [PATCH] Change Illegal Argument Exception to SQL Exception for Date, Time and Timestamp types --- .../accessor/ArrowFlightJdbcAccessor.java | 7 ++- ...actArrowFlightJdbcUnionVectorAccessor.java | 10 +-- .../ArrowFlightJdbcVarCharVectorAccessor.java | 61 +++++++++++-------- .../accessor/ArrowFlightJdbcAccessorTest.java | 7 ++- ...rrowFlightJdbcUnionVectorAccessorTest.java | 7 ++- ...owFlightJdbcVarCharVectorAccessorTest.java | 6 +- 6 files changed, 57 insertions(+), 41 deletions(-) diff --git a/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java b/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java index aa4513cce451..0f50d3c4a461 100644 --- a/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java +++ b/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessor.java @@ -30,6 +30,7 @@ import java.sql.Date; import java.sql.NClob; import java.sql.Ref; +import java.sql.SQLException; import java.sql.SQLXML; import java.sql.Struct; import java.sql.Time; @@ -182,17 +183,17 @@ public Struct getStruct() { } @Override - public Date getDate(final Calendar calendar) { + public Date getDate(final Calendar calendar) throws SQLException { throw getOperationNotSupported(this.getClass()); } @Override - public Time getTime(final Calendar calendar) { + public Time getTime(final Calendar calendar) throws SQLException { throw getOperationNotSupported(this.getClass()); } @Override - public Timestamp getTimestamp(final Calendar calendar) { + public Timestamp getTimestamp(final Calendar calendar) throws SQLException { throw getOperationNotSupported(this.getClass()); } diff --git a/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java b/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java index c24296bbc102..e276660cd04f 100644 --- a/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java +++ b/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessor.java @@ -27,6 +27,7 @@ import java.sql.Date; import java.sql.NClob; import java.sql.Ref; +import java.sql.SQLException; import java.sql.SQLXML; import java.sql.Struct; import java.sql.Time; @@ -57,8 +58,7 @@ public abstract class AbstractArrowFlightJdbcUnionVectorAccessor extends ArrowFl new ArrowFlightJdbcNullVectorAccessor((boolean wasNull) -> { }); - protected AbstractArrowFlightJdbcUnionVectorAccessor( - IntSupplier currentRowSupplier, + protected AbstractArrowFlightJdbcUnionVectorAccessor(IntSupplier currentRowSupplier, ArrowFlightJdbcAccessorFactory.WasNullConsumer setCursorWasNull) { super(currentRowSupplier, setCursorWasNull); } @@ -213,17 +213,17 @@ public Struct getStruct() { } @Override - public Date getDate(Calendar calendar) { + public Date getDate(Calendar calendar) throws SQLException { return getAccessor().getDate(calendar); } @Override - public Time getTime(Calendar calendar) { + public Time getTime(Calendar calendar) throws SQLException { return getAccessor().getTime(calendar); } @Override - public Timestamp getTimestamp(Calendar calendar) { + public Timestamp getTimestamp(Calendar calendar) throws SQLException { return getAccessor().getTimestamp(calendar); } diff --git a/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java b/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java index 99f294d86587..fc6d5458f0db 100644 --- a/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java +++ b/java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessor.java @@ -23,6 +23,7 @@ import java.io.Reader; import java.math.BigDecimal; import java.sql.Date; +import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; @@ -156,38 +157,50 @@ public Reader getCharacterStream() { } @Override - public Date getDate(Calendar calendar) { - Date date = Date.valueOf(getString()); - if (calendar == null) { - return date; - } + public Date getDate(Calendar calendar) throws SQLException { + try { + Date date = Date.valueOf(getString()); + if (calendar == null) { + return date; + } - // Use Calendar to apply time zone's offset - long milliseconds = date.getTime(); - return new Date(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); + // Use Calendar to apply time zone's offset + long milliseconds = date.getTime(); + return new Date(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); + } catch (Exception e) { + throw new SQLException(e); + } } @Override - public Time getTime(Calendar calendar) { - Time time = Time.valueOf(getString()); - if (calendar == null) { - return time; - } + public Time getTime(Calendar calendar) throws SQLException { + try { + Time time = Time.valueOf(getString()); + if (calendar == null) { + return time; + } - // Use Calendar to apply time zone's offset - long milliseconds = time.getTime(); - return new Time(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); + // Use Calendar to apply time zone's offset + long milliseconds = time.getTime(); + return new Time(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); + } catch (Exception e) { + throw new SQLException(e); + } } @Override - public Timestamp getTimestamp(Calendar calendar) { - Timestamp timestamp = Timestamp.valueOf(getString()); - if (calendar == null) { - return timestamp; - } + public Timestamp getTimestamp(Calendar calendar) throws SQLException { + try { + Timestamp timestamp = Timestamp.valueOf(getString()); + if (calendar == null) { + return timestamp; + } - // Use Calendar to apply time zone's offset - long milliseconds = timestamp.getTime(); - return new Timestamp(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); + // Use Calendar to apply time zone's offset + long milliseconds = timestamp.getTime(); + return new Timestamp(DateTimeUtils.applyCalendarOffset(milliseconds, calendar)); + } catch (Exception e) { + throw new SQLException(e); + } } } diff --git a/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java b/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java index 5a46d4e1221b..aab023da4b55 100644 --- a/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java +++ b/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/ArrowFlightJdbcAccessorTest.java @@ -22,6 +22,7 @@ import java.math.BigDecimal; import java.nio.charset.StandardCharsets; +import java.sql.SQLException; import java.util.HashMap; import java.util.Map; @@ -332,19 +333,19 @@ public void testShouldFailToGetNCharacterStream() { } @Test(expected = UnsupportedOperationException.class) - public void testShouldFailToGetDate() { + public void testShouldFailToGetDate() throws SQLException { when(accessor.getDate(null)).thenCallRealMethod(); accessor.getDate(null); } @Test(expected = UnsupportedOperationException.class) - public void testShouldFailToGetTime() { + public void testShouldFailToGetTime() throws SQLException { when(accessor.getTime(null)).thenCallRealMethod(); accessor.getTime(null); } @Test(expected = UnsupportedOperationException.class) - public void testShouldFailToGetTimestamp() { + public void testShouldFailToGetTimestamp() throws SQLException { when(accessor.getTimestamp(null)).thenCallRealMethod(); accessor.getTimestamp(null); } diff --git a/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java b/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java index f42289cb5f5e..c6ed42ba9d6c 100644 --- a/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java +++ b/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcUnionVectorAccessorTest.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.sql.SQLException; import java.util.Calendar; import java.util.Map; @@ -230,21 +231,21 @@ public void testGetObjectWithClassUsesSpecificAccessor() { } @Test - public void testGetTimestampUsesSpecificAccessor() { + public void testGetTimestampUsesSpecificAccessor() throws SQLException { Calendar calendar = Calendar.getInstance(); accessor.getTimestamp(calendar); verify(innerAccessor).getTimestamp(calendar); } @Test - public void testGetTimeUsesSpecificAccessor() { + public void testGetTimeUsesSpecificAccessor() throws SQLException { Calendar calendar = Calendar.getInstance(); accessor.getTime(calendar); verify(innerAccessor).getTime(calendar); } @Test - public void testGetDateUsesSpecificAccessor() { + public void testGetDateUsesSpecificAccessor() throws SQLException { Calendar calendar = Calendar.getInstance(); accessor.getDate(calendar); verify(innerAccessor).getDate(calendar); diff --git a/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java b/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java index 09f6a1cd9e77..57ba97200372 100644 --- a/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java +++ b/java/flight/flight-jdbc-driver/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/text/ArrowFlightJdbcVarCharVectorAccessorTest.java @@ -473,7 +473,7 @@ public void testShouldGetDateThrowsExceptionForNonDateValue() throws Exception { Text value = new Text("Invalid value for date."); when(getter.get(0)).thenReturn(value); - thrown.expect(IllegalArgumentException.class); + thrown.expect(SQLException.class); accessor.getDate(null); } @@ -513,7 +513,7 @@ public void testShouldGetTimeThrowsExceptionForNonTimeValue() throws Exception { Text value = new Text("Invalid value for time."); when(getter.get(0)).thenReturn(value); - thrown.expect(IllegalArgumentException.class); + thrown.expect(SQLException.class); accessor.getTime(null); } @@ -549,7 +549,7 @@ public void testShouldGetTimestampThrowsExceptionForNonTimeValue() throws Except Text value = new Text("Invalid value for timestamp."); when(getter.get(0)).thenReturn(value); - thrown.expect(IllegalArgumentException.class); + thrown.expect(SQLException.class); accessor.getTimestamp(null); }