Coverage Report

Created: 2026-11-24 06:40

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
22.2k
std::string DoubleDebugString(double value) {
40
22.2k
  if (std::isfinite(value)) {
41
21.7k
    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.60k
      return absl::StrCat(value);
45
3.60k
    }
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
18.1k
    std::string stringified = absl::StrCat(value);
50
18.1k
    if (!absl::StrContains(stringified, '.')) {
51
10.1k
      absl::StrAppend(&stringified, ".0");
52
10.1k
    } else {
53
      // absl::StrCat has a decimal now? Use it directly.
54
7.94k
    }
55
18.1k
    return stringified;
56
21.7k
  }
57
478
  if (std::isnan(value)) {
58
250
    return "nan";
59
250
  }
60
228
  if (std::signbit(value)) {
61
66
    return "-infinity";
62
66
  }
63
162
  return "+infinity";
64
228
}
65
66
}  // namespace
67
68
22.2k
std::string DoubleValue::DebugString() const {
69
22.2k
  return DoubleDebugString(NativeValue());
70
22.2k
}
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
18.0k
    google::protobuf::Message* absl_nonnull json) const {
94
18.0k
  ABSL_DCHECK(descriptor_pool != nullptr);
95
18.0k
  ABSL_DCHECK(message_factory != nullptr);
96
18.0k
  ABSL_DCHECK(json != nullptr);
97
18.0k
  ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
98
18.0k
                 google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
99
100
18.0k
  ValueReflection value_reflection;
101
18.0k
  CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
102
18.0k
  value_reflection.SetNumberValue(json, NativeValue());
103
104
18.0k
  return absl::OkStatus();
105
18.0k
}
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.01k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
112
3.01k
  ABSL_DCHECK(descriptor_pool != nullptr);
113
3.01k
  ABSL_DCHECK(message_factory != nullptr);
114
3.01k
  ABSL_DCHECK(arena != nullptr);
115
3.01k
  ABSL_DCHECK(result != nullptr);
116
117
3.01k
  if (auto other_value = other.AsDouble(); other_value.has_value()) {
118
248
    *result = BoolValue{NativeValue() == other_value->NativeValue()};
119
248
    return absl::OkStatus();
120
248
  }
121
2.76k
  if (auto other_value = other.AsInt(); other_value.has_value()) {
122
1.17k
    *result =
123
1.17k
        BoolValue{internal::Number::FromDouble(NativeValue()) ==
124
1.17k
                  internal::Number::FromInt64(other_value->NativeValue())};
125
1.17k
    return absl::OkStatus();
126
1.17k
  }
127
1.59k
  if (auto other_value = other.AsUint(); other_value.has_value()) {
128
933
    *result =
129
933
        BoolValue{internal::Number::FromDouble(NativeValue()) ==
130
933
                  internal::Number::FromUint64(other_value->NativeValue())};
131
933
    return absl::OkStatus();
132
933
  }
133
659
  *result = FalseValue();
134
659
  return absl::OkStatus();
135
1.59k
}
136
137
}  // namespace cel