forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Highlights of the this change: - Ensure multimap_agg treats all NaN binary representations are equal - Introduces a utility class for floating point types that provide comparator and hash functor to implement consistent behavior or NaNs across the codebase. These can be passed to standard containers and functions like std::map and std::sort, etc. - Fix NaN handling of floating points for map variants which uses a std::map to hold key and value variants. Without this, using NaN as a key would result in an inconsistent state because once NaN is added as the first value, it cannot compare NaN with anything else or if NaN is added after some initial regular values, then it does not accept NaN for the same reason. Differential Revision: D57187815
- Loading branch information
1 parent
49c3ebb
commit 34e7295
Showing
10 changed files
with
202 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <array> | ||
#include <cmath> | ||
#include <vector> | ||
|
||
namespace facebook::velox { | ||
|
||
/// A utility class that holds custom comparator and hash functors for floating | ||
/// point types. These are designed to ensure consistent NaN handling according | ||
/// to the following rules: | ||
/// - NaN == NaN returns true, even for NaNs with differing binary | ||
/// representations. | ||
/// - NaN is considered greater than infinity. | ||
/// These can be passed to standard containers and functions like std::map and | ||
/// std::sort, etc. | ||
template <typename FLOAT> | ||
class FloatingPointUtil { | ||
public: | ||
static_assert( | ||
std::is_floating_point<FLOAT>::value, | ||
"A floating point type is required."); | ||
|
||
struct NaNAwareEquals { | ||
bool operator()(const FLOAT& lhs, const FLOAT& rhs) const { | ||
if (std::isnan(lhs) && std::isnan(rhs)) { | ||
return true; | ||
} | ||
return lhs == rhs; | ||
} | ||
}; | ||
|
||
struct NaNAwareLessThan { | ||
bool operator()(const FLOAT& lhs, const FLOAT& rhs) const { | ||
if (!std::isnan(lhs) && std::isnan(rhs)) { | ||
return true; | ||
} | ||
return lhs < rhs; | ||
} | ||
}; | ||
|
||
struct NaNAwareHash { | ||
std::size_t operator()(const FLOAT& val) const noexcept { | ||
static const std::size_t kNanHash = | ||
std::hash<FLOAT>{}(std::numeric_limits<FLOAT>::quiet_NaN()); | ||
if (std::isnan(val)) { | ||
return kNanHash; | ||
} | ||
return std::hash<FLOAT>{}(val); | ||
} | ||
}; | ||
}; | ||
|
||
/// A static class that holds helper functions for DOUBLE type. | ||
class DoubleUtil { | ||
public: | ||
static const std::array<double, 309> kPowersOfTen; | ||
|
||
}; // DoubleUtil | ||
} // namespace facebook::velox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters