Skip to content

Commit 1d15179

Browse files
authored
Merge pull request #369 from youngsofun/ts
feat: support java.time and timestamp_tz
2 parents 91a395e + 8814e07 commit 1d15179

7 files changed

Lines changed: 318 additions & 262 deletions

File tree

README.md

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
![Apache License 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)
44
[![databend-jdbc](https://img.shields.io/maven-central/v/com.databend/databend-jdbc?style=flat-square)](https://central.sonatype.dev/artifact/com.databend/databend-jdbc/0.0.1)
55

6+
## Highlights
7+
8+
- Databend-specific interfaces to stream files into tables or stages with `loadStreamToTable`, `uploadStream`, and `downloadStream`.
9+
- Temporal APIs use session timezone to avoid depending on JVM default zone and support modern `java.time`.
10+
611
## Prerequisites
712

813
The Databend JDBC driver requires Java 8 or later.
@@ -73,36 +78,61 @@ public class Main {
7378
you can call `while(r.next(){})` to iterate over the result set.
7479
3. For other SQL such as `create/drop table` non-query type SQL, you can call `statement.execute()` directly.
7580

76-
## JDBC Java type mapping
77-
The Databend type is mapped to Java type as follows:
7881

79-
| Databend Type | Java Type |
80-
|---------------|------------|
81-
| TINYINT | Byte |
82-
| SMALLINT | Short |
83-
| INT | Integer |
84-
| BIGINT | Long |
85-
| UInt8 | Short |
86-
| UInt16 | Integer |
87-
| UInt32 | Long |
88-
| UInt64 | BigInteger |
89-
| Float32 | Float |
90-
| Float64 | Double |
91-
| String | String |
92-
| Date | String |
93-
| TIMESTAMP | String |
94-
| Bitmap | byte[] |
95-
| Array | String |
96-
| Decimal | BigDecimal |
97-
| Tuple | String |
98-
| Map | String |
99-
| VARIANT | String |
82+
## Connection Parameters
10083

10184
For detailed references, please take a look at the following Links:
10285

10386
1. [Connection Parameters](./docs/Connection.md) : detailed documentation about how to use connection parameters in a
10487
jdbc connection
10588

89+
## JDBC Java type mapping
90+
The Databend type is mapped to Java type as follows:
91+
92+
| Databend Type | Java Type |
93+
|---------------|----------------|
94+
| TINYINT | Byte |
95+
| SMALLINT | Short |
96+
| INT | Integer |
97+
| BIGINT | Long |
98+
| UInt8 | Short |
99+
| UInt16 | Integer |
100+
| UInt32 | Long |
101+
| UInt64 | BigInteger |
102+
| Float32 | Float |
103+
| Float64 | Double |
104+
| Decimal | BigDecimal |
105+
| String | String |
106+
| Date | LocalDate |
107+
| TIMESTAMP | ZonedDateTime |
108+
| TIMESTAMP_TZ | OffsetDateTime |
109+
| Bitmap | byte[] |
110+
| Array | String |
111+
| Tuple | String |
112+
| Map | String |
113+
| VARIANT | String |
114+
115+
### Temporal types
116+
117+
we recommend using `java.time` to avoid ambiguity and letting the driver format values via these APIs:
118+
119+
```
120+
void setObject(int parameterIndex, Object x)
121+
<T> T getObject(int columnIndex, Class<T> type)
122+
```
123+
124+
- TIMESTAMP_TZ and TIMESTAMP map to `OffsetDateTime`, `ZonedDateTime`, `Instant` and `LocalDateTime` (TIMESTAMP_TZ can return `OffsetDateTime` but not `ZonedDateTime`).
125+
- Date maps to `LocalDate`, and `getObject(..., LocalDate.class)` now mirrors what `getDate().toLocalDate()` returns.
126+
- When parameters do not contain a timezone, Databend uses the session timezone (not the JVM zone) when storing/returning dates on databend-jdbc ≥ 0.4.3 AND databend-query ≥1.2.844.
127+
- `getString` return the display of default mapping type.
128+
129+
Timestamp/Date are also supported, note that:
130+
131+
- `getTimestamp(int, Calendar cal)` is the same as `getTimestamp(int)` (the cal is omitted) and
132+
`getObject(int, Instant.classes).toTimestamp()`
133+
- `setTimestamp(int, Calendar cal)` is diff with `setTimestamp(int)`, the epoch is adjusted according to timezone in cal
134+
- `setDate`/`getDate` still use the JVM timezone, and `setObject(localDate)` is equivalent to `setDate(Date.valueOf(localDate))`.
135+
106136

107137
# Unwrapping to Databend-specific interfaces
108138

@@ -171,4 +201,3 @@ Download a single file in the stage as `InputStream`
171201
```
172202
InputStream downloadStream(String stageName, String filePathInStage) throws SQLException;
173203
```
174-

databend-client/src/main/java/com/databend/client/data/DatabendDataType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public enum DatabendDataType {
5555

5656
DATE(Types.DATE, DatabendTypes.DATE, false, 10, true, "Date"),
5757
TIMESTAMP(Types.TIMESTAMP, DatabendTypes.TIMESTAMP, false, 26, true, "DateTime", "TIMESTAMP"),
58+
TIMESTAMP_TZ(Types.TIMESTAMP_WITH_TIMEZONE, DatabendTypes.TIMESTAMP, false, 32, true, "TIMESTAMP_TZ"),
5859

5960
ARRAY(Types.ARRAY, DatabendTypes.ARRAY, false, 0, false, "Array"),
6061
MAP(Types.OTHER, DatabendTypes.MAP, false, 0, false, "Map"),
@@ -119,6 +120,8 @@ public static DatabendDataType getByTypeName(String typeName) {
119120
return DATE;
120121
} else if (DatabendTypes.TIMESTAMP.equalsIgnoreCase(typeName)) {
121122
return TIMESTAMP;
123+
} else if (DatabendTypes.TIMESTAMP_TZ.equalsIgnoreCase(typeName)) {
124+
return TIMESTAMP_TZ;
122125
} else if (DatabendTypes.VARIANT.equalsIgnoreCase(typeName)) {
123126
return VARIANT;
124127
} else if (DatabendTypes.BITMAP.equalsIgnoreCase(typeName)) {

databend-client/src/main/java/com/databend/client/data/DatabendTypes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public final class DatabendTypes {
3232
public static final String DATETIME = "datetime";
3333
public static final String DATETIME64 = "datetime64";
3434
public static final String TIMESTAMP = "timestamp";
35+
public static final String TIMESTAMP_TZ = "timestamp_tz";
3536
public static final String STRING = "string";
3637
public static final String STRUCT = "struct";
3738
public static final String ARRAY = "array";

0 commit comments

Comments
 (0)