diff --git a/cpp/velox/CMakeLists.txt b/cpp/velox/CMakeLists.txt index b762854c8181..300abd6ca365 100644 --- a/cpp/velox/CMakeLists.txt +++ b/cpp/velox/CMakeLists.txt @@ -240,6 +240,7 @@ set(VELOX_SRCS memory/VeloxColumnarBatch.cc memory/VeloxMemoryManager.cc operators/functions/RegistrationAllFunctions.cc + operators/functions/RowConstructorWithNull.cc operators/serializer/VeloxColumnarToRowConverter.cc operators/serializer/VeloxColumnarBatchSerializer.cc operators/serializer/VeloxRowToColumnarConverter.cc diff --git a/cpp/velox/operators/functions/RegistrationAllFunctions.cc b/cpp/velox/operators/functions/RegistrationAllFunctions.cc index cacd20cb6e0d..83923be5f802 100644 --- a/cpp/velox/operators/functions/RegistrationAllFunctions.cc +++ b/cpp/velox/operators/functions/RegistrationAllFunctions.cc @@ -14,10 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "RegistrationAllFunctions.h" -#include "Arithmetic.h" -#include "RowConstructor.h" +#include "operators/functions/RegistrationAllFunctions.h" +#include "operators/functions/Arithmetic.h" +#include "operators/functions/RowConstructorWithNull.h" +#include "operators/functions/RowFunctionWithNull.h" +#include "velox/expression/SpecialFormRegistry.h" #include "velox/expression/VectorFunction.h" #include "velox/functions/lib/RegistrationHelpers.h" #include "velox/functions/prestosql/aggregates/RegisterAggregateFunctions.h" @@ -43,13 +45,21 @@ void registerFunctionOverwrite() { velox::functions::sparksql::xxhash64WithSeedSignatures(), velox::functions::sparksql::makeXxHash64WithSeed); - facebook::velox::functions::registerUnaryNumeric({"round"}); - facebook::velox::registerFunction({"round"}); - facebook::velox::registerFunction({"round"}); - facebook::velox::registerFunction({"round"}); - facebook::velox::registerFunction({"round"}); - facebook::velox::registerFunction({"round"}); - facebook::velox::registerFunction({"round"}); + velox::functions::registerUnaryNumeric({"round"}); + velox::registerFunction({"round"}); + velox::registerFunction({"round"}); + velox::registerFunction({"round"}); + velox::registerFunction({"round"}); + velox::registerFunction({"round"}); + velox::registerFunction({"round"}); + + velox::exec::registerVectorFunction( + "row_constructor_with_null", + std::vector>{}, + std::make_unique()); + velox::exec::registerFunctionCallToSpecialForm( + RowConstructorWithNullCallToSpecialForm::kRowConstructorWithNull, + std::make_unique()); } } // namespace diff --git a/cpp/velox/operators/functions/RowConstructorWithNull.cc b/cpp/velox/operators/functions/RowConstructorWithNull.cc new file mode 100644 index 000000000000..411dd478e07e --- /dev/null +++ b/cpp/velox/operators/functions/RowConstructorWithNull.cc @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 "RowConstructorWithNull.h" +#include "velox/expression/VectorFunction.h" + +namespace gluten { +facebook::velox::TypePtr RowConstructorWithNullCallToSpecialForm::resolveType( + const std::vector& argTypes) { + auto numInput = argTypes.size(); + std::vector names(numInput); + std::vector types(numInput); + for (auto i = 0; i < numInput; i++) { + types[i] = argTypes[i]; + names[i] = fmt::format("c{}", i + 1); + } + return facebook::velox::ROW(std::move(names), std::move(types)); +} + +facebook::velox::exec::ExprPtr RowConstructorWithNullCallToSpecialForm::constructSpecialForm( + const std::string& name, + const facebook::velox::TypePtr& type, + std::vector&& compiledChildren, + bool trackCpuUsage, + const facebook::velox::core::QueryConfig& config) { + auto rowConstructorVectorFunction = + facebook::velox::exec::vectorFunctionFactories().withRLock([&config, &name](auto& functionMap) { + auto functionIterator = functionMap.find(name); + return functionIterator->second.factory(name, {}, config); + }); + + return std::make_shared( + type, std::move(compiledChildren), rowConstructorVectorFunction, name, trackCpuUsage); +} + +facebook::velox::exec::ExprPtr RowConstructorWithNullCallToSpecialForm::constructSpecialForm( + const facebook::velox::TypePtr& type, + std::vector&& compiledChildren, + bool trackCpuUsage, + const facebook::velox::core::QueryConfig& config) { + return constructSpecialForm(kRowConstructorWithNull, type, std::move(compiledChildren), trackCpuUsage, config); +} +} // namespace gluten diff --git a/cpp/velox/operators/functions/RowConstructorWithNull.h b/cpp/velox/operators/functions/RowConstructorWithNull.h new file mode 100644 index 000000000000..6cfeaee37a6d --- /dev/null +++ b/cpp/velox/operators/functions/RowConstructorWithNull.h @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 gluten { +class RowConstructorWithNullCallToSpecialForm : public facebook::velox::exec::FunctionCallToSpecialForm { + public: + facebook::velox::TypePtr resolveType(const std::vector& argTypes) override; + + facebook::velox::exec::ExprPtr constructSpecialForm( + const facebook::velox::TypePtr& type, + std::vector&& compiledChildren, + bool trackCpuUsage, + const facebook::velox::core::QueryConfig& config) override; + + static constexpr const char* kRowConstructorWithNull = "row_constructor_with_null"; + + protected: + facebook::velox::exec::ExprPtr constructSpecialForm( + const std::string& name, + const facebook::velox::TypePtr& type, + std::vector&& compiledChildren, + bool trackCpuUsage, + const facebook::velox::core::QueryConfig& config); +}; +} // namespace gluten diff --git a/cpp/velox/operators/functions/RowConstructor.h b/cpp/velox/operators/functions/RowFunctionWithNull.h similarity index 93% rename from cpp/velox/operators/functions/RowConstructor.h rename to cpp/velox/operators/functions/RowFunctionWithNull.h index a9c83427e099..d66fda99e8ed 100644 --- a/cpp/velox/operators/functions/RowConstructor.h +++ b/cpp/velox/operators/functions/RowFunctionWithNull.h @@ -23,9 +23,9 @@ namespace gluten { /** - * A customized RowConstructor function to set struct as null when one of its argument is null. + * A customized RowFunction to set struct as null when one of its argument is null. */ -class RowConstructor final : public facebook::velox::exec::VectorFunction { +class RowFunctionWithNull final : public facebook::velox::exec::VectorFunction { void apply( const facebook::velox::SelectivityVector& rows, std::vector& args,