@@ -168,42 +168,46 @@ Literal TruncateLiteralImpl<TypeId::kBinary>(const Literal& literal, int32_t wid
168168}
169169
170170template <TypeId type_id>
171- Result<Literal> TruncateLiteralMaxImpl (const Literal& literal, int32_t width) = delete;
171+ Result<std::optional<Literal>> TruncateLiteralMaxImpl (const Literal& literal,
172+ int32_t width) = delete;
172173
173174template <>
174- Result<Literal> TruncateLiteralMaxImpl<TypeId::kString >(const Literal& literal,
175- int32_t width) {
175+ Result<std::optional< Literal>> TruncateLiteralMaxImpl<TypeId::kString >(
176+ const Literal& literal, int32_t width) {
176177 const auto & str = std::get<std::string>(literal.value ());
177- ICEBERG_ASSIGN_OR_RAISE (std::string truncated,
178- TruncateUtils::TruncateUTF8Max (str, width));
179- return Literal::String (std::move (truncated));
178+ ICEBERG_ASSIGN_OR_RAISE (auto truncated, TruncateUtils::TruncateUTF8Max (str, width));
179+ if (!truncated.has_value ()) {
180+ return std::nullopt ;
181+ }
182+ return std::optional<Literal>(Literal::String (std::move (truncated.value ())));
180183}
181184
182185template <>
183- Result<Literal> TruncateLiteralMaxImpl<TypeId::kBinary >(const Literal& literal,
184- int32_t width) {
186+ Result<std::optional< Literal>> TruncateLiteralMaxImpl<TypeId::kBinary >(
187+ const Literal& literal, int32_t width) {
185188 const auto & data = std::get<std::vector<uint8_t >>(literal.value ());
186189 if (static_cast <int32_t >(data.size ()) <= width) {
187- return literal;
190+ return std::optional<Literal>( literal) ;
188191 }
189192
190193 std::vector<uint8_t > truncated (data.begin (), data.begin () + width);
191194 for (auto it = truncated.rbegin (); it != truncated.rend (); ++it) {
192195 if (*it < 0xFF ) {
193196 ++(*it);
194197 truncated.resize (truncated.size () - std::distance (truncated.rbegin (), it));
195- return Literal::Binary (std::move (truncated));
198+ return std::optional< Literal>( Literal ::Binary (std::move (truncated) ));
196199 }
197200 }
198- return InvalidArgument ( " Cannot truncate upper bound for binary: all bytes are 0xFF " ) ;
201+ return std:: nullopt ;
199202}
200203
201204} // namespace
202205
203- Result<std::string> TruncateUtils::TruncateUTF8Max (const std::string& source, size_t L) {
206+ Result<std::optional<std::string>> TruncateUtils::TruncateUTF8Max (
207+ const std::string& source, size_t L) {
204208 std::string truncated = TruncateUTF8 (source, L);
205209 if (truncated == source) {
206- return truncated;
210+ return std::optional<std::string>( std::move ( truncated)) ;
207211 }
208212
209213 // Try incrementing code points from the end
@@ -232,13 +236,12 @@ Result<std::string> TruncateUtils::TruncateUTF8Max(const std::string& source, si
232236 if (next_code_point <= kUtf8MaxCodePoint ) {
233237 truncated.resize (cp_start);
234238 AppendUtf8CodePoint (next_code_point, truncated);
235- return truncated;
239+ return std::optional<std::string>( std::move ( truncated)) ;
236240 }
237241 }
238242 last_cp_start = cp_start;
239243 }
240- return InvalidArgument (
241- " Cannot truncate upper bound for string: all code points are 0x10FFFF" );
244+ return std::nullopt ;
242245}
243246
244247Decimal TruncateUtils::TruncateDecimal (const Decimal& decimal, int32_t width) {
@@ -275,10 +278,11 @@ Result<Literal> TruncateUtils::TruncateLiteral(const Literal& literal, int32_t w
275278 case TYPE_ID: \
276279 return TruncateLiteralMaxImpl<TYPE_ID>(literal, width);
277280
278- Result<Literal> TruncateUtils::TruncateLiteralMax (const Literal& literal, int32_t width) {
281+ Result<std::optional<Literal>> TruncateUtils::TruncateLiteralMax (const Literal& literal,
282+ int32_t width) {
279283 if (literal.IsNull ()) [[unlikely]] {
280284 // Return null as is
281- return literal;
285+ return std::optional<Literal>( literal) ;
282286 }
283287
284288 if (literal.IsAboveMax () || literal.IsBelowMin ()) [[unlikely]] {
@@ -305,14 +309,14 @@ Result<Literal> TruncateUtils::TruncateLowerBound(const PrimitiveType& type,
305309 }
306310}
307311
308- Result<Literal> TruncateUtils::TruncateUpperBound (const PrimitiveType& type,
309- const Literal& value, int32_t length) {
312+ Result<std::optional< Literal>> TruncateUtils::TruncateUpperBound (
313+ const PrimitiveType& type, const Literal& value, int32_t length) {
310314 switch (type.type_id ()) {
311315 case TypeId::kString :
312316 case TypeId::kBinary :
313317 return TruncateLiteralMax (value, length);
314318 default :
315- return value;
319+ return std::optional<Literal>( value) ;
316320 }
317321}
318322
0 commit comments