/proc/self/cwd/common/values/double_value.cc
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <cmath> |
16 | | #include <string> |
17 | | |
18 | | #include "google/protobuf/wrappers.pb.h" |
19 | | #include "absl/base/nullability.h" |
20 | | #include "absl/log/absl_check.h" |
21 | | #include "absl/status/status.h" |
22 | | #include "absl/strings/match.h" |
23 | | #include "absl/strings/str_cat.h" |
24 | | #include "common/value.h" |
25 | | #include "internal/number.h" |
26 | | #include "internal/status_macros.h" |
27 | | #include "internal/well_known_types.h" |
28 | | #include "google/protobuf/arena.h" |
29 | | #include "google/protobuf/descriptor.h" |
30 | | #include "google/protobuf/io/zero_copy_stream.h" |
31 | | #include "google/protobuf/message.h" |
32 | | |
33 | | namespace cel { |
34 | | |
35 | | namespace { |
36 | | |
37 | | using ::cel::well_known_types::ValueReflection; |
38 | | |
39 | 2.32k | std::string DoubleDebugString(double value) { |
40 | 2.32k | if (std::isfinite(value)) { |
41 | 2.22k | if (std::floor(value) != value) { |
42 | | // The double is not representable as a whole number, so use |
43 | | // absl::StrCat which will add decimal places. |
44 | 982 | return absl::StrCat(value); |
45 | 982 | } |
46 | | // absl::StrCat historically would represent 0.0 as 0, and we want the |
47 | | // decimal places so ZetaSQL correctly assumes the type as double |
48 | | // instead of int64. |
49 | 1.23k | std::string stringified = absl::StrCat(value); |
50 | 1.23k | if (!absl::StrContains(stringified, '.')) { |
51 | 654 | absl::StrAppend(&stringified, ".0"); |
52 | 654 | } else { |
53 | | // absl::StrCat has a decimal now? Use it directly. |
54 | 584 | } |
55 | 1.23k | return stringified; |
56 | 2.22k | } |
57 | 109 | if (std::isnan(value)) { |
58 | 15 | return "nan"; |
59 | 15 | } |
60 | 94 | if (std::signbit(value)) { |
61 | 56 | return "-infinity"; |
62 | 56 | } |
63 | 38 | return "+infinity"; |
64 | 94 | } |
65 | | |
66 | | } // namespace |
67 | | |
68 | 2.32k | std::string DoubleValue::DebugString() const { |
69 | 2.32k | return DoubleDebugString(NativeValue()); |
70 | 2.32k | } |
71 | | |
72 | | absl::Status DoubleValue::SerializeTo( |
73 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
74 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
75 | 0 | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const { |
76 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
77 | 0 | ABSL_DCHECK(message_factory != nullptr); |
78 | 0 | ABSL_DCHECK(output != nullptr); |
79 | |
|
80 | 0 | google::protobuf::DoubleValue message; |
81 | 0 | message.set_value(NativeValue()); |
82 | 0 | if (!message.SerializePartialToZeroCopyStream(output)) { |
83 | 0 | return absl::UnknownError( |
84 | 0 | absl::StrCat("failed to serialize message: ", message.GetTypeName())); |
85 | 0 | } |
86 | | |
87 | 0 | return absl::OkStatus(); |
88 | 0 | } |
89 | | |
90 | | absl::Status DoubleValue::ConvertToJson( |
91 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
92 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
93 | 0 | google::protobuf::Message* absl_nonnull json) const { |
94 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
95 | 0 | ABSL_DCHECK(message_factory != nullptr); |
96 | 0 | ABSL_DCHECK(json != nullptr); |
97 | 0 | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
98 | 0 | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
99 | |
|
100 | 0 | ValueReflection value_reflection; |
101 | 0 | CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor())); |
102 | 0 | value_reflection.SetNumberValue(json, NativeValue()); |
103 | |
|
104 | 0 | return absl::OkStatus(); |
105 | 0 | } |
106 | | |
107 | | absl::Status DoubleValue::Equal( |
108 | | const Value& other, |
109 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
110 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
111 | 1.14k | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const { |
112 | 1.14k | ABSL_DCHECK(descriptor_pool != nullptr); |
113 | 1.14k | ABSL_DCHECK(message_factory != nullptr); |
114 | 1.14k | ABSL_DCHECK(arena != nullptr); |
115 | 1.14k | ABSL_DCHECK(result != nullptr); |
116 | | |
117 | 1.14k | if (auto other_value = other.AsDouble(); other_value.has_value()) { |
118 | 138 | *result = BoolValue{NativeValue() == other_value->NativeValue()}; |
119 | 138 | return absl::OkStatus(); |
120 | 138 | } |
121 | 1.00k | if (auto other_value = other.AsInt(); other_value.has_value()) { |
122 | 478 | *result = |
123 | 478 | BoolValue{internal::Number::FromDouble(NativeValue()) == |
124 | 478 | internal::Number::FromInt64(other_value->NativeValue())}; |
125 | 478 | return absl::OkStatus(); |
126 | 478 | } |
127 | 526 | if (auto other_value = other.AsUint(); other_value.has_value()) { |
128 | 454 | *result = |
129 | 454 | BoolValue{internal::Number::FromDouble(NativeValue()) == |
130 | 454 | internal::Number::FromUint64(other_value->NativeValue())}; |
131 | 454 | return absl::OkStatus(); |
132 | 454 | } |
133 | 72 | *result = FalseValue(); |
134 | 72 | return absl::OkStatus(); |
135 | 526 | } |
136 | | |
137 | | } // namespace cel |