Skip to content

Commit

Permalink
Add RowConstructorWithNull expression
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Sep 7, 2023
1 parent c79be9b commit f466517
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions velox/expression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_library(
VectorFunction.cpp
RegisterSpecialForm.cpp
RowConstructor.cpp
RowConstructorWithNull.cpp
SimpleFunctionRegistry.cpp
SpecialFormRegistry.cpp
SwitchExpr.cpp
Expand Down
4 changes: 4 additions & 0 deletions velox/expression/RegisterSpecialForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "velox/expression/ConjunctExpr.h"
#include "velox/expression/FunctionCallToSpecialForm.h"
#include "velox/expression/RowConstructor.h"
#include "velox/expression/RowConstructorWithNull.h"
#include "velox/expression/SpecialFormRegistry.h"
#include "velox/expression/SwitchExpr.h"
#include "velox/expression/TryExpr.h"
Expand All @@ -47,5 +48,8 @@ void registerFunctionCallToSpecialForms() {
registerFunctionCallToSpecialForm(
RowConstructorCallToSpecialForm::kRowConstructor,
std::make_unique<RowConstructorCallToSpecialForm>());
registerFunctionCallToSpecialForm(
RowConstructorWithNullCallToSpecialForm::kRowConstructorWithNull,
std::make_unique<RowConstructorWithNullCallToSpecialForm>());
}
} // namespace facebook::velox::exec
66 changes: 66 additions & 0 deletions velox/expression/RowConstructorWithNull.cpp
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
44 changes: 44 additions & 0 deletions velox/expression/RowConstructorWithNull.h
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
1 change: 1 addition & 0 deletions velox/functions/prestosql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ add_library(
Repeat.cpp
Reverse.cpp
RowFunction.cpp
RowFunctionWithNull.cpp
Sequence.cpp
SimpleComparisonMatcher.cpp
Slice.cpp
Expand Down
72 changes: 72 additions & 0 deletions velox/functions/prestosql/RowFunctionWithNull.cpp
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void registerAllSpecialFormGeneralFunctions() {
exec::registerFunctionCallToSpecialForms();
VELOX_REGISTER_VECTOR_FUNCTION(udf_in, "in");
VELOX_REGISTER_VECTOR_FUNCTION(udf_concat_row, "row_constructor");
VELOX_REGISTER_VECTOR_FUNCTION(
udf_concat_row_with_null, "row_constructor_with_null");
registerIsNullFunction("is_null");
}

Expand Down

0 comments on commit f466517

Please sign in to comment.