-
Notifications
You must be signed in to change notification settings - Fork 664
chore(sqlparser): add do-apply-parser-test #8486
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f717884
chore(sqlparser): add a tool do-apply-parser-test
TennyZhuang 8a510a7
Merge remote-tracking branch 'origin/main' into chore/do-apply-parser…
TennyZhuang f32b9d2
Update src/sqlparser/tests/testdata/.gitignore
TennyZhuang a632b98
Update src/sqlparser/test_runner/sqlparser_test.toml
TennyZhuang 26052ba
fix ut
TennyZhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
[tasks.update-parser-test] | ||
description = "Update parser test data" | ||
private = true | ||
script = ''' | ||
#!/usr/bin/env bash | ||
set -e | ||
cargo run --bin parser-test-apply | ||
''' | ||
|
||
[tasks.apply-parser-test] | ||
description = "Generate parser test data" | ||
dependencies = [ | ||
"update-parser-test" | ||
] | ||
script = ''' | ||
#!/usr/bin/env bash | ||
set -e | ||
cd src/sqlparser/tests/testdata/ | ||
|
||
for f in *.apply.yaml | ||
do | ||
diff "$f" "$(basename "$f" .apply.yaml).yaml" || true | ||
done | ||
|
||
echo "If you want to apply the parser test data, run: $(tput setaf 2)./risedev do-apply-parser-test$(tput sgr 0)" | ||
''' | ||
category = "RiseDev - Test" | ||
|
||
[tasks.do-apply-parser-test] | ||
description = "Apply parser test data" | ||
dependencies = [ | ||
"update-parser-test" | ||
] | ||
script = ''' | ||
#!/usr/bin/env bash | ||
set -e | ||
cd src/sqlparser/tests/testdata/ | ||
|
||
for f in *.apply.yaml | ||
do | ||
SOURCE="$(basename $f .apply.yaml).yaml" | ||
if [ -f "$SOURCE" ]; then | ||
cat <<EOF > temp.apply.yaml | ||
# This file is automatically generated. See \`src/sqlparser/test_runner/src/bin/apply.rs\` for more information. | ||
EOF | ||
cat "$f" >> temp.apply.yaml | ||
mv temp.apply.yaml "$SOURCE" | ||
fi | ||
done | ||
|
||
rm *.apply.yaml | ||
|
||
echo "$(tput setaf 2)Diff applied!$(tput sgr 0)" | ||
''' | ||
category = "RiseDev - Test" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed 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 std::path::Path; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use anyhow::Result; | ||
use console::style; | ||
use futures::future::try_join_all; | ||
use risingwave_sqlparser::ast::Statement; | ||
use risingwave_sqlparser::parser::Parser; | ||
use risingwave_sqlparser_test_runner::TestCase; | ||
use walkdir::WalkDir; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let manifest_dir = env!("CARGO_MANIFEST_DIR"); | ||
let dir = Path::new(manifest_dir) | ||
.parent() | ||
.unwrap() | ||
.join("tests") | ||
.join("testdata"); | ||
println!("Using test cases from {:?}", dir); | ||
|
||
let mut futs = vec![]; | ||
|
||
let log_lock = Arc::new(Mutex::new(())); | ||
|
||
for entry in WalkDir::new(dir) { | ||
let entry = entry.unwrap(); | ||
let path = entry.path(); | ||
|
||
if !path.is_file() { | ||
continue; | ||
} | ||
|
||
if path.is_file() | ||
&& path.extension().map_or(false, |p| { | ||
p.eq_ignore_ascii_case("yml") || p.eq_ignore_ascii_case("yaml") | ||
}) | ||
&& !path | ||
.file_name() | ||
.unwrap() | ||
.to_string_lossy() | ||
.ends_with(".apply.yaml") | ||
{ | ||
let target = path.with_extension("apply.yaml"); | ||
|
||
let path = path.to_path_buf(); | ||
let log_lock = Arc::clone(&log_lock); | ||
futs.push(async move { | ||
let file_content = tokio::fs::read_to_string(&path).await?; | ||
|
||
let cases: Vec<TestCase> = serde_yaml::from_str(&file_content)?; | ||
|
||
let mut new_cases = Vec::with_capacity(cases.len()); | ||
|
||
for case in cases { | ||
let input = &case.input; | ||
let ast = Parser::parse_sql(input); | ||
let actual_case = match ast { | ||
Ok(ast) => { | ||
let [ast]: [Statement; 1] = ast | ||
.try_into() | ||
.expect("Only one statement is supported now."); | ||
|
||
let actual_formatted_sql = | ||
case.formatted_sql.as_ref().map(|_| format!("{}", ast)); | ||
let actual_formatted_ast = | ||
case.formatted_ast.as_ref().map(|_| format!("{:?}", ast)); | ||
|
||
TestCase { | ||
input: input.clone(), | ||
formatted_sql: actual_formatted_sql, | ||
formatted_ast: actual_formatted_ast, | ||
error_msg: None, | ||
} | ||
} | ||
Err(err) => { | ||
let actual_error_msg = format!("{}", err); | ||
TestCase { | ||
input: input.clone(), | ||
formatted_sql: None, | ||
formatted_ast: None, | ||
error_msg: Some(actual_error_msg), | ||
} | ||
} | ||
}; | ||
|
||
if actual_case != case { | ||
let _guard = log_lock.lock(); | ||
println!("{}\n{}\n", style(&case).red(), style(&actual_case).green()) | ||
} | ||
|
||
new_cases.push(actual_case); | ||
} | ||
|
||
let output_content = serde_yaml::to_string(&new_cases)?; | ||
|
||
tokio::fs::write(target, output_content).await?; | ||
|
||
Ok::<_, anyhow::Error>(()) | ||
}); | ||
} | ||
} | ||
|
||
let _res = try_join_all(futs).await?; | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.apply.yaml | ||
TennyZhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
# This file is automatically generated. See `src/sqlparser/test_runner/src/bin/apply.rs` for more information. | ||
- input: ALTER USER user WITH SUPERUSER CREATEDB PASSWORD 'password' | ||
formatted_sql: ALTER USER user WITH SUPERUSER CREATEDB PASSWORD 'password' | ||
|
||
- input: ALTER USER user RENAME TO another | ||
formatted_sql: ALTER USER user RENAME TO another | ||
|
||
- input: ALTER SYSTEM SET a = 'abc' | ||
formatted_sql: ALTER SYSTEM SET a = 'abc' | ||
|
||
- input: ALTER SYSTEM SET a = DEFAULT | ||
formatted_sql: ALTER SYSTEM SET a = DEFAULT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,21 @@ | ||
# This file is automatically generated. See `src/sqlparser/test_runner/src/bin/apply.rs` for more information. | ||
- input: CREATE TABLE t(a int[]); | ||
formatted_sql: CREATE TABLE t (a INT[]) | ||
|
||
- input: CREATE TABLE t(a int[][]); | ||
formatted_sql: CREATE TABLE t (a INT[][]) | ||
|
||
- input: CREATE TABLE t(a int[][][]); | ||
formatted_sql: CREATE TABLE t (a INT[][][]) | ||
|
||
- input: CREATE TABLE t(a int[); | ||
error_msg: | | ||
sql parser error: Expected ], found: ) | ||
|
||
error_msg: 'sql parser error: Expected ], found: )' | ||
- input: CREATE TABLE t(a int[[]); | ||
error_msg: | | ||
sql parser error: Expected ], found: [ | ||
|
||
error_msg: 'sql parser error: Expected ], found: [' | ||
- input: CREATE TABLE t(a int]); | ||
error_msg: | | ||
sql parser error: Expected ',' or ')' after column definition, found: ] | ||
|
||
error_msg: 'sql parser error: Expected '','' or '')'' after column definition, found: ]' | ||
- input: SELECT foo[0] FROM foos | ||
formatted_sql: SELECT foo[0] FROM foos | ||
|
||
- input: SELECT foo[0][0] FROM foos | ||
formatted_sql: SELECT foo[0][0] FROM foos | ||
|
||
- input: SELECT (CAST(ARRAY[ARRAY[2, 3]] AS INT[][]))[1][2] | ||
formatted_sql: SELECT (CAST(ARRAY[ARRAY[2, 3]] AS INT[][]))[1][2] | ||
|
||
- input: SELECT ARRAY[] | ||
formatted_sql: SELECT ARRAY[] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.