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

feat: add sqllogictests crate #7134

Merged
merged 10 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
47 changes: 47 additions & 0 deletions datafusion/sqllogictest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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"}
sqllogictest = "0.15.0"
sqlparser.workspace = true
thiserror = "1.0.44"
tokio = {version = "1.0"}
125 changes: 125 additions & 0 deletions datafusion/sqllogictest/src/engines/conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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.

use arrow::datatypes::{Decimal128Type, DecimalType};
use bigdecimal::BigDecimal;
use half::f16;
use rust_decimal::prelude::*;

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

/// Converts a bool value into a string.
///
/// # Arguments
///
/// * `value` - The bool value to convert.
pub fn bool_to_str(value: bool) -> String {
if value {
"true".to_string()
} else {
"false".to_string()
}
}

/// Converts a varchar into a string, trimming end line breaks.
/// Returns "(empty)" string for empty input.
///
/// # Arguments
///
/// * `value` - The varchar value to convert.
pub fn varchar_to_str(value: &str) -> String {
if value.is_empty() {
"(empty)".to_string()
} else {
value.trim_end_matches('\n').to_string()
}
}

/// Converts a 16-bit floating-point number into a string.
///
/// # Arguments
///
/// * `value` - The 16-bit floating-point number to convert.
pub fn f16_to_str(value: f16) -> String {
if value.is_nan() {
"NaN".to_string()
} else if value == f16::INFINITY {
"Infinity".to_string()
} else if value == f16::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap())
}
}

/// Converts a 32-bit floating-point number into a string.
///
/// # Arguments
///
/// * `value` - The 32-bit floating-point number to convert.
pub fn f32_to_str(value: f32) -> String {
if value.is_nan() {
"NaN".to_string()
} else if value == f32::INFINITY {
"Infinity".to_string()
} else if value == f32::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap())
}
}

/// Converts a 64-bit floating-point number into a string.
///
/// # Arguments
///
/// * `value` - The 64-bit floating-point number to convert.
pub fn f64_to_str(value: f64) -> String {
if value.is_nan() {
"NaN".to_string()
} else if value == f64::INFINITY {
"Infinity".to_string()
} else if value == f64::NEG_INFINITY {
"-Infinity".to_string()
} else {
big_decimal_to_str(BigDecimal::from_str(&value.to_string()).unwrap())
}
}

/// Converts a 128-bit integer into a string using specified precision and scale.
///
/// # Arguments
///
/// * `value` - The 128-bit integer to convert.
/// * `precision` - The number of significant digits.
/// * `scale` - The number of digits to the right of the decimal point.
pub 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(),
)
}

/// Converts a BigDecimal into a string, rounding the result to 12 decimal places.
///
/// # Arguments
///
/// * `value` - The BigDecimal value to convert.
pub fn big_decimal_to_str(value: BigDecimal) -> String {
value.round(12).normalized().to_string()
}
50 changes: 50 additions & 0 deletions datafusion/sqllogictest/src/engines/datafusion_engine/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.

use arrow::error::ArrowError;
use datafusion_common::DataFusionError;
use sqllogictest::TestError;
use sqlparser::parser::ParserError;
use thiserror::Error;

pub type Result<T, E = DFSqlLogicTestError> = std::result::Result<T, E>;

/// DataFusion sql-logicaltest error
#[derive(Debug, Error)]
pub enum DFSqlLogicTestError {
/// Error from sqllogictest-rs
#[error("SqlLogicTest error(from sqllogictest-rs crate): {0}")]
SqlLogicTest(#[from] TestError),
/// Error from datafusion
#[error("DataFusion error: {0}")]
DataFusion(#[from] DataFusionError),
/// Error returned when SQL is syntactically incorrect.
#[error("SQL Parser error: {0}")]
Sql(#[from] ParserError),
/// Error from arrow-rs
#[error("Arrow error: {0}")]
Arrow(#[from] ArrowError),
/// Generic error
#[error("Other Error: {0}")]
Other(String),
}

impl From<String> for DFSqlLogicTestError {
fn from(value: String) -> Self {
DFSqlLogicTestError::Other(value)
}
}
7 changes: 7 additions & 0 deletions datafusion/sqllogictest/src/engines/datafusion_engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file needs the apache license header (just copy/paste from another crate)

Also if you could add some high level crate comment like

/// DataFusion driver for sqllogictest

or something I think that would help future readers

mod normalize;
mod runner;

pub use error::*;
pub use normalize::*;
pub use runner::*;
Loading
Loading