Skip to content

How to print the ParsedQuery (Or other AST types?) returned from parse() #45

Open
@SteveLauC

Description

@SteveLauC

This is probably a really stupid question, I am trying to print the parsed AST out, but the ParsedQuery type does not have either Debug or Display implemented, and, it is a private type so you cannot check its documentation, a post I found shows that it was Debug:

use pg_query;

fn main() {
    let simple = "SELECT * FROM x";

    let medium = "SELECT COUNT(1) AS count, name section FROM t GROUP BY name LIMIT 10";

    let complex = "
SELECT
        country.country_name_eng,
        SUM(CASE WHEN talk.id IS NOT NULL THEN 1 ELSE 0 END) AS talks,
        AVG(ISNULL(DATEDIFF(SECOND, talk.start_time, talk.end_time),0)) AS avg_difference
FROM country
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON city.id = customer.city_id
LEFT JOIN talk ON talk.customer_id = customer.id
GROUP BY
        country.id,
        country.country_name_eng
HAVING AVG(ISNULL(DATEDIFF(SECOND, talk.start_time, talk.end_time),0)) > (SELECT AVG(DATEDIFF(SECOND, talk.start_time, talk.end_time)) FROM talk)
ORDER BY talks DESC, country.id ASC;
";

    let simplebad = "SELECT * FROM GROUP BY age";

    let tests = vec![simple, medium, complex, simplebad];

    for test in tests {
        match pg_query::parse(test) {
            Ok(ast) => println!("AST: {:#?}", ast),
            Err(err) => println!("Error: {:#?}", err),
        }
    }
}

But the above code won't compile now because ParsedQuery is not Debug:

$ cargo r
   Compiling rust v0.1.0 (/Users/steve/Documents/workspace/rust)
error[E0277]: `pg_query::parse_result::ParseResult` doesn't implement `Debug`
  --> src/main.rs:30:47
   |
30 |             Ok(ast) => println!("AST: {:#?}", ast),
   |                                               ^^^ `pg_query::parse_result::ParseResult` cannot be formatted using `{:?}` because it doesn't implement `Debug`
   |
   = help: the trait `Debug` is not implemented for `pg_query::parse_result::ParseResult`
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `rust` (bin "rust") due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions