Skip to content

Commit af67caa

Browse files
refactor(optimizer): add .with_schema for defining test tables (apache#16138)
Added `tables: HashMap<String, Arc<dyn TableSource>>` and `MyContextProvider::with_schema` method for dynamically defining tables for optimizer integration tests.
1 parent 2add813 commit af67caa

1 file changed

Lines changed: 39 additions & 28 deletions

File tree

datafusion/optimizer/tests/optimizer_integration.rs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,32 @@ fn test_sql(sql: &str) -> Result<LogicalPlan> {
492492
.with_expr_planners(vec![
493493
Arc::new(AggregateFunctionPlanner),
494494
Arc::new(WindowFunctionPlanner),
495-
]);
495+
])
496+
.with_schema(
497+
"test",
498+
Schema::new_with_metadata(
499+
vec![
500+
Field::new("col_int32", DataType::Int32, true),
501+
Field::new("col_uint32", DataType::UInt32, true),
502+
Field::new("col_utf8", DataType::Utf8, true),
503+
Field::new("col_date32", DataType::Date32, true),
504+
Field::new("col_date64", DataType::Date64, true),
505+
// timestamp with no timezone
506+
Field::new(
507+
"col_ts_nano_none",
508+
DataType::Timestamp(TimeUnit::Nanosecond, None),
509+
true,
510+
),
511+
// timestamp with UTC timezone
512+
Field::new(
513+
"col_ts_nano_utc",
514+
DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
515+
true,
516+
),
517+
],
518+
HashMap::new(),
519+
),
520+
);
496521

497522
let sql_to_rel = SqlToRel::new(&context_provider);
498523
let plan = sql_to_rel.sql_statement_to_plan(statement.clone())?;
@@ -510,6 +535,7 @@ fn observe(_plan: &LogicalPlan, _rule: &dyn OptimizerRule) {}
510535
#[derive(Default)]
511536
struct MyContextProvider {
512537
options: ConfigOptions,
538+
tables: HashMap<String, Arc<dyn TableSource>>,
513539
udafs: HashMap<String, Arc<AggregateUDF>>,
514540
expr_planners: Vec<Arc<dyn ExprPlanner>>,
515541
}
@@ -525,38 +551,23 @@ impl MyContextProvider {
525551
self.expr_planners = expr_planners;
526552
self
527553
}
554+
555+
fn with_schema(mut self, name: impl Into<String>, schema: Schema) -> Self {
556+
self.tables.insert(
557+
name.into(),
558+
Arc::new(MyTableSource {
559+
schema: Arc::new(schema),
560+
}),
561+
);
562+
self
563+
}
528564
}
529565

530566
impl ContextProvider for MyContextProvider {
531567
fn get_table_source(&self, name: TableReference) -> Result<Arc<dyn TableSource>> {
532568
let table_name = name.table();
533-
if table_name.starts_with("test") {
534-
let schema = Schema::new_with_metadata(
535-
vec![
536-
Field::new("col_int32", DataType::Int32, true),
537-
Field::new("col_uint32", DataType::UInt32, true),
538-
Field::new("col_utf8", DataType::Utf8, true),
539-
Field::new("col_date32", DataType::Date32, true),
540-
Field::new("col_date64", DataType::Date64, true),
541-
// timestamp with no timezone
542-
Field::new(
543-
"col_ts_nano_none",
544-
DataType::Timestamp(TimeUnit::Nanosecond, None),
545-
true,
546-
),
547-
// timestamp with UTC timezone
548-
Field::new(
549-
"col_ts_nano_utc",
550-
DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
551-
true,
552-
),
553-
],
554-
HashMap::new(),
555-
);
556-
557-
Ok(Arc::new(MyTableSource {
558-
schema: Arc::new(schema),
559-
}))
569+
if let Some(table) = self.tables.get(table_name) {
570+
Ok(table.clone())
560571
} else {
561572
plan_err!("table does not exist")
562573
}

0 commit comments

Comments
 (0)