Skip to content

Commit

Permalink
feat(query): support SHOW VARIABLES
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Sep 6, 2024
1 parent a9a6630 commit 6b580e5
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 2 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/query/ast/src/ast/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub enum Statement {
identifiers: Vec<Identifier>,
},

ShowVariables {
show_options: Option<ShowOptions>,
},

SetRole {
is_default: bool,
role_name: String,
Expand Down Expand Up @@ -431,6 +435,12 @@ impl Display for Statement {
write!(f, " {show_options}")?;
}
}
Statement::ShowVariables { show_options } => {
write!(f, "SHOW VARIABLES")?;
if let Some(show_options) = show_options {
write!(f, " {show_options}")?;
}
}
Statement::ShowProcessList { show_options } => {
write!(f, "SHOW PROCESSLIST")?;
if let Some(show_options) = show_options {
Expand Down
7 changes: 7 additions & 0 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
},
|(_, _, show_options)| Statement::ShowSettings { show_options },
);
let show_variables = map(
rule! {
SHOW ~ VARIABLES ~ #show_options?
},
|(_, _, show_options)| Statement::ShowVariables { show_options },
);
let show_stages = value(Statement::ShowStages, rule! { SHOW ~ STAGES });
let show_process_list = map(
rule! {
Expand Down Expand Up @@ -2170,6 +2176,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
| #explain : "`EXPLAIN [PIPELINE | GRAPH] <statement>`"
| #explain_analyze : "`EXPLAIN ANALYZE <statement>`"
| #show_settings : "`SHOW SETTINGS [<show_limit>]`"
| #show_variables : "`SHOW VARIABLES [<show_limit>]`"
| #show_stages : "`SHOW STAGES`"
| #show_engines : "`SHOW ENGINES`"
| #show_process_list : "`SHOW PROCESSLIST`"
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ pub enum TokenKind {
SESSION,
#[token("SETTINGS", ignore(ascii_case))]
SETTINGS,
#[token("VARIABLES", ignore(ascii_case))]
VARIABLES,
#[token("STAGES", ignore(ascii_case))]
STAGES,
#[token("STATISTIC", ignore(ascii_case))]
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ fn test_statement() {
r#"UNSET (max_threads, sql_dialect);"#,
r#"UNSET session (max_threads, sql_dialect);"#,
r#"SET variable a = 3"#,
r#"show variables"#,
r#"show variables like 'v%'"#,
r#"SET variable a = select 3"#,
r#"SET variable a = (select max(number) from numbers(10))"#,
r#"select $1 FROM '@my_stage/my data/'"#,
Expand Down
2 changes: 1 addition & 1 deletion src/query/ast/tests/it/testdata/stmt-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ error:
--> SQL:1:6
|
1 | SHOW GRANT FOR ROLE 'role1';
| ^^^^^ unexpected `GRANT`, expecting `GRANTS`, `CREATE`, `NETWORK`, `VIRTUAL`, `CATALOGS`, `STREAMS`, `FUNCTIONS`, `DATABASES`, `CONNECTIONS`, `TABLE_FUNCTIONS`, `DROP`, `TABLE`, `ROLES`, `TASKS`, `INDEXES`, `COLUMNS`, `PASSWORD`, `PROCEDURES`, `PROCESSLIST`, `STAGES`, `TABLES`, `DICTIONARIES`, `ENGINES`, `METRICS`, `SETTINGS`, `LOCKS`, `SCHEMAS`, `FIELDS`, `VIEWS`, `USERS`, `USER`, `FILE`, or `FULL`
| ^^^^^ unexpected `GRANT`, expecting `GRANTS`, `CREATE`, `NETWORK`, `VIRTUAL`, `CATALOGS`, `STREAMS`, `FUNCTIONS`, `DATABASES`, `CONNECTIONS`, `TABLE_FUNCTIONS`, `DROP`, `TABLE`, `ROLES`, `TASKS`, `INDEXES`, `COLUMNS`, `PASSWORD`, `PROCEDURES`, `PROCESSLIST`, `STAGES`, `TABLES`, `DICTIONARIES`, `ENGINES`, `METRICS`, `SETTINGS`, `VARIABLES`, `LOCKS`, `SCHEMAS`, `FIELDS`, `VIEWS`, `USERS`, `USER`, `FILE`, or `FULL`


---------- Input ----------
Expand Down
34 changes: 34 additions & 0 deletions src/query/ast/tests/it/testdata/stmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16199,6 +16199,40 @@ SetStmt {
}


---------- Input ----------
show variables
---------- Output ---------
SHOW VARIABLES
---------- AST ------------
ShowVariables {
show_options: Some(
ShowOptions {
show_limit: None,
limit: None,
},
),
}


---------- Input ----------
show variables like 'v%'
---------- Output ---------
SHOW VARIABLES LIKE 'v%'
---------- AST ------------
ShowVariables {
show_options: Some(
ShowOptions {
show_limit: Some(
Like {
pattern: "v%",
},
),
limit: None,
},
),
}


---------- Input ----------
SET variable a = select 3
---------- Output ---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl AccessChecker for ManagementModeAccess {
| RewriteKind::ShowColumns(_, _, _)
| RewriteKind::ShowEngines
| RewriteKind::ShowSettings
| RewriteKind::ShowVariables
| RewriteKind::ShowFunctions
| RewriteKind::ShowUserFunctions
| RewriteKind::ShowTableFunctions
Expand Down
1 change: 1 addition & 0 deletions src/query/service/src/table_functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod srf;
mod sync_crash_me;
mod table_function;
mod table_function_factory;
mod show_variables;

pub use numbers::generate_numbers_parts;
pub use numbers::NumbersPartInfo;
Expand Down
16 changes: 16 additions & 0 deletions src/query/service/src/table_functions/show_variables/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod show_variables_table;
pub use show_variables_table::ShowVariables;
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::any::Any;
use std::sync::Arc;

use databend_common_pipeline_sources::AsyncSource;
use databend_common_catalog::plan::DataSourcePlan;
use databend_common_catalog::plan::PartStatistics;
use databend_common_catalog::plan::Partitions;
use databend_common_catalog::plan::PushDownInfo;
use databend_common_catalog::table::Table;
use databend_common_catalog::table_args::TableArgs;
use databend_common_catalog::table_context::TableContext;
use databend_common_catalog::table_function::TableFunction;
use databend_common_exception::Result;
use databend_common_expression::types::StringType;
use databend_common_expression::DataBlock;
use databend_common_expression::FromData;
use databend_common_expression::TableDataType;
use databend_common_expression::TableField;
use databend_common_expression::TableSchema;
use databend_common_expression::TableSchemaRefExt;
use databend_common_meta_app::schema::TableIdent;
use databend_common_meta_app::schema::TableInfo;
use databend_common_meta_app::schema::TableMeta;
use databend_common_pipeline_core::processors::OutputPort;
use databend_common_pipeline_core::processors::ProcessorPtr;
use databend_common_pipeline_core::Pipeline;
use databend_common_pipeline_sources::AsyncSourcer;
use databend_common_sql::validate_function_arg;

const SHOW_VARIABLES: &str = "show_variables";

pub struct ShowVariables {
table_info: TableInfo,
}

// show variables
impl ShowVariables {
pub fn create(
database_name: &str,
table_func_name: &str,
table_id: u64,
table_args: TableArgs,
) -> Result<Arc<dyn TableFunction>> {
let args = table_args.positioned;
// Check args len.
validate_function_arg(table_func_name, args.len(), None, 0)?;

let table_info = TableInfo {
ident: TableIdent::new(table_id, 0),
desc: format!("'{}'.'{}'", database_name, table_func_name),
name: table_func_name.to_string(),
meta: TableMeta {
schema: Self::schema(),
engine: SHOW_VARIABLES.to_owned(),
..Default::default()
},
..Default::default()
};

Ok(Arc::new(Self {
table_info,
}))
}

fn schema() -> Arc<TableSchema> {
TableSchemaRefExt::create(vec![
TableField::new("name", TableDataType::String),
TableField::new("value", TableDataType::String),
TableField::new("type", TableDataType::String),
])
}
}

#[async_trait::async_trait]
impl Table for ShowVariables {
fn as_any(&self) -> &dyn Any {
self
}

fn get_table_info(&self) -> &TableInfo {
&self.table_info
}

#[async_backtrace::framed]
async fn read_partitions(
&self,
_ctx: Arc<dyn TableContext>,
_push_downs: Option<PushDownInfo>,
_dry_run: bool,
) -> Result<(PartStatistics, Partitions)> {
Ok((PartStatistics::default(), Partitions::default()))
}

fn table_args(&self) -> Option<TableArgs> {
Some(TableArgs::new_positioned(vec![]))
}

fn read_data(
&self,
ctx: Arc<dyn TableContext>,
_plan: &DataSourcePlan,
pipeline: &mut Pipeline,
_put_cache: bool,
) -> Result<()> {
pipeline.add_source(
|output| {
ShowVariablesSource::create(
ctx.clone(),
output,
)
},
1,
)?;

Ok(())
}
}

struct ShowVariablesSource {
ctx: Arc<dyn TableContext>,
finished: bool,
}

impl ShowVariablesSource {
pub fn create(
ctx: Arc<dyn TableContext>,
output: Arc<OutputPort>,
) -> Result<ProcessorPtr> {
AsyncSourcer::create(ctx.clone(), output, ShowVariablesSource {
ctx,
finished: false,
})
}
}

#[async_trait::async_trait]
impl AsyncSource for ShowVariablesSource {
const NAME: &'static str = "show_variables";

#[async_backtrace::framed]
async fn generate(&mut self) -> Result<Option<DataBlock>> {
if self.finished {
return Ok(None);
}

let res = show_variables(self.ctx.clone()).await?;

// Mark done.
self.finished = true;
Ok(res)
}
}

async fn show_variables(
ctx: Arc<dyn TableContext>,
) -> Result<Option<DataBlock>> {
let var = ctx.get_all_variables();

let mut names = vec![];
let mut vars = vec![];
let mut types = vec![];
for (name, var) in var {
names.push(name.to_string());
vars.push(var.to_string());
types.push(var.as_ref().infer_data_type().to_string());
}

Ok(Some(DataBlock::new_from_columns(vec![
StringType::from_data(names),
StringType::from_data(vars),
StringType::from_data(types),
])))
}

impl TableFunction for ShowVariables {
fn function_name(&self) -> &str {
self.name()
}

fn as_table<'a>(self: Arc<Self>) -> Arc<dyn Table + 'a>
where Self: 'a {
self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use crate::table_functions::inspect_parquet::InspectParquetTable;
use crate::table_functions::list_stage::ListStageTable;
use crate::table_functions::numbers::NumbersTable;
use crate::table_functions::show_grants::ShowGrants;
use crate::table_functions::show_variables::ShowVariables;
use crate::table_functions::srf::RangeTable;
use crate::table_functions::sync_crash_me::SyncCrashMeTable;
use crate::table_functions::GPT2SQLTable;
Expand Down Expand Up @@ -283,6 +284,11 @@ impl TableFunctionFactory {
(next_id(), Arc::new(TaskHistoryTable::create)),
);

creators.insert(
"show_variables".to_string(),
(next_id(), Arc::new(ShowVariables::create)),
);

TableFunctionFactory {
creators: RwLock::new(creators),
}
Expand Down
1 change: 1 addition & 0 deletions src/query/sql/src/planner/binder/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl<'a> Binder {
Statement::ShowProcessList { show_options } => self.bind_show_process_list(bind_context, show_options).await?,
Statement::ShowEngines { show_options } => self.bind_show_engines(bind_context, show_options).await?,
Statement::ShowSettings { show_options } => self.bind_show_settings(bind_context, show_options).await?,
Statement::ShowVariables { show_options } => self.bind_show_variables(bind_context, show_options).await?,
Statement::ShowIndexes { show_options } => self.bind_show_indexes(bind_context, show_options).await?,
Statement::ShowLocks(stmt) => self.bind_show_locks(bind_context, stmt).await?,
// Catalogs
Expand Down
Loading

0 comments on commit 6b580e5

Please sign in to comment.