Skip to content

Commit 90484bb

Browse files
authored
Move sqllogictests to sqllogictests crate to break cyclic dependency (#7284)
* Move sqllogictests to datafusion-sqllogictest module * Port code to be in datafusion * Update expected output * update workflows * update flow * update postgres check * fmt * clippy * Add moved * Add RAT to MOVED.md * Fix tpch instructions and tests
1 parent 6ad7916 commit 90484bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+308
-271
lines changed

.github/workflows/dev_pr/labeler.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ substrait:
4747
- datafusion/substrait/**/*
4848

4949
sqllogictest:
50-
- datafusion/core/tests/sqllogictests/**/*
50+
- datafusion/sqllogictest/**/*

.github/workflows/rust.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,17 @@ jobs:
229229
rust-version: stable
230230
- name: Generate benchmark data and expected query results
231231
run: |
232-
mkdir -p datafusion/core/tests/sqllogictests/test_files/tpch/data
232+
mkdir -p datafusion/sqllogictest/test_files/tpch/data
233233
git clone https://github.com/databricks/tpch-dbgen.git
234234
cd tpch-dbgen
235235
make
236236
./dbgen -f -s 0.1
237-
mv *.tbl ../datafusion/core/tests/sqllogictests/test_files/tpch/data
237+
mv *.tbl ../datafusion/sqllogictest/test_files/tpch/data
238238
- name: Verify that benchmark queries return expected results
239239
run: |
240-
export TPCH_DATA=`realpath datafusion/core/tests/sqllogictests/test_files/tpch/data`
240+
export TPCH_DATA=`realpath datafusion/sqllogictest/test_files/tpch/data`
241241
cargo test serde_q --profile release-nonlto --features=ci -- --test-threads=1
242-
INCLUDE_TPCH=true cargo test -p datafusion --test sqllogictests
242+
INCLUDE_TPCH=true cargo test --test sqllogictests
243243
- name: Verify Working Directory Clean
244244
run: git diff --exit-code
245245

@@ -270,7 +270,7 @@ jobs:
270270
rustup toolchain install stable
271271
rustup default stable
272272
- name: Run sqllogictest
273-
run: PG_COMPAT=true PG_URI="postgresql://postgres:postgres@localhost:$POSTGRES_PORT/db_test" cargo test -p datafusion --test sqllogictests
273+
run: PG_COMPAT=true PG_URI="postgresql://postgres:postgres@localhost:$POSTGRES_PORT/db_test" cargo test --features=postgres --test sqllogictests
274274
env:
275275
POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
276276

datafusion/core/Cargo.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ bigdecimal = "0.4.1"
102102
criterion = { version = "0.5", features = ["async_tokio"] }
103103
csv = "1.1.6"
104104
ctor = "0.2.0"
105-
datafusion-sqllogictest = { path = "../sqllogictest", version = "29.0.0", features = ["postgres"] }
106105
doc-comment = "0.3"
107106
env_logger = "0.10"
108107
half = "2.2.1"
@@ -111,7 +110,6 @@ postgres-types = { version = "0.2.4", features = ["derive", "with-chrono-0_4"] }
111110
regex = "1.5.4"
112111
rstest = "0.18.0"
113112
rust_decimal = { version = "1.27.0", features = ["tokio-pg"] }
114-
sqllogictest = "0.15.0"
115113
test-utils = { path = "../../test-utils" }
116114
thiserror = "1.0.37"
117115
tokio-postgres = "0.7.7"
@@ -161,8 +159,3 @@ name = "sql_query_with_io"
161159
[[bench]]
162160
harness = false
163161
name = "sort"
164-
165-
[[test]]
166-
harness = false
167-
name = "sqllogictests"
168-
path = "tests/sqllogictests/src/main.rs"

datafusion/core/tests/sqllogictests/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!---
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
The SQL Logic Test code has moved to `datafusion/sqllogictest`

datafusion/sqllogictest/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.py
2+
test_files/tpch/data

datafusion/sqllogictest/Cargo.toml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,25 @@ rust_decimal = {version = "1.27.0"}
4444
log = "^0.4"
4545
sqllogictest = "0.15.0"
4646
sqlparser.workspace = true
47+
tempfile = "3"
4748
thiserror = "1.0.44"
4849
tokio = {version = "1.0"}
4950
bytes = {version = "1.4.0", optional = true}
50-
futures = {version = "0.3.28", optional = true}
51+
futures = {version = "0.3.28"}
5152
chrono = {version = "0.4.26", optional = true}
5253
tokio-postgres = {version = "0.7.7", optional = true}
5354
postgres-types = {version = "0.2.4", optional = true}
5455
postgres-protocol = {version = "0.6.4", optional = true}
5556

5657
[features]
57-
postgres = ["bytes", "futures", "chrono", "tokio-postgres", "postgres-types", "postgres-protocol"]
58+
postgres = ["bytes", "chrono", "tokio-postgres", "postgres-types", "postgres-protocol"]
59+
60+
[dev-dependencies]
61+
env_logger = "0.10"
62+
num_cpus = "1.13.0"
63+
64+
65+
[[test]]
66+
harness = false
67+
name = "sqllogictests"
68+
path = "bin/sqllogictests.rs"

datafusion/core/tests/sqllogictests/README.md renamed to datafusion/sqllogictest/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ in [`test_files`](test_files).
3333

3434
```shell
3535
# Run all tests
36-
cargo test -p datafusion --test sqllogictests
36+
cargo test --test sqllogictests
3737
```
3838

3939
```shell
4040
# Run all tests, with debug logging enabled
41-
RUST_LOG=debug cargo test -p datafusion --test sqllogictests
41+
RUST_LOG=debug cargo test --test sqllogictests
4242
```
4343

4444
```shell
4545
# Run only the tests in `information_schema.slt`
46-
cargo test -p datafusion --test sqllogictests -- information_schema
46+
cargo test --test sqllogictests -- information_schema
4747
```
4848

4949
```shell
5050
# Automatically update ddl.slt with expected output
51-
cargo test -p datafusion --test sqllogictests -- ddl --complete
51+
cargo test --test sqllogictests -- ddl --complete
5252
```
5353

5454
```shell
5555
# Run ddl.slt, printing debug logging to stdout
56-
RUST_LOG=debug cargo test -p datafusion --test sqllogictests -- ddl
56+
RUST_LOG=debug cargo test --test sqllogictests -- ddl
5757
```
5858

5959
#### Cookbook: Adding Tests
@@ -76,7 +76,7 @@ SELECT * from foo;
7676
Running the following command will update `my_awesome_test.slt` with the expected output:
7777

7878
```shell
79-
cargo test -p datafusion --test sqllogictests -- my_awesome_test --complete
79+
cargo test --test sqllogictests -- my_awesome_test --complete
8080
```
8181

8282
3. Verify the content
@@ -105,14 +105,14 @@ file to the output produced by that run.
105105
For example, to run all tests suites in validation mode
106106

107107
```shell
108-
cargo test -p datafusion --test sqllogictests
108+
cargo test --test sqllogictests
109109
```
110110

111111
sqllogictests also supports `cargo test` style substring matches on file names to restrict which tests to run
112112

113113
```shell
114114
# information_schema.slt matches due to substring matching `information`
115-
cargo test -p datafusion --test sqllogictests -- information
115+
cargo test --test sqllogictests -- information
116116
```
117117

118118
#### Running tests: Postgres compatibility
@@ -123,7 +123,7 @@ with Postgres by running the same script files both with DataFusion and with Pos
123123
In order to run the sqllogictests running against a previously running Postgres instance, do:
124124

125125
```shell
126-
PG_COMPAT=true PG_URI="postgresql://[email protected]/postgres" cargo test -p datafusion --test sqllogictests
126+
PG_COMPAT=true PG_URI="postgresql://[email protected]/postgres" cargo test --features=postgres --test sqllogictests
127127
```
128128

129129
The environemnt variables:
@@ -153,15 +153,16 @@ command to generate tpch data, assuming you are in the repository
153153
root:
154154

155155
```shell
156+
mkdir -p datafusion/sqllogictest/test_files/tpch/data
156157
docker run -it \
157-
-v "$(realpath datafusion/core/tests/sqllogictests/test_files/tpch/data)":/data \
158+
-v "$(realpath datafusion/sqllogictest/test_files/tpch/data)":/data \
158159
ghcr.io/databloom-ai/tpch-docker:main -vf -s 0.1
159160
```
160161

161162
Then you need to add `INCLUDE_TPCH=true` to run tpch tests:
162163

163164
```shell
164-
INCLUDE_TPCH=true cargo test -p datafusion --test sqllogictests
165+
INCLUDE_TPCH=true cargo test --test sqllogictests
165166
```
166167

167168
#### Updating tests: Completion Mode
@@ -173,7 +174,7 @@ You can update the tests / generate expected output by passing the `--complete`
173174

174175
```shell
175176
# Update ddl.slt with output from running
176-
cargo test -p datafusion --test sqllogictests -- ddl --complete
177+
cargo test --test sqllogictests -- ddl --complete
177178
```
178179

179180
#### sqllogictests

datafusion/core/tests/sqllogictests/src/main.rs renamed to datafusion/sqllogictest/bin/sqllogictests.rs

Lines changed: 12 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ use std::path::{Path, PathBuf};
2020
#[cfg(target_family = "windows")]
2121
use std::thread;
2222

23-
use datafusion_sqllogictest::{DataFusion, Postgres};
23+
use datafusion_sqllogictest::{DataFusion, TestContext};
2424
use futures::stream::StreamExt;
2525
use log::info;
2626
use sqllogictest::strict_column_validator;
27-
use tempfile::TempDir;
2827

29-
use datafusion::prelude::{SessionConfig, SessionContext};
3028
use datafusion_common::{DataFusionError, Result};
3129

32-
mod setup;
33-
34-
const TEST_DIRECTORY: &str = "tests/sqllogictests/test_files/";
30+
const TEST_DIRECTORY: &str = "test_files/";
3531
const PG_COMPAT_FILE_PREFIX: &str = "pg_compat_";
3632

3733
#[cfg(target_family = "windows")]
@@ -121,7 +117,7 @@ async fn run_test_file(test_file: TestFile) -> Result<()> {
121117
relative_path,
122118
} = test_file;
123119
info!("Running with DataFusion runner: {}", path.display());
124-
let Some(test_ctx) = context_for_test_file(&relative_path).await else {
120+
let Some(test_ctx) = TestContext::try_new_for_test_file(&relative_path).await else {
125121
info!("Skipping: {}", path.display());
126122
return Ok(());
127123
};
@@ -138,7 +134,9 @@ async fn run_test_file(test_file: TestFile) -> Result<()> {
138134
.map_err(|e| DataFusionError::External(Box::new(e)))
139135
}
140136

137+
#[cfg(feature = "postgres")]
141138
async fn run_test_file_with_postgres(test_file: TestFile) -> Result<()> {
139+
use datafusion_sqllogictest::Postgres;
142140
let TestFile {
143141
path,
144142
relative_path,
@@ -154,6 +152,12 @@ async fn run_test_file_with_postgres(test_file: TestFile) -> Result<()> {
154152
Ok(())
155153
}
156154

155+
#[cfg(not(feature = "postgres"))]
156+
async fn run_test_file_with_postgres(_test_file: TestFile) -> Result<()> {
157+
use datafusion_common::plan_err;
158+
plan_err!("Can not run with postgres as postgres feature is not enabled")
159+
}
160+
157161
async fn run_complete_file(test_file: TestFile) -> Result<()> {
158162
let TestFile {
159163
path,
@@ -163,7 +167,7 @@ async fn run_complete_file(test_file: TestFile) -> Result<()> {
163167

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

166-
let Some(test_ctx) = context_for_test_file(&relative_path).await else {
170+
let Some(test_ctx) = TestContext::try_new_for_test_file(&relative_path).await else {
167171
info!("Skipping: {}", path.display());
168172
return Ok(());
169173
};
@@ -250,96 +254,6 @@ fn read_dir_recursive<P: AsRef<Path>>(path: P) -> Box<dyn Iterator<Item = PathBu
250254
)
251255
}
252256

253-
/// Create a SessionContext, configured for the specific test, if
254-
/// possible.
255-
///
256-
/// If `None` is returned (e.g. because some needed feature is not
257-
/// enabled), the file should be skipped
258-
async fn context_for_test_file(relative_path: &Path) -> Option<TestContext> {
259-
let config = SessionConfig::new()
260-
// hardcode target partitions so plans are deterministic
261-
.with_target_partitions(4);
262-
263-
let test_ctx = TestContext::new(SessionContext::with_config(config));
264-
265-
let file_name = relative_path.file_name().unwrap().to_str().unwrap();
266-
match file_name {
267-
"scalar.slt" => {
268-
info!("Registering scalar tables");
269-
setup::register_scalar_tables(test_ctx.session_ctx()).await;
270-
}
271-
"information_schema_table_types.slt" => {
272-
info!("Registering local temporary table");
273-
setup::register_temp_table(test_ctx.session_ctx()).await;
274-
}
275-
"information_schema_columns.slt" => {
276-
info!("Registering table with many types");
277-
setup::register_table_with_many_types(test_ctx.session_ctx()).await;
278-
}
279-
"avro.slt" => {
280-
#[cfg(feature = "avro")]
281-
{
282-
let mut test_ctx = test_ctx;
283-
info!("Registering avro tables");
284-
setup::register_avro_tables(&mut test_ctx).await;
285-
return Some(test_ctx);
286-
}
287-
#[cfg(not(feature = "avro"))]
288-
{
289-
info!("Skipping {file_name} because avro feature is not enabled");
290-
return None;
291-
}
292-
}
293-
"joins.slt" => {
294-
info!("Registering partition table tables");
295-
296-
let mut test_ctx = test_ctx;
297-
setup::register_partition_table(&mut test_ctx).await;
298-
return Some(test_ctx);
299-
}
300-
_ => {
301-
info!("Using default SessionContext");
302-
}
303-
};
304-
Some(test_ctx)
305-
}
306-
307-
/// Context for running tests
308-
pub struct TestContext {
309-
/// Context for running queries
310-
ctx: SessionContext,
311-
/// Temporary directory created and cleared at the end of the test
312-
test_dir: Option<TempDir>,
313-
}
314-
315-
impl TestContext {
316-
pub fn new(ctx: SessionContext) -> Self {
317-
Self {
318-
ctx,
319-
test_dir: None,
320-
}
321-
}
322-
323-
/// Enables the test directory feature. If not enabled,
324-
/// calling `testdir_path` will result in a panic.
325-
pub fn enable_testdir(&mut self) {
326-
if self.test_dir.is_none() {
327-
self.test_dir = Some(TempDir::new().expect("failed to create testdir"));
328-
}
329-
}
330-
331-
/// Returns the path to the test directory. Panics if the test
332-
/// directory feature is not enabled via `enable_testdir`.
333-
pub fn testdir_path(&self) -> &Path {
334-
self.test_dir.as_ref().expect("testdir not enabled").path()
335-
}
336-
337-
/// Returns a reference to the internal SessionContext
338-
fn session_ctx(&self) -> &SessionContext {
339-
&self.ctx
340-
}
341-
}
342-
343257
/// Parsed command line options
344258
struct Options {
345259
// regex like

datafusion/sqllogictest/src/engines/conversion.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub(crate) fn i128_to_str(value: i128, precision: &u8, scale: &i8) -> String {
8282
)
8383
}
8484

85+
#[cfg(feature = "postgres")]
8586
pub(crate) fn decimal_to_str(value: Decimal) -> String {
8687
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap())
8788
}

datafusion/sqllogictest/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
//! DataFusion sqllogictest driver
19+
1820
mod engines;
1921

2022
pub use engines::DataFusion;
2123

2224
#[cfg(feature = "postgres")]
2325
pub use engines::Postgres;
26+
27+
mod test_context;
28+
pub use test_context::TestContext;

0 commit comments

Comments
 (0)