Skip to content

Commit

Permalink
Bug fix log function diff
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinyhZou authored and zouyunhe committed Dec 12, 2023
1 parent ccf95f5 commit 8a6dda1
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,16 @@ class GlutenClickHouseTPCHParquetSuite extends GlutenClickHouseTPCHAbstractSuite
runQueryAndCompare(
"select trunc(l_shipdate, 'MM'), trunc(l_shipdate, 'YEAR'), trunc(l_shipdate, 'WEEK'), " +
"trunc(l_shipdate, 'QUARTER') from lineitem"
}
}

test("GLUTEN-3934: log10/log2/ln") {
withSQLConf(
SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> (ConstantFolding.ruleName + "," + NullPropagation.ruleName)) {
runQueryAndCompare(
"select log10(n_regionkey), log10(-1.0), log10(0), log10(n_regionkey - 100000), " +
"log2(n_regionkey), log2(-1.0), log2(0), log2(n_regionkey - 100000), " +
"ln(n_regionkey), ln(-1.0), ln(0), ln(n_regionkey - 100000) from nation"
)(checkOperatorMatch[ProjectExecTransformer])
}
}
Expand Down
3 changes: 0 additions & 3 deletions cpp-ch/local-engine/Parser/SerializedPlanParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ static const std::map<std::string, std::string> SCALAR_FUNCTIONS
{"unhex", "unhex"},
{"hypot", "hypot"},
{"sign", "sign"},
{"log10", "log10"},
{"log2", "log2"},
{"log", "log"},
{"radians", "radians"},
{"greatest", "greatest"},
{"least", "least"},
Expand Down
39 changes: 39 additions & 0 deletions cpp-ch/local-engine/Parser/scalar_function_parser/ln.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 <Parser/scalar_function_parser/logarithm.h>

namespace local_engine
{

class FunctionParserLn : public FunctionParserLogBase
{
public:
explicit FunctionParserLn(SerializedPlanParser * plan_parser_) : FunctionParserLogBase(plan_parser_) {}
~FunctionParserLn() override = default;

static constexpr auto name = "log";

String getName() const override { return name; }
String getCHFunctionName() const override { return "log"; }
const DB::ActionsDAG::Node * getParameterLowerBound(ActionsDAGPtr & actions_dag, const DataTypePtr & data_type) const override
{
return addColumnToActionsDAG(actions_dag, data_type, 0.0);
}
};

static FunctionParserRegister<FunctionParserLn> register_ln;
}
39 changes: 39 additions & 0 deletions cpp-ch/local-engine/Parser/scalar_function_parser/log10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 <Parser/scalar_function_parser/logarithm.h>

namespace local_engine
{

class FunctionParserLog10 : public FunctionParserLogBase
{
public:
explicit FunctionParserLog10(SerializedPlanParser * plan_parser_) : FunctionParserLogBase(plan_parser_) {}
~FunctionParserLog10() override = default;

static constexpr auto name = "log10";

String getName() const override { return name; }
String getCHFunctionName() const override { return "log10"; }
const DB::ActionsDAG::Node * getParameterLowerBound(ActionsDAGPtr & actions_dag, const DataTypePtr & data_type) const override
{
return addColumnToActionsDAG(actions_dag, data_type, 0.0);
}
};

static FunctionParserRegister<FunctionParserLog10> register_log10;
}
49 changes: 6 additions & 43 deletions cpp-ch/local-engine/Parser/scalar_function_parser/log1p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <Parser/FunctionParser.h>
#include <Common/CHUtil.h>
#include <Core/Field.h>
#include <DataTypes/IDataType.h>

namespace DB
{

namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
}
#include <Parser/scalar_function_parser/logarithm.h>

namespace local_engine
{

class FunctionParserLog1p : public FunctionParser
class FunctionParserLog1p : public FunctionParserLogBase
{
public:
explicit FunctionParserLog1p(SerializedPlanParser * plan_parser_) : FunctionParser(plan_parser_) {}
explicit FunctionParserLog1p(SerializedPlanParser * plan_parser_) : FunctionParserLogBase(plan_parser_) {}
~FunctionParserLog1p() override = default;

static constexpr auto name = "log1p";

String getName() const override { return name; }

const ActionsDAG::Node * parse(
const substrait::Expression_ScalarFunction & substrait_func,
ActionsDAGPtr & actions_dag) const override
String getCHFunctionName() const override { return "log1p"; }
const DB::ActionsDAG::Node * getParameterLowerBound(ActionsDAGPtr & actions_dag, const DataTypePtr & data_type) const override
{
/*
parse log1p(x) as
if (x <= -1.0)
null
else
log1p(x)
*/
auto parsed_args = parseFunctionArguments(substrait_func, "", actions_dag);
if (parsed_args.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires exactly one arguments", getName());

const auto * arg_node = parsed_args[0];
const auto * log1p_node = toFunctionNode(actions_dag, "log1p", {arg_node});

auto result_type = log1p_node->result_type;
auto nullable_result_type = makeNullable(result_type);

const auto * null_const_node = addColumnToActionsDAG(actions_dag, nullable_result_type, Field());
const auto * nullable_log1p_node = ActionsDAGUtil::convertNodeType(actions_dag, log1p_node, nullable_result_type->getName(), log1p_node->result_name);

const auto * le_node = toFunctionNode(actions_dag, "lessOrEquals", {arg_node, addColumnToActionsDAG(actions_dag, result_type, -1.0)});
const auto * result_node = toFunctionNode(actions_dag, "if", {le_node, null_const_node, nullable_log1p_node});

return convertNodeTypeIfNeeded(substrait_func, result_node, actions_dag);
return addColumnToActionsDAG(actions_dag, data_type, -1.0);
}
};

Expand Down
39 changes: 39 additions & 0 deletions cpp-ch/local-engine/Parser/scalar_function_parser/log2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 <Parser/scalar_function_parser/logarithm.h>

namespace local_engine
{

class FunctionParserLog2 : public FunctionParserLogBase
{
public:
explicit FunctionParserLog2(SerializedPlanParser * plan_parser_) : FunctionParserLogBase(plan_parser_) {}
~FunctionParserLog2() override = default;

static constexpr auto name = "log2";

String getName() const override { return name; }
String getCHFunctionName() const override { return "log2"; }
const DB::ActionsDAG::Node * getParameterLowerBound(ActionsDAGPtr & actions_dag, const DataTypePtr & data_type) const override
{
return addColumnToActionsDAG(actions_dag, data_type, 0.0);
}
};

static FunctionParserRegister<FunctionParserLog2> register_log2;
}
77 changes: 77 additions & 0 deletions cpp-ch/local-engine/Parser/scalar_function_parser/logarithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 <Parser/FunctionParser.h>
#include <Common/CHUtil.h>
#include <Core/Field.h>
#include <DataTypes/IDataType.h>

namespace DB
{

namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int NOT_IMPLEMENTED;
}
}

namespace local_engine
{
class FunctionParserLogBase : public FunctionParser
{
public:
explicit FunctionParserLogBase(SerializedPlanParser * plan_parser_) : FunctionParser(plan_parser_) {}
~FunctionParserLogBase() override = default;

virtual DB::String getCHFunctionName() const { return "log"; }
virtual const DB::ActionsDAG::Node * getParameterLowerBound(ActionsDAGPtr &, const DataTypePtr &) const { return nullptr; }

const ActionsDAG::Node * parse(
const substrait::Expression_ScalarFunction & substrait_func,
ActionsDAGPtr & actions_dag) const override
{
/*
parse log(x) as
if (x <= c)
null
else
log(x)
*/
auto parsed_args = parseFunctionArguments(substrait_func, "", actions_dag);
if (parsed_args.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires exactly one arguments", getName());

const auto * arg_node = parsed_args[0];

const std::string ch_function_name = getCHFunctionName();
const auto * log_node = toFunctionNode(actions_dag, ch_function_name, {arg_node});
auto nullable_result_type = makeNullable(log_node->result_type);

const auto * null_const_node = addColumnToActionsDAG(actions_dag, nullable_result_type, Field());
const auto * lower_bound_node = getParameterLowerBound(actions_dag, arg_node->result_type);
if (!lower_bound_node)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Vritual function {} may not implement for {}", "getParameterLowerBound", getName());

const auto * le_node = toFunctionNode(actions_dag, "lessOrEquals", {arg_node, lower_bound_node});
const auto * result_node = toFunctionNode(actions_dag, "if", {le_node, null_const_node, log_node});

return convertNodeTypeIfNeeded(substrait_func, result_node, actions_dag);
}
};

}

0 comments on commit 8a6dda1

Please sign in to comment.