databend icon indicating copy to clipboard operation
databend copied to clipboard

Improve API of view catalog object

Open leiysky opened this issue 2 years ago • 8 comments

So far, if we want to get the query text of an exists view, we have to:

  • Get a Table object from Catalog with Catalog::get_table
  • Check if it is a view object by validating Table::engine() == "VIEW" (even case-sensitive)
  • If it is a view object, then there is a deterministic key-value pair in Table::options(), which is "query" -> query_text. Thus we need to get the query text with Table::options().get("query").unwrap().

IMO, it's an anti-intuitive API design. Since view object is a very common data source object in database, it deserve a well-typed structure.

I propose to introduce a data source API like:

struct View {
    pub view_name: Vec<String>,
    pub query_text: String,
}

pub enum DataSource {
    Table(Arc<dyn Table>),
    TableFunction(Arc<dyn TableFunction>),
    View(View),
}

fn main() {
    let catalog = Catalog::new(); // whatever, just give a Catalog object
    let data_source = catalog.get_datasource(... /* Qualified table reference name */)?;
    match data_source {
        DataSource::Table(table) => // do sth with Table
        DataSource::TableFunction(table_func) => // do sth with TableFunction
        DataSource::View(view) => // do sth with View
    }
}

And also, it's better to make Engine a well-typed structure as well:

enum TableEngine {
    Fuse, // "FUSE"
    Github, // "GITHUB"
    Memory, // "MEMORY"
    Hive, // "HIVE"
    ... // Other static engine types

    Dynamic(String)
}

impl Display for TableEngine {...}

leiysky avatar May 30 '22 04:05 leiysky