1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with 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,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #include "arrow/array/array_primitive.h"
19 
20 #include <cstdint>
21 #include <memory>
22 
23 #include "arrow/array/array_base.h"
24 #include "arrow/type.h"
25 #include "arrow/util/bit_block_counter.h"
26 #include "arrow/util/bitmap_ops.h"
27 #include "arrow/util/logging.h"
28 
29 namespace arrow {
30 
31 // ----------------------------------------------------------------------
32 // Primitive array base
33 
PrimitiveArray(const std::shared_ptr<DataType> & type,int64_t length,const std::shared_ptr<Buffer> & data,const std::shared_ptr<Buffer> & null_bitmap,int64_t null_count,int64_t offset)34 PrimitiveArray::PrimitiveArray(const std::shared_ptr<DataType>& type, int64_t length,
35                                const std::shared_ptr<Buffer>& data,
36                                const std::shared_ptr<Buffer>& null_bitmap,
37                                int64_t null_count, int64_t offset) {
38   SetData(ArrayData::Make(type, length, {null_bitmap, data}, null_count, offset));
39 }
40 
41 // ----------------------------------------------------------------------
42 // BooleanArray
43 
BooleanArray(const std::shared_ptr<ArrayData> & data)44 BooleanArray::BooleanArray(const std::shared_ptr<ArrayData>& data)
45     : PrimitiveArray(data) {
46   ARROW_CHECK_EQ(data->type->id(), Type::BOOL);
47 }
48 
BooleanArray(int64_t length,const std::shared_ptr<Buffer> & data,const std::shared_ptr<Buffer> & null_bitmap,int64_t null_count,int64_t offset)49 BooleanArray::BooleanArray(int64_t length, const std::shared_ptr<Buffer>& data,
50                            const std::shared_ptr<Buffer>& null_bitmap, int64_t null_count,
51                            int64_t offset)
52     : PrimitiveArray(boolean(), length, data, null_bitmap, null_count, offset) {}
53 
false_count() const54 int64_t BooleanArray::false_count() const {
55   return this->length() - this->null_count() - this->true_count();
56 }
57 
true_count() const58 int64_t BooleanArray::true_count() const {
59   if (data_->null_count.load() != 0) {
60     DCHECK(data_->buffers[0]);
61     internal::BinaryBitBlockCounter bit_counter(data_->buffers[0]->data(), data_->offset,
62                                                 data_->buffers[1]->data(), data_->offset,
63                                                 data_->length);
64     int64_t count = 0;
65     while (true) {
66       internal::BitBlockCount block = bit_counter.NextAndWord();
67       if (block.length == 0) {
68         break;
69       }
70       count += block.popcount;
71     }
72     return count;
73   } else {
74     return internal::CountSetBits(data_->buffers[1]->data(), data_->offset,
75                                   data_->length);
76   }
77 }
78 
79 // ----------------------------------------------------------------------
80 // Day time interval
81 
DayTimeIntervalArray(const std::shared_ptr<ArrayData> & data)82 DayTimeIntervalArray::DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data) {
83   SetData(data);
84 }
85 
DayTimeIntervalArray(const std::shared_ptr<DataType> & type,int64_t length,const std::shared_ptr<Buffer> & data,const std::shared_ptr<Buffer> & null_bitmap,int64_t null_count,int64_t offset)86 DayTimeIntervalArray::DayTimeIntervalArray(const std::shared_ptr<DataType>& type,
87                                            int64_t length,
88                                            const std::shared_ptr<Buffer>& data,
89                                            const std::shared_ptr<Buffer>& null_bitmap,
90                                            int64_t null_count, int64_t offset)
91     : PrimitiveArray(type, length, data, null_bitmap, null_count, offset) {}
92 
DayTimeIntervalArray(int64_t length,const std::shared_ptr<Buffer> & data,const std::shared_ptr<Buffer> & null_bitmap,int64_t null_count,int64_t offset)93 DayTimeIntervalArray::DayTimeIntervalArray(int64_t length,
94                                            const std::shared_ptr<Buffer>& data,
95                                            const std::shared_ptr<Buffer>& null_bitmap,
96                                            int64_t null_count, int64_t offset)
97     : PrimitiveArray(day_time_interval(), length, data, null_bitmap, null_count, offset) {
98 }
99 
GetValue(int64_t i) const100 DayTimeIntervalType::DayMilliseconds DayTimeIntervalArray::GetValue(int64_t i) const {
101   DCHECK(i < length());
102   return *reinterpret_cast<const DayTimeIntervalType::DayMilliseconds*>(
103       raw_values_ + (i + data_->offset) * byte_width());
104 }
105 
106 // ----------------------------------------------------------------------
107 // Month, day and Nanos interval
108 
MonthDayNanoIntervalArray(const std::shared_ptr<ArrayData> & data)109 MonthDayNanoIntervalArray::MonthDayNanoIntervalArray(
110     const std::shared_ptr<ArrayData>& data) {
111   SetData(data);
112 }
113 
MonthDayNanoIntervalArray(const std::shared_ptr<DataType> & type,int64_t length,const std::shared_ptr<Buffer> & data,const std::shared_ptr<Buffer> & null_bitmap,int64_t null_count,int64_t offset)114 MonthDayNanoIntervalArray::MonthDayNanoIntervalArray(
115     const std::shared_ptr<DataType>& type, int64_t length,
116     const std::shared_ptr<Buffer>& data, const std::shared_ptr<Buffer>& null_bitmap,
117     int64_t null_count, int64_t offset)
118     : PrimitiveArray(type, length, data, null_bitmap, null_count, offset) {}
119 
MonthDayNanoIntervalArray(int64_t length,const std::shared_ptr<Buffer> & data,const std::shared_ptr<Buffer> & null_bitmap,int64_t null_count,int64_t offset)120 MonthDayNanoIntervalArray::MonthDayNanoIntervalArray(
121     int64_t length, const std::shared_ptr<Buffer>& data,
122     const std::shared_ptr<Buffer>& null_bitmap, int64_t null_count, int64_t offset)
123     : PrimitiveArray(month_day_nano_interval(), length, data, null_bitmap, null_count,
124                      offset) {}
125 
GetValue(int64_t i) const126 MonthDayNanoIntervalType::MonthDayNanos MonthDayNanoIntervalArray::GetValue(
127     int64_t i) const {
128   DCHECK(i < length());
129   return *reinterpret_cast<const MonthDayNanoIntervalType::MonthDayNanos*>(
130       raw_values_ + (i + data_->offset) * byte_width());
131 }
132 
133 }  // namespace arrow
134