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
35#include "iceberg/expression/literal.h"
36#include "iceberg/result.h"
37#include "iceberg/type_fwd.h"
39
40namespace iceberg {
41
46using Scalar = std::variant<std::monostate, // for null
47 bool, // for boolean
48 int32_t, // for int, date
49 int64_t, // for long, timestamp, timestamp_tz, and time
50 float, // for float
51 double, // for double
52 std::string_view, // for non-owned string, binary and fixed
53 Decimal, // for decimal
54 std::shared_ptr<StructLike>, // for struct
55 std::shared_ptr<ArrayLike>, // for list
56 std::shared_ptr<MapLike>>; // for map
57
59Result<Scalar> LiteralToScalar(const Literal& literal);
60
62class ICEBERG_EXPORT StructLike {
63 public:
64 virtual ~StructLike() = default;
65
68 virtual Result<Scalar> GetField(size_t pos) const = 0;
69
71 virtual size_t num_fields() const = 0;
72};
73
75class ICEBERG_EXPORT ArrayLike {
76 public:
77 virtual ~ArrayLike() = default;
78
81 virtual Result<Scalar> GetElement(size_t pos) const = 0;
82
84 virtual size_t size() const = 0;
85};
86
88class ICEBERG_EXPORT MapLike {
89 public:
90 virtual ~MapLike() = default;
91
94 virtual Result<Scalar> GetKey(size_t pos) const = 0;
95
98 virtual Result<Scalar> GetValue(size_t pos) const = 0;
99
101 virtual size_t size() const = 0;
102};
103
105class ICEBERG_EXPORT StructLikeAccessor {
106 public:
107 explicit StructLikeAccessor(std::shared_ptr<Type> type,
108 std::span<const size_t> position_path);
109
111 Result<Scalar> Get(const StructLike& struct_like) const {
112 return accessor_(struct_like);
113 }
114
119 Result<Literal> GetLiteral(const StructLike& struct_like) const;
120
122 const Type& type() const { return *type_; }
123
125 const std::vector<size_t>& position_path() const { return position_path_; }
126
127 private:
128 std::shared_ptr<Type> type_;
129 std::function<Result<Scalar>(const StructLike&)> accessor_;
130 std::vector<size_t> position_path_;
131};
132
133} // namespace iceberg
An immutable array-like wrapper.
Definition struct_like.h:75
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:39
An immutable map-like wrapper.
Definition struct_like.h:88
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:105
const std::vector< size_t > & position_path() const
Get the position path of the value that this accessor bounded to.
Definition struct_like.h:125
const Type & type() const
Get the type of the value that this accessor is bound to.
Definition struct_like.h:122
Result< Scalar > Get(const StructLike &struct_like) const
Get the scalar value at the given position.
Definition struct_like.h:111
An immutable struct-like wrapper.
Definition struct_like.h:62
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
128-bit fixed-point decimal numbers. Adapted from Apache Arrow with only Decimal128 support....
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:56