]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | .. default-domain:: cpp | |
19 | .. highlight:: cpp | |
20 | ||
21 | Conversion of range of ``std::tuple``-like to ``Table`` instances | |
22 | ================================================================= | |
23 | ||
24 | While the above example shows a quite manual approach of a row to columnar | |
25 | conversion, Arrow also provides some template logic to convert ranges of | |
26 | ``std::tuple<..>``-like objects to tables. | |
27 | ||
28 | In the most simple case, you only need to provide the input data and the | |
29 | type conversion is then inferred at compile time. | |
30 | ||
31 | .. code:: | |
32 | ||
33 | std::vector<std::tuple<double, std::string>> rows = .. | |
34 | std::shared_ptr<Table> table; | |
35 | ||
36 | if (!arrow::stl::TableFromTupleRange( | |
37 | arrow::default_memory_pool(), | |
38 | rows, names, &table).ok() | |
39 | ) { | |
40 | // Error handling code should go here. | |
41 | } | |
42 | ||
43 | In reverse, you can use ``TupleRangeFromTable`` to fill an already | |
44 | pre-allocated range with the data from a ``Table`` instance. | |
45 | ||
46 | .. code:: | |
47 | ||
48 | // An important aspect here is that the table columns need to be in the | |
49 | // same order as the columns will later appear in the tuple. As the tuple | |
50 | // is unnamed, matching is done on positions. | |
51 | std::shared_ptr<Table> table = .. | |
52 | ||
53 | // The range needs to be pre-allocated to the respective amount of rows. | |
54 | // This allows us to pass in an arbitrary range object, not only | |
55 | // `std::vector`. | |
56 | std::vector<std::tuple<double, std::string>> rows(2); | |
57 | if (!arrow::stl::TupleRangeFromTable(*table, &rows).ok()) { | |
58 | // Error handling code should go here. | |
59 | } | |
60 | ||
61 | Arrow itself already supports some C(++) data types for this conversion. If you | |
62 | want to support additional data types, you need to implement a specialization | |
63 | of ``arrow::stl::ConversionTraits<T>`` and the more general | |
64 | ``arrow::CTypeTraits<T>``. | |
65 | ||
66 | ||
67 | .. code:: | |
68 | ||
69 | namespace arrow { | |
70 | ||
71 | template<> | |
72 | struct CTypeTraits<boost::posix_time::ptime> { | |
73 | using ArrowType = ::arrow::TimestampType; | |
74 | ||
75 | static std::shared_ptr<::arrow::DataType> type_singleton() { | |
76 | return ::arrow::timestamp(::arrow::TimeUnit::MICRO); | |
77 | } | |
78 | }; | |
79 | ||
80 | } | |
81 | ||
82 | namespace arrow { namespace stl { | |
83 | ||
84 | template <> | |
85 | struct ConversionTraits<boost::posix_time::ptime> : public CTypeTraits<boost::posix_time::ptime> { | |
86 | constexpr static bool nullable = false; | |
87 | ||
88 | // This is the specialization to load a scalar value into an Arrow builder. | |
89 | static Status AppendRow( | |
90 | typename TypeTraits<TimestampType>::BuilderType& builder, | |
91 | boost::posix_time::ptime cell) { | |
92 | boost::posix_time::ptime const epoch({1970, 1, 1}, {0, 0, 0, 0}); | |
93 | return builder.Append((cell - epoch).total_microseconds()); | |
94 | } | |
95 | ||
96 | // Specify how we can fill the tuple from the values stored in the Arrow | |
97 | // array. | |
98 | static boost::posix_time::ptime GetEntry( | |
99 | const TimestampArray& array, size_t j) { | |
100 | return psapp::arrow::internal::timestamp_epoch | |
101 | + boost::posix_time::time_duration(0, 0, 0, array.Value(j)); | |
102 | } | |
103 | }; | |
104 | ||
105 | }} | |
106 |