From 77904adaaf9c970fc7487f62480e7fa737090baf Mon Sep 17 00:00:00 2001 From: taobo Date: Mon, 24 Jun 2024 22:12:36 +0800 Subject: [PATCH] fix: `region_peers` returns same region_id for multi logical tables (#4190) * fix: `region_peers` returns same region_id for multi logical tables * test: add sqlness test for information_schema.region_peers * refactor: region_peers sqlness --- .../src/information_schema/region_peers.rs | 15 ++++-- .../information_schema/region_peers.result | 53 +++++++++++++++++++ .../information_schema/region_peers.sql | 34 ++++++++++++ 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 tests/cases/standalone/common/information_schema/region_peers.result create mode 100644 tests/cases/standalone/common/information_schema/region_peers.sql diff --git a/src/catalog/src/information_schema/region_peers.rs b/src/catalog/src/information_schema/region_peers.rs index b571ab230f..06b55ebf43 100644 --- a/src/catalog/src/information_schema/region_peers.rs +++ b/src/catalog/src/information_schema/region_peers.rs @@ -31,7 +31,7 @@ use datatypes::value::Value; use datatypes::vectors::{Int64VectorBuilder, StringVectorBuilder, UInt64VectorBuilder}; use futures::{StreamExt, TryStreamExt}; use snafu::{OptionExt, ResultExt}; -use store_api::storage::{ScanRequest, TableId}; +use store_api::storage::{RegionId, ScanRequest, TableId}; use table::metadata::TableType; use super::REGION_PEERS; @@ -205,8 +205,8 @@ impl InformationSchemaRegionPeersBuilder { table_ids.into_iter().map(|id| (id, vec![])).collect() }; - for routes in table_routes.values() { - self.add_region_peers(&predicates, routes); + for (table_id, routes) in table_routes { + self.add_region_peers(&predicates, table_id, &routes); } } } @@ -214,9 +214,14 @@ impl InformationSchemaRegionPeersBuilder { self.finish() } - fn add_region_peers(&mut self, predicates: &Predicates, routes: &[RegionRoute]) { + fn add_region_peers( + &mut self, + predicates: &Predicates, + table_id: TableId, + routes: &[RegionRoute], + ) { for route in routes { - let region_id = route.region.id.as_u64(); + let region_id = RegionId::new(table_id, route.region.id.region_number()).as_u64(); let peer_id = route.leader_peer.clone().map(|p| p.id); let peer_addr = route.leader_peer.clone().map(|p| p.addr); let status = if let Some(status) = route.leader_status { diff --git a/tests/cases/standalone/common/information_schema/region_peers.result b/tests/cases/standalone/common/information_schema/region_peers.result new file mode 100644 index 0000000000..2ab814dc5c --- /dev/null +++ b/tests/cases/standalone/common/information_schema/region_peers.result @@ -0,0 +1,53 @@ +--- test information_schema.region_peers ---- +USE INFORMATION_SCHEMA; + +Affected Rows: 0 + +CREATE TABLE region_peers_phy (ts timestamp time index, val double) engine = metric with ("physical_metric_table" = ""); + +Affected Rows: 0 + +CREATE TABLE region_peers_t1 ( + ts timestamp time index, + val double, + host string primary key +) engine = metric with ("on_physical_table" = "region_peers_phy"); + +Affected Rows: 0 + +CREATE TABLE region_peers_t2 ( + ts timestamp time index, + job string primary key, + val double +) engine = metric with ("on_physical_table" = "region_peers_phy"); + +Affected Rows: 0 + +CREATE TABLE region_peers_test ( + a int primary key, + b string, + ts timestamp time index, +) PARTITION ON COLUMNS (a) ( + a < 10, + a >= 10 AND a < 20, + a >= 20, +); + +Affected Rows: 0 + +SELECT COUNT(distinct region_id) FROM region_peers; + ++----------------------------------------+ +| COUNT(DISTINCT region_peers.region_id) | ++----------------------------------------+ +| 6 | ++----------------------------------------+ + +DROP TABLE region_peers_t1, region_peers_t2, region_peers_phy, region_peers_test; + +Affected Rows: 0 + +USE public; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/information_schema/region_peers.sql b/tests/cases/standalone/common/information_schema/region_peers.sql new file mode 100644 index 0000000000..bcc7d75c64 --- /dev/null +++ b/tests/cases/standalone/common/information_schema/region_peers.sql @@ -0,0 +1,34 @@ +--- test information_schema.region_peers ---- +USE INFORMATION_SCHEMA; + +CREATE TABLE region_peers_phy (ts timestamp time index, val double) engine = metric with ("physical_metric_table" = ""); + +CREATE TABLE region_peers_t1 ( + ts timestamp time index, + val double, + host string primary key +) engine = metric with ("on_physical_table" = "region_peers_phy"); + +CREATE TABLE region_peers_t2 ( + ts timestamp time index, + job string primary key, + val double +) engine = metric with ("on_physical_table" = "region_peers_phy"); + +CREATE TABLE region_peers_test ( + a int primary key, + b string, + ts timestamp time index, +) PARTITION ON COLUMNS (a) ( + a < 10, + a >= 10 AND a < 20, + a >= 20, +); + +SELECT COUNT(distinct region_id) FROM region_peers; + +DROP TABLE region_peers_t1, region_peers_t2, region_peers_phy, region_peers_test; + +USE public; + +