Run the fuzzer
mainTo start fuzzing, navigate to the fuzz directory and use cargo hfuzz run followed by the target name.
cd fuzz
cargo hfuzz run <target>repository·main·Indexed 25 days ago
https://github.com/apache/datafusion-sqlparser-rsAn extensible SQL Lexer and Parser written in Rust with support for ANSI SQL:2011. The library provides tools for AST traversal via Visit and VisitMut derive macros, support for custom SQL dialects through the derive_dialect! macro, and utilities for benchmarking and fuzzing.
To start fuzzing, navigate to the fuzz directory and use cargo hfuzz run followed by the target name.
cd fuzz
cargo hfuzz run <target>cargo bench command within the sqlparser_bench crate. The benchmarking results are reported using the criterion library. Note that the benchmarking suite is located in a separate crate (sqlparser_bench) to prevent increasing the build time of the core sqlparser crate.In version 0.55.0, the Expr::Value enum variant was changed to contain a ValueWithSpan instead of a direct Value.
When pattern matching on Expr::Value, you must now destructure the ValueWithSpan struct to access the underlying value and the span.
To create a new expression with a value, use the Expr::value() method (lowercase 'v') instead of the Expr::Value variant. This method creates a ValueWithSpan containing an empty span.
- Expr::Value(Value::SingleQuotedString(my_string)) => { ... }
+ Expr::Value(ValueWithSpan{ value: Value::SingleQuotedString(my_string), span: _ }) => { ... }
- Expr::Value(Value::SingleQuotedString(my_string))
+ Expr::value(Value::SingleQuotedString(my_string))To execute the benchmarks for the sqlparser crate, use the cargo bench command targeting the sqlparser_bench benchmark suite.
cargo bench --bench sqlparser_benchTo perform fuzzing on this project, you must install honggfuzz using cargo and ensure your system has the necessary dependencies installed for honggfuzz-rs.
cargo install honggfuzzYou can profile the sqlparser benchmarks and generate a flamegraph using the cargo flamegraph command. This requires the flamegraph crate to be installed/available in your environment.
cargo flamegraph --bench sqlparser_benchIn version 0.55.0, the ObjectName structure was updated to use ObjectNamePart instead of Ident for its segments.
ObjectNameInstead of passing a vector of Ident directly to the ObjectName tuple struct, use the From implementation.
To access the span of an ObjectName, use the .span() method instead of accessing a .span field.
- pub struct ObjectName(pub Vec<Ident>);
+ pub struct ObjectName(pub Vec<ObjectNamePart>)
- name: ObjectName(vec![Ident::new("f")]),
+ name: ObjectName::from(vec![Ident::new("f")]),
- name.span
+ name.span()Since version 0.53.0, AST nodes include source span information for better error reporting. If you are manually constructing AST nodes or pattern matching on them, you must account for the new span field.
When creating nodes like Ident, you must now provide a span. Use Span::empty() if no specific location is available.
Old way:
Ident {
value: "name".into(),
quote_style: None,
}New way:
Ident {
value: "name".into(),
quote_style: None,
span: Span::empty(),
}You must update all existing pattern matches to include the span field to avoid compilation errors.
If the fuzzer discovers a panic, you can retrieve a stack trace using cargo hfuzz run-debug. You must provide the target name and the path to the generated fuzz files (typically found in hfuzz_workspace/<target>/*.fuzz).
# Example for the fuzz_parse_sql target
cargo hfuzz run fuzz_parse_sql
cargo hfuzz run-debug fuzz_parse_sql hfuzz_workspace/fuzz_parse_sql/*.fuzzYou can instruct the macro to call a specific visitor method for a type by using the #[visit(with = "method_name")] attribute on the type definition. This is useful when you want the visitor to trigger specific pre_visit_ and post_visit_ hooks for that type during traversal.
#[derive(Visit, VisitMut)]
#[visit(with = "visit_expr")]
enum Expr {
IsNull(Box<Expr>),
..
}The sqlparser_derive crate provides procedural macros to automatically implement the Visit and VisitMut traits from the sqlparser crate for your structs and enums. This allows you to traverse and mutate AST structures easily.
Basic usage involves adding #[derive(Visit, VisitMut)] to your types.
#[derive(Visit, VisitMut)]
struct Foo {
boolean: bool,
bar: Bar,
}
#[derive(Visit, VisitMut)]
enum Bar {
A(),
B(String, bool),
C { named: i32 },
}