Skip to content

Commit

Permalink
feat: Improve the parse and performance of DESCRIBE query
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jun 15, 2024
1 parent ca81fca commit 224000d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
38 changes: 13 additions & 25 deletions crates/gitql-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,41 +165,29 @@ fn evaluate_describe_query(
env: &mut Environment,
stmt: DescribeStatement,
) -> Result<EvaluationResult, String> {
let mut gitql_object = GitQLObject::default();
let hidden_selections = vec![];

let table_fields = env
.schema
.tables_fields_names
.get(&stmt.table_name.as_str());

if table_fields.is_none() {
return Err(format!("Table {:?} doesnt exist", &stmt.table_name));
}

let table_fields = table_fields.unwrap();
.get(&stmt.table_name.as_str())
.unwrap();

for title in ["Field", "Type"] {
gitql_object.titles.push(title.to_owned());
}
let mut gitql_object = GitQLObject::default();
gitql_object.titles.push("Field".to_owned());
gitql_object.titles.push("Type".to_owned());

let mut rows: Vec<Row> = Vec::with_capacity(table_fields.len());
for field in table_fields {
let value = env.schema.tables_fields_types.get(field).unwrap();

gitql_object.groups.push(Group {
rows: vec![Row {
values: vec![
Value::Text(field.to_owned().to_owned()),
Value::Text(value.to_string()),
],
}],
rows.push(Row {
values: vec![
Value::Text(field.to_owned().to_owned()),
Value::Text(value.to_string()),
],
})
}

Ok(EvaluationResult::SelectedGroups(
gitql_object,
hidden_selections,
))
gitql_object.groups.push(Group { rows });
Ok(EvaluationResult::SelectedGroups(gitql_object, vec![]))
}

fn evaluate_show_tables_query(env: &mut Environment) -> Result<EvaluationResult, String> {
Expand Down
42 changes: 29 additions & 13 deletions crates/gitql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn parse_gql(tokens: Vec<Token>, env: &mut Environment) -> Result<Query, Box
TokenKind::Do => parse_do_query(env, &tokens, &mut position),
TokenKind::Set => parse_set_query(env, &tokens, &mut position),
TokenKind::Select => parse_select_query(env, &tokens, &mut position),
TokenKind::Describe => parse_describe_query(&tokens, &mut position),
TokenKind::Describe => parse_describe_query(env, &tokens, &mut position),
TokenKind::Show => parse_show_query(&tokens, &mut position),
_ => Err(un_expected_statement_error(&tokens, &mut position)),
};
Expand Down Expand Up @@ -140,25 +140,41 @@ fn parse_set_query(
}))
}

fn parse_describe_query(tokens: &[Token], position: &mut usize) -> Result<Query, Box<Diagnostic>> {
let len = tokens.len();

// Consume Set keyword
fn parse_describe_query(
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
) -> Result<Query, Box<Diagnostic>> {
// Consume `DESCRIBE` keyword
*position += 1;

if *position >= len || tokens[*position].kind != TokenKind::Symbol {
return Err(Diagnostic::error("Expect table name after DESC Statement")
.with_location(get_safe_location(tokens, *position - 1))
.as_boxed());
if *position >= tokens.len() || tokens[*position].kind != TokenKind::Symbol {
return Err(
Diagnostic::error("Expect table name after DESCRIBE Statement")
.with_location(get_safe_location(tokens, *position))
.as_boxed(),
);
}

let name = &tokens[*position].literal;
// Make sure table name is valid
let table_name = tokens[*position].literal.to_string();
if !env
.schema
.tables_fields_names
.contains_key(table_name.as_str())
{
return Err(
Diagnostic::error(&format!("Unresolved table name `{}`", table_name))
.add_help("You can use the command `SHOW TABLES` to get list of current tables")
.with_location(get_safe_location(tokens, *position))
.as_boxed(),
);
}

// Consume Table Name
*position += 1;

Ok(Query::Describe(DescribeStatement {
table_name: name.to_string(),
}))
Ok(Query::Describe(DescribeStatement { table_name }))
}

fn parse_show_query(tokens: &[Token], position: &mut usize) -> Result<Query, Box<Diagnostic>> {
Expand Down

0 comments on commit 224000d

Please sign in to comment.