forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add InputGenerator for TimestampWithTimeZone (facebookincubator…
…#12481) Summary: This diff adds an implementation of AbstractInputGenerator for the TimestampWithTimeZone type. It uses a random int64_t for the number of milliseconds (there may be overflow since we only have 52 bits for the milliseconds but that doesn't matter since it's random anyway) and selects a random time zone ID from a list of valid time zone IDs provided by TimeZoneMap. Differential Revision: D70362561
- Loading branch information
1 parent
27cb3c6
commit 06250b5
Showing
9 changed files
with
208 additions
and
3 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
26 changes: 26 additions & 0 deletions
26
velox/functions/prestosql/types/fuzzer_utils/CMakeLists.txt
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,26 @@ | ||
# 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. | ||
velox_add_library(velox_presto_types_fuzzer_utils | ||
TimestampWithTimeZoneInputGenerator.cpp) | ||
|
||
velox_link_libraries( | ||
velox_presto_types_fuzzer_utils | ||
velox_type | ||
velox_common_fuzzer_util | ||
velox_presto_types | ||
velox_type_tz) | ||
|
||
if(${VELOX_BUILD_TESTING}) | ||
add_subdirectory(tests) | ||
endif() |
47 changes: 47 additions & 0 deletions
47
velox/functions/prestosql/types/fuzzer_utils/TimestampWithTimeZoneInputGenerator.cpp
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,47 @@ | ||
/* | ||
* 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/functions/prestosql/types/fuzzer_utils/TimestampWithTimeZoneInputGenerator.h" | ||
|
||
#include "velox/common/fuzzer/Utils.h" | ||
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h" | ||
#include "velox/type/Variant.h" | ||
#include "velox/type/tz/TimeZoneMap.h" | ||
|
||
namespace facebook::velox::fuzzer { | ||
TimestampWithTimeZoneInputGenerator::TimestampWithTimeZoneInputGenerator( | ||
const size_t seed, | ||
const double nullRatio) | ||
: AbstractInputGenerator( | ||
seed, | ||
TIMESTAMP_WITH_TIME_ZONE(), | ||
nullptr, | ||
nullRatio), | ||
timeZoneIds_(tz::getTimeZoneIDs()) {} | ||
|
||
variant TimestampWithTimeZoneInputGenerator::generate() { | ||
if (coinToss(rng_, nullRatio_)) { | ||
return variant::null(type_->kind()); | ||
} | ||
|
||
int16_t timeZoneId = timeZoneIds_ | ||
[boost::random::uniform_int_distribution<size_t>()(rng_) % | ||
timeZoneIds_.size()]; | ||
|
||
return pack( | ||
boost::random::uniform_int_distribution<int64_t>()(rng_), timeZoneId); | ||
} | ||
} // namespace facebook::velox::fuzzer |
33 changes: 33 additions & 0 deletions
33
velox/functions/prestosql/types/fuzzer_utils/TimestampWithTimeZoneInputGenerator.h
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,33 @@ | ||
/* | ||
* 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 "velox/type/Type.h" | ||
|
||
namespace facebook::velox::fuzzer { | ||
class TimestampWithTimeZoneInputGenerator : public AbstractInputGenerator { | ||
public: | ||
TimestampWithTimeZoneInputGenerator( | ||
const size_t seed, | ||
const double nullRatio); | ||
|
||
variant generate() override; | ||
|
||
private: | ||
const std::vector<int16_t> timeZoneIds_; | ||
}; | ||
} // namespace facebook::velox::fuzzer |
28 changes: 28 additions & 0 deletions
28
velox/functions/prestosql/types/fuzzer_utils/tests/CMakeLists.txt
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,28 @@ | ||
# 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. | ||
|
||
add_executable(velox_presto_types_fuzzer_utils_test | ||
TimestampWithTimeZoneInputGeneratorTest.cpp) | ||
|
||
add_test(velox_presto_types_fuzzer_utils_test | ||
velox_presto_types_fuzzer_utils_test) | ||
|
||
target_link_libraries( | ||
velox_presto_types_fuzzer_utils_test | ||
velox_presto_types_fuzzer_utils | ||
velox_presto_types | ||
velox_type | ||
velox_type_tz | ||
GTest::gtest | ||
GTest::gtest_main) |
47 changes: 47 additions & 0 deletions
47
.../functions/prestosql/types/fuzzer_utils/tests/TimestampWithTimeZoneInputGeneratorTest.cpp
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,47 @@ | ||
/* | ||
* 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/functions/prestosql/types/fuzzer_utils/TimestampWithTimeZoneInputGenerator.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h" | ||
#include "velox/type/Variant.h" | ||
#include "velox/type/tz/TimeZoneMap.h" | ||
|
||
namespace facebook::velox::fuzzer::test { | ||
|
||
TEST(TimestampWithTimeZoneInputGeneratorTest, generate) { | ||
TimestampWithTimeZoneInputGenerator generator(123456, 0.1); | ||
|
||
size_t numTrials = 100; | ||
for (size_t i = 0; i < numTrials; ++i) { | ||
variant generated = generator.generate(); | ||
|
||
if (generated.isNull()) { | ||
continue; | ||
} | ||
|
||
generated.checkIsKind(TypeKind::BIGINT); | ||
const auto value = generated.value<TypeKind::BIGINT>(); | ||
|
||
// The value can be any random int64_t with the one restriction that the | ||
// time zone should be valid. | ||
auto zoneKey = unpackZoneKeyId(value); | ||
EXPECT_NE(tz::locateZone(zoneKey), nullptr); | ||
} | ||
} | ||
} // namespace facebook::velox::fuzzer::test |
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