Skip to content

Commit

Permalink
Code style cleanup (#1784)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #1784

Reviewed By: spershin

Differential Revision: D37088299

Pulled By: mbasmanova

fbshipit-source-id: aae88f6d067a8e982b530235ec482bd5e67ca74d
  • Loading branch information
zhejiangxiaomai authored and facebook-github-bot committed Jun 13, 2022
1 parent 3574493 commit 0e4d3a5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 50 deletions.
30 changes: 15 additions & 15 deletions velox/substrait/SubstraitParser.h
Expand Up @@ -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<std::shared_ptr<SubstraitParser::SubstraitType>> parseNamedStruct(
const ::substrait::NamedStruct& namedStruct);

/// Used to parse Substrait Type.
/// Parse Substrait Type.
std::shared_ptr<SubstraitType> 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<std::string> 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:
/// <function name>:<short_arg_type0>_<short_arg_type1>_..._<short_arg_typeN>.
Expand All @@ -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<std::string>& types) const;

/// Find the Velox function name according to the function id
Expand All @@ -85,13 +85,13 @@ class SubstraitParser {
const std::unordered_map<uint64_t, std::string>& 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<std::string, std::string> substraitVeloxFunctionMap_ = {
{"add", "plus"},
Expand Down
59 changes: 31 additions & 28 deletions velox/substrait/SubstraitToVeloxExpr.cpp
Expand Up @@ -21,21 +21,21 @@ namespace facebook::velox::substrait {

std::shared_ptr<const core::FieldAccessTypedExpr>
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<core::FieldAccessTypedExpr>(
inputTypes[colIdx],
std::make_shared<core::InputTypedExpr>(inputTypes[colIdx]),
Expand All @@ -52,39 +52,42 @@ SubstraitVeloxExprConverter::toVeloxExpr(

std::shared_ptr<const core::ITypedExpr>
SubstraitVeloxExprConverter::toVeloxExpr(
const ::substrait::Expression::ScalarFunction& sFunc,
const ::substrait::Expression::ScalarFunction& substraitFunc,
const RowTypePtr& inputType) {
std::vector<std::shared_ptr<const core::ITypedExpr>> 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<const core::CallTypedExpr>(
veloxType, std::move(params), veloxFunction);
}

std::shared_ptr<const core::ConstantTypedExpr>
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<core::ConstantTypedExpr>(variant(sLit.boolean()));
return std::make_shared<core::ConstantTypedExpr>(
variant(substraitLit.boolean()));
case ::substrait::Expression_Literal::LiteralTypeCase::kI32:
return std::make_shared<core::ConstantTypedExpr>(variant(sLit.i32()));
return std::make_shared<core::ConstantTypedExpr>(
variant(substraitLit.i32()));
case ::substrait::Expression_Literal::LiteralTypeCase::kI64:
return std::make_shared<core::ConstantTypedExpr>(variant(sLit.i64()));
return std::make_shared<core::ConstantTypedExpr>(
variant(substraitLit.i64()));
case ::substrait::Expression_Literal::LiteralTypeCase::kFp64:
return std::make_shared<core::ConstantTypedExpr>(variant(sLit.fp64()));
return std::make_shared<core::ConstantTypedExpr>(
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<core::ConstantTypedExpr>(
veloxType, variant::null(veloxType->kind()));
}
Expand All @@ -111,19 +114,19 @@ SubstraitVeloxExprConverter::toVeloxExpr(

std::shared_ptr<const core::ITypedExpr>
SubstraitVeloxExprConverter::toVeloxExpr(
const ::substrait::Expression& sExpr,
const ::substrait::Expression& substraitExpr,
const RowTypePtr& inputType) {
std::shared_ptr<const core::ITypedExpr> 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);
Expand Down
14 changes: 7 additions & 7 deletions velox/substrait/SubstraitToVeloxExpr.h
Expand Up @@ -32,26 +32,26 @@ class SubstraitVeloxExprConverter {
const std::unordered_map<uint64_t, std::string>& functionMap)
: functionMap_(functionMap) {}

/// Used to convert Substrait Field into Velox Field Expression.
/// Convert Substrait Field into Velox Field Expression.
std::shared_ptr<const core::FieldAccessTypedExpr> 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<const core::ITypedExpr> toVeloxExpr(
const ::substrait::Expression::ScalarFunction& sFunc,
const ::substrait::Expression::ScalarFunction& substraitFunc,
const RowTypePtr& inputType);

/// Convert Substrait CastExpression to Velox Expression.
std::shared_ptr<const core::ITypedExpr> toVeloxExpr(
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<const core::ConstantTypedExpr> 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<const core::ITypedExpr> toVeloxExpr(
const ::substrait::Expression& substraitExpr,
const RowTypePtr& inputType);
Expand Down

0 comments on commit 0e4d3a5

Please sign in to comment.