-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cf6a58
commit df7331a
Showing
10 changed files
with
188 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
cpp-ch/local-engine/Parser/scalar_function_parser/arraysOverlap.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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 <Core/Field.h> | ||
#include <DataTypes/IDataType.h> | ||
#include <DataTypes/DataTypeArray.h> | ||
#include <Parser/FunctionParser.h> | ||
#include <Common/CHUtil.h> | ||
|
||
namespace DB | ||
{ | ||
namespace ErrorCodes | ||
{ | ||
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; | ||
} | ||
} | ||
|
||
namespace local_engine | ||
{ | ||
|
||
class FunctionParserArraysOverlap : public FunctionParser | ||
{ | ||
public: | ||
explicit FunctionParserArraysOverlap(SerializedPlanParser * plan_parser_) : FunctionParser(plan_parser_) { } | ||
~FunctionParserArraysOverlap() override = default; | ||
|
||
static constexpr auto name = "arrays_overlap"; | ||
|
||
String getName() const override { return name; } | ||
|
||
const ActionsDAG::Node * parse( | ||
const substrait::Expression_ScalarFunction & substrait_func, | ||
ActionsDAG & actions_dag) const override | ||
{ | ||
/** | ||
parse arrays_overlap(arr1, arr2) as | ||
if (isNull(arr1) || isNull(arr2)) | ||
return NULL | ||
else if (isEmpty(arr1) || isEmpty(arr2)) | ||
return false; | ||
else if (arr1.hasAny(arr2)) | ||
{ | ||
if (!arr1.has(NULL) || !arr2.has(NULL)) | ||
return true; | ||
else if (arr1.intersect(arr2) != NULL) | ||
return true | ||
else | ||
return NULL; | ||
} | ||
else if (arr1.has(NULL) || arr2.has(NULL)) | ||
return NULL; | ||
else | ||
return false; | ||
*/ | ||
|
||
auto parsed_args = parseFunctionArguments(substrait_func, actions_dag); | ||
if (parsed_args.size() != 2) | ||
throw Exception(DB::ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires exactly two arguments", getName()); | ||
|
||
auto ch_function_name = getCHFunctionName(substrait_func); | ||
|
||
const auto * arr1_node = parsed_args[0]; | ||
const auto * arr2_node = parsed_args[1]; | ||
|
||
const auto * arr1_is_null_node = toFunctionNode(actions_dag, "isNull", {arr1_node}); | ||
const auto * arr2_is_null_node = toFunctionNode(actions_dag, "isNull", {arr2_node}); | ||
|
||
const auto * arr1_not_null_node = toFunctionNode(actions_dag, "assumeNotNull", {arr1_node}); | ||
const auto * arr2_not_null_node = toFunctionNode(actions_dag, "assumeNotNull", {arr2_node}); | ||
const auto * arrs_or_null_node = toFunctionNode(actions_dag, "or", {arr1_is_null_node, arr2_is_null_node}); | ||
|
||
const DataTypeArray * arr_type = static_cast<const DataTypeArray *>(arr1_not_null_node->result_type.get()); | ||
const auto * null_type_node = addColumnToActionsDAG(actions_dag, makeNullable(arr_type->getNestedType()), Field{}); | ||
const auto * null_const_node = addColumnToActionsDAG(actions_dag, makeNullable(std::make_shared<DataTypeUInt8>()), Field{}); | ||
const auto * true_const_node = addColumnToActionsDAG(actions_dag, std::make_shared<DataTypeUInt8>(), 1); | ||
const auto * false_const_node = addColumnToActionsDAG(actions_dag, std::make_shared<DataTypeUInt8>(), 0); | ||
const auto * one_const_node = addColumnToActionsDAG(actions_dag, std::make_shared<DataTypeUInt64>(), 1); | ||
|
||
const auto * arr1_has_null_node = toFunctionNode(actions_dag, "has", {arr1_not_null_node, null_type_node}); | ||
const auto * arr2_has_null_node = toFunctionNode(actions_dag, "has", {arr2_not_null_node, null_type_node}); | ||
const auto * arr1_not_has_null_node = toFunctionNode(actions_dag, "not", {arr1_has_null_node}); | ||
const auto * arr2_not_has_null_node = toFunctionNode(actions_dag, "not", {arr2_has_null_node}); | ||
|
||
const auto * arrs_or_has_null_node = toFunctionNode(actions_dag, "or", {arr1_has_null_node, arr2_has_null_node}); | ||
const auto * arrs_one_not_has_null_node = toFunctionNode(actions_dag, "or", {arr1_not_has_null_node, arr2_not_has_null_node}); | ||
const auto * arrs_not_has_null_node = toFunctionNode(actions_dag, "and", {arr1_not_has_null_node, arr2_not_has_null_node}); | ||
|
||
const auto * arr1_is_empty_node = toFunctionNode(actions_dag, "empty", {arr1_not_null_node}); | ||
const auto * arr2_is_empty_node = toFunctionNode(actions_dag, "empty", {arr2_not_null_node}); | ||
const auto * arrs_or_empty_node = toFunctionNode(actions_dag, "or", {arr1_is_empty_node, arr2_is_empty_node}); | ||
|
||
const auto * arrs_has_any_node = toFunctionNode(actions_dag, "hasAny", {arr1_not_null_node, arr2_not_null_node}); | ||
const auto * arrs_intersect_node = toFunctionNode(actions_dag, "arrayIntersect", {arr1_not_null_node, arr2_not_null_node}); | ||
const auto * arrs_intersect_len_node = toFunctionNode(actions_dag, "length", {arrs_intersect_node}); | ||
const auto * arrs_intersect_is_single_node = toFunctionNode(actions_dag, "equals", {arrs_intersect_len_node, one_const_node}); | ||
const auto * arrs_intersect_has_null_node = toFunctionNode(actions_dag, "has", {arrs_intersect_node, null_type_node}); | ||
const auto * arrs_intersect_single_has_null = toFunctionNode(actions_dag, "and", {arrs_intersect_is_single_node, arrs_intersect_has_null_node}); | ||
|
||
const auto * arrs_intersect_single_has_null_result = toFunctionNode(actions_dag, "if", {arrs_intersect_single_has_null, null_const_node, true_const_node}); | ||
const auto * arrs_if_has_null_node = toFunctionNode(actions_dag, "if", {arrs_one_not_has_null_node, true_const_node, arrs_intersect_single_has_null_result}); | ||
const auto * arrs_overlap_node = toFunctionNode(actions_dag, "multiIf", { | ||
arrs_or_null_node, null_const_node, | ||
arrs_or_empty_node, false_const_node, | ||
arrs_has_any_node, arrs_if_has_null_node, | ||
arrs_or_has_null_node, null_const_node, | ||
false_const_node}); | ||
return convertNodeTypeIfNeeded(substrait_func, arrs_overlap_node, actions_dag); | ||
} | ||
|
||
String getCHFunctionName(const substrait::Expression_ScalarFunction & /*substrait_func*/) const override | ||
{ | ||
return "hasAny"; | ||
} | ||
}; | ||
|
||
static FunctionParserRegister<FunctionParserArraysOverlap> register_array_position; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters