This is an automated email from the ASF dual-hosted git repository.
chengpan pushed a commit to branch branch-1.8
in repository https://gitbox.apache.org/repos/asf/kyuubi.git
The following commit(s) were added to refs/heads/branch-1.8 by this push:
new 7ec5c72eb [KYUUBI #5752] JDBC driver implements getDate, getTimestamp
and getTime with Calendar in ResultSet
7ec5c72eb is described below
commit 7ec5c72ebb4c8427bff4716b9357a78c3957162b
Author: hezhao2 <[email protected]>
AuthorDate: Tue Dec 5 15:01:48 2023 +0800
[KYUUBI #5752] JDBC driver implements getDate, getTimestamp and getTime
with Calendar in ResultSet
# :mag: Description
## Issue References ๐
This pull request fixes #5650
## Describe Your Solution ๐ง
implemented getDate, getTimestamp and getTime with Calendar in Kyuubi JDBC
driver side
## Types of changes :bookmark:
- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
## Test Plan ๐งช
#### Behavior Without This Pull Request :coffin:
#### Behavior With This Pull Request :tada:
#### Related Unit Tests
---
# Checklists
## ๐ Author Self Checklist
- [x] My code follows the [style
guidelines](https://kyuubi.readthedocs.io/en/master/contributing/code/style.html)
of this project
- [x] I have performed a self-review
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature
works
- [ ] New and existing unit tests pass locally with my changes
- [ ] This patch was not authored or co-authored using [Generative
Tooling](https://www.apache.org/legal/generative-tooling.html)
## ๐ Committer Pre-Merge Checklist
- [ ] Pull request title is okay.
- [ ] No license issues.
- [ ] Milestone correctly set?
- [ ] Test coverage is ok
- [ ] Assignees are selected.
- [ ] Minimum number of approvals
- [ ] No changes are requested
**Be nice. Be informative.**
Closes #5752 from zhaohehuhu/dev-1122.
Closes #5752
ddff6a7aa [Cheng Pan] Update
kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
595226d04 [Cheng Pan] Update
kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
8b386f151 [Cheng Pan] Update
kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
190c6b051 [Cheng Pan] Update
kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
dbc1e1ea7 [Cheng Pan] Update
kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
e1a36d3e8 [Cheng Pan] Update
kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
36710d0b1 [hezhao2] refactor
cd2e36f7c [hezhao2] add UTs in SparkDataTypeTests
fe60822b5 [hezhao2] add UTs in SparkDataTypeTests
77313c224 [hezhao2] reformat code style
33e4305e3 [hezhao2] JDBC driver implements Result#getTimestamp and
Result#getTime with Calendar
7623b854e [hezhao2] add Overwrite annotation and remove this method from
the parent abstract class
757f03701 [hezhao2] JDBC driver implements Result#getDate with Calendar
Lead-authored-by: hezhao2 <[email protected]>
Co-authored-by: Cheng Pan <[email protected]>
Signed-off-by: Cheng Pan <[email protected]>
(cherry picked from commit fcb41f4038625c22e9e476ec3ec653b9d8d58177)
Signed-off-by: Cheng Pan <[email protected]>
---
.../kyuubi/operation/SparkDataTypeTests.scala | 38 ++++++++
.../jdbc/hive/KyuubiArrowBasedResultSet.java | 104 +++++++++++++++++++++
.../kyuubi/jdbc/hive/KyuubiBaseResultSet.java | 104 +++++++++++++++++++++
.../kyuubi/jdbc/hive/adapter/SQLResultSet.java | 31 ------
4 files changed, 246 insertions(+), 31 deletions(-)
diff --git
a/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkDataTypeTests.scala
b/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkDataTypeTests.scala
index 2709bc861..49f6b85d8 100644
---
a/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkDataTypeTests.scala
+++
b/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkDataTypeTests.scala
@@ -18,6 +18,7 @@
package org.apache.kyuubi.operation
import java.sql.{Date, Timestamp}
+import java.util.Calendar
import org.apache.kyuubi.util.SparkVersionUtil
@@ -160,6 +161,23 @@ trait SparkDataTypeTests extends HiveJDBCTestHelper with
SparkVersionUtil {
}
}
+ test("execute statement - select date with calendar") {
+ withJdbcStatement() { statement =>
+ val resultSet = statement.executeQuery("SELECT DATE '2018-11-17' AS col")
+ assert(resultSet.next())
+ assert(resultSet.getDate(
+ "col",
+ Calendar.getInstance()) === Date.valueOf("2018-11-17"))
+ assert(resultSet.getDate(
+ 1,
+ Calendar.getInstance()) === Date.valueOf("2018-11-17"))
+ val metaData = resultSet.getMetaData
+ assert(metaData.getColumnType(1) === java.sql.Types.DATE)
+ assert(metaData.getPrecision(1) === 10)
+ assert(metaData.getScale(1) === 0)
+ }
+ }
+
test("execute statement - select timestamp - second") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery(
@@ -213,6 +231,26 @@ trait SparkDataTypeTests extends HiveJDBCTestHelper with
SparkVersionUtil {
}
}
+ test("execute statement - select timestamp - second with calendar") {
+ withJdbcStatement() { statement =>
+ val resultSet = statement.executeQuery(
+ "SELECT TIMESTAMP '2018-11-17 13:33:33' AS col")
+ assert(resultSet.next())
+ assert(resultSet.getTimestamp(
+ "col",
+ Calendar.getInstance()) === Timestamp.valueOf(
+ "2018-11-17 13:33:33"))
+ assert(resultSet.getTimestamp(
+ 1,
+ Calendar.getInstance()) === Timestamp.valueOf(
+ "2018-11-17 13:33:33"))
+ val metaData = resultSet.getMetaData
+ assert(metaData.getColumnType(1) === java.sql.Types.TIMESTAMP)
+ assert(metaData.getPrecision(1) === 29)
+ assert(metaData.getScale(1) === 9)
+ }
+ }
+
test("execute statement - select daytime interval") {
assume(
resultFormat == "thrift" ||
diff --git
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
index ef5008503..e38eb2b92 100644
---
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
+++
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
@@ -23,6 +23,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.nio.charset.StandardCharsets;
import java.sql.*;
+import java.util.Calendar;
import java.util.List;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.VectorSchemaRoot;
@@ -198,6 +199,32 @@ public abstract class KyuubiArrowBasedResultSet implements
SQLResultSet {
return getDate(findColumn(columnName));
}
+ @Override
+ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
+ Date value = getDate(columnIndex);
+ if (value == null) {
+ return null;
+ }
+ try {
+ return parseDate(value, cal);
+ } catch (IllegalArgumentException e) {
+ throw new KyuubiSQLException("Cannot convert column " + columnIndex + "
to date: " + e, e);
+ }
+ }
+
+ @Override
+ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
+ return this.getDate(findColumn(columnLabel), cal);
+ }
+
+ private Date parseDate(Date value, Calendar cal) {
+ if (cal == null) {
+ cal = Calendar.getInstance();
+ }
+ cal.setTime(value);
+ return new Date(cal.getTimeInMillis());
+ }
+
@Override
public double getDouble(int columnIndex) throws SQLException {
try {
@@ -406,6 +433,83 @@ public abstract class KyuubiArrowBasedResultSet implements
SQLResultSet {
return getTimestamp(findColumn(columnName));
}
+ @Override
+ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws
SQLException {
+ Timestamp value = getTimestamp(columnIndex);
+ if (value == null) {
+ return null;
+ }
+ try {
+ return parseTimestamp(value, cal);
+ } catch (IllegalArgumentException e) {
+ throw new KyuubiSQLException(
+ "Cannot convert column " + columnIndex + " to timestamp: " + e, e);
+ }
+ }
+
+ @Override
+ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws
SQLException {
+ return this.getTimestamp(findColumn(columnLabel), cal);
+ }
+
+ private Timestamp parseTimestamp(Timestamp timestamp, Calendar cal) {
+ if (cal == null) {
+ cal = Calendar.getInstance();
+ }
+ long v = timestamp.getTime();
+ cal.setTimeInMillis(v);
+ timestamp = new Timestamp(cal.getTime().getTime());
+ return timestamp;
+ }
+
+ @Override
+ public Time getTime(int columnIndex) throws SQLException {
+ Object obj = getObject(columnIndex);
+ if (obj == null) {
+ return null;
+ }
+ if (obj instanceof Time) {
+ return (Time) obj;
+ }
+ if (obj instanceof String) {
+ return Time.valueOf((String) obj);
+ }
+ throw new KyuubiSQLException("Illegal conversion");
+ }
+
+ @Override
+ public Time getTime(String columnLabel) throws SQLException {
+ return getTime(findColumn(columnLabel));
+ }
+
+ @Override
+ public Time getTime(int columnIndex, Calendar cal) throws SQLException {
+ Time value = getTime(columnIndex);
+ if (value == null) {
+ return null;
+ }
+ try {
+ return parseTime(value, cal);
+ } catch (IllegalArgumentException e) {
+ throw new KyuubiSQLException("Cannot convert column " + columnIndex + "
to time: " + e, e);
+ }
+ }
+
+ @Override
+ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
+ return this.getTime(findColumn(columnLabel), cal);
+ }
+
+ private Time parseTime(Time date, Calendar cal) {
+ if (cal == null) {
+ cal = Calendar.getInstance();
+ }
+ long v = date.getTime();
+ cal.setTimeInMillis(v);
+ date = new Time(cal.getTime().getTime());
+ return date;
+ }
+
@Override
public int getType() throws SQLException {
return ResultSet.TYPE_FORWARD_ONLY;
diff --git
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
index a9d32e8ca..7386fc7dd 100644
---
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
+++
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
@@ -23,6 +23,7 @@ import java.math.BigDecimal;
import java.math.MathContext;
import java.nio.charset.StandardCharsets;
import java.sql.*;
+import java.util.Calendar;
import java.util.List;
import org.apache.hive.service.rpc.thrift.TTableSchema;
import org.apache.hive.service.rpc.thrift.TTypeId;
@@ -182,6 +183,32 @@ public abstract class KyuubiBaseResultSet implements
SQLResultSet {
return getDate(findColumn(columnName));
}
+ @Override
+ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
+ Date value = getDate(columnIndex);
+ if (value == null) {
+ return null;
+ }
+ try {
+ return parseDate(value, cal);
+ } catch (IllegalArgumentException e) {
+ throw new KyuubiSQLException("Cannot convert column " + columnIndex + "
to date: " + e, e);
+ }
+ }
+
+ @Override
+ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
+ return this.getDate(findColumn(columnLabel), cal);
+ }
+
+ private Date parseDate(Date value, Calendar cal) {
+ if (cal == null) {
+ cal = Calendar.getInstance();
+ }
+ cal.setTime(value);
+ return new Date(cal.getTimeInMillis());
+ }
+
@Override
public double getDouble(int columnIndex) throws SQLException {
try {
@@ -412,6 +439,83 @@ public abstract class KyuubiBaseResultSet implements
SQLResultSet {
return getTimestamp(findColumn(columnName));
}
+ @Override
+ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws
SQLException {
+ Timestamp value = getTimestamp(columnIndex);
+ if (value == null) {
+ return null;
+ }
+ try {
+ return parseTimestamp(value, cal);
+ } catch (IllegalArgumentException e) {
+ throw new KyuubiSQLException(
+ "Cannot convert column " + columnIndex + " to timestamp: " + e, e);
+ }
+ }
+
+ @Override
+ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws
SQLException {
+ return this.getTimestamp(findColumn(columnLabel), cal);
+ }
+
+ private Timestamp parseTimestamp(Timestamp timestamp, Calendar cal) {
+ if (cal == null) {
+ cal = Calendar.getInstance();
+ }
+ long v = timestamp.getTime();
+ cal.setTimeInMillis(v);
+ timestamp = new Timestamp(cal.getTime().getTime());
+ return timestamp;
+ }
+
+ @Override
+ public Time getTime(int columnIndex) throws SQLException {
+ Object obj = getObject(columnIndex);
+ if (obj == null) {
+ return null;
+ }
+ if (obj instanceof Time) {
+ return (Time) obj;
+ }
+ if (obj instanceof String) {
+ return Time.valueOf((String) obj);
+ }
+ throw new KyuubiSQLException("Illegal conversion");
+ }
+
+ @Override
+ public Time getTime(String columnLabel) throws SQLException {
+ return getTime(findColumn(columnLabel));
+ }
+
+ @Override
+ public Time getTime(int columnIndex, Calendar cal) throws SQLException {
+ Time value = getTime(columnIndex);
+ if (value == null) {
+ return null;
+ }
+ try {
+ return parseTime(value, cal);
+ } catch (IllegalArgumentException e) {
+ throw new KyuubiSQLException("Cannot convert column " + columnIndex + "
to time: " + e, e);
+ }
+ }
+
+ @Override
+ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
+ return this.getTime(findColumn(columnLabel), cal);
+ }
+
+ private Time parseTime(Time date, Calendar cal) {
+ if (cal == null) {
+ cal = Calendar.getInstance();
+ }
+ long v = date.getTime();
+ cal.setTimeInMillis(v);
+ date = new Time(cal.getTime().getTime());
+ return date;
+ }
+
@Override
public int getType() throws SQLException {
return ResultSet.TYPE_FORWARD_ONLY;
diff --git
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/adapter/SQLResultSet.java
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/adapter/SQLResultSet.java
index 70c8ff4fe..32f1c88bc 100644
---
a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/adapter/SQLResultSet.java
+++
b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/adapter/SQLResultSet.java
@@ -22,7 +22,6 @@ import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
-import java.util.Calendar;
import java.util.Map;
@SuppressWarnings("deprecation")
@@ -436,36 +435,6 @@ public interface SQLResultSet extends ResultSet {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- @Override
- default Date getDate(int columnIndex, Calendar cal) throws SQLException {
- throw new SQLFeatureNotSupportedException("Method not supported");
- }
-
- @Override
- default Date getDate(String columnLabel, Calendar cal) throws SQLException {
- throw new SQLFeatureNotSupportedException("Method not supported");
- }
-
- @Override
- default Time getTime(int columnIndex, Calendar cal) throws SQLException {
- throw new SQLFeatureNotSupportedException("Method not supported");
- }
-
- @Override
- default Time getTime(String columnLabel, Calendar cal) throws SQLException {
- throw new SQLFeatureNotSupportedException("Method not supported");
- }
-
- @Override
- default Timestamp getTimestamp(int columnIndex, Calendar cal) throws
SQLException {
- throw new SQLFeatureNotSupportedException("Method not supported");
- }
-
- @Override
- default Timestamp getTimestamp(String columnLabel, Calendar cal) throws
SQLException {
- throw new SQLFeatureNotSupportedException("Method not supported");
- }
-
@Override
default URL getURL(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");