-
Notifications
You must be signed in to change notification settings - Fork 644
[GLUTEN-10134][VL] Preserve store assignment cast modes (STORE_ASSIGNMENT_POLICY defaults to ANSI)
#12051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GLUTEN-10134][VL] Preserve store assignment cast modes (STORE_ASSIGNMENT_POLICY defaults to ANSI)
#12051
Changes from 1 commit
10414a2
44d0731
6e4b609
0b6c65a
222f1f1
a998015
eea937a
b2f41b5
89f3284
8e1a1cb
36133ca
44eb11f
2de3e9e
ec071d3
331bfb9
1350edb
9cc99a2
653285b
469c319
570e3c2
a21cffc
06856ba
7dc2bef
abb474c
83d88df
d69e751
71c3610
dac6f8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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 "operators/functions/SparkCastModeSpecialForms.h" | ||
|
|
||
| #include "velox/expression/SpecialFormRegistry.h" | ||
| #include "velox/functions/sparksql/specialforms/SparkCastExpr.h" | ||
| #include "velox/functions/sparksql/specialforms/SparkCastHooks.h" | ||
|
|
||
| namespace gluten { | ||
| namespace { | ||
|
|
||
| using namespace facebook::velox; | ||
| using facebook::velox::functions::sparksql::SparkCastExpr; | ||
| using facebook::velox::functions::sparksql::SparkCastHooks; | ||
|
|
||
| bool isIntegralType(const TypePtr& type) { | ||
| return type == TINYINT() || type == SMALLINT() || type == INTEGER() || | ||
| type == BIGINT(); | ||
| } | ||
|
|
||
| bool isAnsiSupported(const TypePtr& fromType, const TypePtr& toType) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we really need it, to reduce code maintenance effort, could we make it public in Velox to allow calling it here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created the pr here facebookincubator/velox#17687 |
||
| if (fromType->isVarchar()) { | ||
| return toType->isBoolean() || toType->isDate() || isIntegralType(toType); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| 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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am still wondering if we can move or keep these code on Velox side, and just call the register API in Gluten C++ code. Could you please clarify?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Existing Velox Spark |
||
| public: | ||
| exec::ExprPtr constructSpecialForm( | ||
| const TypePtr& type, | ||
| std::vector<exec::ExprPtr>&& compiledChildren, | ||
| 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 = !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, | ||
| 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 | ||
|
|
||
| void registerSparkCastModeSpecialForms() { | ||
| exec::registerFunctionCallToSpecialForm( | ||
| kSparkAnsiCast, std::make_unique<SparkAnsiCastCallToSpecialForm>()); | ||
| exec::registerFunctionCallToSpecialForm( | ||
| kSparkLegacyCast, std::make_unique<SparkLegacyCastCallToSpecialForm>()); | ||
| } | ||
|
|
||
| } // namespace gluten | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| namespace gluten { | ||
|
|
||
| constexpr const char* kSparkAnsiCast = "spark_ansi_cast"; | ||
| constexpr const char* kSparkLegacyCast = "spark_legacy_cast"; | ||
|
|
||
| void registerSparkCastModeSpecialForms(); | ||
|
|
||
| } // namespace gluten |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,25 @@ class GlutenInsertSuite | |
| } | ||
| } | ||
|
|
||
| testGluten("storeAssignmentPolicy default ANSI is independent from ANSI mode") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't appear to be a variant of a Spark test. Can we move it to a new suite under |
||
| withTable("store_assignment_ansi") { | ||
| withSQLConf(SQLConf.ANSI_ENABLED.key -> "false") { | ||
| assert(SQLConf.get.storeAssignmentPolicy == SQLConf.StoreAssignmentPolicy.ANSI) | ||
|
|
||
| spark.sql("CREATE TABLE store_assignment_ansi (c INT) USING PARQUET") | ||
| intercept[Exception] { | ||
| spark.sql("INSERT INTO store_assignment_ansi SELECT '2147483648'").collect() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a check for the exception message to confirm the expected exception is thrown? |
||
| } | ||
|
|
||
| withSQLConf( | ||
| SQLConf.STORE_ASSIGNMENT_POLICY.key -> SQLConf.StoreAssignmentPolicy.LEGACY.toString) { | ||
| spark.sql("INSERT INTO store_assignment_ansi SELECT '2147483648'").collect() | ||
| checkAnswer(spark.table("store_assignment_ansi"), Row(null)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ignoreGluten("Cleanup staging files if job failed") { | ||
| // Using a unique table name in this test. Sometimes, the table is not removed for some unknown | ||
| // reason, which can cause test failure (location already exists) if other following tests have | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also a check on the Velox side. Will we maintain the ANSI support check only here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intended to mirror the existing Velox ANSI support side check not to replace it, since that check is private today, I added this local helper for the expression level ANSI/legacy cast mode. I’ll add a comment to make sure we keep it aligned with Velox