Skip to content

Commit

Permalink
Fix sqllogictest when the expected output starts with a whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ggevay committed Jan 18, 2025
1 parent 2b20b11 commit d91e922
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
31 changes: 29 additions & 2 deletions src/sqllogictest/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,22 @@ impl<'a> Parser<'a> {
&EOF_REGEX
} else {
&DOUBLE_LINE_REGEX
})?
.trim_start();
})?;

// The `split_at(&QUERY_OUTPUT_REGEX)` stopped at the end of `----`, so `output_str` usually
// starts with a newline, which is not actually part of the expected output. Strip off this
// newline.
output_str = if let Some(output_str_stripped) = regexp_strip_prefix(output_str, &LINE_REGEX) {
output_str_stripped
} else {
// There should always be a newline after `----`, because we have a lint that there is
// always a newline at the end of a file. However, we can still get here, when
// the expected output is empty, in which case the EOF_REGEX or DOUBLE_LINE_REGEX eats
// the newline at the end of the `----`.
assert!(output_str.is_empty());
output_str
};

// We don't want to advance the expected output past the column names so rewriting works,
// but need to be able to parse past them, so remember the position before possible column
// names.
Expand Down Expand Up @@ -434,3 +448,16 @@ pub(crate) fn split_cols(line: &str, expected_columns: usize) -> Vec<&str> {
line.split_whitespace().collect()
}
}

pub fn regexp_strip_prefix<'a>(text: &'a str, regexp: &Regex) -> Option<&'a str> {
match regexp.find(text) {
Some(found) => {
if found.start() == 0 {
Some(&text[found.end()..])
} else {
None
}
}
None => None,
}
}
2 changes: 1 addition & 1 deletion test/sqllogictest/mztimestamp.slt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ SELECT 1::mz_catalog.mz_timestamp
query T
SELECT '1970-01-02'::date::mz_timestamp
----
86400000
86400000

# Casts to timestamp[tz]. 8210266815600000 is roughly `HIGH_DATE` for `CheckedTimestamp`.
query T
Expand Down
4 changes: 1 addition & 3 deletions test/sqllogictest/with_mutually_recursive.slt
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ WITH MUTUALLY RECURSIVE
SELECT x FROM bar

## Adapted from https://www.sqlite.org/lang_with.html#outlandish_recursive_query_examples
## The 'x' at the beginning is to work around an sqllogictest bug.
query T multiline
WITH MUTUALLY RECURSIVE
xaxis(x double) AS (VALUES(-2.0) UNION ALL SELECT x+0.05 FROM xaxis WHERE x<1.2),
Expand All @@ -689,9 +688,8 @@ WITH MUTUALLY RECURSIVE
SELECT string_agg( substr(' .+*#', 1+least(iter/7,4), 1), '' ORDER BY cx), cy
FROM m2 GROUP BY cy
)
SELECT 'x' || chr(10) || string_agg(rtrim(t), chr(10) ORDER BY cy) FROM a;
SELECT string_agg(rtrim(t), chr(10) ORDER BY cy) FROM a;
----
x
....#
..#*..
..+####+.
Expand Down

0 comments on commit d91e922

Please sign in to comment.