]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/consumer/DateConsumer.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / adapter / jdbc / src / main / java / org / apache / arrow / adapter / jdbc / consumer / DateConsumer.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.arrow.adapter.jdbc.consumer;
19
20 import java.sql.Date;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.util.Calendar;
24 import java.util.concurrent.TimeUnit;
25
26 import org.apache.arrow.vector.DateDayVector;
27 import org.apache.arrow.vector.DateMilliVector;
28
29 /**
30 * Consumer which consume date type values from {@link ResultSet}.
31 * Write the data to {@link org.apache.arrow.vector.DateDayVector}.
32 */
33 public class DateConsumer {
34
35 /**
36 * Creates a consumer for {@link DateMilliVector}.
37 */
38 public static JdbcConsumer<DateDayVector> createConsumer(
39 DateDayVector vector, int index, boolean nullable, Calendar calendar) {
40 if (nullable) {
41 return new NullableDateConsumer(vector, index, calendar);
42 } else {
43 return new NonNullableDateConsumer(vector, index, calendar);
44 }
45 }
46
47 /**
48 * Nullable consumer for date.
49 */
50 static class NullableDateConsumer extends BaseConsumer<DateDayVector> {
51
52 protected final Calendar calendar;
53
54 /**
55 * Instantiate a DateConsumer.
56 */
57 public NullableDateConsumer(DateDayVector vector, int index) {
58 this(vector, index, /* calendar */null);
59 }
60
61 /**
62 * Instantiate a DateConsumer.
63 */
64 public NullableDateConsumer(DateDayVector vector, int index, Calendar calendar) {
65 super(vector, index);
66 this.calendar = calendar;
67 }
68
69 @Override
70 public void consume(ResultSet resultSet) throws SQLException {
71 Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) :
72 resultSet.getDate(columnIndexInResultSet, calendar);
73 if (!resultSet.wasNull()) {
74 // for fixed width vectors, we have allocated enough memory proactively,
75 // so there is no need to call the setSafe method here.
76 vector.set(currentIndex, Math.toIntExact(TimeUnit.MILLISECONDS.toDays(date.getTime())));
77 }
78 currentIndex++;
79 }
80 }
81
82 /**
83 * Non-nullable consumer for date.
84 */
85 static class NonNullableDateConsumer extends BaseConsumer<DateDayVector> {
86
87 protected final Calendar calendar;
88
89 /**
90 * Instantiate a DateConsumer.
91 */
92 public NonNullableDateConsumer(DateDayVector vector, int index) {
93 this(vector, index, /* calendar */null);
94 }
95
96 /**
97 * Instantiate a DateConsumer.
98 */
99 public NonNullableDateConsumer(DateDayVector vector, int index, Calendar calendar) {
100 super(vector, index);
101 this.calendar = calendar;
102 }
103
104 @Override
105 public void consume(ResultSet resultSet) throws SQLException {
106 Date date = calendar == null ? resultSet.getDate(columnIndexInResultSet) :
107 resultSet.getDate(columnIndexInResultSet, calendar);
108 // for fixed width vectors, we have allocated enough memory proactively,
109 // so there is no need to call the setSafe method here.
110 vector.set(currentIndex, Math.toIntExact(TimeUnit.MILLISECONDS.toDays(date.getTime())));
111 currentIndex++;
112 }
113 }
114 }
115
116