mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-01-03 20:02:54 +00:00
feat: don't allow creating logical table with partitions (#6249)
* feat: don't allow creating logical table with partitions Signed-off-by: Ruihang Xia <waynestxia@gmail.com> * fix clippy Signed-off-by: Ruihang Xia <waynestxia@gmail.com> --------- Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
@@ -18,6 +18,7 @@ pub mod create_table;
|
||||
pub mod datanode_handler;
|
||||
pub mod flownode_handler;
|
||||
|
||||
use std::assert_matches::assert_matches;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use api::v1::meta::Partition;
|
||||
@@ -75,8 +76,6 @@ pub async fn create_logical_table(
|
||||
physical_table_id: TableId,
|
||||
table_name: &str,
|
||||
) -> TableId {
|
||||
use std::assert_matches::assert_matches;
|
||||
|
||||
let tasks = vec![test_create_logical_table_task(table_name)];
|
||||
let mut procedure = CreateLogicalTablesProcedure::new(tasks, physical_table_id, ddl_context);
|
||||
let status = procedure.on_prepare().await.unwrap();
|
||||
|
||||
@@ -187,24 +187,42 @@ impl StatementExecutor {
|
||||
}
|
||||
);
|
||||
|
||||
// Check if is creating logical table
|
||||
if create_table.engine == METRIC_ENGINE_NAME
|
||||
&& create_table
|
||||
.table_options
|
||||
.contains_key(LOGICAL_TABLE_METADATA_KEY)
|
||||
{
|
||||
return self
|
||||
.create_logical_tables(std::slice::from_ref(create_table), query_ctx)
|
||||
// Create logical tables
|
||||
ensure!(
|
||||
partitions.is_none(),
|
||||
InvalidPartitionRuleSnafu {
|
||||
reason: "logical table in metric engine should not have partition rule, it will be inherited from physical table",
|
||||
}
|
||||
);
|
||||
self.create_logical_tables(std::slice::from_ref(create_table), query_ctx)
|
||||
.await?
|
||||
.into_iter()
|
||||
.next()
|
||||
.context(error::UnexpectedSnafu {
|
||||
violated: "expected to create a logical table",
|
||||
});
|
||||
violated: "expected to create logical tables",
|
||||
})
|
||||
} else {
|
||||
// Create other normal table
|
||||
self.create_non_logic_table(create_table, partitions, query_ctx)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub async fn create_non_logic_table(
|
||||
&self,
|
||||
create_table: &mut CreateTableExpr,
|
||||
partitions: Option<Partitions>,
|
||||
query_ctx: QueryContextRef,
|
||||
) -> Result<TableRef> {
|
||||
let _timer = crate::metrics::DIST_CREATE_TABLE.start_timer();
|
||||
|
||||
// Check if schema exists
|
||||
let schema = self
|
||||
.table_metadata_manager
|
||||
.schema_manager()
|
||||
@@ -214,7 +232,6 @@ impl StatementExecutor {
|
||||
))
|
||||
.await
|
||||
.context(TableMetadataManagerSnafu)?;
|
||||
|
||||
ensure!(
|
||||
schema.is_some(),
|
||||
SchemaNotFoundSnafu {
|
||||
|
||||
@@ -211,6 +211,12 @@ impl ColumnExtensions {
|
||||
}
|
||||
}
|
||||
|
||||
/// Partition on columns or values.
|
||||
///
|
||||
/// - `column_list` is the list of columns in `PARTITION ON COLUMNS` clause.
|
||||
/// - `exprs` is the list of expressions in `PARTITION ON VALUES` clause, like
|
||||
/// `host <= 'host1'`, `host > 'host1' and host <= 'host2'` or `host > 'host2'`.
|
||||
/// Each expression stands for a partition.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Visit, VisitMut, Serialize)]
|
||||
pub struct Partitions {
|
||||
pub column_list: Vec<Ident>,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
create table metric_engine_partition (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
cpu double,
|
||||
)
|
||||
partition on columns (host) (
|
||||
host <= 'host1',
|
||||
host > 'host1' and host <= 'host2',
|
||||
host > 'host2'
|
||||
)
|
||||
engine = metric
|
||||
with (
|
||||
physical_metric_table = "true",
|
||||
);
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
select count(*) from metric_engine_partition;
|
||||
|
||||
+----------+
|
||||
| count(*) |
|
||||
+----------+
|
||||
| 0 |
|
||||
+----------+
|
||||
|
||||
create table logical_table_1 (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
cpu double,
|
||||
)
|
||||
partition on columns (host) ()
|
||||
engine = metric
|
||||
with (
|
||||
on_physical_table = "metric_engine_partition",
|
||||
);
|
||||
|
||||
Error: 1004(InvalidArguments), Invalid partition rule: logical table in metric engine should not have partition rule, it will be inherited from physical table
|
||||
|
||||
create table logical_table_2 (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
cpu double,
|
||||
)
|
||||
engine = metric
|
||||
with (
|
||||
on_physical_table = "metric_engine_partition",
|
||||
);
|
||||
|
||||
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' |
|
||||
| | ) |
|
||||
+-----------------+-------------------------------------------------+
|
||||
|
||||
drop table logical_table_2;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
drop table metric_engine_partition;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
create table metric_engine_partition (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
cpu double,
|
||||
)
|
||||
partition on columns (host) (
|
||||
host <= 'host1',
|
||||
host > 'host1' and host <= 'host2',
|
||||
host > 'host2'
|
||||
)
|
||||
engine = metric
|
||||
with (
|
||||
physical_metric_table = "true",
|
||||
);
|
||||
|
||||
select count(*) from metric_engine_partition;
|
||||
|
||||
create table logical_table_1 (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
cpu double,
|
||||
)
|
||||
partition on columns (host) ()
|
||||
engine = metric
|
||||
with (
|
||||
on_physical_table = "metric_engine_partition",
|
||||
);
|
||||
|
||||
create table logical_table_2 (
|
||||
ts timestamp time index,
|
||||
host string primary key,
|
||||
cpu double,
|
||||
)
|
||||
engine = metric
|
||||
with (
|
||||
on_physical_table = "metric_engine_partition",
|
||||
);
|
||||
|
||||
show create table logical_table_2;
|
||||
|
||||
drop table logical_table_2;
|
||||
|
||||
drop table metric_engine_partition;
|
||||
Reference in New Issue
Block a user