Skip to content

Commit

Permalink
feat: add sqllogictests crate (#7134)
Browse files Browse the repository at this point in the history
* feat: add sqllogictests crate

* feat: cleanup the PR from feedback

* fix: fix tests due to name issue

* refactor: cleanup

* refactor: chopped off license

* docs: license and module docstrings

* docs: module docstrings

* docs: update docs for cargo checks

* Fix doc error

---------

Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
tshauck and alamb committed Aug 2, 2023
1 parent eaacc5c commit f2dc6a3
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 90 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

[workspace]
exclude = ["datafusion-cli"]
members = ["datafusion/common", "datafusion/core", "datafusion/expr", "datafusion/execution", "datafusion/optimizer", "datafusion/physical-expr", "datafusion/proto", "datafusion/proto/gen", "datafusion/sql", "datafusion/substrait", "datafusion-examples", "test-utils", "benchmarks",
members = ["datafusion/common", "datafusion/core", "datafusion/expr", "datafusion/execution", "datafusion/optimizer", "datafusion/physical-expr", "datafusion/proto", "datafusion/proto/gen", "datafusion/sql", "datafusion/sqllogictest", "datafusion/substrait", "datafusion-examples", "test-utils", "benchmarks",
]
resolver = "2"

Expand Down Expand Up @@ -56,4 +56,4 @@ lto = false
opt-level = 3
overflow-checks = false
panic = 'unwind'
rpath = false
rpath = false
1 change: 1 addition & 0 deletions datafusion/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ bigdecimal = "0.4.1"
criterion = { version = "0.5", features = ["async_tokio"] }
csv = "1.1.6"
ctor = "0.2.0"
datafusion-sqllogictest = { path = "../sqllogictest", version = "28.0.0", features = ["postgres"] }
doc-comment = "0.3"
env_logger = "0.10"
half = "2.2.1"
Expand Down
55 changes: 0 additions & 55 deletions datafusion/core/tests/sqllogictests/src/engines/datafusion/util.rs

This file was deleted.

5 changes: 1 addition & 4 deletions datafusion/core/tests/sqllogictests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::path::{Path, PathBuf};
#[cfg(target_family = "windows")]
use std::thread;

use datafusion_sqllogictest::{DataFusion, Postgres};
use futures::stream::StreamExt;
use log::info;
use sqllogictest::strict_column_validator;
Expand All @@ -28,10 +29,6 @@ use tempfile::TempDir;
use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion_common::{DataFusionError, Result};

use crate::engines::datafusion::DataFusion;
use crate::engines::postgres::Postgres;

mod engines;
mod setup;

const TEST_DIRECTORY: &str = "tests/sqllogictests/test_files/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ statement ok
drop table t1

statement ok
drop table t2
drop table t2
57 changes: 57 additions & 0 deletions datafusion/sqllogictest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
name = "datafusion-sqllogictest"
readme.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[lib]
name = "datafusion_sqllogictest"
path = "src/lib.rs"

[dependencies]
arrow = {workspace = true}
async-trait = "0.1.41"
bigdecimal = "0.4.1"
datafusion = {path = "../core", version = "28.0.0"}
datafusion-common = {path = "../common", version = "28.0.0"}
half = "2.2.1"
itertools = "0.11"
lazy_static = {version = "^1.4.0"}
object_store = "0.6.1"
rust_decimal = {version = "1.27.0"}
log = "^0.4"
sqllogictest = "0.15.0"
sqlparser.workspace = true
thiserror = "1.0.44"
tokio = {version = "1.0"}
bytes = {version = "1.4.0", optional = true}
futures = {version = "0.3.28", optional = true}
chrono = {version = "0.4.26", optional = true}
tokio-postgres = {version = "0.7.7", optional = true}
postgres-types = {version = "0.2.4", optional = true}
postgres-protocol = {version = "0.6.4", optional = true}

[features]
postgres = ["bytes", "futures", "chrono", "tokio-postgres", "postgres-types", "postgres-protocol"]
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ use arrow::datatypes::{Decimal128Type, DecimalType};
use bigdecimal::BigDecimal;
use half::f16;
use rust_decimal::prelude::*;
use rust_decimal::Decimal;

/// Represents a constant for NULL string in your database.
pub const NULL_STR: &str = "NULL";

pub fn bool_to_str(value: bool) -> String {
pub(crate) fn bool_to_str(value: bool) -> String {
if value {
"true".to_string()
} else {
"false".to_string()
}
}

pub fn varchar_to_str(value: &str) -> String {
pub(crate) fn varchar_to_str(value: &str) -> String {
if value.is_empty() {
"(empty)".to_string()
} else {
value.trim_end_matches('\n').to_string()
}
}

pub fn f16_to_str(value: f16) -> String {
pub(crate) fn f16_to_str(value: f16) -> String {
if value.is_nan() {
"NaN".to_string()
} else if value == f16::INFINITY {
Expand All @@ -51,7 +51,7 @@ pub fn f16_to_str(value: f16) -> String {
}
}

pub fn f32_to_str(value: f32) -> String {
pub(crate) fn f32_to_str(value: f32) -> String {
if value.is_nan() {
"NaN".to_string()
} else if value == f32::INFINITY {
Expand All @@ -63,7 +63,7 @@ pub fn f32_to_str(value: f32) -> String {
}
}

pub fn f64_to_str(value: f64) -> String {
pub(crate) fn f64_to_str(value: f64) -> String {
if value.is_nan() {
"NaN".to_string()
} else if value == f64::INFINITY {
Expand All @@ -75,17 +75,17 @@ pub fn f64_to_str(value: f64) -> String {
}
}

pub fn i128_to_str(value: i128, precision: &u8, scale: &i8) -> String {
pub(crate) fn i128_to_str(value: i128, precision: &u8, scale: &i8) -> String {
big_decimal_to_str(
BigDecimal::from_str(&Decimal128Type::format_decimal(value, *precision, *scale))
.unwrap(),
)
}

pub fn decimal_to_str(value: Decimal) -> String {
pub(crate) fn decimal_to_str(value: Decimal) -> String {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap())
}

pub fn big_decimal_to_str(value: BigDecimal) -> String {
pub(crate) fn big_decimal_to_str(value: BigDecimal) -> String {
value.round(12).normalized().to_string()
}
25 changes: 25 additions & 0 deletions datafusion/sqllogictest/src/engines/datafusion_engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

/// DataFusion engine implementation for sqllogictest.
mod error;
mod normalize;
mod runner;

pub use error::*;
pub use normalize::*;
pub use runner::*;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::super::conversion::*;
use super::error::{DFSqlLogicTestError, Result};

/// Converts `batches` to a result as expected by sqllogicteset.
pub fn convert_batches(batches: Vec<RecordBatch>) -> Result<Vec<Vec<String>>> {
pub(crate) fn convert_batches(batches: Vec<RecordBatch>) -> Result<Vec<Vec<String>>> {
if batches.is_empty() {
Ok(vec![])
} else {
Expand Down Expand Up @@ -113,13 +113,13 @@ fn expand_row(mut row: Vec<String>) -> impl Iterator<Item = Vec<String>> {

/// normalize path references
///
/// ```
/// ```text
/// CsvExec: files={1 group: [[path/to/datafusion/testing/data/csv/aggregate_test_100.csv]]}, ...
/// ```
///
/// into:
///
/// ```
/// ```text
/// CsvExec: files={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, ...
/// ```
fn normalize_paths(mut row: Vec<String>) -> Vec<String> {
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn cell_to_string(col: &ArrayRef, row: usize) -> Result<String> {
}

/// Converts columns to a result as expected by sqllogicteset.
pub fn convert_schema_to_types(columns: &[DFField]) -> Vec<DFColumnType> {
pub(crate) fn convert_schema_to_types(columns: &[DFField]) -> Vec<DFColumnType> {
columns
.iter()
.map(|f| f.data_type())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
// specific language governing permissions and limitations
// under the License.

use std::path::PathBuf;
use std::time::Duration;
use std::{path::PathBuf, time::Duration};

use crate::engines::output::{DFColumnType, DFOutput};

use self::error::{DFSqlLogicTestError, Result};
use arrow::record_batch::RecordBatch;
use async_trait::async_trait;
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::prelude::SessionContext;
use log::info;
use sqllogictest::DBOutput;

mod error;
mod normalize;
mod util;
use super::{error::Result, normalize, DFSqlLogicTestError};

use crate::engines::output::{DFColumnType, DFOutput};

pub struct DataFusion {
ctx: SessionContext,
Expand Down Expand Up @@ -61,7 +57,7 @@ impl sqllogictest::AsyncDB for DataFusion {
"DataFusion"
}

/// [`Runner`] calls this function to perform sleep.
/// [`DataFusion`] calls this function to perform sleep.
///
/// The default implementation is `std::thread::sleep`, which is universal to any async runtime
/// but would block the current thread. If you are running in tokio runtime, you should override
Expand Down
29 changes: 29 additions & 0 deletions datafusion/sqllogictest/src/engines/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

/// Implementation of sqllogictest for datafusion.
mod conversion;
mod datafusion_engine;
mod output;

pub use datafusion_engine::DataFusion;

#[cfg(feature = "postgres")]
mod postgres_engine;

#[cfg(feature = "postgres")]
pub use postgres_engine::Postgres;
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ impl ColumnType for DFColumnType {
}
}

pub type DFOutput = DBOutput<DFColumnType>;
pub(crate) type DFOutput = DBOutput<DFColumnType>;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

/// Postgres engine implementation for sqllogictest.
use std::path::{Path, PathBuf};
use std::str::FromStr;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// specific language governing permissions and limitations
// under the License.

mod conversion;
pub mod datafusion;
mod output;
pub mod postgres;
mod engines;

pub use engines::DataFusion;

#[cfg(feature = "postgres")]
pub use engines::Postgres;

0 comments on commit f2dc6a3

Please sign in to comment.