Skip to content

Commit

Permalink
fix: Alter table renames view successfully (#2678)
Browse files Browse the repository at this point in the history
Fixes: #2676

---------

Signed-off-by: Vaibhav <vrongmeal@gmail.com>
  • Loading branch information
vrongmeal authored Feb 21, 2024
1 parent fb3c288 commit f7206bd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
8 changes: 4 additions & 4 deletions crates/datafusion_ext/src/planner/relation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,6 @@ fn infer_func_for_file(path: &str) -> Result<OwnedTableReference> {
schema: "public".into(),
table: "read_parquet".into(),
},
"xlsx" => OwnedTableReference::Partial {
schema: "public".into(),
table: "read_excel".into(),
},
"csv" => OwnedTableReference::Partial {
schema: "public".into(),
table: "read_csv".into(),
Expand All @@ -266,6 +262,10 @@ fn infer_func_for_file(path: &str) -> Result<OwnedTableReference> {
schema: "public".into(),
table: "read_bson".into(),
},
"xlsx" => OwnedTableReference::Partial {
schema: "public".into(),
table: "read_excel".into(),
},
ext => {
if let Ok(compression_type) = ext.parse::<FileCompressionType>() {
let ext = compression_type.get_ext();
Expand Down
20 changes: 9 additions & 11 deletions crates/metastore/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,19 +935,17 @@ impl State {
Some(id) => id,
};

let mut table = match self.entries.remove(&oid)?.unwrap() {
CatalogEntry::Table(ent) => ent,
other => unreachable!("unexpected entry type: {:?}", other),
};
let mut ent = self.entries.remove(&oid)?.unwrap();

// The entry must be a "table" or a "view".
assert!(
matches!(ent, CatalogEntry::Table(_) | CatalogEntry::View(_)),
"unexpected entry type: {ent:?}"
);

table.meta.name = new_name;
ent.get_meta_mut().name = new_name;

self.try_insert_table_namespace(
CatalogEntry::Table(table.clone()),
schema_id,
table.meta.id,
CreatePolicy::Create,
)?;
self.try_insert_table_namespace(ent, schema_id, oid, CreatePolicy::Create)?;
}
AlterTableOperation::SetAccessMode { access_mode } => {
let oid = match objs.tables.get(&alter_table.name) {
Expand Down
20 changes: 20 additions & 0 deletions testdata/sqllogictests/alter.slt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ alter table t1 rename to t2;
statement ok
drop table if exists t1, t2;

# Tests alter views (with the same syntax as tables)

statement ok
create view v1 as values (1), (2);

statement ok
alter table v1 rename to v2;

statement error Missing database object
alter table v1 rename to v2;

statement ok
create view v1 as values (3), (4);

statement error Duplicate name
alter table v1 rename to v2;

statement ok
drop view if exists v1, v2;

# Tests alter database

statement ok
Expand Down

0 comments on commit f7206bd

Please sign in to comment.