Skip to content

bump sqllogictest to 0.9.0 #4547

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

Merged
merged 5 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions datafusion-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion datafusion/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ doc-comment = "0.3"
env_logger = "0.10"
parquet-test-utils = { path = "../../parquet-test-utils" }
rstest = "0.16.0"
sqllogictest = "0.8.0"
sqllogictest = "0.9.0"
sqlparser = "0.27"
test-utils = { path = "../../test-utils" }

Expand Down
6 changes: 4 additions & 2 deletions datafusion/core/tests/sqllogictests/src/insert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use datafusion::prelude::SessionContext;
use datafusion_common::{DFSchema, DataFusionError};
use datafusion_expr::Expr as DFExpr;
use datafusion_sql::planner::{object_name_to_table_reference, PlannerContext, SqlToRel};
use sqllogictest::DBOutput;
use sqlparser::ast::{Expr, SetExpr, Statement as SQLStatement};
use std::sync::Arc;

pub async fn insert(ctx: &SessionContext, insert_stmt: SQLStatement) -> Result<String> {
pub async fn insert(ctx: &SessionContext, insert_stmt: SQLStatement) -> Result<DBOutput> {
// First, use sqlparser to get table name and insert values
let table_reference;
let insert_values: Vec<Vec<Expr>>;
Expand Down Expand Up @@ -60,6 +61,7 @@ pub async fn insert(ctx: &SessionContext, insert_stmt: SQLStatement) -> Result<S
// Third, transfer insert values to `RecordBatch`
// Attention: schema info can be ignored. (insert values don't contain schema info)
let sql_to_rel = SqlToRel::new(&LogicTestContextProvider {});
let num_rows = insert_values.len();
for row in insert_values.into_iter() {
let logical_exprs = row
.into_iter()
Expand All @@ -85,5 +87,5 @@ pub async fn insert(ctx: &SessionContext, insert_stmt: SQLStatement) -> Result<S
let new_provider = MemTable::try_new(schema, vec![origin_batches])?;
ctx.register_table(&table_reference, Arc::new(new_provider))?;

Ok("".to_string())
Ok(DBOutput::StatementComplete(num_rows as u64))
}
36 changes: 30 additions & 6 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 datafusion::prelude::{SessionConfig, SessionContext};
use datafusion_sql::parser::{DFParser, Statement};
use log::info;
use normalize::normalize_batch;
use sqllogictest::{ColumnType, DBOutput};
use sqlparser::ast::Statement as SQLStatement;
use std::path::{Path, PathBuf};
use std::time::Duration;
Expand All @@ -46,7 +47,7 @@ pub struct DataFusion {
impl sqllogictest::AsyncDB for DataFusion {
type Error = DFSqlLogicTestError;

async fn run(&mut self, sql: &str) -> Result<String> {
async fn run(&mut self, sql: &str) -> Result<DBOutput> {
println!("[{}] Running query: \"{}\"", self.file_name, sql);
let result = run_query(&self.ctx, sql).await?;
Ok(result)
Expand Down Expand Up @@ -172,19 +173,42 @@ async fn context_for_test_file(file_name: &str) -> SessionContext {
}
}

fn format_batches(batches: Vec<RecordBatch>) -> Result<String> {
fn convert_batches(batches: Vec<RecordBatch>) -> Result<DBOutput> {
let mut bytes = vec![];
if batches.is_empty() {
return Ok(DBOutput::StatementComplete(0));
}
// TODO: use the actual types
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 thanks -- we can fill this in

let types = vec![ColumnType::Any; batches[0].num_columns()];

{
let builder = WriterBuilder::new().has_headers(false).with_delimiter(b' ');
let builder = WriterBuilder::new()
.has_headers(false)
.with_delimiter(b'\t');
let mut writer = builder.build(&mut bytes);
for batch in batches {
writer.write(&normalize_batch(batch)).unwrap();
}
}
Ok(String::from_utf8(bytes).unwrap())
let res = String::from_utf8(bytes).unwrap();
let rows = res
.lines()
.map(|s| {
s.split('\t')
.map(|s| {
if s.is_empty() {
"NULL".to_string()
} else {
s.to_string()
}
})
.collect()
})
.collect();
Ok(DBOutput::Rows { types, rows })
}

async fn run_query(ctx: &SessionContext, sql: impl Into<String>) -> Result<String> {
async fn run_query(ctx: &SessionContext, sql: impl Into<String>) -> Result<DBOutput> {
let sql = sql.into();
// Check if the sql is `insert`
if let Ok(mut statements) = DFParser::parse_sql(&sql) {
Expand All @@ -198,6 +222,6 @@ async fn run_query(ctx: &SessionContext, sql: impl Into<String>) -> Result<Strin
}
let df = ctx.sql(sql.as_str()).await?;
let results: Vec<RecordBatch> = df.collect().await?;
let formatted_batches = format_batches(results)?;
let formatted_batches = convert_batches(results)?;
Ok(formatted_batches)
}
6 changes: 2 additions & 4 deletions datafusion/core/tests/sqllogictests/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ use arrow::{
///
/// This is to make the output comparable to the semi-standard .slt format
///
/// Normalizations applied:
/// 1. Null Values (TODO)
/// 2. [Empty Strings]
/// Normalizations applied to [NULL Values and empty strings]
///
/// [Empty Strings]: https://duckdb.org/dev/sqllogictest/result_verification#null-values-and-empty-strings
/// [NULL Values and empty strings]: https://duckdb.org/dev/sqllogictest/result_verification#null-values-and-empty-strings
pub fn normalize_batch(batch: RecordBatch) -> RecordBatch {
let new_columns = batch
.columns()
Expand Down
Loading