From 1a69ba8e5c4599ab0ba3ec5fdc3cebb5e8c437b1 Mon Sep 17 00:00:00 2001 From: Lloyd-Pottiger <60744015+Lloyd-Pottiger@users.noreply.github.com> Date: Thu, 8 Dec 2022 20:40:05 +0800 Subject: [PATCH] *: Refine TiDBTableScan (#6448) ref pingcap/tiflash#6233 --- dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.cpp | 6 +++--- dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.h | 2 +- dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp | 6 +++--- dbms/src/Flash/Coprocessor/GenSchemaAndColumn.cpp | 2 +- dbms/src/Flash/Coprocessor/RemoteRequest.cpp | 2 +- dbms/src/Flash/Coprocessor/TiDBTableScan.cpp | 2 +- dbms/src/Flash/Coprocessor/TiDBTableScan.h | 7 +++++-- dbms/src/Storages/Transaction/TiDB.cpp | 9 +++++++++ dbms/src/Storages/Transaction/TiDB.h | 1 + 9 files changed, 25 insertions(+), 12 deletions(-) diff --git a/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.cpp b/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.cpp index 48774f0c457..498eb0e0a89 100644 --- a/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.cpp +++ b/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.cpp @@ -842,7 +842,7 @@ String DAGExpressionAnalyzer::appendTimeZoneCast( bool DAGExpressionAnalyzer::buildExtraCastsAfterTS( const ExpressionActionsPtr & actions, const std::vector & need_cast_column, - const ::google::protobuf::RepeatedPtrField & table_scan_columns) + const ColumnInfos & table_scan_columns) { bool has_cast = false; @@ -867,9 +867,9 @@ bool DAGExpressionAnalyzer::buildExtraCastsAfterTS( if (need_cast_column[i] == ExtraCastAfterTSMode::AppendDurationCast) { - if (table_scan_columns[i].decimal() > 6) + if (table_scan_columns[i].decimal > 6) throw Exception("fsp must <= 6", ErrorCodes::LOGICAL_ERROR); - auto fsp = table_scan_columns[i].decimal() < 0 ? 0 : table_scan_columns[i].decimal(); + const auto fsp = table_scan_columns[i].decimal < 0 ? 0 : table_scan_columns[i].decimal; tipb::Expr fsp_expr = constructInt64LiteralTiExpr(fsp); fsp_col = getActions(fsp_expr, actions); String casted_name = appendDurationCast(fsp_col, source_columns[i].name, dur_func_name, actions); diff --git a/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.h b/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.h index f0d64b2232e..13e751c4b85 100644 --- a/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.h +++ b/dbms/src/Flash/Coprocessor/DAGExpressionAnalyzer.h @@ -281,7 +281,7 @@ class DAGExpressionAnalyzer : private boost::noncopyable bool buildExtraCastsAfterTS( const ExpressionActionsPtr & actions, const std::vector & need_cast_column, - const ::google::protobuf::RepeatedPtrField & table_scan_columns); + const ColumnInfos & table_scan_columns); std::pair buildJoinKey( const ExpressionActionsPtr & actions, diff --git a/dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp b/dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp index 4c8adefebc3..4f6ed972235 100644 --- a/dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp +++ b/dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp @@ -1041,7 +1041,7 @@ std::tuple> DAGStorageIn for (Int32 i = 0; i < table_scan.getColumnSize(); ++i) { auto const & ci = table_scan.getColumns()[i]; - ColumnID cid = ci.column_id(); + const ColumnID cid = ci.id; // Column ID -1 return the handle column String name; @@ -1062,9 +1062,9 @@ std::tuple> DAGStorageIn source_columns_tmp.emplace_back(std::move(pair)); } required_columns_tmp.emplace_back(std::move(name)); - if (cid != -1 && ci.tp() == TiDB::TypeTimestamp) + if (cid != -1 && ci.tp == TiDB::TypeTimestamp) need_cast_column.push_back(ExtraCastAfterTSMode::AppendTimeZoneCast); - else if (cid != -1 && ci.tp() == TiDB::TypeTime) + else if (cid != -1 && ci.tp == TiDB::TypeTime) need_cast_column.push_back(ExtraCastAfterTSMode::AppendDurationCast); else need_cast_column.push_back(ExtraCastAfterTSMode::None); diff --git a/dbms/src/Flash/Coprocessor/GenSchemaAndColumn.cpp b/dbms/src/Flash/Coprocessor/GenSchemaAndColumn.cpp index f21c74dbfaa..f638190b47c 100644 --- a/dbms/src/Flash/Coprocessor/GenSchemaAndColumn.cpp +++ b/dbms/src/Flash/Coprocessor/GenSchemaAndColumn.cpp @@ -40,7 +40,7 @@ NamesAndTypes genNamesAndTypes(const TiDBTableScan & table_scan, const StringRef names_and_types.reserve(table_scan.getColumnSize()); for (Int32 i = 0; i < table_scan.getColumnSize(); ++i) { - auto column_info = TiDB::toTiDBColumnInfo(table_scan.getColumns()[i]); + const auto column_info = table_scan.getColumns()[i]; switch (column_info.id) { case TiDBPkColumnID: diff --git a/dbms/src/Flash/Coprocessor/RemoteRequest.cpp b/dbms/src/Flash/Coprocessor/RemoteRequest.cpp index 2ecedee82fe..e0111641a4f 100644 --- a/dbms/src/Flash/Coprocessor/RemoteRequest.cpp +++ b/dbms/src/Flash/Coprocessor/RemoteRequest.cpp @@ -59,7 +59,7 @@ RemoteRequest RemoteRequest::build( for (int i = 0; i < table_scan.getColumnSize(); ++i) { const auto & col = table_scan.getColumns()[i]; - auto col_id = col.column_id(); + auto col_id = col.id; if (col_id == DB::TiDBPkColumnID) { diff --git a/dbms/src/Flash/Coprocessor/TiDBTableScan.cpp b/dbms/src/Flash/Coprocessor/TiDBTableScan.cpp index 20a12770c73..97af20992be 100644 --- a/dbms/src/Flash/Coprocessor/TiDBTableScan.cpp +++ b/dbms/src/Flash/Coprocessor/TiDBTableScan.cpp @@ -23,7 +23,7 @@ TiDBTableScan::TiDBTableScan( : table_scan(table_scan_) , executor_id(executor_id_) , is_partition_table_scan(table_scan->tp() == tipb::TypePartitionTableScan) - , columns(is_partition_table_scan ? table_scan->partition_table_scan().columns() : table_scan->tbl_scan().columns()) + , columns(is_partition_table_scan ? std::move(TiDB::toTiDBColumnInfos(table_scan->partition_table_scan().columns())) : std::move(TiDB::toTiDBColumnInfos(table_scan->tbl_scan().columns()))) // Only No-partition table need keep order when tablescan executor required keep order. // If keep_order is not set, keep order for safety. , keep_order(!is_partition_table_scan && (table_scan->tbl_scan().keep_order() || !table_scan->tbl_scan().has_keep_order())) diff --git a/dbms/src/Flash/Coprocessor/TiDBTableScan.h b/dbms/src/Flash/Coprocessor/TiDBTableScan.h index 574d4b0a15f..84e6f41581f 100644 --- a/dbms/src/Flash/Coprocessor/TiDBTableScan.h +++ b/dbms/src/Flash/Coprocessor/TiDBTableScan.h @@ -18,6 +18,9 @@ namespace DB { + +using ColumnInfos = std::vector; + /// TiDBTableScan is a wrap to hide the difference of `TableScan` and `PartitionTableScan` class TiDBTableScan { @@ -34,7 +37,7 @@ class TiDBTableScan { return columns.size(); } - const google::protobuf::RepeatedPtrField & getColumns() const + const ColumnInfos & getColumns() const { return columns; } @@ -65,7 +68,7 @@ class TiDBTableScan const tipb::Executor * table_scan; String executor_id; bool is_partition_table_scan; - const google::protobuf::RepeatedPtrField & columns; + const ColumnInfos columns; /// logical_table_id is the table id for a TiDB' table, while if the /// TiDB table is partition, each partition is a physical table, and /// the partition's table id is the physical table id. diff --git a/dbms/src/Storages/Transaction/TiDB.cpp b/dbms/src/Storages/Transaction/TiDB.cpp index 2f9790bcb37..745839a2476 100644 --- a/dbms/src/Storages/Transaction/TiDB.cpp +++ b/dbms/src/Storages/Transaction/TiDB.cpp @@ -1143,4 +1143,13 @@ ColumnInfo toTiDBColumnInfo(const tipb::ColumnInfo & tipb_column_info) return tidb_column_info; } +std::vector toTiDBColumnInfos(const ::google::protobuf::RepeatedPtrField & tipb_column_infos) +{ + std::vector tidb_column_infos; + tidb_column_infos.reserve(tipb_column_infos.size()); + for (const auto & tipb_column_info : tipb_column_infos) + tidb_column_infos.emplace_back(toTiDBColumnInfo(tipb_column_info)); + return tidb_column_infos; +} + } // namespace TiDB diff --git a/dbms/src/Storages/Transaction/TiDB.h b/dbms/src/Storages/Transaction/TiDB.h index 9bd78abeed3..cd428e57e6e 100644 --- a/dbms/src/Storages/Transaction/TiDB.h +++ b/dbms/src/Storages/Transaction/TiDB.h @@ -423,5 +423,6 @@ String genJsonNull(); tipb::FieldType columnInfoToFieldType(const ColumnInfo & ci); ColumnInfo fieldTypeToColumnInfo(const tipb::FieldType & field_type); ColumnInfo toTiDBColumnInfo(const tipb::ColumnInfo & tipb_column_info); +std::vector toTiDBColumnInfos(const ::google::protobuf::RepeatedPtrField & tipb_column_infos); } // namespace TiDB