-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Use shortest scientific notation for cast(real|double as varchar) #12574
Open
ccat3z
wants to merge
8
commits into
facebookincubator:main
Choose a base branch
from
ccat3z:shortest-sci-double
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3151c0d
Use shortest scientific notation for cast(real|double as varchar)
ccat3z 4877876
code format
ccat3z 7c91fcc
Remove failure case due to JDK-4511638
ccat3z 86cb522
Add fuzzer
ccat3z 2116bf7
code format
ccat3z 13583f8
remove boost::process
ccat3z e7e701e
Remove indirect depends
ccat3z 61ed3e8
fix add_test
ccat3z File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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. | ||
|
||
find_package(Java COMPONENTS Development) | ||
|
||
if(Java_Development_FOUND) | ||
add_custom_command( | ||
OUTPUT FloatGenerator.class | ||
COMMAND ${Java_JAVAC_EXECUTABLE} -d ${CMAKE_CURRENT_BINARY_DIR} | ||
${CMAKE_CURRENT_SOURCE_DIR}/FloatGenerator.java | ||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/FloatGenerator.java) | ||
add_custom_target(java_float_generator DEPENDS FloatGenerator.class) | ||
|
||
add_executable(velox_float_to_string_fuzzer FloatToStringFuzzer.cpp) | ||
add_test(float_to_string_fuzzer float_to_string_fuzzer) | ||
|
||
target_link_libraries( | ||
velox_float_to_string_fuzzer | ||
Boost::headers | ||
fmt::fmt | ||
velox_type | ||
GTest::gtest | ||
GTest::gtest_main | ||
gflags::gflags | ||
glog::glog) | ||
|
||
add_dependencies(velox_float_to_string_fuzzer java_float_generator) | ||
endif() |
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,68 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import java.util.Random; | ||
|
||
public class FloatGenerator { | ||
private static final Random random = new Random(); | ||
|
||
public static void main(String[] args) { | ||
if (args.length != 2) { | ||
System.out.println("usage: java FloatGenerator double|float count"); | ||
System.exit(1); | ||
} | ||
String type = args[0]; | ||
int count = Integer.parseInt(args[1]); | ||
|
||
// Detect whether https://bugs.openjdk.org/browse/JDK-4511638 was fixed. | ||
System.out.println(Double.toString(1.0E23).equals("9.999999999999999E22")); | ||
|
||
if (type.equals("float")) { | ||
for (int i = 0; i < count; i++) { | ||
float randomFloat = Float.parseFloat(generateRandom(1, 10, -37, 38)); | ||
System.out.println(randomFloat); | ||
System.out.println(Float.floatToIntBits(randomFloat)); | ||
} | ||
} else { | ||
for (int i = 0; i < count; i++) { | ||
double randomDouble = Double.parseDouble(generateRandom(1, 18, -307, 308)); | ||
System.out.println(randomDouble); | ||
System.out.println(Double.doubleToLongBits(randomDouble)); | ||
} | ||
} | ||
} | ||
|
||
private static String generateRandom(int minMantissaDigits, int maxMantissaDigits, int minExponent, int maxExponent) { | ||
int numDigits = minMantissaDigits + random.nextInt(maxMantissaDigits - minMantissaDigits + 1); | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
if (random.nextBoolean()) { | ||
sb.append('-'); | ||
} | ||
|
||
sb.append(random.nextInt(9) + 1); | ||
sb.append('.'); | ||
|
||
for (int i = 1; i < numDigits; i++) { | ||
sb.append(random.nextInt(10)); | ||
} | ||
|
||
int exponent = minExponent + random.nextInt(maxExponent - minExponent + 1); | ||
sb.append('E'); | ||
sb.append(exponent); | ||
|
||
return sb.toString(); | ||
} | ||
} |
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,118 @@ | ||
/* | ||
* 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/type/Conversions.h" | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include <tuple> | ||
#include <vector> | ||
#include "boost/process.hpp" | ||
#include "fmt/core.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace bp = boost::process; | ||
|
||
namespace { | ||
template <typename T> | ||
std::tuple<bool, std::vector<T>, std::vector<std::string>> | ||
generateFloatTestCases(int count) { | ||
auto workDir = std::filesystem::canonical("/proc/self/exe").parent_path(); | ||
bp::ipstream pipeStream; | ||
bp::child c( | ||
fmt::format( | ||
"java FloatGenerator {} {}", | ||
std::is_same_v<T, double> ? "double" : "float", | ||
count), | ||
bp::start_dir(workDir.string()), | ||
bp::std_out > pipeStream); | ||
|
||
std::string buggyJavaVersion; | ||
std::vector<T> values; | ||
std::vector<std::string> expects; | ||
values.reserve(count); | ||
expects.reserve(count); | ||
|
||
pipeStream >> buggyJavaVersion; | ||
|
||
std::conditional_t<std::is_same_v<T, double>, int64_t, int32_t> carrierInt; | ||
std::string expect; | ||
|
||
while (pipeStream >> expect >> carrierInt) { | ||
values.emplace_back(reinterpret_cast<const T&>(carrierInt)); | ||
expects.emplace_back(expect); | ||
} | ||
|
||
c.wait(); | ||
int result = c.exit_code(); | ||
if (result != 0) { | ||
throw std::runtime_error( | ||
fmt::format("Process exited with code: {}", result)); | ||
} | ||
|
||
return {buggyJavaVersion == "true", values, expects}; | ||
} | ||
|
||
template <typename T> | ||
void testCastToVarchar( | ||
const std::vector<T> values, | ||
const std::vector<std::string> expects, | ||
bool buggyJavaVersion) { | ||
using namespace facebook::velox; | ||
util::Converter<TypeKind::VARCHAR> convertor; | ||
|
||
ASSERT_EQ(values.size(), expects.size()); | ||
|
||
for (int i = 0; i < values.size(); i++) { | ||
const auto& value = values[i]; | ||
const auto& expect = expects[i]; | ||
auto actual = convertor.tryCast(value).value_or(""); | ||
|
||
// Old java (< 19) may produce longer or incorrect decimal. | ||
// See https://bugs.openjdk.org/browse/JDK-4511638. | ||
// e.g. | ||
// Actual | JDK <= 18 | JDK 19 | ||
// 7.5371334E25 | 7.5371335E25 | 7.5371334E25 # incorrect | ||
// 1.0E23 | 9.999999999999999E22 | 1.0E23 # longer | ||
if (buggyJavaVersion) { | ||
EXPECT_TRUE( | ||
actual == expect || | ||
// Shorter but same decimal | ||
(actual.size() <= expect.size() && | ||
(std::is_same_v<T, double> ? std::stod(actual) | ||
: std::stof(actual)) == value)); | ||
|
||
if (actual != expect) { | ||
std::cerr << "Warning: " << actual << " != " << expect << std::endl; | ||
} | ||
} else { | ||
EXPECT_EQ(actual, expect); | ||
} | ||
} | ||
} | ||
} // namespace | ||
|
||
TEST(FloatToString, float) { | ||
auto [buggyJavaVersion, values, expects] = | ||
generateFloatTestCases<float>(10'000); | ||
testCastToVarchar(values, expects, buggyJavaVersion); | ||
} | ||
|
||
TEST(FloatToString, double) { | ||
auto [buggyJavaVersion, values, expects] = | ||
generateFloatTestCases<double>(10'000); | ||
testCastToVarchar(values, expects, buggyJavaVersion); | ||
} |
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 |
---|---|---|
|
@@ -123,4 +123,15 @@ if(VELOX_ENABLE_BENCHMARKS) | |
GTest::gtest_main | ||
gflags::gflags | ||
glog::glog) | ||
|
||
add_executable(velox_floating_point_benchmark FloatingPointBenchmark.cpp) | ||
target_link_libraries( | ||
velox_floating_point_benchmark | ||
velox_type | ||
Folly::folly | ||
Folly::follybenchmark | ||
GTest::gtest | ||
GTest::gtest_main | ||
gflags::gflags | ||
glog::glog) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
endif() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see gflags and glog being used directly, so these can be removed.