Coverage Report

Created: 2026-11-12 06:42

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