Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check table exists before allocating table id #2546

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/frontend/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ pub enum Error {
#[snafu(display("Failed to find leaders when altering table, table: {}", table))]
LeaderNotFound { table: String, location: Location },

#[snafu(display("Table already exists: `{}`", table))]
TableAlreadyExist { table: String, location: Location },

#[snafu(display("Failed to found context value: {}", key))]
ContextValueNotFound { key: String, location: Location },

Expand Down Expand Up @@ -341,7 +338,6 @@ impl ErrorExt for Error {
| Error::ExecLogicalPlan { source, .. } => source.status_code(),

Error::LeaderNotFound { .. } => StatusCode::StorageUnavailable,
Error::TableAlreadyExist { .. } => StatusCode::TableAlreadyExists,
Error::InvokeRegionServer { source, .. } => source.status_code(),

Error::External { source, .. } => source.status_code(),
Expand Down
5 changes: 5 additions & 0 deletions src/operator/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use snafu::{Location, Snafu};
#[snafu(visibility(pub))]
#[stack_trace_debug]
pub enum Error {
#[snafu(display("Table already exists: `{}`", table))]
TableAlreadyExists { table: String, location: Location },

#[snafu(display("Failed to invalidate table cache"))]
InvalidateTableCache {
location: Location,
Expand Down Expand Up @@ -512,6 +515,8 @@ impl ErrorExt for Error {
| Error::InferFileTableSchema { .. }
| Error::SchemaIncompatible { .. } => StatusCode::InvalidArguments,

Error::TableAlreadyExists { .. } => StatusCode::TableAlreadyExists,

Error::NotSupported { .. } => StatusCode::Unsupported,

Error::TableMetadataManager { source, .. } => source.status_code(),
Expand Down
25 changes: 25 additions & 0 deletions src/operator/src/statement/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ impl StatementExecutor {
.fail();
};

// if table exists.
if let Some(table) = self
.catalog_manager
.table(
&create_table.catalog_name,
&create_table.schema_name,
&create_table.table_name,
)
.await
.context(error::CatalogSnafu)?
{
return if create_table.create_if_not_exists {
Ok(table)
} else {
error::TableAlreadyExistsSnafu {
table: format_full_table_name(
&create_table.catalog_name,
&create_table.schema_name,
&create_table.table_name,
),
}
.fail()
};
}

let table_name = TableName::new(
&create_table.catalog_name,
&create_table.schema_name,
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/standalone/common/create/create.result
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

Affected Rows: 0

CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

Error: 4000(TableAlreadyExists), Table already exists: `greptime.public.test2`

DESC TABLE integers;

+--------+----------------------+-----+------+---------+---------------+
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/standalone/common/create/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX NULL);

CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

CREATE TABLE test2 (i INTEGER, j TIMESTAMP TIME INDEX);

DESC TABLE integers;

DESC TABLE test1;
Expand Down