Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions velox/functions/sparksql/specialforms/SparkCastExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "velox/functions/sparksql/specialforms/SparkCastExpr.h"

#include "velox/expression/SpecialFormRegistry.h"
#include "velox/functions/sparksql/SparkQueryConfig.h"

namespace facebook::velox::functions::sparksql {
Expand All @@ -26,6 +27,70 @@
type == BIGINT();
}

exec::ExprPtr makeSparkCastExpr(
const TypePtr& type,
exec::ExprPtr&& input,
bool trackCpuUsage,
bool isTryCast,
bool allowOverflow,
const core::QueryConfig& config) {
return std::make_shared<SparkCastExpr>(
type,
std::move(input),
trackCpuUsage,
isTryCast,
std::make_shared<SparkCastHooks>(config, allowOverflow));
}

class SparkAnsiCastCallToSpecialForm : public exec::CastCallToSpecialForm {
public:
exec::ExprPtr constructSpecialForm(
const TypePtr& type,
std::vector<exec::ExprPtr>&& compiledChildren,

Check warning on line 49 in velox/functions/sparksql/specialforms/SparkCastExpr.cpp

View workflow job for this annotation

GitHub Actions / Build with GCC / Linux release with adapters

cppcoreguidelines-rvalue-reference-param-not-moved

rvalue reference parameter 'compiledChildren' is never moved from inside the function body
bool trackCpuUsage,
const core::QueryConfig& config) override {
VELOX_CHECK_EQ(
compiledChildren.size(),
1,
"ANSI CAST statements expect exactly 1 argument, received {}.",
compiledChildren.size());

const auto& fromType = compiledChildren[0]->type();
const bool isTryCast =
!SparkCastCallToSpecialForm::isAnsiSupported(fromType, type);
return makeSparkCastExpr(
type,
std::move(compiledChildren[0]),
trackCpuUsage,
isTryCast,
isTryCast,
config);
}
};

class SparkLegacyCastCallToSpecialForm : public exec::CastCallToSpecialForm {
public:
exec::ExprPtr constructSpecialForm(
const TypePtr& type,
std::vector<exec::ExprPtr>&& compiledChildren,

Check warning on line 75 in velox/functions/sparksql/specialforms/SparkCastExpr.cpp

View workflow job for this annotation

GitHub Actions / Build with GCC / Linux release with adapters

cppcoreguidelines-rvalue-reference-param-not-moved

rvalue reference parameter 'compiledChildren' is never moved from inside the function body
bool trackCpuUsage,
const core::QueryConfig& config) override {
VELOX_CHECK_EQ(
compiledChildren.size(),
1,
"LEGACY CAST statements expect exactly 1 argument, received {}.",
compiledChildren.size());

return makeSparkCastExpr(
type,
std::move(compiledChildren[0]),
trackCpuUsage,
true,
true,
config);
}
};

} // namespace

bool SparkCastCallToSpecialForm::isAnsiSupported(
Expand Down Expand Up @@ -96,4 +161,13 @@
std::make_shared<SparkCastHooks>(config, false));
}

void registerSparkCastModeSpecialForms(
const std::string& ansiCastName,
const std::string& legacyCastName) {
exec::registerFunctionCallToSpecialForm(
ansiCastName, std::make_unique<SparkAnsiCastCallToSpecialForm>());
exec::registerFunctionCallToSpecialForm(
legacyCastName, std::make_unique<SparkLegacyCastCallToSpecialForm>());
}

} // namespace facebook::velox::functions::sparksql
7 changes: 6 additions & 1 deletion velox/functions/sparksql/specialforms/SparkCastExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#pragma once

#include <string>

#include "velox/expression/CastExpr.h"
#include "velox/functions/sparksql/specialforms/SparkCastHooks.h"

Expand Down Expand Up @@ -45,7 +47,6 @@ class SparkCastCallToSpecialForm : public exec::CastCallToSpecialForm {
bool trackCpuUsage,
const core::QueryConfig& config) override;

private:
/// Determines if ANSI mode is supported for casting from fromType to toType.
/// TODO: Remove this function once all cast operations support ANSI mode.
/// @param fromType The source type of the cast
Expand All @@ -62,4 +63,8 @@ class SparkTryCastCallToSpecialForm : public exec::TryCastCallToSpecialForm {
bool trackCpuUsage,
const core::QueryConfig& config) override;
};

void registerSparkCastModeSpecialForms(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently no call site wires it into registration.

const std::string& ansiCastName,
const std::string& legacyCastName);
} // namespace facebook::velox::functions::sparksql
Loading