Skip to content

Commit

Permalink
Customizable types follow up: complete types in update_test_file (#164)
Browse files Browse the repository at this point in the history
* Custom types follow up: complete types in `update_test_file`

Signed-off-by: Yevhenii Melnyk <melnyk.yevhenii@gmail.com>

* Check types when there are no results

Signed-off-by: Yevhenii Melnyk <melnyk.yevhenii@gmail.com>

* Use strict validator

Signed-off-by: Yevhenii Melnyk <melnyk.yevhenii@gmail.com>

---------

Signed-off-by: Yevhenii Melnyk <melnyk.yevhenii@gmail.com>
  • Loading branch information
melgenek committed Feb 15, 2023
1 parent 30ba82f commit 30b83ff
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 22 deletions.
5 changes: 5 additions & 0 deletions examples/custom_type/custom_type.slt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ select * from example_typed
1 true
2 false
3 true


query IB
select * from no_results
----
5 changes: 5 additions & 0 deletions examples/custom_type/examples/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ impl sqllogictest::DB for FakeDB {
vec!["3".to_string(), "true".to_string()],
],
})
} else if sql == "select * from no_results" {
Ok(DBOutput::Rows {
types: vec![CustomColumnType::Integer, CustomColumnType::Boolean],
rows: vec![],
})
} else {
Err(FakeDBError)
}
Expand Down
11 changes: 9 additions & 2 deletions sqllogictest-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use itertools::Itertools;
use quick_junit::{NonSuccessKind, Report, TestCase, TestCaseStatus, TestSuite};
use rand::seq::SliceRandom;
use sqllogictest::{
default_validator, update_record_with_output, AsyncDB, Injected, Record, Runner,
default_validator, strict_column_validator, update_record_with_output, AsyncDB, Injected,
Record, Runner,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq, ArgEnum)]
Expand Down Expand Up @@ -684,7 +685,13 @@ async fn update_record<D: AsyncDB>(
}

let record_output = runner.apply_record(record.clone()).await;
match update_record_with_output(&record, &record_output, "\t", default_validator) {
match update_record_with_output(
&record,
&record_output,
"\t",
default_validator,
strict_column_validator,
) {
Some(new_record) => {
writeln!(outfile, "{new_record}")?;
}
Expand Down
28 changes: 26 additions & 2 deletions sqllogictest/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ fn parse_inner<T: ColumnType>(loc: &Location, script: &str) -> Result<Vec<Record
.map_err(|e| e.at(loc.clone()))?;
label = res.get(1).map(|s| s.to_string());
}
_ => return Err(ParseErrorKind::InvalidLine(line.into()).at(loc)),
[] => {}
}

// The SQL for the query is found on second an subsequent lines of the record
Expand Down Expand Up @@ -710,6 +710,30 @@ select * from unknown_type
assert_eq!(error_kind, ParseErrorKind::InvalidType('A'));
}

#[test]
fn test_parse_no_types() {
let script = "\
query
select * from foo;
----
";
let records = parse::<DefaultColumnType>(script).unwrap();

assert_eq!(
records,
vec![Record::Query {
loc: Location::new("<unknown>", 1),
conditions: vec![],
expected_types: vec![],
sort_mode: None,
label: None,
expected_error: None,
sql: "select * from foo;".to_string(),
expected_results: vec![],
}]
);
}

/// Verifies Display impl is consistent with parsing by ensuring
/// roundtrip parse(unparse(parse())) is consistent
fn parse_roundtrip<T: ColumnType>(filename: impl AsRef<Path>) {
Expand All @@ -723,7 +747,7 @@ select * from unknown_type

let output_contents = unparsed.join("\n");

// The orignal and parsed records should be logically requivalent
// The original and parsed records should be logically equivalent
let mut output_file = tempfile::NamedTempFile::new().expect("Error creating tempfile");
output_file
.write_all(output_contents.as_bytes())
Expand Down
77 changes: 59 additions & 18 deletions sqllogictest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ impl<D: AsyncDB> Runner<D> {
filename: impl AsRef<Path>,
col_separator: &str,
validator: Validator,
column_type_validator: ColumnTypeValidator<D::ColumnType>,
) -> Result<(), Box<dyn std::error::Error>> {
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::PathBuf;
Expand Down Expand Up @@ -1024,6 +1025,7 @@ impl<D: AsyncDB> Runner<D> {
&record_output,
col_separator,
validator,
column_type_validator,
)
.unwrap_or(record);
writeln!(outfile, "{record}")?;
Expand Down Expand Up @@ -1052,6 +1054,7 @@ pub fn update_record_with_output<T: ColumnType>(
record_output: &RecordOutput<T>,
col_separator: &str,
validator: Validator,
column_type_validator: ColumnTypeValidator<T>,
) -> Option<Record<T>> {
match (record.clone(), record_output) {
(_, RecordOutput::Nothing) => None,
Expand Down Expand Up @@ -1147,13 +1150,7 @@ pub fn update_record_with_output<T: ColumnType>(
sql,
expected_results,
},
RecordOutput::Query {
// FIXME: maybe we should use output's types instead of orignal query's types
// Fix it after https://github.com/risinglightdb/sqllogictest-rs/issues/36 is resolved.
types: _,
rows,
error,
},
RecordOutput::Query { types, rows, error },
) => {
match (error, expected_error) {
(None, _) => {}
Expand Down Expand Up @@ -1192,12 +1189,19 @@ pub fn update_record_with_output<T: ColumnType>(
rows.iter().map(|cols| cols.join(col_separator)).collect()
};

let types = if column_type_validator(types, &expected_types) {
// If validation is successful, we respect the original file's expected types.
expected_types
} else {
types.clone()
};

Some(Record::Query {
sql,
expected_error: None,
loc,
conditions,
expected_types,
expected_types: types,
sort_mode,
label,
expected_results: results,
Expand All @@ -1214,6 +1218,27 @@ mod tests {
use super::*;
use crate::DefaultColumnType;

#[test]
fn test_query_replacement_no_changes() {
let record = "query I?\n\
select * from foo;\n\
----\n\
3 4";
TestCase {
// keep the input values
input: record,

// Model a run that produced a 3,4 as output
record_output: query_output(
&[&["3", "4"]],
vec![DefaultColumnType::Integer, DefaultColumnType::Any],
),

expected: Some(record),
}
.run()
}

#[test]
fn test_query_replacement() {
TestCase {
Expand All @@ -1224,10 +1249,13 @@ mod tests {
1 2",

// Model a run that produced a 3,4 as output
record_output: query_output(&[&["3", "4"]]),
record_output: query_output(
&[&["3", "4"]],
vec![DefaultColumnType::Integer, DefaultColumnType::Any],
),

expected: Some(
"query III\n\
"query I?\n\
select * from foo;\n\
----\n\
3 4",
Expand All @@ -1240,15 +1268,18 @@ mod tests {
fn test_query_replacement_no_input() {
TestCase {
// input has no query results
input: "query III\n\
input: "query\n\
select * from foo;\n\
----",

// Model a run that produced a 3,4 as output
record_output: query_output(&[&["3", "4"]]),
record_output: query_output(
&[&["3", "4"]],
vec![DefaultColumnType::Integer, DefaultColumnType::Any],
),

expected: Some(
"query III\n\
"query I?\n\
select * from foo;\n\
----\n\
3 4",
Expand Down Expand Up @@ -1301,7 +1332,10 @@ mod tests {
create table foo;",

// Model a run that produced a 3,4 as output
record_output: query_output(&[&["3", "4"]]),
record_output: query_output(
&[&["3", "4"]],
vec![DefaultColumnType::Integer, DefaultColumnType::Any],
),

expected: Some(
"statement ok\n\
Expand Down Expand Up @@ -1448,7 +1482,13 @@ mod tests {
println!("**expected:\n{}\n", expected.unwrap_or(""));
let input = parse_to_record(input);
let expected = expected.map(parse_to_record);
let output = update_record_with_output(&input, &record_output, " ", default_validator);
let output = update_record_with_output(
&input,
&record_output,
" ",
default_validator,
strict_column_validator,
);

assert_eq!(
&output,
Expand All @@ -1473,14 +1513,15 @@ mod tests {
}

/// Returns a RecordOutput that models the successful execution of a query
fn query_output(rows: &[&[&str]]) -> RecordOutput<DefaultColumnType> {
fn query_output(
rows: &[&[&str]],
types: Vec<DefaultColumnType>,
) -> RecordOutput<DefaultColumnType> {
let rows = rows
.iter()
.map(|cols| cols.iter().map(|c| c.to_string()).collect::<Vec<_>>())
.collect::<Vec<_>>();

let types = rows.iter().map(|_| DefaultColumnType::Any).collect();

RecordOutput::Query {
types,
rows,
Expand Down

0 comments on commit 30b83ff

Please sign in to comment.