Skip to content

Commit 8be23f4

Browse files
feat(sqlsmith): add statement-level reducer (risingwavelabs#8507)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 320d755 commit 8be23f4

File tree

7 files changed

+506
-41
lines changed

7 files changed

+506
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/sqlsmith/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ clap = { version = "4", features = ["derive"] }
2020
itertools = "0.10"
2121
rand = { version = "0.8", features = ["small_rng"] }
2222
rand_chacha = { version = "0.3.1" }
23+
regex = "1"
2324
risingwave_common = { path = "../../common" }
2425
risingwave_expr = { path = "../../expr" }
2526
risingwave_frontend = { path = "../../frontend" }

src/tests/sqlsmith/scripts/gen_queries.sh

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ echo_err() {
3030
}
3131

3232
################## EXTRACT
33+
# TODO(kwannoel): Write tests for these
3334

3435
# Get reason for generation crash.
3536
get_failure_reason() {
@@ -67,7 +68,7 @@ extract_failing_query() {
6768
grep "\[EXECUTING .*\]: " | tail -n 1 | sed -E 's/^.*\[EXECUTING .*\]: (.*)$/\1;/' || true
6869
}
6970

70-
# Extract fail info from logs in log dir
71+
# Extract fail info from [`generate-*.log`] in log dir
7172
extract_fail_info_from_logs() {
7273
for LOGFILENAME in $(ls "$LOGDIR" | grep "generate")
7374
do
@@ -86,14 +87,17 @@ extract_fail_info_from_logs() {
8687
FAIL_DIR="$OUTDIR/failed/$SEED"
8788
mkdir -p "$FAIL_DIR"
8889
echo -e "$DDL" "\n$GLOBAL_SESSION" "\n$DML" "\n$TEST_SESSION" "\n$QUERY" > "$FAIL_DIR/queries.sql"
89-
echo_err "[INFO] WROTE FAIL QUERY to $FAIL_DIR/queries.log"
90+
echo_err "[INFO] WROTE FAIL QUERY to $FAIL_DIR/queries.sql"
9091
echo -e "$REASON" > "$FAIL_DIR/fail.log"
9192
echo_err "[INFO] WROTE FAIL REASON to $FAIL_DIR/fail.log"
93+
9294
cp "$LOGFILE" "$FAIL_DIR/$LOGFILENAME"
9395
fi
9496
done
9597
}
9698

99+
################# Generate
100+
97101
# Prefer to use [`generate_deterministic`], it is faster since
98102
# runs with all-in-one binary.
99103
generate_deterministic() {
@@ -123,6 +127,8 @@ generate_sqlsmith() {
123127
--generate "$OUTDIR/$1"
124128
}
125129

130+
############################# Checks
131+
126132
# Check that queries are different
127133
check_different_queries() {
128134
if [[ -z $(diff "$OUTDIR/1/queries.sql" "$OUTDIR/2/queries.sql") ]]; then
@@ -142,34 +148,6 @@ check_failed_to_generate_queries() {
142148
fi
143149
}
144150

145-
# sync step
146-
# Some queries maybe be added
147-
sync_queries() {
148-
set +x
149-
pushd $OUTDIR
150-
git checkout main
151-
git pull
152-
set +e
153-
git branch -D stage
154-
set -e
155-
git checkout -b stage
156-
popd
157-
set -x
158-
}
159-
160-
# Upload step
161-
upload_queries() {
162-
set +x
163-
pushd "$OUTDIR"
164-
git add .
165-
git commit -m 'update queries'
166-
git push -f origin stage
167-
git checkout -
168-
git branch -D stage
169-
popd
170-
set -x
171-
}
172-
173151
# Run it to make sure it should have no errors
174152
run_queries() {
175153
echo "" > $LOGDIR/run_deterministic.stdout.log
@@ -180,6 +158,7 @@ run_queries() {
180158
&& rm $LOGDIR/fuzzing-{}.log"
181159
}
182160

161+
# Generated query sets should not fail.
183162
check_failed_to_run_queries() {
184163
FAILED_LOGS=$(ls "$LOGDIR" | grep fuzzing || true)
185164
if [[ -n "$FAILED_LOGS" ]]; then
@@ -223,11 +202,39 @@ validate() {
223202
echo_err "[INFO] Passed checks"
224203
}
225204

205+
# sync step
206+
# Some queries maybe be added
207+
sync_queries() {
208+
set +x
209+
pushd $OUTDIR
210+
git checkout main
211+
git pull
212+
set +e
213+
git branch -D stage
214+
set -e
215+
git checkout -b stage
216+
popd
217+
set -x
218+
}
219+
226220
sync() {
227221
sync_queries
228222
echo_err "[INFO] Synced"
229223
}
230224

225+
# Upload step
226+
upload_queries() {
227+
set +x
228+
pushd "$OUTDIR"
229+
git add .
230+
git commit -m 'update queries'
231+
git push -f origin stage
232+
git checkout -
233+
git branch -D stage
234+
popd
235+
set -x
236+
}
237+
231238
upload() {
232239
upload_queries
233240
echo_err "[INFO] Uploaded"
@@ -238,6 +245,8 @@ cleanup() {
238245
echo_err "[INFO] Success!"
239246
}
240247

248+
################### MAIN
249+
241250
main() {
242251
setup
243252

@@ -250,4 +259,4 @@ main() {
250259
cleanup
251260
}
252261

253-
main
262+
main

src/tests/sqlsmith/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(let_chains)]
1616
#![feature(if_let_guard)]
1717
#![feature(once_cell)]
18+
#![feature(box_patterns)]
1819

1920
use rand::prelude::SliceRandom;
2021
use rand::Rng;
@@ -25,8 +26,10 @@ use risingwave_sqlparser::parser::Parser;
2526

2627
use crate::sql_gen::SqlGenerator;
2728

29+
pub mod reducer;
2830
pub mod runner;
2931
mod sql_gen;
32+
mod utils;
3033
pub mod validation;
3134
pub use validation::is_permissible_error;
3235

@@ -76,7 +79,8 @@ pub fn session_sql_gen<R: Rng>(rng: &mut R) -> String {
7679

7780
/// Parse SQL
7881
/// FIXME(Noel): Introduce error type for sqlsmith for this.
79-
pub fn parse_sql(sql: &str) -> Vec<Statement> {
82+
pub fn parse_sql<S: AsRef<str>>(sql: S) -> Vec<Statement> {
83+
let sql = sql.as_ref();
8084
Parser::parse_sql(sql).unwrap_or_else(|_| panic!("Failed to parse SQL: {}", sql))
8185
}
8286

0 commit comments

Comments
 (0)