-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor comparison functors out of LeastGreatest.cpp (#302)
Summary: Pull Request resolved: #302 SQL comparisons (<, >, and ==) will be used in many functions. Ideally we'd have these orderings defined in one common locations. In this diff, I put these in a separate header Comparisons.h and update LeastGreatest.cpp to use them. This required a little bit of refactoring. Reviewed By: beroyfb Differential Revision: D31185337 fbshipit-source-id: bc8b4534284b47cf4110efae04d7559f2d31623d
- Loading branch information
1 parent
cac6072
commit 7325060
Showing
3 changed files
with
117 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#include "velox/expression/VectorFunction.h" | ||
|
||
namespace facebook::velox::functions::sparksql { | ||
|
||
// Comparison functions that implement SparkSQL semantics. | ||
// Intended to be used with scalar types (including strings and timestamps). | ||
template <typename T> | ||
static inline bool isNan(const T& value) { | ||
if constexpr (std::is_floating_point<T>::value) { | ||
return std::isnan(value); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
template <typename T> | ||
struct Less { | ||
constexpr bool operator()(const T& a, const T& b) const { | ||
if (isNan(a)) { | ||
return false; | ||
} | ||
if (isNan(b)) { | ||
return true; | ||
} | ||
return a < b; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
struct Greater : private Less<T> { | ||
constexpr bool operator()(const T& a, const T& b) const { | ||
return Less<T>::operator()(b, a); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
struct Equal { | ||
constexpr bool operator()(const T& a, const T& b) const { | ||
// In SparkSQL, NaN is defined to equal NaN. | ||
if (isNan(a)) { | ||
return isNan(b); | ||
} | ||
return a == b; | ||
} | ||
}; | ||
|
||
} // namespace facebook::velox::functions::sparksql |
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