forked from facebookincubator/velox
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace coalesce function with a special form (facebookincubator#301)
Summary: X-link: pytorch/torcharrow#301 Coalesce needs to be a special form expression and cannot be a function because it is not supposed to evaluate remaining inputs once there are no nulls. For example, if column a has no nulls, the following expression should not try to evaluate `b / 0` and should not fail. ``` coalesce(a, b / 0) ``` This change introduces CoalesceExpr special form and deletes coalesce function. Also, fix a crash due to null pointer dereference in FlatVector::mutableRawValues. Pull Request resolved: facebookincubator#1430 Reviewed By: laithsakka Differential Revision: D35674128 Pulled By: mbasmanova fbshipit-source-id: 2d16cb3404843322da67b699e7163d53757225bd
- Loading branch information
1 parent
5f20256
commit 0e5431e
Showing
15 changed files
with
150 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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/CoalesceExpr.h" | ||
#include "velox/expression/VarSetter.h" | ||
|
||
namespace facebook::velox::exec { | ||
|
||
CoalesceExpr::CoalesceExpr(TypePtr type, std::vector<ExprPtr>&& inputs) | ||
: SpecialForm(std::move(type), std::move(inputs), kCoalesce) { | ||
for (auto i = 1; i < inputs_.size(); i++) { | ||
VELOX_USER_CHECK_EQ( | ||
inputs_[0]->type()->kind(), | ||
inputs_[i]->type()->kind(), | ||
"Inputs to coalesce must have the same type"); | ||
} | ||
} | ||
|
||
void CoalesceExpr::evalSpecialForm( | ||
const SelectivityVector& rows, | ||
EvalCtx* context, | ||
VectorPtr* result) { | ||
// Make sure to include current expression in the error message in case of an | ||
// exception. | ||
ExceptionContextSetter exceptionContext( | ||
{[](auto* expr) { return static_cast<Expr*>(expr)->toString(); }, this}); | ||
|
||
// Null positions to populate. | ||
exec::LocalSelectivityVector activeRowsHolder(context, rows.end()); | ||
auto activeRows = activeRowsHolder.get(); | ||
*activeRows = rows; | ||
|
||
// Fix finalSelection at "rows" unless already fixed. | ||
VarSetter finalSelection( | ||
context->mutableFinalSelection(), &rows, context->isFinalSelection()); | ||
VarSetter isFinalSelection(context->mutableIsFinalSelection(), false); | ||
|
||
for (int i = 0; i < inputs_.size(); i++) { | ||
inputs_[i]->eval(*activeRows, context, result); | ||
|
||
const uint64_t* rawNulls = (*result)->flatRawNulls(*activeRows); | ||
if (!rawNulls) { | ||
// No nulls left. | ||
return; | ||
} | ||
|
||
activeRows->deselectNonNulls(rawNulls, 0, activeRows->end()); | ||
if (!activeRows->hasSelections()) { | ||
// No nulls left. | ||
return; | ||
} | ||
} | ||
} | ||
} // namespace facebook::velox::exec |
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,37 @@ | ||
/* | ||
* 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/expression/ControlExpr.h" | ||
|
||
namespace facebook::velox::exec { | ||
|
||
const char* const kCoalesce = "coalesce"; | ||
|
||
class CoalesceExpr : public SpecialForm { | ||
public: | ||
CoalesceExpr(TypePtr type, std::vector<ExprPtr>&& inputs); | ||
|
||
void evalSpecialForm( | ||
const SelectivityVector& rows, | ||
EvalCtx* context, | ||
VectorPtr* result) override; | ||
|
||
bool propagatesNulls() const override { | ||
return false; | ||
} | ||
}; | ||
} // namespace facebook::velox::exec |
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 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
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