diff --git a/velox/substrait/SubstraitParser.h b/velox/substrait/SubstraitParser.h index 41249617cc22..e82020e79eb6 100644 --- a/velox/substrait/SubstraitParser.h +++ b/velox/substrait/SubstraitParser.h @@ -31,35 +31,35 @@ namespace facebook::velox::substrait { /// components, and convert them into recognizable representations. class SubstraitParser { public: - /// Used to store the type name and nullability. + /// Stores the type name and nullability. struct SubstraitType { std::string type; bool nullable; }; - /// Used to parse Substrait NamedStruct. + /// Parse Substrait NamedStruct. std::vector> parseNamedStruct( const ::substrait::NamedStruct& namedStruct); - /// Used to parse Substrait Type. + /// Parse Substrait Type. std::shared_ptr parseType( const ::substrait::Type& substraitType); - /// Used to parse Substrait ReferenceSegment. + /// Parse Substrait ReferenceSegment. int32_t parseReferenceSegment( const ::substrait::Expression::ReferenceSegment& refSegment); - /// Used to make names in the format of {prefix}_{index}. + /// Make names in the format of {prefix}_{index}. std::vector makeNames(const std::string& prefix, int size); - /// Used to make node name in the format of n{nodeId}_{colIdx}. + /// Make node name in the format of n{nodeId}_{colIdx}. std::string makeNodeName(int nodeId, int colIdx); - /// Used to get the column index from a node name in the format of + /// Get the column index from a node name in the format of /// n{nodeId}_{colIdx}. int getIdxFromNodeName(const std::string& nodeName); - /// Used to find the Substrait function name according to the function id + /// Find the Substrait function name according to the function id /// from a pre-constructed function map. The function specification can be /// a simple name or a compound name. The compound name format is: /// :__..._. @@ -72,11 +72,11 @@ class SubstraitParser { /// Extracts the function name for a function from specified compound name. /// When the input is a simple name, it will be returned. - std::string getFunctionName(const std::string& subFuncSpec) const; + std::string getFunctionName(const std::string& functionSpec) const; /// Extracts argument types for a function from specified compound name. void getFunctionTypes( - const std::string& subFuncSpec, + const std::string& functionSpec, std::vector& types) const; /// Find the Velox function name according to the function id @@ -85,13 +85,13 @@ class SubstraitParser { const std::unordered_map& functionMap, uint64_t id) const; - /// Map the Substrait function key word into Velox function key word. - std::string mapToVeloxFunction(const std::string& subFunc) const; + /// Map the Substrait function keyword into Velox function keyword. + std::string mapToVeloxFunction(const std::string& substraitFunction) const; private: - /// Used for mapping Substrait function key words into Velox functions' key - /// words. Key: the Substrait function key word, Value: the Velox function key - /// word. For those functions with different names in Substrait and Velox, + /// A map used for mapping Substrait function keywords into Velox functions' + /// keywords. Key: the Substrait function keyword, Value: the Velox function + /// keyword. For those functions with different names in Substrait and Velox, /// a mapping relation should be added here. std::unordered_map substraitVeloxFunctionMap_ = { {"add", "plus"}, diff --git a/velox/substrait/SubstraitToVeloxExpr.cpp b/velox/substrait/SubstraitToVeloxExpr.cpp index 80d9978cb969..fee7c43f4478 100644 --- a/velox/substrait/SubstraitToVeloxExpr.cpp +++ b/velox/substrait/SubstraitToVeloxExpr.cpp @@ -21,21 +21,21 @@ namespace facebook::velox::substrait { std::shared_ptr SubstraitVeloxExprConverter::toVeloxExpr( - const ::substrait::Expression::FieldReference& sField, + const ::substrait::Expression::FieldReference& substraitField, const RowTypePtr& inputType) { - auto typeCase = sField.reference_type_case(); + auto typeCase = substraitField.reference_type_case(); switch (typeCase) { case ::substrait::Expression::FieldReference::ReferenceTypeCase:: kDirectReference: { - auto dRef = sField.direct_reference(); - int32_t colIdx = substraitParser_.parseReferenceSegment(dRef); + const auto& directRef = substraitField.direct_reference(); + int32_t colIdx = substraitParser_.parseReferenceSegment(directRef); const auto& inputTypes = inputType->children(); const auto& inputNames = inputType->names(); const int64_t inputSize = inputNames.size(); if (colIdx <= inputSize) { - // convert type to row + // Convert type to row. return std::make_shared( inputTypes[colIdx], std::make_shared(inputTypes[colIdx]), @@ -52,39 +52,42 @@ SubstraitVeloxExprConverter::toVeloxExpr( std::shared_ptr SubstraitVeloxExprConverter::toVeloxExpr( - const ::substrait::Expression::ScalarFunction& sFunc, + const ::substrait::Expression::ScalarFunction& substraitFunc, const RowTypePtr& inputType) { std::vector> params; - params.reserve(sFunc.args().size()); - for (const auto& sArg : sFunc.args()) { + params.reserve(substraitFunc.args().size()); + for (const auto& sArg : substraitFunc.args()) { params.emplace_back(toVeloxExpr(sArg, inputType)); } - auto functionId = sFunc.function_reference(); - auto veloxFunction = - substraitParser_.findVeloxFunction(functionMap_, functionId); - auto substraitType = substraitParser_.parseType(sFunc.output_type()); - auto veloxType = toVeloxType(substraitType->type); + const auto& veloxFunction = substraitParser_.findVeloxFunction( + functionMap_, substraitFunc.function_reference()); + const auto& veloxType = toVeloxType( + substraitParser_.parseType(substraitFunc.output_type())->type); + return std::make_shared( veloxType, std::move(params), veloxFunction); } std::shared_ptr SubstraitVeloxExprConverter::toVeloxExpr( - const ::substrait::Expression::Literal& sLit) { - auto typeCase = sLit.literal_type_case(); + const ::substrait::Expression::Literal& substraitLit) { + auto typeCase = substraitLit.literal_type_case(); switch (typeCase) { case ::substrait::Expression_Literal::LiteralTypeCase::kBoolean: - return std::make_shared(variant(sLit.boolean())); + return std::make_shared( + variant(substraitLit.boolean())); case ::substrait::Expression_Literal::LiteralTypeCase::kI32: - return std::make_shared(variant(sLit.i32())); + return std::make_shared( + variant(substraitLit.i32())); case ::substrait::Expression_Literal::LiteralTypeCase::kI64: - return std::make_shared(variant(sLit.i64())); + return std::make_shared( + variant(substraitLit.i64())); case ::substrait::Expression_Literal::LiteralTypeCase::kFp64: - return std::make_shared(variant(sLit.fp64())); + return std::make_shared( + variant(substraitLit.fp64())); case ::substrait::Expression_Literal::LiteralTypeCase::kNull: { - auto substraitType = substraitParser_.parseType(sLit.null()); - auto veloxType = toVeloxType(substraitType->type); - + auto veloxType = + toVeloxType(substraitParser_.parseType(substraitLit.null())->type); return std::make_shared( veloxType, variant::null(veloxType->kind())); } @@ -111,19 +114,19 @@ SubstraitVeloxExprConverter::toVeloxExpr( std::shared_ptr SubstraitVeloxExprConverter::toVeloxExpr( - const ::substrait::Expression& sExpr, + const ::substrait::Expression& substraitExpr, const RowTypePtr& inputType) { std::shared_ptr veloxExpr; - auto typeCase = sExpr.rex_type_case(); + auto typeCase = substraitExpr.rex_type_case(); switch (typeCase) { case ::substrait::Expression::RexTypeCase::kLiteral: - return toVeloxExpr(sExpr.literal()); + return toVeloxExpr(substraitExpr.literal()); case ::substrait::Expression::RexTypeCase::kScalarFunction: - return toVeloxExpr(sExpr.scalar_function(), inputType); + return toVeloxExpr(substraitExpr.scalar_function(), inputType); case ::substrait::Expression::RexTypeCase::kSelection: - return toVeloxExpr(sExpr.selection(), inputType); + return toVeloxExpr(substraitExpr.selection(), inputType); case ::substrait::Expression::RexTypeCase::kCast: - return toVeloxExpr(sExpr.cast(), inputType); + return toVeloxExpr(substraitExpr.cast(), inputType); default: VELOX_NYI( "Substrait conversion not supported for Expression '{}'", typeCase); diff --git a/velox/substrait/SubstraitToVeloxExpr.h b/velox/substrait/SubstraitToVeloxExpr.h index dfe1e42903cb..4f310262edcb 100644 --- a/velox/substrait/SubstraitToVeloxExpr.h +++ b/velox/substrait/SubstraitToVeloxExpr.h @@ -32,14 +32,14 @@ class SubstraitVeloxExprConverter { const std::unordered_map& functionMap) : functionMap_(functionMap) {} - /// Used to convert Substrait Field into Velox Field Expression. + /// Convert Substrait Field into Velox Field Expression. std::shared_ptr toVeloxExpr( - const ::substrait::Expression::FieldReference& sField, + const ::substrait::Expression::FieldReference& substraitField, const RowTypePtr& inputType); - /// Used to convert Substrait ScalarFunction into Velox Expression. + /// Convert Substrait ScalarFunction into Velox Expression. std::shared_ptr toVeloxExpr( - const ::substrait::Expression::ScalarFunction& sFunc, + const ::substrait::Expression::ScalarFunction& substraitFunc, const RowTypePtr& inputType); /// Convert Substrait CastExpression to Velox Expression. @@ -47,11 +47,11 @@ class SubstraitVeloxExprConverter { const ::substrait::Expression::Cast& castExpr, const RowTypePtr& inputType); - /// Used to convert Substrait Literal into Velox Expression. + /// Convert Substrait Literal into Velox Expression. std::shared_ptr toVeloxExpr( - const ::substrait::Expression::Literal& sLit); + const ::substrait::Expression::Literal& substraitLit); - /// Used to convert Substrait Expression into Velox Expression. + /// Convert Substrait Expression into Velox Expression. std::shared_ptr toVeloxExpr( const ::substrait::Expression& substraitExpr, const RowTypePtr& inputType);