iceberg-cpp
Loading...
Searching...
No Matches
struct_like.h
Go to the documentation of this file.
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#pragma once
21
28
29#include <functional>
30#include <memory>
31#include <span>
32#include <string_view>
33#include <variant>
34#include <vector>
35
37#include "iceberg/result.h"
38#include "iceberg/type_fwd.h"
40
41namespace iceberg {
42
47using Scalar = std::variant<std::monostate, // for null
48 bool, // for boolean
49 int32_t, // for int, date
50 int64_t, // for long, timestamp, timestamp_tz, and time
51 float, // for float
52 double, // for double
53 std::string_view, // for non-owned string, binary and fixed
54 Decimal, // for decimal
55 std::shared_ptr<StructLike>, // for struct
56 std::shared_ptr<ArrayLike>, // for list
57 std::shared_ptr<MapLike>>; // for map
58
61
63class ICEBERG_EXPORT StructLike {
64 public:
65 virtual ~StructLike() = default;
66
69 virtual Result<Scalar> GetField(size_t pos) const = 0;
70
72 virtual size_t num_fields() const = 0;
73};
74
76class ICEBERG_EXPORT ArrayLike {
77 public:
78 virtual ~ArrayLike() = default;
79
82 virtual Result<Scalar> GetElement(size_t pos) const = 0;
83
85 virtual size_t size() const = 0;
86};
87
89class ICEBERG_EXPORT MapLike {
90 public:
91 virtual ~MapLike() = default;
92
95 virtual Result<Scalar> GetKey(size_t pos) const = 0;
96
99 virtual Result<Scalar> GetValue(size_t pos) const = 0;
100
102 virtual size_t size() const = 0;
103};
104
106class ICEBERG_EXPORT StructLikeAccessor {
107 public:
108 explicit StructLikeAccessor(std::shared_ptr<Type> type,
109 std::span<const size_t> position_path,
110 std::vector<bool> is_optional);
111
112 // Deep-path accessors capture this, so the object must keep a stable address.
113 StructLikeAccessor(const StructLikeAccessor&) = delete;
114 StructLikeAccessor& operator=(const StructLikeAccessor&) = delete;
116 StructLikeAccessor& operator=(StructLikeAccessor&&) = delete;
117
120 Result<Scalar> Get(const StructLike& struct_like) const {
121 return accessor_(struct_like);
122 }
123
128 Result<Literal> GetLiteral(const StructLike& struct_like) const;
129
131 const Type& type() const { return *type_; }
132
134 const std::vector<size_t>& position_path() const { return position_path_; }
135
136 private:
137 std::shared_ptr<Type> type_;
138 std::function<Result<Scalar>(const StructLike&)> accessor_;
139 std::vector<size_t> position_path_;
140 // One entry per position, including the leaf field.
141 std::vector<bool> is_optional_;
142};
143
144} // namespace iceberg
An immutable array-like wrapper.
Definition struct_like.h:76
virtual Result< Scalar > GetElement(size_t pos) const =0
Get the array element at the given position.
virtual size_t size() const =0
Get the number of elements in the array.
Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represen...
Definition decimal.h:46
Literal is a literal value that is associated with a primitive type.
Definition literal.h:42
An immutable map-like wrapper.
Definition struct_like.h:89
virtual size_t size() const =0
Get the number of entries in the map.
virtual Result< Scalar > GetValue(size_t pos) const =0
Get the value at the given position.
virtual Result< Scalar > GetKey(size_t pos) const =0
Get the key at the given position.
An accessor for a struct-like object.
Definition struct_like.h:106
const std::vector< size_t > & position_path() const
Get the position path of the value that this accessor is bound to.
Definition struct_like.h:134
Result< Literal > GetLiteral(const StructLike &struct_like) const
Get the literal value at the given position.
const Type & type() const
Get the type of the value that this accessor is bound to.
Definition struct_like.h:131
Result< Scalar > Get(const StructLike &struct_like) const
Get the scalar value at the given position. A null optional parent returns monostate; a null required...
Definition struct_like.h:120
An immutable struct-like wrapper.
Definition struct_like.h:63
virtual Result< Scalar > GetField(size_t pos) const =0
Get the field value at the given position.
virtual size_t num_fields() const =0
Get the number of fields in the struct.
Interface for a data type for a field.
Definition type.h:44
Definition expected.h:1199
128-bit fixed-point decimal numbers. Adapted from Apache Arrow with only Decimal128 support....
Define typed literal values used by expressions.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
std::variant< std::monostate, bool, int32_t, int64_t, float, double, std::string_view, Decimal, std::shared_ptr< StructLike >, std::shared_ptr< ArrayLike >, std::shared_ptr< MapLike > > Scalar
A scalar value depending on its data type.
Definition struct_like.h:57
Result< Scalar > LiteralToScalar(const Literal &literal)
Convert a Literal to a Scalar.
Define Result, Status, and error helpers.