]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/r/src/scalar.cpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / r / src / scalar.cpp
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_types.h"
19
20 #if defined(ARROW_R_WITH_ARROW)
21
22 #include <arrow/array/array_base.h>
23 #include <arrow/array/util.h>
24 #include <arrow/scalar.h>
25 #include <arrow/type.h>
26
27 namespace cpp11 {
28
29 const char* r6_class_name<arrow::Scalar>::get(
30 const std::shared_ptr<arrow::Scalar>& scalar) {
31 if (scalar->type->id() == arrow::Type::STRUCT) {
32 return "StructScalar";
33 }
34 return "Scalar";
35 }
36
37 } // namespace cpp11
38
39 // [[arrow::export]]
40 std::shared_ptr<arrow::Scalar> Array__GetScalar(const std::shared_ptr<arrow::Array>& x,
41 int64_t i) {
42 return ValueOrStop(x->GetScalar(i));
43 }
44
45 // [[arrow::export]]
46 std::string Scalar__ToString(const std::shared_ptr<arrow::Scalar>& s) {
47 return s->ToString();
48 }
49
50 // [[arrow::export]]
51 std::shared_ptr<arrow::Scalar> StructScalar__field(
52 const std::shared_ptr<arrow::StructScalar>& s, int i) {
53 return ValueOrStop(s->field(i));
54 }
55
56 // [[arrow::export]]
57 std::shared_ptr<arrow::Scalar> StructScalar__GetFieldByName(
58 const std::shared_ptr<arrow::StructScalar>& s, const std::string& name) {
59 return ValueOrStop(s->field(name));
60 }
61
62 // [[arrow::export]]
63 SEXP Scalar__as_vector(const std::shared_ptr<arrow::Scalar>& scalar) {
64 auto array = ValueOrStop(arrow::MakeArrayFromScalar(*scalar, 1, gc_memory_pool()));
65
66 // defined in array_to_vector.cpp
67 SEXP Array__as_vector(const std::shared_ptr<arrow::Array>& array);
68 return Array__as_vector(array);
69 }
70
71 // [[arrow::export]]
72 std::shared_ptr<arrow::Array> MakeArrayFromScalar(
73 const std::shared_ptr<arrow::Scalar>& scalar, int n) {
74 return ValueOrStop(arrow::MakeArrayFromScalar(*scalar, n, gc_memory_pool()));
75 }
76
77 // [[arrow::export]]
78 bool Scalar__is_valid(const std::shared_ptr<arrow::Scalar>& s) { return s->is_valid; }
79
80 // [[arrow::export]]
81 std::shared_ptr<arrow::DataType> Scalar__type(const std::shared_ptr<arrow::Scalar>& s) {
82 return s->type;
83 }
84
85 // [[arrow::export]]
86 bool Scalar__Equals(const std::shared_ptr<arrow::Scalar>& lhs,
87 const std::shared_ptr<arrow::Scalar>& rhs) {
88 return lhs->Equals(rhs);
89 }
90
91 // [[arrow::export]]
92 bool Scalar__ApproxEquals(const std::shared_ptr<arrow::Scalar>& lhs,
93 const std::shared_ptr<arrow::Scalar>& rhs) {
94 return lhs->ApproxEquals(*rhs);
95 }
96
97 #endif