@@ -29,7 +29,9 @@ use datafusion::physical_plan::metrics::MetricValue;
2929use datafusion:: physical_plan:: operator_statistics:: StatisticsRegistry ;
3030use datafusion:: physical_plan:: { ExecutionPlan , collect} ;
3131use datafusion:: prelude:: { ParquetReadOptions , SessionConfig , SessionContext } ;
32- use datafusion:: sql:: parser:: { DFParser , Statement } ;
32+ use datafusion:: sql:: parser:: { DFParserBuilder , Statement } ;
33+ use datafusion:: sql:: sqlparser:: dialect:: dialect_from_str;
34+ use datafusion_common:: config:: { Dialect , SqlParserOptions } ;
3335use datafusion_common:: config_err;
3436use datafusion_common:: stats:: Precision ;
3537use regex:: Regex ;
@@ -61,6 +63,7 @@ impl RunOpt {
6163 let mut config = SessionConfig :: from_env ( ) ?. with_collect_statistics ( true ) ;
6264 config. options_mut ( ) . optimizer . prefer_hash_join = true ;
6365 let ctx = SessionContext :: new_with_config ( config) ;
66+ let sql_parser_options = ctx. state ( ) . config_options ( ) . sql_parser . clone ( ) ;
6467 register_parquet_files ( & ctx, & self . path ) . await ?;
6568
6669 let branch = current_branch_name ( ) ;
@@ -85,7 +88,7 @@ impl RunOpt {
8588 . to_string_lossy ( )
8689 . to_string ( ) ;
8790 let sql = fs:: read_to_string ( query_path) ?;
88- let statements = match sql_statements ( & sql) {
91+ let statements = match sql_statements ( & sql, & sql_parser_options ) {
8992 Ok ( statements) => statements,
9093 Err ( error) => {
9194 let report = QueryReport {
@@ -580,8 +583,20 @@ fn serialize_report(report: &[QueryReport]) -> Result<String> {
580583 . map_err ( |error| DataFusionError :: External ( Box :: new ( error) ) )
581584}
582585
583- fn sql_statements ( sql : & str ) -> Result < VecDeque < Statement > > {
584- DFParser :: parse_sql ( sql)
586+ fn sql_statements ( sql : & str , options : & SqlParserOptions ) -> Result < VecDeque < Statement > > {
587+ let dialect = dialect_from_str ( options. dialect ) . ok_or_else ( || {
588+ DataFusionError :: Plan ( format ! (
589+ "Unsupported SQL dialect: {}. Available dialects: {}." ,
590+ options. dialect,
591+ Dialect :: available( )
592+ ) )
593+ } ) ?;
594+
595+ DFParserBuilder :: new ( sql)
596+ . with_dialect ( dialect. as_ref ( ) )
597+ . with_recursion_limit ( options. recursion_limit . get ( ) )
598+ . build ( ) ?
599+ . parse_statements ( )
585600}
586601
587602fn query_files ( path : & Path , query : Option < & str > ) -> Result < Vec < PathBuf > > {
@@ -687,6 +702,7 @@ mod tests {
687702 "CREATE EXTERNAL TABLE t(c1 int) STORED AS CSV \
688703 PARTITIONED BY (p1, p2) LOCATION 'foo.csv' \
689704 OPTIONS (format.delimiter '|'); SELECT ';'",
705+ & SessionConfig :: new ( ) . options ( ) . sql_parser ,
690706 )
691707 . unwrap ( ) ;
692708
@@ -699,6 +715,19 @@ mod tests {
699715 assert_eq ! ( table. options. len( ) , 1 ) ;
700716 }
701717
718+ #[ test]
719+ fn parses_statements_with_session_sql_dialect ( ) {
720+ let sql = "# MySQL comment\n SELECT 1" ;
721+ assert ! ( sql_statements( sql, & SessionConfig :: new( ) . options( ) . sql_parser) . is_err( ) ) ;
722+
723+ let mut config = SessionConfig :: new ( ) ;
724+ config. options_mut ( ) . sql_parser . dialect = Dialect :: MySQL ;
725+
726+ let statements = sql_statements ( sql, & config. options ( ) . sql_parser ) . unwrap ( ) ;
727+
728+ assert_eq ! ( statements. len( ) , 1 ) ;
729+ }
730+
702731 #[ test]
703732 fn persists_failed_reports ( ) {
704733 let directory = tempdir ( ) . unwrap ( ) ;
0 commit comments