From 455acb3461918a3bf173bc86a4399f0ca03b411e Mon Sep 17 00:00:00 2001 From: shuiyisong <113876041+shuiyisong@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:17:41 +0000 Subject: [PATCH] fix: reject primary key encoding options on logical metric tables (#10624) Signed-off-by: shuiyisong --- src/table/src/error.rs | 16 +++++++++++++--- src/table/src/requests.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/table/src/error.rs b/src/table/src/error.rs index 2c8da6c29bd..9705ab3f9a2 100644 --- a/src/table/src/error.rs +++ b/src/table/src/error.rs @@ -127,6 +127,16 @@ pub enum Error { #[snafu(display("Unsupported operation: {}", operation))] Unsupported { operation: String }, + #[snafu(display( + "Cannot set {} on a logical table; configure it on the physical table", + key + ))] + PhysicalTableOption { + key: String, + #[snafu(implicit)] + location: Location, + }, + #[snafu(display("Failed to parse table option, key: {}, value: {}", key, value))] ParseTableOption { key: String, @@ -239,9 +249,9 @@ impl ErrorExt for Error { Error::InvalidColumnOption { .. } => StatusCode::InvalidArguments, Error::ColumnNotExists { .. } => StatusCode::TableColumnNotFound, Error::Unsupported { .. } => StatusCode::Unsupported, - Error::ParseTableOption { .. } | Error::ConflictingTableOptions { .. } => { - StatusCode::InvalidArguments - } + Error::PhysicalTableOption { .. } + | Error::ParseTableOption { .. } + | Error::ConflictingTableOptions { .. } => StatusCode::InvalidArguments, Error::MissingTimeIndexColumn { .. } => StatusCode::IllegalState, Error::SetSkippingOptions { .. } | Error::UnsetSkippingOptions { .. } diff --git a/src/table/src/requests.rs b/src/table/src/requests.rs index 826fe836b21..4bfe4c888d2 100644 --- a/src/table/src/requests.rs +++ b/src/table/src/requests.rs @@ -34,7 +34,8 @@ use greptime_proto::v1::region::{build_index_request, compact_request}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; use store_api::metric_engine_consts::{ - LOGICAL_TABLE_METADATA_KEY, PHYSICAL_TABLE_METADATA_KEY, is_metric_engine_option_key, + LOGICAL_TABLE_METADATA_KEY, MEMTABLE_PARTITION_TREE_PRIMARY_KEY_ENCODING, + PHYSICAL_TABLE_METADATA_KEY, PRIMARY_KEY_ENCODING, is_metric_engine_option_key, }; use store_api::mito_engine_options::{ APPEND_MODE_KEY, COMPACTION_TYPE, EXPERIMENTAL_SST_FLOAT_FIELD_ENCODING, FloatFieldEncoding, @@ -47,7 +48,9 @@ use store_api::mito_engine_options::{ }; use store_api::region_request::{SetRegionOption, UnsetRegionOption}; -use crate::error::{ConflictingTableOptionsSnafu, ParseTableOptionSnafu, Result}; +use crate::error::{ + ConflictingTableOptionsSnafu, ParseTableOptionSnafu, PhysicalTableOptionSnafu, Result, +}; use crate::metadata::{TableId, TableVersion}; mod semantic; @@ -314,6 +317,17 @@ impl TableOptions { .map(|(k, v)| (k.to_string(), v.to_string())) .collect(); + if kvs.contains_key(LOGICAL_TABLE_METADATA_KEY) { + for key in [ + PRIMARY_KEY_ENCODING, + MEMTABLE_PARTITION_TREE_PRIMARY_KEY_ENCODING, + ] { + if kvs.contains_key(key) { + return PhysicalTableOptionSnafu { key }.fail(); + } + } + } + normalize_twcs_trigger_options(&mut kvs).map_err(|conflict| { ConflictingTableOptionsSnafu { first_key: TWCS_TRIGGER_FILE_NUM, @@ -1156,6 +1170,24 @@ mod tests { assert!(!validate_database_option("foo")); } + #[test] + fn test_primary_key_encoding_on_metric_tables() { + for key in [ + PRIMARY_KEY_ENCODING, + MEMTABLE_PARTITION_TREE_PRIMARY_KEY_ENCODING, + ] { + let error = TableOptions::try_from_iter([ + (LOGICAL_TABLE_METADATA_KEY, "metrics"), + (key, "sparse"), + ]) + .unwrap_err(); + assert_eq!(StatusCode::InvalidArguments, error.status_code()); + assert!(error.to_string().contains(key)); + TableOptions::try_from_iter([(PHYSICAL_TABLE_METADATA_KEY, ""), (key, "sparse")]) + .unwrap(); + } + } + #[test] fn test_parse_float_field_encoding_option() { let options = TableOptions::try_from_iter([(