|
18 | 18 | use std::collections::HashMap; |
19 | 19 |
|
20 | 20 | use super::*; |
| 21 | +use datafusion::assert_batches_eq; |
21 | 22 | use datafusion_common::{metadata::Literal, ParamValues, ScalarValue}; |
22 | 23 | use insta::assert_snapshot; |
23 | 24 |
|
@@ -321,35 +322,44 @@ async fn test_named_parameter_not_bound() -> Result<()> { |
321 | 322 |
|
322 | 323 | #[tokio::test] |
323 | 324 | async fn test_query_parameters_with_metadata() -> Result<()> { |
324 | | - let tmp_dir = TempDir::new()?; |
325 | | - let partition_count = 4; |
326 | | - let ctx = create_ctx_with_partition(&tmp_dir, partition_count).await?; |
| 325 | + let ctx = SessionContext::new(); |
327 | 326 |
|
328 | | - let metadata0 = HashMap::from([( |
329 | | - "some_key".to_string(), |
330 | | - "some_value".to_string(), |
331 | | - )]); |
332 | | - let metadata1 = HashMap::from([( |
333 | | - "some_other_key".to_string(), |
334 | | - "some_other_value".to_string(), |
335 | | - )]); |
| 327 | + let df = ctx.sql("SELECT $1, $2").await.unwrap(); |
336 | 328 |
|
337 | | - // sql to statement then to logical plan with parameters |
338 | | - let df = ctx.sql("SELECT $1, $2").await?; |
| 329 | + let metadata1 = HashMap::from([("some_key".to_string(), "some_value".to_string())]); |
| 330 | + let metadata2 = |
| 331 | + HashMap::from([("some_other_key".to_string(), "some_other_value".to_string())]); |
| 332 | + |
| 333 | + let df_with_params_replaced = df |
| 334 | + .with_param_values(ParamValues::List(vec![ |
| 335 | + Literal::new(ScalarValue::UInt32(Some(1)), Some(metadata1.clone().into())), |
| 336 | + Literal::new( |
| 337 | + ScalarValue::Utf8(Some("two".to_string())), |
| 338 | + Some(metadata2.clone().into()), |
| 339 | + ), |
| 340 | + ])) |
| 341 | + .unwrap(); |
339 | 342 |
|
340 | | - let df_with_params_replaced = df.with_param_values(ParamValues::List(vec![ |
341 | | - Literal::new(ScalarValue::UInt32(Some(3)), Some(metadata0.clone().into())), |
342 | | - Literal::new( |
343 | | - ScalarValue::Utf8(Some("bar_value".to_string())), |
344 | | - Some(metadata1.clone().into()), |
345 | | - ), |
346 | | - ]))?; |
| 343 | + // df_with_params_replaced.schema() is not correct here |
| 344 | + // https://github.com/apache/datafusion/issues/18102 |
| 345 | + let batches = df_with_params_replaced.clone().collect().await.unwrap(); |
| 346 | + let schema = batches[0].schema(); |
347 | 347 |
|
348 | | - let schema = df_with_params_replaced.schema(); |
349 | 348 | assert_eq!(schema.field(0).data_type(), &DataType::UInt32); |
350 | | - assert_eq!(schema.field(0).metadata(), &metadata0); |
| 349 | + assert_eq!(schema.field(0).metadata(), &metadata1); |
351 | 350 | assert_eq!(schema.field(1).data_type(), &DataType::Utf8); |
352 | | - assert_eq!(schema.field(1).metadata(), &metadata1); |
| 351 | + assert_eq!(schema.field(1).metadata(), &metadata2); |
| 352 | + |
| 353 | + assert_batches_eq!( |
| 354 | + vec![ |
| 355 | + "+----+-----+", |
| 356 | + "| $1 | $2 |", |
| 357 | + "+----+-----+", |
| 358 | + "| 1 | two |", |
| 359 | + "+----+-----+", |
| 360 | + ], |
| 361 | + &batches |
| 362 | + ); |
353 | 363 |
|
354 | 364 | Ok(()) |
355 | 365 | } |
|
0 commit comments