Skip to content
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-8921][GLUTEN-8922][CH] Fix checkDecimalOverflowSparkOrNull and lead function #8929

Merged
merged 1 commit into from
Mar 8, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,38 @@ class GlutenClickhouseFunctionSuite extends GlutenClickHouseTPCHAbstractSuite {
}
}

test("GLUTEN-8921: Type mismatch at checkDecimalOverflowSparkOrNull") {
compareResultsAgainstVanillaSpark(
"""
|select l_shipdate, avg(l_quantity), count(0) over() COU,
|SUM(-1.1) over() SU, AVG(-2) over() AV,
|max(-1.1) over() MA, min(-3) over() MI
|from lineitem
|where l_shipdate <= date'1998-09-02'
|group by l_shipdate
|order by l_shipdate
""".stripMargin,
true,
{ _ => }
)
}

test("GLUTEN-8922: Incorrect result in lead function with constant col") {
compareResultsAgainstVanillaSpark(
"""
|select l_shipdate,
|FIRST_VALUE(-2) over() FI,
|LAST_VALUE(-2) over() LA,
|lag(-2) over(order by l_shipdate) lag0,
|lead(-2) over(order by l_shipdate) lead0
|from lineitem
|where l_shipdate <= date'1998-09-02'
|group by l_shipdate
|order by l_shipdate
""".stripMargin,
true,
{ _ => }
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,20 @@ class FunctionCheckDecimalOverflow : public IFunction
UInt32 precision = extractArgument(arguments[1]);
UInt32 scale = extractArgument(arguments[2]);
auto return_type = createDecimal<DataTypeDecimal>(precision, scale);
if constexpr (exception_mode == CheckExceptionMode::Null)
{
if (!arguments[0].type->isNullable())
return std::make_shared<DataTypeNullable>(return_type);
}
if (isReturnTypeNullable(arguments[0]))
return std::make_shared<DataTypeNullable>(return_type);
return return_type;
}

bool isReturnTypeNullable(const ColumnWithTypeAndName & arg) const
{
if constexpr (exception_mode == CheckExceptionMode::Null)
return true;
if (arg.type->isNullable())
return true;
return false;
}

ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
UInt32 to_precision = extractArgument(arguments[1]);
Expand All @@ -133,7 +139,10 @@ class FunctionCheckDecimalOverflow : public IFunction
auto from_scale = getDecimalScale(*src_col.type);
if (from_precision == to_precision && from_scale == to_scale)
{
dst_col = src_col.column;
if (isReturnTypeNullable(arguments[0]))
dst_col = makeNullable(src_col.column);
else
dst_col = src_col.column;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ LeadParser::parseFunctionArguments(const CommonFunctionInfo & func_info, DB::Act
/// The 3rd arg is default value
/// when it is set to null, the 1st arg must be nullable
const auto & arg2 = func_info.arguments[2].value();
const auto * arg0_col = actions_dag.getInputs()[arg0.selection().direct_reference().struct_field().field()];
const auto * arg0_col = parseExpression(actions_dag, arg0);
auto arg0_col_name = arg0_col->result_name;
auto arg0_col_type= arg0_col->result_type;
const DB::ActionsDAG::Node * node = nullptr;
if (arg2.has_literal() && arg2.literal().has_null() && !arg0_col_type->isNullable())
{
node = ActionsDAGUtil::convertNodeType(
actions_dag,
&actions_dag.findInOutputs(arg0_col_name),
arg0_col,
DB::makeNullable(arg0_col_type),
arg0_col_name);
actions_dag.addOrReplaceInOutputs(*node);
Expand Down Expand Up @@ -76,15 +76,15 @@ LagParser::parseFunctionArguments(const CommonFunctionInfo & func_info, DB::Acti
/// The 3rd arg is default value
/// when it is set to null, the 1st arg must be nullable
const auto & arg2 = func_info.arguments[2].value();
const auto * arg0_col = actions_dag.getInputs()[arg0.selection().direct_reference().struct_field().field()];
const auto * arg0_col = parseExpression(actions_dag, arg0);
auto arg0_col_name = arg0_col->result_name;
auto arg0_col_type = arg0_col->result_type;
const DB::ActionsDAG::Node * node = nullptr;
if (arg2.has_literal() && arg2.literal().has_null() && !arg0_col->result_type->isNullable())
{
node = ActionsDAGUtil::convertNodeType(
actions_dag,
&actions_dag.findInOutputs(arg0_col_name),
arg0_col,
makeNullable(arg0_col_type),
arg0_col_name);
actions_dag.addOrReplaceInOutputs(*node);
Expand Down