Skip to content

Commit

Permalink
fix: resolve aliased catalogs (#2793)
Browse files Browse the repository at this point in the history
closes #2779 

Note for Reviewers: 

There isn't a real good way that I know of to add a test for this, so i
will just share the steps I took to manually test it.


1. go to console.glaredb.com
2. create a view in there: `create view "my_view" as select 1;`
3. connect to cloud via local
```sh
> just run local
> \open glaredb://<USER>:<PASS>@o_xTz3cuY.remote.glaredb.com:6443/quiet_frog
Connected to Cloud deployment (TLS enabled): quiet_frog
> select * from "quiet_frog"."public"."my_view";
┌──────────┐
│ Int64(1) │
│       ── │
│    Int64 │
╞══════════╡
│        1 │
└──────────┘
```

it also works the other way around. Previously a create statement
`create view quiet_frog.public.my_view_2` would save it to the "default"
db, now it properly saves it under the alias name

```sql
> create view "quiet_frog"."public"."my_view_2" as select 2;
View created
> select * from my_view_2;
┌──────────┐
│ Int64(2) │
│       ── │
│    Int64 │
╞══════════╡
│        2 │
└──────────┘
> select * from "quiet_frog"."public"."my_view_2";
┌──────────┐
│ Int64(2) │
│       ── │
│    Int64 │
╞══════════╡
│        2 │
└──────────┘
>
```
  • Loading branch information
universalmind303 authored Mar 18, 2024
1 parent f1b558a commit 94d547d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/catalog/src/session_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct ResolveConfig {
/// from the remote state provided by metastore.
#[derive(Clone, Debug)]
pub struct SessionCatalog {
/// Optional alias for referencing objects in this catalog
alias: Option<String>,
/// The state retrieved from a remote Metastore.
state: Arc<CatalogState>,
/// Map database names to their ids.
Expand All @@ -69,6 +71,7 @@ impl SessionCatalog {
/// Create a new session catalog with an initial state.
pub fn new(state: Arc<CatalogState>, resolve_conf: ResolveConfig) -> SessionCatalog {
let mut catalog = SessionCatalog {
alias: None,
state,
database_names: HashMap::new(),
tunnel_names: HashMap::new(),
Expand All @@ -82,6 +85,20 @@ impl SessionCatalog {
catalog
}

pub fn new_with_alias(
state: Arc<CatalogState>,
resolve_conf: ResolveConfig,
alias: String,
) -> SessionCatalog {
let mut catalog = Self::new(state, resolve_conf);
catalog.alias = Some(alias);
catalog
}

pub fn alias(&self) -> Option<&str> {
self.alias.as_deref()
}

/// Get the version of this catalog state.
pub fn version(&self) -> u64 {
self.state.version
Expand Down
3 changes: 2 additions & 1 deletion crates/sqlexec/src/remote/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,13 @@ impl RemoteClient {

Ok((
remote_sess_client,
SessionCatalog::new(
SessionCatalog::new_with_alias(
Arc::new(resp.catalog),
ResolveConfig {
default_schema_oid: SCHEMA_DEFAULT.oid,
session_schema_oid: SCHEMA_CURRENT_SESSION.oid,
},
self.get_deployment_name().to_string(),
),
))
}
Expand Down
13 changes: 13 additions & 0 deletions crates/sqlexec/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ impl<'a> EntryResolver<'a> {
schema,
table,
} => {
// if the catalog has an alias, we need to check if the
// reference is to the alias and if so, resolve it to the
// actual catalog name.
if let Some(catalog_alias) = self.catalog.alias() {
if catalog == catalog_alias {
if let Some(ent) =
self.catalog.resolve_entry(DEFAULT_CATALOG, schema, table)
{
return Ok(ResolvedEntry::Entry(ent.clone()));
}
}
}

// If catalog is anything but "default", we know we need to do
// external resolution since we don't store info about
// individual tables.
Expand Down

0 comments on commit 94d547d

Please sign in to comment.