-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(cudf): Add NotFunction, IsNullFunction, IsNotNullFunction #17314
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
Open
mattgara
wants to merge
4
commits into
facebookincubator:main
Choose a base branch
from
mattgara:logical-null-functions
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,162 @@ | ||
| /* | ||
| * 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/experimental/cudf/CudfConfig.h" | ||
| #include "velox/experimental/cudf/exec/ToCudf.h" | ||
| #include "velox/experimental/cudf/expression/AstExpression.h" | ||
| #include "velox/experimental/cudf/expression/ExpressionEvaluator.h" | ||
|
|
||
| #include "velox/common/file/FileSystems.h" | ||
| #include "velox/exec/tests/utils/OperatorTestBase.h" | ||
| #include "velox/exec/tests/utils/PlanBuilder.h" | ||
|
|
||
| using namespace facebook::velox; | ||
| using namespace facebook::velox::exec::test; | ||
|
|
||
| namespace { | ||
|
|
||
| // Exercises the `not` / `is_null` / `isnotnull` CudfFunction classes | ||
| // introduced by this PR. The AST evaluator natively supports all three | ||
| // operators, and its default priority (100) beats the Function evaluator | ||
| // (50), so under normal configuration these CudfFunction classes are dormant | ||
| // for primitive types. This fixture re-registers the AST evaluator at | ||
| // priority 0, which drops AST to a last-resort fallback and forces | ||
| // expressions that FunctionExpression can handle through the new CudfFunction | ||
| // classes. Tests use primitive types only so they don't depend on the | ||
| // timestamp or decimal PRs. | ||
| class CudfLogicalFunctionsTest : public OperatorTestBase { | ||
| protected: | ||
| void SetUp() override { | ||
| OperatorTestBase::SetUp(); | ||
| filesystems::registerLocalFileSystem(); | ||
| cudf_velox::CudfConfig::getInstance().allowCpuFallback = false; | ||
| cudf_velox::registerCudf(); | ||
|
|
||
| // Overwrite the AST registration with priority 0 so any expression that | ||
| // FunctionExpression can also evaluate routes through the Function path. | ||
| cudf_velox::registerCudfExpressionEvaluator( | ||
| cudf_velox::kAstEvaluatorName, | ||
| /*priority=*/0, | ||
| [](std::shared_ptr<exec::Expr> expr) { | ||
| return cudf_velox::ASTExpression::canEvaluate(expr); | ||
| }, | ||
| [](std::shared_ptr<exec::Expr> expr, const RowTypePtr& row) { | ||
| return std::make_shared<cudf_velox::ASTExpression>( | ||
| std::move(expr), row); | ||
| }, | ||
| /*overwrite=*/true); | ||
| } | ||
|
|
||
| void TearDown() override { | ||
| cudf_velox::unregisterCudf(); | ||
| OperatorTestBase::TearDown(); | ||
| } | ||
|
|
||
| void runProject( | ||
| const std::vector<RowVectorPtr>& input, | ||
| const std::string& projection, | ||
| const std::string& sql) { | ||
| createDuckDbTable(input); | ||
| auto plan = | ||
| PlanBuilder().values(input).project({projection}).planNode(); | ||
| assertQuery(plan, sql); | ||
| } | ||
| }; | ||
|
|
||
| // NotFunction: negation of a boolean column. Base column-only path. | ||
| TEST_F(CudfLogicalFunctionsTest, notColumn) { | ||
| auto data = makeRowVector( | ||
| {"a"}, {makeFlatVector<bool>({true, false, true, false})}); | ||
| runProject({data}, "NOT a AS r", "SELECT NOT a AS r FROM tmp"); | ||
| } | ||
|
|
||
| // NotFunction: negation of a comparison. `NotFunction` wraps the comparison | ||
| // result, which itself comes from a nested FunctionExpression path. | ||
| TEST_F(CudfLogicalFunctionsTest, notComparison) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, {makeFlatVector<int32_t>({1, 2, 3, 4, 5})}); | ||
| runProject( | ||
| {data}, "NOT (c0 = 3) AS r", "SELECT NOT (c0 = 3) AS r FROM tmp"); | ||
| } | ||
|
|
||
| // NotFunction: null row. `cudf::unary_operation` with `NOT` should propagate | ||
| // null through untouched. | ||
| TEST_F(CudfLogicalFunctionsTest, notWithNullRows) { | ||
| auto data = makeRowVector( | ||
| {"a"}, | ||
| {makeNullableFlatVector<bool>( | ||
| {true, false, std::nullopt, true, std::nullopt})}); | ||
| runProject({data}, "NOT a AS r", "SELECT NOT a AS r FROM tmp"); | ||
| } | ||
|
|
||
| // IsNullFunction: nullable INTEGER column with a mix of nulls and non-nulls. | ||
| TEST_F(CudfLogicalFunctionsTest, isNullInteger) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, | ||
| {makeNullableFlatVector<int32_t>( | ||
| {1, std::nullopt, 3, std::nullopt, 5})}); | ||
| runProject({data}, "c0 IS NULL AS r", "SELECT c0 IS NULL AS r FROM tmp"); | ||
| } | ||
|
|
||
| // IsNullFunction: nullable VARCHAR column. Exercises string-typed input. | ||
| TEST_F(CudfLogicalFunctionsTest, isNullVarchar) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, | ||
| {makeNullableFlatVector<std::string>( | ||
| {"x", std::nullopt, "y", std::nullopt, ""})}); | ||
| runProject({data}, "c0 IS NULL AS r", "SELECT c0 IS NULL AS r FROM tmp"); | ||
| } | ||
|
|
||
| // IsNullFunction: column with no nulls. Result is all false. | ||
| TEST_F(CudfLogicalFunctionsTest, isNullNoNulls) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, {makeFlatVector<int32_t>({1, 2, 3, 4, 5})}); | ||
| runProject({data}, "c0 IS NULL AS r", "SELECT c0 IS NULL AS r FROM tmp"); | ||
| } | ||
|
|
||
| // IsNotNullFunction: nullable INTEGER column. Inverse of IS NULL. | ||
| TEST_F(CudfLogicalFunctionsTest, isNotNullInteger) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, | ||
| {makeNullableFlatVector<int32_t>( | ||
| {1, std::nullopt, 3, std::nullopt, 5})}); | ||
| runProject( | ||
| {data}, "c0 IS NOT NULL AS r", "SELECT c0 IS NOT NULL AS r FROM tmp"); | ||
| } | ||
|
|
||
| // IsNotNullFunction: nullable VARCHAR column. | ||
| TEST_F(CudfLogicalFunctionsTest, isNotNullVarchar) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, | ||
| {makeNullableFlatVector<std::string>( | ||
| {"x", std::nullopt, "y", std::nullopt, ""})}); | ||
| runProject( | ||
| {data}, "c0 IS NOT NULL AS r", "SELECT c0 IS NOT NULL AS r FROM tmp"); | ||
| } | ||
|
|
||
| // Composition: NOT wrapping IS NULL. Exercises NotFunction operating on the | ||
| // column produced by IsNullFunction. | ||
| TEST_F(CudfLogicalFunctionsTest, notOfIsNull) { | ||
| auto data = makeRowVector( | ||
| {"c0"}, | ||
| {makeNullableFlatVector<int32_t>( | ||
| {1, std::nullopt, 3, std::nullopt, 5})}); | ||
| runProject( | ||
| {data}, | ||
| "NOT (c0 IS NULL) AS r", | ||
| "SELECT NOT (c0 IS NULL) AS r FROM tmp"); | ||
| } | ||
|
|
||
| } // namespace |
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.
Instead of making a NotFunction, you can make a UnaryFunction and during registration, provide a factory like so: