mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-14 12:00:40 +00:00
fix: remap column indices on overriding logical table partitions (#6446)
* fix: remap column indices on overriding logical table partitions Signed-off-by: Ruihang Xia <waynestxia@gmail.com> * sqlness Signed-off-by: Ruihang Xia <waynestxia@gmail.com> * refactor map query Signed-off-by: Ruihang Xia <waynestxia@gmail.com> --------- Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
use std::any::Any;
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
use async_stream::try_stream;
|
||||
@@ -166,32 +166,29 @@ impl KvBackendCatalogManager {
|
||||
.context(TableMetadataManagerSnafu)?
|
||||
{
|
||||
let mut new_table_info = (*table.table_info()).clone();
|
||||
// Gather all column names from the logical table
|
||||
let logical_column_names: HashSet<_> = new_table_info
|
||||
.meta
|
||||
.schema
|
||||
.column_schemas()
|
||||
.iter()
|
||||
.map(|col| &col.name)
|
||||
.collect();
|
||||
|
||||
// Only preserve partition key indices where the corresponding columns exist in logical table
|
||||
// Remap partition key indices from physical table to logical table
|
||||
new_table_info.meta.partition_key_indices = physical_table_info_value
|
||||
.table_info
|
||||
.meta
|
||||
.partition_key_indices
|
||||
.iter()
|
||||
.filter(|&&index| {
|
||||
.filter_map(|&physical_index| {
|
||||
// Get the column name from the physical table using the physical index
|
||||
physical_table_info_value
|
||||
.table_info
|
||||
.meta
|
||||
.schema
|
||||
.column_schemas
|
||||
.get(index)
|
||||
.map(|physical_column| logical_column_names.contains(&physical_column.name))
|
||||
.unwrap_or(false)
|
||||
.get(physical_index)
|
||||
.and_then(|physical_column| {
|
||||
// Find the corresponding index in the logical table schema
|
||||
new_table_info
|
||||
.meta
|
||||
.schema
|
||||
.column_index_by_name(physical_column.name.as_str())
|
||||
})
|
||||
})
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let new_table = DistTable::table(Arc::new(new_table_info));
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
create table metric_engine_partition (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
host string,
|
||||
cpu double,
|
||||
`one_partition_key` string,
|
||||
`another_partition_key` string,
|
||||
primary key(host, `one_partition_key`, `another_partition_key`)
|
||||
)
|
||||
partition on columns (host) (
|
||||
partition on columns (host, `one_partition_key`, `another_partition_key`) (
|
||||
host <= 'host1',
|
||||
host > 'host1' and host <= 'host2',
|
||||
host > 'host2'
|
||||
@@ -64,26 +67,26 @@ Affected Rows: 0
|
||||
|
||||
show create table logical_table_2;
|
||||
|
||||
+-----------------+-------------------------------------------------+
|
||||
| Table | Create Table |
|
||||
+-----------------+-------------------------------------------------+
|
||||
| logical_table_2 | CREATE TABLE IF NOT EXISTS "logical_table_2" ( |
|
||||
| | "cpu" DOUBLE NULL, |
|
||||
| | "host" STRING NULL, |
|
||||
| | "ts" TIMESTAMP(3) NOT NULL, |
|
||||
| | TIME INDEX ("ts"), |
|
||||
| | PRIMARY KEY ("host") |
|
||||
| | ) |
|
||||
| | PARTITION ON COLUMNS ("host") ( |
|
||||
| | host <= 'host1', |
|
||||
| | host > 'host2', |
|
||||
| | host > 'host1' AND host <= 'host2' |
|
||||
| | ) |
|
||||
| | ENGINE=metric |
|
||||
| | WITH( |
|
||||
| | on_physical_table = 'metric_engine_partition' |
|
||||
| | ) |
|
||||
+-----------------+-------------------------------------------------+
|
||||
+-----------------+-------------------------------------------------------------------------------+
|
||||
| Table | Create Table |
|
||||
+-----------------+-------------------------------------------------------------------------------+
|
||||
| logical_table_2 | CREATE TABLE IF NOT EXISTS "logical_table_2" ( |
|
||||
| | "cpu" DOUBLE NULL, |
|
||||
| | "host" STRING NULL, |
|
||||
| | "ts" TIMESTAMP(3) NOT NULL, |
|
||||
| | TIME INDEX ("ts"), |
|
||||
| | PRIMARY KEY ("host") |
|
||||
| | ) |
|
||||
| | PARTITION ON COLUMNS ("host", "one_partition_key", "another_partition_key") ( |
|
||||
| | host <= 'host1', |
|
||||
| | host > 'host2', |
|
||||
| | host > 'host1' AND host <= 'host2' |
|
||||
| | ) |
|
||||
| | ENGINE=metric |
|
||||
| | WITH( |
|
||||
| | on_physical_table = 'metric_engine_partition' |
|
||||
| | ) |
|
||||
+-----------------+-------------------------------------------------------------------------------+
|
||||
|
||||
select count(*) from logical_table_2;
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
create table metric_engine_partition (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
host string,
|
||||
cpu double,
|
||||
`one_partition_key` string,
|
||||
`another_partition_key` string,
|
||||
primary key(host, `one_partition_key`, `another_partition_key`)
|
||||
)
|
||||
partition on columns (host) (
|
||||
partition on columns (host, `one_partition_key`, `another_partition_key`) (
|
||||
host <= 'host1',
|
||||
host > 'host1' and host <= 'host2',
|
||||
host > 'host2'
|
||||
|
||||
Reference in New Issue
Block a user