Skip to content

Commit

Permalink
[GLUTEN-8074][CH] Fix adjust output constant column
Browse files Browse the repository at this point in the history
  • Loading branch information
lwz9103 committed Nov 28, 2024
1 parent 6bedcfa commit f7f385e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,18 @@ class GlutenClickHouseDecimalSuite
)
}

test("GLUTEN-8074 Fix adjust output constant column") {
val sql =
s"""
|select bround(1002.5786, -3),
| bround(-10.8, 0),
| bround(13.888888888889, 5)
|from $decimalTable
|WHERE bround(cast(decimal_field as decimal(30, 2)), 1) > 0 LIMIT 2;
|""".stripMargin
runQueryAndCompare(sql)(_ => {})
}

test("fix decimal32 with negative value") {
val schema = StructType.apply(
Seq(
Expand Down
27 changes: 19 additions & 8 deletions cpp-ch/local-engine/Parser/SerializedPlanParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,31 @@ void SerializedPlanParser::adjustOutput(const DB::QueryPlanPtr & query_plan, con
else
{
need_final_project = true;

bool need_const = origin_column.column && isColumnConst(*origin_column.column);
ColumnWithTypeAndName final_column(
need_const ? final_type->createColumnConst(0, assert_cast<const ColumnConst &>(*origin_column.column).getField())
: final_type->createColumn(),
final_type,
origin_column.name);
final_columns.emplace_back(std::move(final_column));
if (need_const)
{
const DB::ContextPtr context = DB::CurrentThread::get().getQueryContext();
const FunctionOverloadResolverPtr & cast_resolver = FunctionFactory::instance().get("CAST", context);
const DataTypePtr string_type = DB::DataTypeFactory::instance().get("String");
ColumnWithTypeAndName to_type_column = {string_type->createColumnConst(1, final_type->getName()), string_type, "__cast_const__"};
FunctionBasePtr cast_function = cast_resolver->build({origin_column, to_type_column});
ColumnPtr cast_col = cast_function->execute({origin_column, to_type_column}, final_type, 1);
Field cast_field;
cast_col->get(0, cast_field);
ColumnWithTypeAndName final_column(final_type->createColumnConst(0, cast_field), final_type, origin_column.name);
final_columns.emplace_back(std::move(final_column));
}
else
{
ColumnWithTypeAndName final_column(final_type->createColumn(), final_type, origin_column.name);
final_columns.emplace_back(std::move(final_column));
}
}
}

if (need_final_project)
{
ActionsDAG final_project = ActionsDAG::makeConvertingActions(origin_columns, final_columns, ActionsDAG::MatchColumnsMode::Position);
ActionsDAG final_project = ActionsDAG::makeConvertingActions(origin_columns, final_columns, ActionsDAG::MatchColumnsMode::Position, true);
QueryPlanStepPtr final_project_step
= std::make_unique<ExpressionStep>(query_plan->getCurrentHeader(), std::move(final_project));
final_project_step->setStepDescription("Project for output schema");
Expand Down

0 comments on commit f7f385e

Please sign in to comment.