Skip to content

Commit

Permalink
feat: remove the requirement that partition column must be PK (Grepti…
Browse files Browse the repository at this point in the history
…meTeam#4647)

Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored and CookiePieWw committed Sep 17, 2024
1 parent d286c62 commit 545e16f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 61 deletions.
13 changes: 0 additions & 13 deletions src/operator/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,18 +676,6 @@ pub enum Error {
location: Location,
},

#[snafu(display(
"Invalid partition columns when creating table '{}', reason: {}",
table,
reason
))]
InvalidPartitionColumns {
table: String,
reason: String,
#[snafu(implicit)]
location: Location,
},

#[snafu(display("Failed to prepare file table"))]
PrepareFileTable {
#[snafu(implicit)]
Expand Down Expand Up @@ -812,7 +800,6 @@ impl ErrorExt for Error {
| Error::ProjectSchema { .. }
| Error::UnsupportedFormat { .. }
| Error::ColumnNoneDefaultValue { .. }
| Error::InvalidPartitionColumns { .. }
| Error::PrepareFileTable { .. }
| Error::InferFileTableSchema { .. }
| Error::SchemaIncompatible { .. }
Expand Down
52 changes: 5 additions & 47 deletions src/operator/src/statement/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ use super::StatementExecutor;
use crate::error::{
self, AlterExprToRequestSnafu, CatalogSnafu, ColumnDataTypeSnafu, ColumnNotFoundSnafu,
CreateLogicalTablesSnafu, CreateTableInfoSnafu, DeserializePartitionSnafu, EmptyDdlExprSnafu,
ExtractTableNamesSnafu, FlowNotFoundSnafu, InvalidPartitionColumnsSnafu,
InvalidPartitionRuleSnafu, InvalidPartitionSnafu, InvalidTableNameSnafu, InvalidViewNameSnafu,
InvalidViewStmtSnafu, ParseSqlValueSnafu, Result, SchemaInUseSnafu, SchemaNotFoundSnafu,
SchemaReadOnlySnafu, SubstraitCodecSnafu, TableAlreadyExistsSnafu, TableMetadataManagerSnafu,
TableNotFoundSnafu, UnrecognizedTableOptionSnafu, ViewAlreadyExistsSnafu,
ExtractTableNamesSnafu, FlowNotFoundSnafu, InvalidPartitionRuleSnafu, InvalidPartitionSnafu,
InvalidTableNameSnafu, InvalidViewNameSnafu, InvalidViewStmtSnafu, ParseSqlValueSnafu, Result,
SchemaInUseSnafu, SchemaNotFoundSnafu, SchemaReadOnlySnafu, SubstraitCodecSnafu,
TableAlreadyExistsSnafu, TableMetadataManagerSnafu, TableNotFoundSnafu,
UnrecognizedTableOptionSnafu, ViewAlreadyExistsSnafu,
};
use crate::expr_factory;
use crate::statement::show::create_partitions_stmt;
Expand Down Expand Up @@ -239,7 +239,6 @@ impl StatementExecutor {
);

let (partitions, partition_cols) = parse_partitions(create_table, partitions, &query_ctx)?;
validate_partition_columns(create_table, &partition_cols)?;
let mut table_info = create_table_info(create_table, partition_cols, schema_opts)?;

let resp = self
Expand Down Expand Up @@ -1209,22 +1208,6 @@ impl StatementExecutor {
}
}

fn validate_partition_columns(
create_table: &CreateTableExpr,
partition_cols: &[String],
) -> Result<()> {
ensure!(
partition_cols
.iter()
.all(|col| &create_table.time_index == col || create_table.primary_keys.contains(col)),
InvalidPartitionColumnsSnafu {
table: &create_table.table_name,
reason: "partition column must belongs to primary keys or equals to time index"
}
);
Ok(())
}

/// Parse partition statement [Partitions] into [MetaPartition] and partition columns.
fn parse_partitions(
create_table: &CreateTableExpr,
Expand Down Expand Up @@ -1519,31 +1502,6 @@ mod test {
assert!(NAME_PATTERN_REG.is_match("hello"));
}

#[test]
fn test_validate_partition_columns() {
let create_table = CreateTableExpr {
table_name: "my_table".to_string(),
time_index: "ts".to_string(),
primary_keys: vec!["a".to_string(), "b".to_string()],
..Default::default()
};

assert!(validate_partition_columns(&create_table, &[]).is_ok());
assert!(validate_partition_columns(&create_table, &["ts".to_string()]).is_ok());
assert!(validate_partition_columns(&create_table, &["a".to_string()]).is_ok());
assert!(
validate_partition_columns(&create_table, &["b".to_string(), "a".to_string()]).is_ok()
);

assert_eq!(
validate_partition_columns(&create_table, &["a".to_string(), "c".to_string()])
.unwrap_err()
.to_string(),
"Invalid partition columns when creating table 'my_table', \
reason: partition column must belongs to primary keys or equals to time index",
);
}

#[tokio::test]
#[ignore = "TODO(ruihang): WIP new partition rule"]
async fn test_parse_partitions() {
Expand Down
2 changes: 1 addition & 1 deletion src/sql/src/parsers/create_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<'a> ParserContext<'a> {
Ok(options.into())
}

/// "PARTITION BY ..." clause
/// "PARTITION ON COLUMNS (...)" clause
fn parse_partitions(&mut self) -> Result<Option<Partitions>> {
if !self.parser.parse_keyword(Keyword::PARTITION) {
return Ok(None);
Expand Down

0 comments on commit 545e16f

Please sign in to comment.