qstommyshu commented on PR #15578:
URL: https://github.com/apache/datafusion/pull/15578#issuecomment-2781397695

   Hi @alamb and @blaginin 
   
   I found a way to migrate `roundtrip_statement_with_dialect()` now, just want 
to confirm if you like to proceed with it. What I did below is to incorporate 
all core functions into a macro, and try to minimize the lines of code (so that 
we don't have too much lines of code increase in this migration) for each test 
case but still expressive, by adding the variable name before each variable in 
the macro (because rust analyzer doesn't indicate types for macro). 
   
   ```Rust
   #[macro_export]
   macro_rules! roundtrip_statement_with_dialect_helper {
       (
           query: $sql:expr,
           parser_dialect: $parser_dialect:expr,
           unparser_dialect: $unparser_dialect:expr,
           expected: @ $expected:literal $(,)?
       ) => {{
           let statement = Parser::new(&$parser_dialect)
               .try_with_sql($sql)?
               .parse_statement()?;
   
           let state = MockSessionState::default()
               .with_aggregate_function(max_udaf())
               .with_aggregate_function(min_udaf())
               .with_expr_planner(Arc::new(CoreFunctionPlanner::default()))
               .with_expr_planner(Arc::new(NestedFunctionPlanner));
   
           let context = MockContextProvider { state };
           let sql_to_rel = SqlToRel::new(&context);
           let plan = sql_to_rel
               .sql_statement_to_plan(statement)
               .unwrap_or_else(|e| panic!("Failed to parse sql: {}\n{e}", 
$sql));
   
           let unparser = Unparser::new(&$unparser_dialect);
           let roundtrip_statement = unparser.plan_to_sql(&plan)?;
   
           let actual = &roundtrip_statement.to_string();
           insta::assert_snapshot!(actual, @ $expected);
       }};
   }
   
   // each test case would look like this
   #[test]
   fn roundtrip_statement_with_dialect_1() -> Result<(), DataFusionError> {
       roundtrip_statement_with_dialect_helper!(
           query: "select min(ta.j1_id) as j1_min from j1 ta order by 
min(ta.j1_id) limit 10;",
           parser_dialect: MySqlDialect {},
           unparser_dialect: UnparserMySqlDialect {},
           expected: @"SELECT `j1_min` FROM (SELECT min(`ta`.`j1_id`) AS 
`j1_min`, min(`ta`.`j1_id`) FROM `j1` AS `ta` ORDER BY min(`ta`.`j1_id`) ASC) 
AS `derived_sort` LIMIT 10",
       );
       Ok(())
   }
   
   ```
   
   However, I feel like this is not a standard way to write test. Please let me 
know if you want to proceed with this way.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to