Skip to content

Commit

Permalink
add explain to sql parser
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-du committed Nov 16, 2022
1 parent e823cde commit 15ae646
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/datanode/src/instance/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ impl Instance {
Statement::ShowTables(stmt) => {
self.sql_handler.execute(SqlRequest::ShowTables(stmt)).await
}
Statement::Explain(_explain) => {
unimplemented!()
}
Statement::ShowCreateTable(_stmt) => {
unimplemented!("SHOW CREATE TABLE is unimplemented yet");
}
Expand Down
9 changes: 9 additions & 0 deletions src/frontend/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ impl Instance {
.context(AlterTableSnafu)
}

/// Handle explain expr
pub async fn handle_explain(&self) -> Result<Output> {
todo!()
}

/// Handle batch inserts
pub async fn handle_inserts(&self, insert_expr: &[InsertExpr]) -> Result<Output> {
let mut success = 0;
Expand Down Expand Up @@ -624,6 +629,10 @@ impl SqlQueryHandler for Instance {
.await
.map_err(BoxedError::new)
.context(server_error::ExecuteQuerySnafu { query }),
Statement::Explain(_explain) => {
// self.handle_explain()
unimplemented!()
}
Statement::ShowCreateTable(_) => {
return server_error::NotSupportedSnafu { feat: query }.fail()
}
Expand Down
1 change: 1 addition & 0 deletions src/query/src/datafusion/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ where
| Statement::CreateTable(_)
| Statement::CreateDatabase(_)
| Statement::Alter(_)
| Statement::Explain(_)
| Statement::Insert(_) => unreachable!(),
}
}
Expand Down
32 changes: 30 additions & 2 deletions src/sql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use sqlparser::tokenizer::{Token, Tokenizer};
use crate::error::{
self, InvalidDatabaseNameSnafu, InvalidTableNameSnafu, Result, SyntaxSnafu, TokenizerSnafu,
};
use crate::statements::explain::Explain;
use crate::statements::show::{ShowCreateTable, ShowDatabases, ShowKind, ShowTables};
use crate::statements::statement::Statement;
use crate::statements::statement::*;

/// GrepTime SQL parser context, a simple wrapper for Datafusion SQL parser.
pub struct ParserContext<'a> {
Expand Down Expand Up @@ -218,7 +219,34 @@ impl<'a> ParserContext<'a> {
}

fn parse_explain(&mut self) -> Result<Statement> {
todo!()
let has_describe_alias = match self.parser.next_token() {
Token::Word(word) => match word.keyword {
Keyword::DESC => true,
Keyword::DESCRIBE => true,
_ => false,
},
_ => false,
};

let explain_statement = if has_describe_alias {
self.parser
.parse_explain(false)
.with_context(|_| error::UnexpectedSnafu {
sql: self.sql,
expected: "a statement",
actual: self.peek_token_as_string(),
})?
} else {
self.parser
.parse_explain(false)
.with_context(|_| error::UnexpectedSnafu {
sql: self.sql,
expected: "a statement",
actual: self.peek_token_as_string(),
})?
};

Ok(Statement::Explain(Explain::try_from(explain_statement)?))
}

// Report unexpected token
Expand Down
1 change: 1 addition & 0 deletions src/sql/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

pub mod alter;
pub mod create;
pub mod explain;
pub mod insert;
pub mod query;
pub mod show;
Expand Down
31 changes: 31 additions & 0 deletions src/sql/src/statements/explain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 Greptime Team
//
// 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 sqlparser::ast::Statement;

/// Explain statement instance.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Explain {
pub inner: Statement,
}

use crate::error::Error;

impl TryFrom<Statement> for Explain {
type Error = Error;

fn try_from(value: Statement) -> Result<Self, Self::Error> {
Ok(Explain { inner: value })
}
}
4 changes: 4 additions & 0 deletions src/sql/src/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sqlparser::parser::ParserError;

use crate::statements::alter::AlterTable;
use crate::statements::create::{CreateDatabase, CreateTable};
use crate::statements::explain::Explain;
use crate::statements::insert::Insert;
use crate::statements::query::Query;
use crate::statements::show::{ShowCreateTable, ShowDatabases, ShowTables};
Expand All @@ -40,6 +41,8 @@ pub enum Statement {
ShowTables(ShowTables),
// SHOW CREATE TABLE
ShowCreateTable(ShowCreateTable),
// EXPLAIN [[ DESCRIBE| DESC ]] select_statement
Explain(Explain),
}

/// Converts Statement to sqlparser statement
Expand All @@ -62,6 +65,7 @@ impl TryFrom<Statement> for SpStatement {
Statement::CreateDatabase(_) | Statement::CreateTable(_) | Statement::Alter(_) => {
unimplemented!()
}
Statement::Explain(e) => Ok(e.inner),
}
}
}
Expand Down

0 comments on commit 15ae646

Please sign in to comment.