Skip to content

Commit d01002c

Browse files
e1ijah1alamb
andauthored
[sqllogictest] port tests in avro.rs to sqllogictest (#6362)
* feat: port tests in avro.rs to sqllogictest * fix: add test setup for avro_query_multiple_files * run cargo fmt * Run hash collisions test with avro support too * fix clippy --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent 0536219 commit d01002c

File tree

6 files changed

+183
-167
lines changed

6 files changed

+183
-167
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ jobs:
461461
- name: Run tests
462462
run: |
463463
cd datafusion
464-
cargo test --lib --tests --features=force_hash_collisions
464+
cargo test --lib --tests --features=force_hash_collisions,avro
465465
466466
cargo-toml-formatting-checks:
467467
name: check Cargo.toml formatting

datafusion/core/tests/sql/avro.rs

Lines changed: 0 additions & 157 deletions
This file was deleted.

datafusion/core/tests/sql/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ macro_rules! test_expression {
8181
pub mod aggregates;
8282
pub mod arrow_files;
8383
#[cfg(feature = "avro")]
84-
pub mod avro;
8584
pub mod create_drop;
8685
pub mod explain_analyze;
8786
pub mod expr;

datafusion/core/tests/sqllogictests/src/main.rs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::thread;
2222

2323
use log::info;
2424
use sqllogictest::strict_column_validator;
25+
use tempfile::TempDir;
2526

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

@@ -83,7 +84,8 @@ async fn run_test_file(
8384
relative_path: PathBuf,
8485
) -> Result<(), Box<dyn Error>> {
8586
info!("Running with DataFusion runner: {}", path.display());
86-
let ctx = context_for_test_file(&relative_path).await;
87+
let test_ctx = context_for_test_file(&relative_path).await;
88+
let ctx = test_ctx.session_ctx().clone();
8789
let mut runner = sqllogictest::Runner::new(DataFusion::new(ctx, relative_path));
8890
runner.with_column_validator(strict_column_validator);
8991
runner.run_file_async(path).await?;
@@ -110,7 +112,8 @@ async fn run_complete_file(
110112

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

113-
let ctx = context_for_test_file(&relative_path).await;
115+
let test_ctx = context_for_test_file(&relative_path).await;
116+
let ctx = test_ctx.session_ctx().clone();
114117
let mut runner = sqllogictest::Runner::new(DataFusion::new(ctx, relative_path));
115118
let col_separator = " ";
116119
runner
@@ -160,27 +163,67 @@ fn read_dir_recursive<P: AsRef<Path>>(path: P) -> Box<dyn Iterator<Item = PathBu
160163
}
161164

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

168-
let ctx = SessionContext::with_config(config);
171+
let mut test_ctx = TestContext::new(SessionContext::with_config(config));
169172

170173
match relative_path.file_name().unwrap().to_str().unwrap() {
171174
"aggregate.slt" => {
172175
info!("Registering aggregate tables");
173-
setup::register_aggregate_tables(&ctx).await;
176+
setup::register_aggregate_tables(test_ctx.session_ctx()).await;
174177
}
175178
"scalar.slt" => {
176179
info!("Registering scalar tables");
177-
setup::register_scalar_tables(&ctx).await;
180+
setup::register_scalar_tables(test_ctx.session_ctx()).await;
181+
}
182+
"avro.slt" => {
183+
info!("Registering avro tables");
184+
setup::register_avro_tables(&mut test_ctx).await;
178185
}
179186
_ => {
180187
info!("Using default SessionContext");
181188
}
182189
};
183-
ctx
190+
test_ctx
191+
}
192+
193+
/// Context for running tests
194+
pub struct TestContext {
195+
/// Context for running queries
196+
ctx: SessionContext,
197+
/// Temporary directory created and cleared at the end of the test
198+
test_dir: Option<TempDir>,
199+
}
200+
201+
impl TestContext {
202+
fn new(ctx: SessionContext) -> Self {
203+
Self {
204+
ctx,
205+
test_dir: None,
206+
}
207+
}
208+
209+
/// Enables the test directory feature. If not enabled,
210+
/// calling `testdir_path` will result in a panic.
211+
fn enable_testdir(&mut self) {
212+
if self.test_dir.is_none() {
213+
self.test_dir = Some(TempDir::new().expect("failed to create testdir"));
214+
}
215+
}
216+
217+
/// Returns the path to the test directory. Panics if the test
218+
/// directory feature is not enabled via `enable_testdir`.
219+
fn testdir_path(&self) -> &Path {
220+
self.test_dir.as_ref().expect("testdir not enabled").path()
221+
}
222+
223+
/// Returns a reference to the internal SessionContext
224+
fn session_ctx(&self) -> &SessionContext {
225+
&self.ctx
226+
}
184227
}
185228

186229
/// Parsed command line options

datafusion/core/tests/sqllogictests/src/setup.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use datafusion::prelude::AvroReadOptions;
1819
use datafusion::{
1920
arrow::{
2021
array::{
@@ -30,7 +31,40 @@ use datafusion::{
3031
};
3132
use std::sync::Arc;
3233

33-
use crate::utils;
34+
use crate::{utils, TestContext};
35+
36+
pub async fn register_avro_tables(ctx: &mut TestContext) {
37+
register_avro_test_data(ctx).await;
38+
}
39+
40+
async fn register_avro_test_data(ctx: &mut TestContext) {
41+
ctx.enable_testdir();
42+
43+
let table_path = ctx.testdir_path().join("avro");
44+
std::fs::create_dir(&table_path).expect("failed to create avro table path");
45+
46+
let testdata = datafusion::test_util::arrow_test_data();
47+
let alltypes_plain_file = format!("{testdata}/avro/alltypes_plain.avro");
48+
std::fs::copy(
49+
&alltypes_plain_file,
50+
format!("{}/alltypes_plain1.avro", table_path.display()),
51+
)
52+
.unwrap();
53+
std::fs::copy(
54+
&alltypes_plain_file,
55+
format!("{}/alltypes_plain2.avro", table_path.display()),
56+
)
57+
.unwrap();
58+
59+
ctx.session_ctx()
60+
.register_avro(
61+
"alltypes_plain_multi_files",
62+
table_path.display().to_string().as_str(),
63+
AvroReadOptions::default(),
64+
)
65+
.await
66+
.unwrap();
67+
}
3468

3569
pub async fn register_aggregate_tables(ctx: &SessionContext) {
3670
register_aggregate_test_100(ctx).await;

0 commit comments

Comments
 (0)