Skip to content

Commit

Permalink
feat: reserve internal column (GreptimeTeam#2396)
Browse files Browse the repository at this point in the history
* feat: reserve internal column

Signed-off-by: ZhuZiyi <[email protected]>

* test: add function test

Signed-off-by: ZhuZiyi <[email protected]>

* fix: spell typos

Signed-off-by: ZhuZiyi <[email protected]>

* chore: resolve conversation

Signed-off-by: ZhuZiyi <[email protected]>

---------

Signed-off-by: ZhuZiyi <[email protected]>
  • Loading branch information
Nateiru authored and paomian committed Oct 19, 2023
1 parent aa9c965 commit 1fe3e36
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/store-api/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use serde::{Deserialize, Deserializer, Serialize};
use snafu::{ensure, Location, OptionExt, ResultExt, Snafu};

use crate::region_request::{AddColumn, AddColumnLocation, AlterKind};
use crate::storage::consts::is_internal_column;
use crate::storage::{ColumnId, RegionId};

pub type Result<T> = std::result::Result<T, MetadataError>;
Expand Down Expand Up @@ -368,6 +369,16 @@ impl RegionMetadata {
);
}

ensure!(
!is_internal_column(&column_metadata.column_schema.name),
InvalidMetaSnafu {
reason: format!(
"{} is internal column name that can not be used",
column_metadata.column_schema.name
),
}
);

Ok(())
}
}
Expand Down Expand Up @@ -998,4 +1009,24 @@ mod test {
let err = builder.build().unwrap_err();
assert_eq!(StatusCode::InvalidArguments, err.status_code());
}

#[test]
fn test_invalid_column_name() {
let mut builder = create_builder();
builder.push_column_metadata(ColumnMetadata {
column_schema: ColumnSchema::new(
"__sequence",
ConcreteDataType::timestamp_millisecond_datatype(),
false,
),
semantic_type: SemanticType::Timestamp,
column_id: 1,
});
let err = builder.build().unwrap_err();
assert!(
err.to_string()
.contains("internal column name that can not be used"),
"unexpected err: {err}",
);
}
}
25 changes: 25 additions & 0 deletions src/store-api/src/storage/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ pub const OP_TYPE_COLUMN_NAME: &str = "__op_type";
/// Name for reserved column: primary_key
pub const PRIMARY_KEY_COLUMN_NAME: &str = "__primary_key";

/// Internal Column Name
static INTERNAL_COLUMN_VEC: [&str; 3] = [
SEQUENCE_COLUMN_NAME,
OP_TYPE_COLUMN_NAME,
PRIMARY_KEY_COLUMN_NAME,
];

pub fn is_internal_column(name: &str) -> bool {
INTERNAL_COLUMN_VEC.contains(&name)
}

// -----------------------------------------------------------------------------

// ---------- Default options --------------------------------------------------
Expand All @@ -104,4 +115,18 @@ mod tests {
assert_eq!(0x80000001, ReservedColumnId::sequence());
assert_eq!(0x80000002, ReservedColumnId::op_type());
}

#[test]
fn test_is_internal_column() {
// contain internal column names
assert!(is_internal_column(SEQUENCE_COLUMN_NAME));
assert!(is_internal_column(OP_TYPE_COLUMN_NAME));
assert!(is_internal_column(PRIMARY_KEY_COLUMN_NAME));

// don't contain internal column names
assert!(!is_internal_column("my__column"));
assert!(!is_internal_column("my__sequence"));
assert!(!is_internal_column("my__op_type"));
assert!(!is_internal_column("my__primary_key"));
}
}

0 comments on commit 1fe3e36

Please sign in to comment.