Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sqllogictest] port tests in avro.rs to sqllogictest #6362

Merged
merged 6 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 0 additions & 157 deletions datafusion/core/tests/sql/avro.rs

This file was deleted.

1 change: 0 additions & 1 deletion datafusion/core/tests/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ macro_rules! test_expression {

pub mod aggregates;
#[cfg(feature = "avro")]
pub mod avro;
pub mod create_drop;
pub mod explain_analyze;
pub mod expr;
Expand Down
58 changes: 51 additions & 7 deletions datafusion/core/tests/sqllogictests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::thread;

use log::info;
use sqllogictest::strict_column_validator;
use tempfile::TempDir;

use datafusion::prelude::{SessionConfig, SessionContext};

Expand Down Expand Up @@ -83,7 +84,8 @@ async fn run_test_file(
relative_path: PathBuf,
) -> Result<(), Box<dyn Error>> {
info!("Running with DataFusion runner: {}", path.display());
let ctx = context_for_test_file(&relative_path).await;
let test_ctx = context_for_test_file(&relative_path).await;
let ctx = test_ctx.session_ctx().clone();
let mut runner = sqllogictest::Runner::new(DataFusion::new(ctx, relative_path));
runner.with_column_validator(strict_column_validator);
runner.run_file_async(path).await?;
Expand All @@ -110,7 +112,8 @@ async fn run_complete_file(

info!("Using complete mode to complete: {}", path.display());

let ctx = context_for_test_file(&relative_path).await;
let test_ctx = context_for_test_file(&relative_path).await;
let ctx = test_ctx.session_ctx().clone();
let mut runner = sqllogictest::Runner::new(DataFusion::new(ctx, relative_path));
let col_separator = " ";
runner
Expand Down Expand Up @@ -160,29 +163,70 @@ fn read_dir_recursive<P: AsRef<Path>>(path: P) -> Box<dyn Iterator<Item = PathBu
}

/// Create a SessionContext, configured for the specific test
async fn context_for_test_file(relative_path: &Path) -> SessionContext {
async fn context_for_test_file(relative_path: &Path) -> TestContext {
let config = SessionConfig::new()
// hardcode target partitions so plans are deterministic
.with_target_partitions(4);

let ctx = SessionContext::with_config(config);
let mut test_ctx = TestContext::new(SessionContext::with_config(config));

match relative_path.file_name().unwrap().to_str().unwrap() {
"aggregate.slt" => {
info!("Registering aggregate tables");
setup::register_aggregate_tables(&ctx).await;
setup::register_aggregate_tables(&test_ctx.session_ctx()).await;
}
"scalar.slt" => {
info!("Registering scalar tables");
setup::register_scalar_tables(&ctx).await;
setup::register_scalar_tables(&test_ctx.session_ctx()).await;
}
"avro.slt" => {
info!("Registering avro tables");
setup::register_avro_tables(&mut test_ctx).await;
}
_ => {
info!("Using default SessionContext");
}
};
ctx
test_ctx
}

/// Context for running tests
pub struct TestContext {
/// Context for running queries
ctx: SessionContext,
/// Temporary directory created and cleared at the end of the test
test_dir: Option<TempDir>,
}

impl TestContext {
fn new(ctx: SessionContext) -> Self {
Self { ctx, test_dir: None }
}

/// Enables the test directory feature. If not enabled,
/// calling `testdir_path` will result in a panic.
fn enable_testdir(&mut self) {
if self.test_dir.is_none() {
self.test_dir = Some(TempDir::new().expect("failed to create testdir"));
}
}

/// Returns the path to the test directory. Panics if the test
/// directory feature is not enabled via `enable_testdir`.
fn testdir_path(&self) -> &Path {
self.test_dir
.as_ref()
.expect("testdir not enabled")
.path()
}

/// Returns a reference to the internal SessionContext
fn session_ctx(&self) -> &SessionContext {
&self.ctx
}
}


/// Parsed command line options
struct Options {
// regex like
Expand Down
35 changes: 34 additions & 1 deletion datafusion/core/tests/sqllogictests/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,41 @@ use datafusion::{
test_util,
};
use std::sync::Arc;
use datafusion::prelude::AvroReadOptions;

use crate::utils;
use crate::{TestContext, utils};

pub async fn register_avro_tables(ctx: &mut TestContext) {
register_avro_test_data(ctx).await;
}

async fn register_avro_test_data(ctx: &mut TestContext) {
ctx.enable_testdir();
e1ijah1 marked this conversation as resolved.
Show resolved Hide resolved

let table_path = ctx.testdir_path().join("avro");
std::fs::create_dir(&table_path).expect("failed to create avro table path");

let testdata = datafusion::test_util::arrow_test_data();
let alltypes_plain_file = format!("{testdata}/avro/alltypes_plain.avro");
std::fs::copy(
&alltypes_plain_file,
format!("{}/alltypes_plain1.avro", table_path.display()),
)
.unwrap();
std::fs::copy(
&alltypes_plain_file,
format!("{}/alltypes_plain2.avro", table_path.display()),
)
.unwrap();

ctx.session_ctx().register_avro(
"alltypes_plain_multi_files",
table_path.display().to_string().as_str(),
AvroReadOptions::default(),
)
.await
.unwrap();
}

pub async fn register_aggregate_tables(ctx: &SessionContext) {
register_aggregate_test_100(ctx).await;
Expand Down
Loading