forked from ClickHouse/clickhouse-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickHouseDateTimeValue.java
More file actions
397 lines (345 loc) · 12.2 KB
/
ClickHouseDateTimeValue.java
File metadata and controls
397 lines (345 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package com.clickhouse.data.value;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
import com.clickhouse.data.ClickHouseChecker;
import com.clickhouse.data.ClickHouseDataConfig;
import com.clickhouse.data.ClickHouseValue;
import com.clickhouse.data.ClickHouseValues;
/**
* Wrapper class of {@link LocalDateTime}.
*/
public class ClickHouseDateTimeValue extends ClickHouseObjectValue<LocalDateTime> {
/**
* Default value.
*/
public static final LocalDateTime DEFAULT = ClickHouseInstantValue.DEFAULT.atOffset(ZoneOffset.UTC)
.toLocalDateTime();
static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* Create a new instance representing null getValue().
*
* @param scale scale
* @param tz time zone, null is treated as {@code UTC}
* @return new instance representing null value
*/
public static ClickHouseDateTimeValue ofNull(int scale, TimeZone tz) {
return ofNull(null, scale, tz);
}
/**
* Update given value to null or create a new instance if {@code ref} is null.
*
* @param ref object to update, could be null
* @param scale scale, only used when {@code ref} is null
* @param tz time zone, null is treated as {@code UTC}
* @return same object as {@code ref} or a new instance if it's null
*/
public static ClickHouseDateTimeValue ofNull(ClickHouseValue ref, int scale, TimeZone tz) {
return ref instanceof ClickHouseDateTimeValue
? (ClickHouseDateTimeValue) ((ClickHouseDateTimeValue) ref).set(null)
: new ClickHouseDateTimeValue(null, scale, tz);
}
/**
* Wrap the given getValue().
*
* @param value value
* @param scale scale
* @param tz time zone, null is treated as {@code UTC}
* @return object representing the value
*/
public static ClickHouseDateTimeValue of(LocalDateTime value, int scale, TimeZone tz) {
return of(null, value, scale, tz);
}
/**
* Wrap the given getValue().
*
* @param value UTC date time in string
* @param scale scale
* @param tz time zone, null is treated as {@code UTC}
* @return object representing the value
*/
public static ClickHouseDateTimeValue of(String value, int scale, TimeZone tz) {
return of(null, value, scale, tz);
}
/**
* Update value of the given object or create a new instance if {@code ref} is
* null.
*
* @param ref object to update, could be null
* @param value value
* @param scale scale, only used when {@code ref} is null
* @param tz time zone, null is treated as {@code UTC}
* @return same object as {@code ref} or a new instance if it's null
*/
public static ClickHouseDateTimeValue of(ClickHouseValue ref, LocalDateTime value, int scale, TimeZone tz) {
return ref instanceof ClickHouseDateTimeValue
? (ClickHouseDateTimeValue) ((ClickHouseDateTimeValue) ref).set(value)
: new ClickHouseDateTimeValue(value, scale, tz);
}
/**
* Update value of the given object or create a new instance if {@code ref} is
* null.
*
* @param ref object to update, could be null
* @param value UTC date time in string
* @param scale scale
* @param tz time zone, null is treated as {@code UTC}
* @return same object as {@code ref} or a new instance if it's null
*/
public static ClickHouseDateTimeValue of(ClickHouseValue ref, String value, int scale, TimeZone tz) {
LocalDateTime dateTime = value == null || value.isEmpty() ? null
: LocalDateTime.parse(value, ClickHouseValues.DATETIME_FORMATTER);
return of(ref, dateTime, scale, tz);
}
private final int scale;
private final TimeZone tz;
private final LocalDateTime defaultValue;
protected ClickHouseDateTimeValue(LocalDateTime value, int scale, TimeZone tz) {
super(value);
this.scale = ClickHouseChecker.between(scale, ClickHouseValues.PARAM_SCALE, 0, 9);
this.tz = tz != null ? tz : ClickHouseValues.UTC_TIMEZONE;
this.defaultValue = this.tz.equals(ClickHouseValues.UTC_TIMEZONE) ? DEFAULT
: ClickHouseOffsetDateTimeValue.DEFAULT.toZonedDateTime().withZoneSameInstant(this.tz.toZoneId())
.toLocalDateTime();
}
public int getScale() {
return scale;
}
@Override
public ClickHouseDateTimeValue copy(boolean deep) {
return new ClickHouseDateTimeValue(getValue(), scale, tz);
}
@Override
public byte asByte() {
return isNullOrEmpty() ? (byte) 0 : (byte) getValue().atZone(tz.toZoneId()).toEpochSecond();
}
@Override
public short asShort() {
return isNullOrEmpty() ? (short) 0 : (short) getValue().atZone(tz.toZoneId()).toEpochSecond();
}
@Override
public int asInteger() {
return isNullOrEmpty() ? 0 : (int) getValue().atZone(tz.toZoneId()).toEpochSecond();
}
@Override
public long asLong() {
return isNullOrEmpty() ? 0L : getValue().atZone(tz.toZoneId()).toEpochSecond();
}
@Override
public float asFloat() {
return isNullOrEmpty() ? 0F
: getValue().atZone(tz.toZoneId()).toEpochSecond()
+ getValue().getNano() / ClickHouseValues.NANOS.floatValue();
}
@Override
public double asDouble() {
return isNullOrEmpty() ? 0D
: getValue().atZone(tz.toZoneId()).toEpochSecond()
+ getValue().getNano() / ClickHouseValues.NANOS.doubleValue();
}
@Override
public BigInteger asBigInteger() {
return isNullOrEmpty() ? null : BigInteger.valueOf(getValue().atZone(tz.toZoneId()).toEpochSecond());
}
@Override
public BigDecimal asBigDecimal(int scale) {
LocalDateTime value = getValue();
BigDecimal v = null;
if (value != null) {
int nanoSeconds = value.getNano();
v = new BigDecimal(BigInteger.valueOf(value.atZone(tz.toZoneId()).toEpochSecond()), scale);
if (scale != 0 && nanoSeconds != 0) {
v = v.add(BigDecimal.valueOf(nanoSeconds).divide(ClickHouseValues.NANOS).setScale(scale,
ClickHouseDataConfig.DEFAULT_ROUNDING_MODE));
}
}
return v;
}
@Override
public LocalDate asDate() {
return isNullOrEmpty() ? null : asDateTime(0).toLocalDate();
}
@Override
public LocalDateTime asDateTime(int scale) {
return getValue();
}
@Override
public Instant asInstant(int scale) {
return isNullOrEmpty() ? null : getValue().atZone(tz.toZoneId()).toInstant();
}
@Override
public OffsetDateTime asOffsetDateTime(int scale) {
if (isNullOrEmpty()) {
return null;
}
return getValue().atZone(tz.toZoneId()).toOffsetDateTime();
}
@Override
public ZonedDateTime asZonedDateTime(int scale) {
if (isNullOrEmpty()) {
return null;
}
return getValue().atZone(tz.toZoneId());
}
@Override
public Object asObject() {
return getValue();
}
@Override
public String asString() {
if (isNullOrEmpty()) {
return null;
}
// different formatter for each scale?
return getValue().format(scale > 0 ? ClickHouseValues.DATETIME_FORMATTER : dateTimeFormatter);
}
@Override
public ClickHouseDateTimeValue resetToDefault() {
set(defaultValue);
return this;
}
@Override
public String toSqlExpression() {
if (isNullOrEmpty()) {
return ClickHouseValues.NULL_EXPR;
}
return new StringBuilder().append('\'')
.append(getValue().format(scale > 0 ? ClickHouseValues.DATETIME_FORMATTER : dateTimeFormatter))
.append('\'').toString();
}
@Override
public ClickHouseDateTimeValue update(byte value) {
return update(BigInteger.valueOf(value));
}
@Override
public ClickHouseDateTimeValue update(short value) {
return update(BigInteger.valueOf(value));
}
@Override
public ClickHouseDateTimeValue update(int value) {
return update(BigInteger.valueOf(value));
}
@Override
public ClickHouseDateTimeValue update(long value) {
return update(BigInteger.valueOf(value));
}
@Override
public ClickHouseDateTimeValue update(float value) {
return update(BigDecimal.valueOf(value));
}
@Override
public ClickHouseDateTimeValue update(double value) {
return update(BigDecimal.valueOf(value));
}
@Override
public ClickHouseDateTimeValue update(BigInteger value) {
if (value == null) {
resetToNullOrEmpty();
} else if (scale == 0) {
set(LocalDateTime.ofInstant(ClickHouseValues.convertToInstant(new BigDecimal(value, 0)), tz.toZoneId()));
} else {
set(LocalDateTime.ofInstant(ClickHouseValues.convertToInstant(new BigDecimal(value, scale)), tz.toZoneId()));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(BigDecimal value) {
if (value == null) {
resetToNullOrEmpty();
} else {
if (value.scale() != scale) {
value = value.setScale(scale, ClickHouseDataConfig.DEFAULT_ROUNDING_MODE);
}
set(LocalDateTime.ofInstant(ClickHouseValues.convertToInstant(value), tz.toZoneId()));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(Enum<?> value) {
if (value == null) {
resetToNullOrEmpty();
} else {
update(BigInteger.valueOf(value.ordinal()));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(LocalDate value) {
if (value == null) {
resetToNullOrEmpty();
} else {
set(LocalDateTime.of(value, LocalTime.MIN));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(LocalTime value) {
if (value == null) {
resetToNullOrEmpty();
} else {
set(LocalDateTime.of(LocalDate.now(), value));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(LocalDateTime value) {
set(value);
return this;
}
@Override
public ClickHouseDateTimeValue update(Instant value) {
if (value == null) {
resetToNullOrEmpty();
} else {
set(LocalDateTime.ofInstant(value, tz.toZoneId()));
}
return this;
}
@Override
public ClickHouseValue update(OffsetDateTime value) {
return update(value != null ? value.atZoneSameInstant(tz.toZoneId()).toLocalDateTime() : null);
}
@Override
public ClickHouseValue update(ZonedDateTime value) {
return update(value != null ? LocalDateTime.ofInstant(value.toInstant(), tz.toZoneId()) : null);
}
@Override
public ClickHouseDateTimeValue update(String value) {
if (value == null) {
resetToNullOrEmpty();
} else if (value.isEmpty()) {
resetToDefault();
} else {
set(LocalDateTime.parse(value, ClickHouseValues.DATETIME_FORMATTER));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(ClickHouseValue value) {
if (value == null || value.isNullOrEmpty()) {
resetToNullOrEmpty();
} else {
set(value.asDateTime(scale));
}
return this;
}
@Override
public ClickHouseDateTimeValue update(Object value) {
if (value instanceof LocalDateTime) {
set((LocalDateTime) value);
} else if (value instanceof String) {
update((String) value);
} else {
super.update(value);
}
return this;
}
}