Copilot commented on code in PR #701: URL: https://github.com/apache/iceberg-cpp/pull/701#discussion_r3636266476
########## src/iceberg/expression/sanitize_expression.cc: ########## @@ -0,0 +1,353 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "iceberg/expression/sanitize_expression.h" + +#include <chrono> +#include <cmath> +#include <cstdint> +#include <format> +#include <limits> +#include <regex> +#include <string> +#include <string_view> +#include <utility> +#include <vector> + +#include "iceberg/expression/binder.h" +#include "iceberg/expression/literal.h" +#include "iceberg/expression/predicate.h" +#include "iceberg/expression/term.h" +#include "iceberg/transform.h" +#include "iceberg/type.h" +#include "iceberg/util/bucket_util.h" +#include "iceberg/util/checked_cast.h" +#include "iceberg/util/temporal_util.h" + +namespace iceberg { + +namespace { + +std::string SanitizeDate(int32_t days, int32_t today) { + std::string is_past = today > days ? "ago" : "from-now"; + uint32_t diff = today > days ? static_cast<uint32_t>(static_cast<uint32_t>(today) - + static_cast<uint32_t>(days)) + : static_cast<uint32_t>(static_cast<uint32_t>(days) - + static_cast<uint32_t>(today)); + if (diff == 0) { + return "(date-today)"; + } else if (diff < 90) { + return "(date-" + std::to_string(diff) + "-days-" + is_past + ")"; + } + + return "(date)"; +} + +std::string SanitizeTimestamp(int64_t micros, int64_t now) { + constexpr int64_t kMicrosPerHour = 60LL * 60LL * 1'000'000LL; + constexpr int64_t kFiveMinutesInMicros = 5LL * 60LL * 1'000'000LL; + constexpr int64_t kThreeDaysInHours = 3LL * 24LL; + constexpr int64_t kNinetyDaysInHours = 90LL * 24LL; + + std::string is_past = now > micros ? "ago" : "from-now"; + uint64_t diff = now > micros ? static_cast<uint64_t>(static_cast<uint64_t>(now) - + static_cast<uint64_t>(micros)) + : static_cast<uint64_t>(static_cast<uint64_t>(micros) - + static_cast<uint64_t>(now)); + if (diff < kFiveMinutesInMicros) { + return "(timestamp-about-now)"; + } + + int64_t hours = diff / kMicrosPerHour; + if (hours <= kThreeDaysInHours) { + return "(timestamp-" + std::to_string(hours) + "-hours-" + is_past + ")"; + } else if (hours < kNinetyDaysInHours) { + int64_t days = hours / 24; + return "(timestamp-" + std::to_string(days) + "-days-" + is_past + ")"; + } + + return "(timestamp)"; +} + +std::string SanitizeNumber(double value, std::string_view type) { + int32_t num_digits = + value == 0 ? 1 : static_cast<int32_t>(std::log10(std::abs(value))) + 1; + return std::format("({}-digit-{})", num_digits, type); +} Review Comment: SanitizeNumber() can produce negative digit counts for small fractional values (e.g., 0.005 -> -1) and the log10/cast path is undefined for NaN/infinity. That yields invalid placeholders like "(-1-digit-float)" and risks UB during sanitization. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
