Skip to content

Commit

Permalink
Add BlockTypeUtils.h
Browse files Browse the repository at this point in the history
  • Loading branch information
baibaichen committed Jul 25, 2024
1 parent ca6d804 commit 084807c
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 55 deletions.
56 changes: 56 additions & 0 deletions cpp-ch/local-engine/Common/BlockTypeUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 "BlockTypeUtils.h"

#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeNullable.h>

namespace local_engine
{
DB::NamesAndTypesList blockToNameAndTypeList(const DB::Block & header)
{
DB::NamesAndTypesList types;
for (const auto & name : header.getNames())
{
const auto * column = header.findByName(name);
types.push_back(DB::NameAndTypePair(column->name, column->type));
}
return types;
}

DB::DataTypePtr wrapNullableType(bool nullable, DB::DataTypePtr nested_type)
{
if (nullable && !nested_type->isNullable())
{
if (nested_type->isLowCardinalityNullable())
return nested_type;
else if (!nested_type->lowCardinality())
return std::make_shared<DB::DataTypeNullable>(nested_type);
else
return std::make_shared<DB::DataTypeLowCardinality>(
std::make_shared<DB::DataTypeNullable>(dynamic_cast<const DB::DataTypeLowCardinality &>(*nested_type).getDictionaryType()));
}


if (nullable && !nested_type->isNullable())
return std::make_shared<DB::DataTypeNullable>(nested_type);
else
return nested_type;
}

}
89 changes: 89 additions & 0 deletions cpp-ch/local-engine/Common/BlockTypeUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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

#include <Core/Block.h>
#include <DataTypes/DataTypeDate32.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <substrait/type.pb.h>

namespace local_engine
{
inline DB::DataTypePtr BIGINT()
{
return std::make_shared<DB::DataTypeInt64>();
}
inline DB::DataTypePtr INT()
{
return std::make_shared<DB::DataTypeInt32>();
}
inline DB::DataTypePtr INT16()
{
return std::make_shared<DB::DataTypeInt16>();
}
inline DB::DataTypePtr INT8()
{
return std::make_shared<DB::DataTypeInt8>();
}
inline DB::DataTypePtr UBIGINT()
{
return std::make_shared<DB::DataTypeUInt64>();
}
inline DB::DataTypePtr UINT()
{
return std::make_shared<DB::DataTypeUInt32>();
}
inline DB::DataTypePtr UINT16()
{
return std::make_shared<DB::DataTypeUInt16>();
}
inline DB::DataTypePtr UINT8()
{
return std::make_shared<DB::DataTypeUInt8>();
}

inline DB::DataTypePtr DOUBLE()
{
return std::make_shared<DB::DataTypeFloat64>();
}

inline DB::DataTypePtr STRING()
{
return std::make_shared<DB::DataTypeString>();
}

inline DB::DataTypePtr DATE()
{
return std::make_shared<DB::DataTypeDate32>();
}

inline DB::Block makeBlockHeader(const DB::ColumnsWithTypeAndName & data_)
{
return DB::Block(data_);
}

DB::NamesAndTypesList blockToNameAndTypeList(const DB::Block & header);
DB::DataTypePtr wrapNullableType(bool nullable, DB::DataTypePtr nested_type);

inline DB::DataTypePtr wrapNullableType(const substrait::Type_Nullability nullable, const DB::DataTypePtr & nested_type)
{
return wrapNullableType(nullable == substrait::Type_Nullability_NULLABILITY_NULLABLE, nested_type);
}

}
1 change: 1 addition & 0 deletions cpp-ch/local-engine/Parser/FunctionParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Parser/TypeParser.h>
#include <Common/BlockTypeUtils.h>
#include <Common/CHUtil.h>

namespace DB
Expand Down
51 changes: 3 additions & 48 deletions cpp-ch/local-engine/Parser/SerializedPlanParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/wrappers.pb.h>
#include <Poco/Util/MapConfiguration.h>
#include <Common/BlockTypeUtils.h>
#include <Common/CHUtil.h>
#include <Common/Exception.h>
#include <Common/JNIUtils.h>
Expand All @@ -99,19 +100,7 @@ extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int INVALID_JOIN_ON_EXPRESSION;
}
}
namespace
{
DB::NamesAndTypesList blockToNameAndTypeList(const DB::Block & header)
{
DB::NamesAndTypesList types;
for (const auto & name : header.getNames())
{
const auto * column = header.findByName(name);
types.push_back(DB::NameAndTypePair(column->name, column->type));
}
return types;
}
}

namespace local_engine
{
using namespace DB;
Expand Down Expand Up @@ -360,37 +349,6 @@ IQueryPlanStep * SerializedPlanParser::addRollbackFilterHeaderStep(QueryPlanPtr
return step_ptr;
}

DataTypePtr wrapNullableType(substrait::Type_Nullability nullable, DataTypePtr nested_type)
{
return wrapNullableType(nullable == substrait::Type_Nullability_NULLABILITY_NULLABLE, nested_type);
}

DataTypePtr wrapNullableType(bool nullable, DataTypePtr nested_type)
{
if (nullable && !nested_type->isNullable())
{
if (nested_type->isLowCardinalityNullable())
{
return nested_type;
}
else
{
if (!nested_type->lowCardinality())
return std::make_shared<DataTypeNullable>(nested_type);
else
return std::make_shared<DataTypeLowCardinality>(
std::make_shared<DataTypeNullable>(
dynamic_cast<const DataTypeLowCardinality &>(*nested_type).getDictionaryType()));
}
}


if (nullable && !nested_type->isNullable())
return std::make_shared<DataTypeNullable>(nested_type);
else
return nested_type;
}

void adjustOutput(const DB::QueryPlanPtr & query_plan, const substrait::PlanRel & root_rel)
{
if (root_rel.root().names_size())
Expand Down Expand Up @@ -795,16 +753,13 @@ const ActionsDAG::Node * SerializedPlanParser::parseFunctionWithDAG(
}

void SerializedPlanParser::parseFunctionArguments(
ActionsDAGPtr & actions_dag,
ActionsDAG::NodeRawConstPtrs & parsed_args,
const substrait::Expression_ScalarFunction & scalar_function)
ActionsDAGPtr & actions_dag, ActionsDAG::NodeRawConstPtrs & parsed_args, const substrait::Expression_ScalarFunction & scalar_function)
{
auto function_signature = function_mapping.at(std::to_string(scalar_function.function_reference()));
const auto & args = scalar_function.arguments();
parsed_args.reserve(args.size());
for (const auto & arg : args)
parsed_args.emplace_back(parseExpression(actions_dag, arg.value()));

}

// Convert signed integer index into unsigned integer index
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 @@ -35,9 +35,6 @@
namespace local_engine
{

DataTypePtr wrapNullableType(substrait::Type_Nullability nullable, DataTypePtr nested_type);
DataTypePtr wrapNullableType(bool nullable, DataTypePtr nested_type);

std::string join(const ActionsDAG::NodeRawConstPtrs & v, char c);

class SerializedPlanParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <Functions/FunctionHelpers.h>
#include <Parser/FunctionParser.h>
#include <Parser/TypeParser.h>
#include <Common/BlockTypeUtils.h>
#include <Common/CHUtil.h>

namespace DB::ErrorCodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <Parser/FunctionParser.h>
#include <Core/Field.h>
#include <DataTypes/IDataType.h>
#include <Parser/FunctionParser.h>
#include <Common/BlockTypeUtils.h>
#include <Common/CHUtil.h>
#include <Core/Field.h>

namespace DB
{
Expand Down
5 changes: 3 additions & 2 deletions cpp-ch/local-engine/Parser/scalar_function_parser/slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* 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>
#include <Parser/FunctionParser.h>
#include <Common/BlockTypeUtils.h>
#include <Common/CHUtil.h>

namespace DB
{
Expand Down

0 comments on commit 084807c

Please sign in to comment.