iceberg-cpp
Loading...
Searching...
No Matches
truncate_util.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
24
25#include <cstdint>
26#include <optional>
27#include <string>
28#include <utility>
29
31#include "iceberg/result.h"
32#include "iceberg/type_fwd.h"
33
34namespace iceberg {
35
36class ICEBERG_EXPORT TruncateUtils {
37 public:
45 static std::string TruncateUTF8(std::string source, size_t L) {
46 size_t code_point_count = 0;
47 size_t safe_point = 0;
48
49 for (size_t i = 0; i < source.size(); ++i) {
50 // Start of a new UTF-8 code point
51 if ((source[i] & 0xC0) != 0x80) {
52 code_point_count++;
53 if (code_point_count > static_cast<size_t>(L)) {
54 safe_point = i;
55 break;
56 }
57 }
58 }
59
60 if (safe_point != 0) {
61 // Resize the string to the safe point
62 source.resize(safe_point);
63 }
64
65 return source;
66 }
67
80 static Result<std::optional<std::string>> TruncateUTF8Max(const std::string& source,
81 size_t L);
82
87 template <typename T>
88 requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t>
89 static inline T TruncateInteger(T v, int32_t W) {
90 return v - (((v % W) + W) % W);
91 }
92
97 static Decimal TruncateDecimal(const Decimal& decimal, int32_t width);
98
107 static Result<Literal> TruncateLiteral(const Literal& literal, int32_t width);
108
121 int32_t width);
122
133 const Literal& value, int32_t width);
134
146 const Literal& value,
147 int32_t width);
148};
149
150} // namespace iceberg
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
A data type that does not have child fields.
Definition type.h:78
Definition truncate_util.h:36
static Result< Literal > TruncateLiteral(const Literal &literal, int32_t width)
Truncate a Literal to a specified width.
static Decimal TruncateDecimal(const Decimal &decimal, int32_t width)
Truncate a Decimal to a specified width.
static T TruncateInteger(T v, int32_t W)
Truncate an integer v, either int32_t or int64_t, to v - (v % W).
Definition truncate_util.h:89
static Result< std::optional< Literal > > TruncateUpperBound(const PrimitiveType &type, const Literal &value, int32_t width)
Truncate the upper bound of a string or binary value.
static Result< Literal > TruncateLowerBound(const PrimitiveType &type, const Literal &value, int32_t width)
Truncate the lower bound of a string or binary value.
static std::string TruncateUTF8(std::string source, size_t L)
Truncate a UTF-8 string to a specified number of code points.
Definition truncate_util.h:45
static Result< std::optional< std::string > > TruncateUTF8Max(const std::string &source, size_t L)
Truncate a UTF-8 string to a specified number of code points for use as an upper-bound value.
static Result< std::optional< Literal > > TruncateLiteralMax(const Literal &value, int32_t width)
Truncate a Literal to a specified width for use as an upper-bound value.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.