Coverage Report

Created: 2026-07-16 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
17.6k
std::string DoubleDebugString(double value) {
40
17.6k
  if (std::isfinite(value)) {
41
16.6k
    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
7.55k
      return absl::StrCat(value);
45
7.55k
    }
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
9.09k
    std::string stringified = absl::StrCat(value);
50
9.09k
    if (!absl::StrContains(stringified, '.')) {
51
7.01k
      absl::StrAppend(&stringified, ".0");
52
7.01k
    } else {
53
      // absl::StrCat has a decimal now? Use it directly.
54
2.07k
    }
55
9.09k
    return stringified;
56
16.6k
  }
57
992
  if (std::isnan(value)) {
58
35
    return "nan";
59
35
  }
60
957
  if (std::signbit(value)) {
61
497
    return "-infinity";
62
497
  }
63
460
  return "+infinity";
64
957
}
65
66
}  // namespace
67
68
17.6k
std::string DoubleValue::DebugString() const {
69
17.6k
  return DoubleDebugString(NativeValue());
70
17.6k
}
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
3.39k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
112
3.39k
  ABSL_DCHECK(descriptor_pool != nullptr);
113
3.39k
  ABSL_DCHECK(message_factory != nullptr);
114
3.39k
  ABSL_DCHECK(arena != nullptr);
115
3.39k
  ABSL_DCHECK(result != nullptr);
116
117
3.39k
  if (auto other_value = other.AsDouble(); other_value.has_value()) {
118
249
    *result = BoolValue{NativeValue() == other_value->NativeValue()};
119
249
    return absl::OkStatus();
120
249
  }
121
3.14k
  if (auto other_value = other.AsInt(); other_value.has_value()) {
122
1.47k
    *result =
123
1.47k
        BoolValue{internal::Number::FromDouble(NativeValue()) ==
124
1.47k
                  internal::Number::FromInt64(other_value->NativeValue())};
125
1.47k
    return absl::OkStatus();
126
1.47k
  }
127
1.66k
  if (auto other_value = other.AsUint(); other_value.has_value()) {
128
1.42k
    *result =
129
1.42k
        BoolValue{internal::Number::FromDouble(NativeValue()) ==
130
1.42k
                  internal::Number::FromUint64(other_value->NativeValue())};
131
1.42k
    return absl::OkStatus();
132
1.42k
  }
133
248
  *result = FalseValue();
134
248
  return absl::OkStatus();
135
1.66k
}
136
137
}  // namespace cel