risingwave_sqlsmith/
validation.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Provides validation logic for expected errors.
16use risingwave_expr::ExprError;
17
18/// Ignore errors related to `0`.
19fn is_zero_err(db_error: &str) -> bool {
20    db_error.contains(&ExprError::DivisionByZero.to_string()) || db_error.contains("can't be zero")
21}
22
23/// `Casting to u32 out of range` occurs when we have functions
24/// which expect non-negative arguments,
25/// e.g. `select 222 << -1`
26// NOTE: If this error occurs too often, perhaps it is better to
27// wrap call sites with `abs(rhs)`, e.g. 222 << abs(-1);
28fn is_numeric_out_of_range_err(db_error: &str) -> bool {
29    db_error.contains(&ExprError::NumericOutOfRange.to_string())
30        || db_error.contains("Casting to u32 out of range")
31}
32
33fn is_parse_err(db_error: &str) -> bool {
34    db_error.contains("Parse error")
35}
36
37/// Skip queries with unimplemented features
38fn is_unimplemented_error(db_error: &str) -> bool {
39    db_error.contains("not yet implemented")
40}
41
42/// This error occurs because we test `implicit` casts as well,
43/// generated expressions may be ambiguous as a result,
44/// if there are multiple candidates signatures.
45/// Additionally.
46fn not_unique_error(db_error: &str) -> bool {
47    db_error.contains("Bind error") && db_error.contains("is not unique")
48}
49
50fn is_window_error(db_error: &str) -> bool {
51    db_error.contains("Bind error: The size arg of window table function should be an interval literal")
52        || db_error.contains("Bind error: The 2nd arg of window table function should be a column name but not complex expression. Consider using an intermediate CTE or view as workaround")
53}
54
55// Streaming nested-loop join is not supported, as it is expensive.
56fn is_nested_loop_join_error(db_error: &str) -> bool {
57    db_error.contains("Not supported: streaming nested-loop join")
58}
59
60fn is_subquery_unnesting_error(db_error: &str) -> bool {
61    db_error.contains("Subquery can not be unnested")
62        || db_error.contains("Scalar subquery might produce more than one row")
63}
64
65/// Can't avoid numeric overflows, we do not eval const expr
66fn is_numeric_overflow_error(db_error: &str) -> bool {
67    db_error.contains("Number") && db_error.contains("overflows")
68}
69
70/// Negative substr error
71fn is_neg_substr_error(db_error: &str) -> bool {
72    db_error.contains("negative substring length not allowed")
73}
74
75/// Zero or negative overlay start error
76fn is_overlay_start_error(db_error: &str) -> bool {
77    db_error.contains("Invalid parameter start") && db_error.contains("is not positive")
78}
79
80/// Broken channel error
81fn is_broken_channel_error(db_error: &str) -> bool {
82    db_error.contains("failed to finish command: channel closed")
83}
84
85/// Permit recovery error
86/// Suppose Out Of Range Error happens in the following query:
87/// ```sql
88/// SELECT sum0(v1) FROM t;
89/// ```
90/// It would be a valid scenario from Sqlsmith.
91/// In that case we would trigger recovery for the materialized view.
92/// We could encounter this error on subsequent queries:
93/// ```text
94/// Barrier read is unavailable for now. Likely the cluster is recovering
95/// ```
96/// Recovery should be successful after a while.
97/// Hence we should retry for some bound.
98pub fn is_recovery_in_progress_error(db_error: &str) -> bool {
99    db_error.contains("Barrier read is unavailable for now. Likely the cluster is recovering")
100        || db_error.contains("Service unavailable: The cluster is starting or recovering")
101}
102
103pub fn is_neg_exp_error(db_error: &str) -> bool {
104    db_error.contains("zero raised to a negative power is undefined")
105}
106
107pub fn is_neg_input_error(db_error: &str) -> bool {
108    db_error.contains("input cannot be negative value")
109}
110
111/// Certain errors are permitted to occur. This is because:
112/// 1. It is more complex to generate queries without these errors.
113/// 2. These errors seldom occur, skipping them won't affect overall effectiveness of sqlsmith.
114pub fn is_permissible_error(db_error: &str) -> bool {
115    is_numeric_out_of_range_err(db_error)
116        || is_zero_err(db_error)
117        || is_parse_err(db_error)
118        || is_unimplemented_error(db_error)
119        || not_unique_error(db_error)
120        || is_window_error(db_error)
121        || is_nested_loop_join_error(db_error)
122        || is_subquery_unnesting_error(db_error)
123        || is_numeric_overflow_error(db_error)
124        || is_neg_substr_error(db_error)
125        || is_overlay_start_error(db_error)
126        || is_broken_channel_error(db_error)
127        || is_neg_exp_error(db_error)
128        || is_neg_input_error(db_error)
129}