Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
util.cc
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <memory>
21 #include <string>
22 
23 #include "arrow/api.h"
24 
25 #include "graphar/util.h"
26 
27 namespace graphar::util {
28 
29 std::shared_ptr<arrow::ChunkedArray> GetArrowColumnByName(
30  std::shared_ptr<arrow::Table> const& table, const std::string& name) {
31  return table->GetColumnByName(name);
32 }
33 
34 std::shared_ptr<arrow::Array> GetArrowArrayByChunkIndex(
35  std::shared_ptr<arrow::ChunkedArray> const& chunk_array,
36  int64_t chunk_index) {
37  return chunk_array->chunk(chunk_index);
38 }
39 
40 Result<const void*> GetArrowArrayData(
41  std::shared_ptr<arrow::Array> const& array) {
42  if (array->type()->Equals(arrow::int8())) {
43  return reinterpret_cast<const void*>(
44  std::dynamic_pointer_cast<arrow::Int8Array>(array)->raw_values());
45  } else if (array->type()->Equals(arrow::uint8())) {
46  return reinterpret_cast<const void*>(
47  std::dynamic_pointer_cast<arrow::UInt8Array>(array)->raw_values());
48  } else if (array->type()->Equals(arrow::int16())) {
49  return reinterpret_cast<const void*>(
50  std::dynamic_pointer_cast<arrow::Int16Array>(array)->raw_values());
51  } else if (array->type()->Equals(arrow::uint16())) {
52  return reinterpret_cast<const void*>(
53  std::dynamic_pointer_cast<arrow::UInt16Array>(array)->raw_values());
54  } else if (array->type()->Equals(arrow::int32())) {
55  return reinterpret_cast<const void*>(
56  std::dynamic_pointer_cast<arrow::Int32Array>(array)->raw_values());
57  } else if (array->type()->Equals(arrow::uint32())) {
58  return reinterpret_cast<const void*>(
59  std::dynamic_pointer_cast<arrow::UInt32Array>(array)->raw_values());
60  } else if (array->type()->Equals(arrow::int64())) {
61  return reinterpret_cast<const void*>(
62  std::dynamic_pointer_cast<arrow::Int64Array>(array)->raw_values());
63  } else if (array->type()->Equals(arrow::uint64())) {
64  return reinterpret_cast<const void*>(
65  std::dynamic_pointer_cast<arrow::UInt64Array>(array)->raw_values());
66  } else if (array->type()->Equals(arrow::float32())) {
67  return reinterpret_cast<const void*>(
68  std::dynamic_pointer_cast<arrow::FloatArray>(array)->raw_values());
69  } else if (array->type()->Equals(arrow::float64())) {
70  return reinterpret_cast<const void*>(
71  std::dynamic_pointer_cast<arrow::DoubleArray>(array)->raw_values());
72  } else if (array->type()->Equals(arrow::utf8())) {
73  return reinterpret_cast<const void*>(
74  std::dynamic_pointer_cast<arrow::StringArray>(array).get());
75  } else if (array->type()->Equals(arrow::large_utf8())) {
76  return reinterpret_cast<const void*>(
77  std::dynamic_pointer_cast<arrow::LargeStringArray>(array).get());
78  } else if (array->type()->Equals(arrow::list(arrow::int32())) ||
79  array->type()->Equals(arrow::large_list(arrow::uint32())) ||
80  array->type()->Equals(arrow::large_list(arrow::int64())) ||
81  array->type()->Equals(arrow::large_list(arrow::uint64())) ||
82  array->type()->Equals(arrow::large_list(arrow::float32())) ||
83  array->type()->Equals(arrow::large_list(arrow::float64()))) {
84  return reinterpret_cast<const void*>(
85  std::dynamic_pointer_cast<arrow::LargeListArray>(array).get());
86  } else if (array->type()->Equals(arrow::null())) {
87  return reinterpret_cast<const void*>(
88  std::dynamic_pointer_cast<arrow::NullArray>(array).get());
89  } else {
90  return Status::TypeError("Array type - ", array->type()->ToString(),
91  " is not supported yet...");
92  }
93 }
94 
95 std::string ValueGetter<std::string>::Value(const void* data, int64_t offset) {
96  return std::string(
97  reinterpret_cast<const arrow::LargeStringArray*>(data)->GetView(offset));
98 }
99 
100 } // namespace graphar::util
static Status TypeError(Args &&... args)
Definition: status.h:178