Coverage Report

Created: 2026-07-11 06:47

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
11.3k
std::string DoubleDebugString(double value) {
40
11.3k
  if (std::isfinite(value)) {
41
11.1k
    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
3.96k
      return absl::StrCat(value);
45
3.96k
    }
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
7.19k
    std::string stringified = absl::StrCat(value);
50
7.19k
    if (!absl::StrContains(stringified, '.')) {
51
5.06k
      absl::StrAppend(&stringified, ".0");
52
5.06k
    } else {
53
      // absl::StrCat has a decimal now? Use it directly.
54
2.13k
    }
55
7.19k
    return stringified;
56
11.1k
  }
57
141
  if (std::isnan(value)) {
58
21
    return "nan";
59
21
  }
60
120
  if (std::signbit(value)) {
61
32
    return "-infinity";
62
32
  }
63
88
  return "+infinity";
64
120
}
65
66
}  // namespace
67
68
11.3k
std::string DoubleValue::DebugString() const {
69
11.3k
  return DoubleDebugString(NativeValue());
70
11.3k
}
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
2.63k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
112
2.63k
  ABSL_DCHECK(descriptor_pool != nullptr);
113
2.63k
  ABSL_DCHECK(message_factory != nullptr);
114
2.63k
  ABSL_DCHECK(arena != nullptr);
115
2.63k
  ABSL_DCHECK(result != nullptr);
116
117
2.63k
  if (auto other_value = other.AsDouble(); other_value.has_value()) {
118
251
    *result = BoolValue{NativeValue() == other_value->NativeValue()};
119
251
    return absl::OkStatus();
120
251
  }
121
2.38k
  if (auto other_value = other.AsInt(); other_value.has_value()) {
122
418
    *result =
123
418
        BoolValue{internal::Number::FromDouble(NativeValue()) ==
124
418
                  internal::Number::FromInt64(other_value->NativeValue())};
125
418
    return absl::OkStatus();
126
418
  }
127
1.96k
  if (auto other_value = other.AsUint(); other_value.has_value()) {
128
1.90k
    *result =
129
1.90k
        BoolValue{internal::Number::FromDouble(NativeValue()) ==
130
1.90k
                  internal::Number::FromUint64(other_value->NativeValue())};
131
1.90k
    return absl::OkStatus();
132
1.90k
  }
133
64
  *result = FalseValue();
134
64
  return absl::OkStatus();
135
1.96k
}
136
137
}  // namespace cel