Skip to content

Commit

Permalink
[KYUUBI #5752] JDBC driver implements getDate, getTimestamp and getTi…
Browse files Browse the repository at this point in the history
…me with Calendar in ResultSet

# 🔍 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 🔖

- [ ] 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 ⚰️

#### Behavior With This Pull Request 🎉

#### 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

ddff6a7 [Cheng Pan] Update kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
595226d [Cheng Pan] Update kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
8b386f1 [Cheng Pan] Update kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiArrowBasedResultSet.java
190c6b0 [Cheng Pan] Update kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
dbc1e1e [Cheng Pan] Update kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
e1a36d3 [Cheng Pan] Update kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiBaseResultSet.java
36710d0 [hezhao2] refactor
cd2e36f [hezhao2] add UTs in SparkDataTypeTests
fe60822 [hezhao2] add UTs in SparkDataTypeTests
77313c2 [hezhao2] reformat code style
33e4305 [hezhao2] JDBC driver implements Result#getTimestamp and Result#getTime with Calendar
7623b85 [hezhao2] add Overwrite annotation and remove this method from the parent abstract class
757f037 [hezhao2] JDBC driver implements Result#getDate with Calendar

Lead-authored-by: hezhao2 <hezhao2@cisco.com>
Co-authored-by: Cheng Pan <pan3793@gmail.com>
Signed-off-by: Cheng Pan <chengpan@apache.org>
(cherry picked from commit fcb41f4)
Signed-off-by: Cheng Pan <chengpan@apache.org>
  • Loading branch information
zhaohehuhu and pan3793 committed Dec 5, 2023
1 parent 2b17e48 commit 7ec5c72
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.kyuubi.operation

import java.sql.{Date, Timestamp}
import java.util.Calendar

import org.apache.kyuubi.util.SparkVersionUtil

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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" ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -198,6 +199,32 @@ public Date getDate(String columnName) throws SQLException {
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 {
Expand Down Expand Up @@ -406,6 +433,83 @@ public Timestamp getTimestamp(String columnName) throws SQLException {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -182,6 +183,32 @@ public Date getDate(String columnName) throws SQLException {
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 {
Expand Down Expand Up @@ -412,6 +439,83 @@ public Timestamp getTimestamp(String columnName) throws SQLException {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;
import java.util.Map;

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -436,36 +435,6 @@ default Array getArray(String columnLabel) throws SQLException {
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");
Expand Down

0 comments on commit 7ec5c72

Please sign in to comment.