24
24
import java .sql .Date ;
25
25
import java .sql .Time ;
26
26
import java .sql .Timestamp ;
27
- import java .time .LocalDateTime ;
28
- import java .time .temporal .ChronoUnit ;
29
27
import java .util .Calendar ;
30
28
import java .util .TimeZone ;
31
29
import java .util .concurrent .TimeUnit ;
35
33
import org .apache .arrow .driver .jdbc .accessor .ArrowFlightJdbcAccessorFactory ;
36
34
import org .apache .arrow .vector .TimeStampVector ;
37
35
import org .apache .arrow .vector .types .pojo .ArrowType ;
38
- import org .apache .arrow .vector .util .DateUtility ;
39
36
40
37
/**
41
38
* Accessor for the Arrow types extending from {@link TimeStampVector}.
@@ -45,16 +42,8 @@ public class ArrowFlightJdbcTimeStampVectorAccessor extends ArrowFlightJdbcAcces
45
42
private final TimeZone timeZone ;
46
43
private final Getter getter ;
47
44
private final TimeUnit timeUnit ;
48
- private final LongToLocalDateTime longToLocalDateTime ;
49
45
private final Holder holder ;
50
46
51
- /**
52
- * Functional interface used to convert a number (in any time resolution) to LocalDateTime.
53
- */
54
- interface LongToLocalDateTime {
55
- LocalDateTime fromLong (long value );
56
- }
57
-
58
47
/**
59
48
* Instantiate a ArrowFlightJdbcTimeStampVectorAccessor for given vector.
60
49
*/
@@ -67,7 +56,6 @@ public ArrowFlightJdbcTimeStampVectorAccessor(TimeStampVector vector,
67
56
68
57
this .timeZone = getTimeZoneForVector (vector );
69
58
this .timeUnit = getTimeUnitForVector (vector );
70
- this .longToLocalDateTime = getLongToLocalDateTimeForVector (vector , this .timeZone );
71
59
}
72
60
73
61
@ Override
@@ -80,55 +68,56 @@ public Object getObject() {
80
68
return this .getTimestamp (null );
81
69
}
82
70
83
- private LocalDateTime getLocalDateTime ( Calendar calendar ) {
71
+ private Long getLocalDateTimeMillis ( ) {
84
72
getter .get (getCurrentRow (), holder );
85
73
this .wasNull = holder .isSet == 0 ;
86
74
this .wasNullConsumer .setWasNull (this .wasNull );
87
75
if (this .wasNull ) {
88
76
return null ;
89
77
}
90
78
91
- long value = holder .value ;
92
-
93
- LocalDateTime localDateTime = this .longToLocalDateTime .fromLong (value );
79
+ final long value = holder .value ;
80
+ final long millis = this .timeUnit .toMillis (value );
94
81
95
- if (calendar != null ) {
96
- TimeZone timeZone = calendar .getTimeZone ();
97
- long millis = this .timeUnit .toMillis (value );
98
- localDateTime = localDateTime
99
- .minus (timeZone .getOffset (millis ) - this .timeZone .getOffset (millis ), ChronoUnit .MILLIS );
100
- }
101
- return localDateTime ;
82
+ return millis + this .timeZone .getOffset (millis );
102
83
}
103
84
104
85
@ Override
105
86
public Date getDate (Calendar calendar ) {
106
- LocalDateTime localDateTime = getLocalDateTime ( calendar );
107
- if (localDateTime == null ) {
87
+ Long millis = getLocalDateTimeMillis ( );
88
+ if (millis == null ) {
108
89
return null ;
109
90
}
110
91
111
- return new Date (Timestamp . valueOf ( localDateTime ). getTime ( ));
92
+ return new Date (applyCalendarOffset ( calendar , millis ));
112
93
}
113
94
114
95
@ Override
115
96
public Time getTime (Calendar calendar ) {
116
- LocalDateTime localDateTime = getLocalDateTime ( calendar );
117
- if (localDateTime == null ) {
97
+ Long millis = getLocalDateTimeMillis ( );
98
+ if (millis == null ) {
118
99
return null ;
119
100
}
120
101
121
- return new Time (Timestamp . valueOf ( localDateTime ). getTime ( ));
102
+ return new Time (applyCalendarOffset ( calendar , millis ));
122
103
}
123
104
124
105
@ Override
125
106
public Timestamp getTimestamp (Calendar calendar ) {
126
- LocalDateTime localDateTime = getLocalDateTime ( calendar );
127
- if (localDateTime == null ) {
107
+ Long millis = getLocalDateTimeMillis ( );
108
+ if (millis == null ) {
128
109
return null ;
129
110
}
130
111
131
- return Timestamp .valueOf (localDateTime );
112
+ return new Timestamp (applyCalendarOffset (calendar , millis ));
113
+ }
114
+
115
+ private long applyCalendarOffset (final Calendar calendar , final long millis ) {
116
+ if (calendar == null ) {
117
+ return millis - Calendar .getInstance (TimeZone .getDefault ()).getTimeZone ().getOffset (millis );
118
+ }
119
+
120
+ return millis - calendar .getTimeZone ().getOffset (millis ) + this .timeZone .getOffset (millis );
132
121
}
133
122
134
123
protected static TimeUnit getTimeUnitForVector (TimeStampVector vector ) {
@@ -149,28 +138,6 @@ protected static TimeUnit getTimeUnitForVector(TimeStampVector vector) {
149
138
}
150
139
}
151
140
152
- protected static LongToLocalDateTime getLongToLocalDateTimeForVector (TimeStampVector vector ,
153
- TimeZone timeZone ) {
154
- String timeZoneID = timeZone .getID ();
155
-
156
- ArrowType .Timestamp arrowType =
157
- (ArrowType .Timestamp ) vector .getField ().getFieldType ().getType ();
158
-
159
- switch (arrowType .getUnit ()) {
160
- case NANOSECOND :
161
- return nanoseconds -> DateUtility .getLocalDateTimeFromEpochNano (nanoseconds , timeZoneID );
162
- case MICROSECOND :
163
- return microseconds -> DateUtility .getLocalDateTimeFromEpochMicro (microseconds , timeZoneID );
164
- case MILLISECOND :
165
- return milliseconds -> DateUtility .getLocalDateTimeFromEpochMilli (milliseconds , timeZoneID );
166
- case SECOND :
167
- return seconds -> DateUtility .getLocalDateTimeFromEpochMilli (
168
- TimeUnit .SECONDS .toMillis (seconds ), timeZoneID );
169
- default :
170
- throw new UnsupportedOperationException ("Invalid Arrow time unit" );
171
- }
172
- }
173
-
174
141
protected static TimeZone getTimeZoneForVector (TimeStampVector vector ) {
175
142
ArrowType .Timestamp arrowType =
176
143
(ArrowType .Timestamp ) vector .getField ().getFieldType ().getType ();
0 commit comments