forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RowConstructorWithNull expression
- Loading branch information
Showing
7 changed files
with
190 additions
and
0 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/RowConstructorWithNull.h" | ||
#include "velox/expression/VectorFunction.h" | ||
|
||
namespace facebook::velox::exec { | ||
|
||
TypePtr RowConstructorWithNullCallToSpecialForm::resolveType( | ||
const std::vector<TypePtr>& argTypes) { | ||
auto numInput = argTypes.size(); | ||
std::vector<std::string> names(numInput); | ||
std::vector<TypePtr> types(numInput); | ||
for (auto i = 0; i < numInput; i++) { | ||
types[i] = argTypes[i]; | ||
names[i] = fmt::format("c{}", i + 1); | ||
} | ||
return ROW(std::move(names), std::move(types)); | ||
} | ||
|
||
ExprPtr RowConstructorWithNullCallToSpecialForm::constructSpecialForm( | ||
const std::string& name, | ||
const TypePtr& type, | ||
std::vector<ExprPtr>&& compiledChildren, | ||
bool trackCpuUsage, | ||
const core::QueryConfig& config) { | ||
auto rowConstructorVectorFunction = | ||
vectorFunctionFactories().withRLock([&config, &name](auto& functionMap) { | ||
auto functionIterator = functionMap.find(name); | ||
return functionIterator->second.factory(name, {}, config); | ||
}); | ||
|
||
return std::make_shared<Expr>( | ||
type, | ||
std::move(compiledChildren), | ||
rowConstructorVectorFunction, | ||
name, | ||
trackCpuUsage); | ||
} | ||
|
||
ExprPtr RowConstructorWithNullCallToSpecialForm::constructSpecialForm( | ||
const TypePtr& type, | ||
std::vector<ExprPtr>&& compiledChildren, | ||
bool trackCpuUsage, | ||
const core::QueryConfig& config) { | ||
return constructSpecialForm( | ||
kRowConstructorWithNull, | ||
type, | ||
std::move(compiledChildren), | ||
trackCpuUsage, | ||
config); | ||
} | ||
} // 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,44 @@ | ||
/* | ||
* 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/FunctionCallToSpecialForm.h" | ||
#include "velox/expression/SpecialForm.h" | ||
|
||
namespace facebook::velox::exec { | ||
class RowConstructorWithNullCallToSpecialForm | ||
: public FunctionCallToSpecialForm { | ||
public: | ||
TypePtr resolveType(const std::vector<TypePtr>& argTypes) override; | ||
|
||
ExprPtr constructSpecialForm( | ||
const TypePtr& type, | ||
std::vector<ExprPtr>&& compiledChildren, | ||
bool trackCpuUsage, | ||
const core::QueryConfig& config) override; | ||
|
||
static constexpr const char* kRowConstructorWithNull = | ||
"row_constructor_with_null"; | ||
|
||
protected: | ||
ExprPtr constructSpecialForm( | ||
const std::string& name, | ||
const TypePtr& type, | ||
std::vector<ExprPtr>&& compiledChildren, | ||
bool trackCpuUsage, | ||
const core::QueryConfig& config); | ||
}; | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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/Expr.h" | ||
#include "velox/expression/VectorFunction.h" | ||
|
||
namespace facebook::velox::functions { | ||
namespace { | ||
|
||
class RowFunctionWithNull : public exec::VectorFunction { | ||
public: | ||
void apply( | ||
const SelectivityVector& rows, | ||
std::vector<VectorPtr>& args, | ||
const TypePtr& outputType, | ||
exec::EvalCtx& context, | ||
VectorPtr& result) const override { | ||
auto argsCopy = args; | ||
|
||
BufferPtr nulls = AlignedBuffer::allocate<char>( | ||
bits::nbytes(rows.size()), context.pool(), 1); | ||
auto* nullsPtr = nulls->asMutable<uint64_t>(); | ||
auto cntNull = 0; | ||
rows.applyToSelected([&](vector_size_t i) { | ||
bits::clearNull(nullsPtr, i); | ||
if (!bits::isBitNull(nullsPtr, i)) { | ||
for (size_t c = 0; c < argsCopy.size(); c++) { | ||
auto arg = argsCopy[c].get(); | ||
if (arg->mayHaveNulls() && arg->isNullAt(i)) { | ||
// If any argument of the struct is null, set the struct as null. | ||
bits::setNull(nullsPtr, i, true); | ||
cntNull++; | ||
break; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
RowVectorPtr row = std::make_shared<RowVector>( | ||
context.pool(), | ||
outputType, | ||
nulls, | ||
rows.size(), | ||
std::move(argsCopy), | ||
cntNull /*nullCount*/); | ||
context.moveOrCopyResult(row, rows, result); | ||
} | ||
|
||
bool isDefaultNullBehavior() const override { | ||
return false; | ||
} | ||
}; | ||
} // namespace | ||
|
||
VELOX_DECLARE_VECTOR_FUNCTION( | ||
udf_concat_row_with_null, | ||
std::vector<std::shared_ptr<exec::FunctionSignature>>{}, | ||
std::make_unique<RowFunctionWithNull>()); | ||
|
||
} // namespace facebook::velox::functions |
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