Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 80 additions & 7 deletions src/sqlancer/voltdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ A SQLancer provider for [VoltDB](https://www.voltactivedata.com/), developed for
VoltDB schemas, both replicated and partitioned tables, fills them with single-row inserts, and runs SQLancer's oracles
against them through the VoltDB JDBC driver.

**Status: milestone 1.** The provider creates schemas and data. Its only oracle, `SMOKE`, reads every table and checks
nothing but that the queries succeed. The logic-bug oracles come next: NoREC (milestone 2), then TLP WHERE
**Status: milestone 2.** The provider creates schemas and data and runs the NoREC logic-bug oracle (the default).
`SMOKE`, from milestone 1, only reads every table and checks that the queries succeed. TLP WHERE comes next
(milestone 3).

## Versions
Expand Down Expand Up @@ -51,10 +51,10 @@ holds data you care about: DDL from SQLancer serializes with all other DDL on th

```bash
java -jar target/sqlancer-*.jar \
--num-threads 4 --num-tries 4 --timeout-seconds 1800 --num-queries 5 \
--num-threads 4 --num-tries 4 --timeout-seconds 1800 --num-queries 100 \
--random-seed 20261102 --username "" --password "" \
--host localhost --port 21212 --database-prefix db \
voltdb --oracle SMOKE
voltdb --oracle NOREC
```

SQLancer's options come before `voltdb`, the provider's after it.
Expand All @@ -63,15 +63,16 @@ SQLancer's options come before `voltdb`, the provider's after it.
| --- | --- |
| `--num-threads`, `--num-tries` | Threads that run at once, and how many run in total. Use the same number for both. Every thread is one SQLancer "database". VoltDB runs one DDL statement at a time, so more than about 4 threads mostly wait. |
| `--timeout-seconds` | How long the run lasts. |
| `--num-queries` | Oracle checks per database before a thread drops it and generates a new one. With `SMOKE`, keep it small so schemas keep changing. |
| `--num-queries` | Oracle checks per database before a thread drops it and generates a new one. 100 suits NoREC; with `SMOKE`, keep it small so schemas keep changing. |
| `--random-seed` | Thread *n* uses seed + *n*. A seed and the same server state give the same statements, but thread timing is not deterministic. |
| `--username`, `--password` | Empty for a server without security (SQLancer's default is `sqlancer`). |
| `--host`, `--port` | A server's client port. Defaults: localhost, 21212. |
| `--database-prefix` | Prefix of each thread's table names. Default `database`; `db` keeps the logs short. |
| `--oracle` | `SMOKE` (the default, and the only oracle so far). |
| `--oracle` | `NOREC` (the default) or `SMOKE`. |
| `--max-num-tables` | Tables per database, 1 to this (default 3). |
| `--max-num-indexes` | CREATE INDEX statements per database, 0 to this (default 4). |
| `--retry-seconds` | How long a statement rejected because of another thread's schema change is retried (default 120). |
| `--avoid-known-bugs` | `true` (the default) keeps the generator away from the VoltDB bugs listed below, so a failure is something new. `false` generates them too. |

## Reading the results

Expand All @@ -96,6 +97,40 @@ Logs are in `logs/voltdb/` under the directory you run from:
The exit code is 0 when no thread failed. To reproduce a failure, start from an empty server and run the statements in
`<db>.log` in order (with `sqlcmd`, for example).

A NoREC failure ends with the two queries, `-- optimized:` and `-- unoptimized:`, and either the two counts that differ
("the row counts of the following queries mismatch") or the unexpected error.

## The NoREC oracle

NoREC ([Rigger and Su, 2020](https://doi.org/10.1145/3368089.3409704)) runs one random predicate two ways and compares
the counts:

```sql
-- optimized: the predicate is a WHERE clause, which the planner can push into index scans, joins and partitions
SELECT COUNT(*) FROM t0, t1 LEFT JOIN t2 ON ... WHERE p;
-- unoptimized: the predicate is evaluated on every row, so the planner cannot use it
SELECT COALESCE(SUM(c), 0) FROM (SELECT CASE WHEN p THEN 1 ELSE 0 END AS c FROM t0, t1 LEFT JOIN t2 ON ...) AS res;
```

Half the time the optimized query selects the rows instead of counting them, and the oracle counts the result set.

The VoltDB expression generator (`gen/VoltDBExpressionGenerator`) builds predicates from columns and constants with
AND, OR, NOT, the comparisons, BETWEEN, IN, LIKE, IS [NOT] NULL, `+ - * /`, `||` and ABS. To stay inside what VoltDB
supports:

- **Joins follow partitioning.** Partitioned tables are joined to the first partitioned table (the anchor) on their
partitioning columns, so every query runs in one partition or as a correct multi-partition join. The ON condition is
the equality alone: VoltDB plans the join only when it can see that equality, and an added condition that folds to
FALSE, such as `pc = pc AND (1 = 0)`, takes it away ("This query is not plannable"). A partitioned table whose
partitioning column cannot be compared with the anchor's is left out. Replicated tables are in the FROM list or,
sometimes, joined explicitly (INNER, LEFT, RIGHT or FULL; RIGHT only when `--avoid-known-bugs` is false) with a random
ON condition.
- **Typed NULLs.** A NULL constant is `CAST(NULL AS type)`; VoltDB rejects a bare NULL whose type it cannot infer.
- **Syntax.** Negative constants are in parentheses (`- -5` is a syntax error), `MOD` is never written as `%`, and an
IN predicate is never the operand of a comparison or another IN (`x = (y IN (1))` is a syntax error).
- **Every WHERE clause references a column.** VoltDB does not support a join filter that depends on none of the joined
tables.

## How the provider maps SQLancer onto VoltDB

- **One schema, many databases.** A VoltDB cluster has a single schema, so each SQLancer database is the set of tables
Expand Down Expand Up @@ -131,6 +166,15 @@ Each is listed in `VoltDBErrors` with its reason. The run fails on any other err
| --- | --- | --- |
| INSERT | `CONSTRAINT VIOLATION` | Random values repeat existing keys. |
| CREATE INDEX | `is not empty` | VoltDB cannot add a UNIQUE index to a table that has rows (an ASSUMEUNIQUE index it can). |
| SELECT | `Join with filters that do not depend on joined tables is not supported` | A predicate that references a column can still fold to a constant, such as `(FALSE AND t.c) = FALSE`. |
| SELECT | `Attempted to divide`, `data exception: division by zero` | Division by zero, at run time or in constants the planner folds. |
| SELECT | `Invalid result value` | A FLOAT result is infinite or NaN. |
| SELECT | `will overflow BigInt storage`, `causing overflow/underflow`, `out of range for the destination type` | Integer, DECIMAL or cast overflow. |
| SELECT | `Numeric conversion error to type DECIMAL` | DECIMAL overflow in an expression of constants, which the planner folds. The message quotes a truncated value. |

The two NoREC queries can evaluate an expression on different rows (the optimized query may skip rows through an index
or a join), so only one of them may hit a run-time error. The oracle treats an expected error in either query as no
result.

VoltDB also drops a CREATE INDEX that duplicates an existing index, logging "Dropping index ... because it duplicates
index ..." at WARN. The statement succeeds, so the provider ignores it.
Expand All @@ -140,9 +184,38 @@ index ..." at WARN. The statement succeeds, so the provider ignores it.
| Ticket | Issue | Workaround |
| --- | --- | --- |
| ENG-30489 | Closing a JDBC connection twice closes the client every other connection to the same URL shares, failing their in-flight calls. SQLancer closes each connection twice. | `VoltDBConnection.close()` is idempotent. |
| ENG-4643 | JDBC `getString` on a NULL returns the type's NULL sentinel (INTEGER, TINYINT, FLOAT) or throws (DECIMAL, BOOLEAN, DATE, TIMESTAMP). SQLancer's oracles compare results with `getString`. | Not needed yet; the NoREC and TLP oracles will need one. |
| ENG-4643 | JDBC `getString` on a NULL returns the type's NULL sentinel (INTEGER, TINYINT, FLOAT) or throws (DECIMAL, BOOLEAN, DATE, TIMESTAMP). | Not needed: NoREC reads only counts. TLP compares values and will need one. |
| ENG-12656 | An index scan for `<`, `<=`, `>`, `>=` or BETWEEN on a VARBINARY(n) column, and for `=` when the column leads a multi-column unique index, fails ("Type VARCHAR can't be cast as VARBINARY") when the value, a constant or another table's column, is longer than n bytes. Found again by NoREC. | While `--avoid-known-bugs` is true, VARBINARY values are never compared with `<`, `>`, `<=`, `>=` or BETWEEN, a VARBINARY comparison has a constant on one side, VARBINARY constants are at most one byte, and VARBINARY partitioning columns are joined only when their sizes match. |
| ENG-22880 | A BIGINT literal at the type's minimum is stored as NULL instead of rejected. A FLOAT at -1.7976931348623157E308 and an INTEGER expression equal to INTEGER's minimum do the same. | Never generated. |

The bugs below were found by NoREC in milestone 2. While `--avoid-known-bugs` is true (the default) the generator
avoids each one as described; set it to false to check whether a fix works.

| Ticket | Issue | Workaround |
| --- | --- | --- |
| ENG-30493 | BOOLEAN values order TRUE < FALSE where the EE compares them: `<`, `>`, BETWEEN and ORDER BY give wrong rows. | BOOLEAN values are only compared with `=` and `<>`. |
| ENG-30494 | BOOLEAN literals are passed to the EE as parameters of the wrong value or type: cast errors and wrong rows. | Boolean constants are written `(1 = 1)` and `(1 = 0)`. |
| ENG-30495 | A WHERE clause that folds to a constant NULL crashes the planner (NPE). | No NULL constants; NULLs come from the data. |
| ENG-30496 | LIKE inside CASE WHEN crashes the planner when its left operand is a constant or its pattern has no wildcard or is just `%`. | LIKE's left operand is a string column; the pattern has a wildcard and a literal character. |
| ENG-30497 | With an outer join, a boolean expression over the inner table, such as `(t.c IS NULL) IS NOT NULL` or `(t.c IS NULL) = TRUE`, turns the join into an inner join: wrong rows. | With an outer join, boolean operands of comparisons, IN and IS NULL are columns or constants. |
| ENG-30499 | Comparing a DATE constant with a TIMESTAMP constant crashes the planner (ClassCastException). | Temporal constants are `CAST('...' AS TIMESTAMP)`. |
| ENG-30500 | A boolean expression `IN (NULL, ...)` gives wrong rows. | IN list elements are constants that are not NULL. |
| ENG-30502 | Unary minus on a NULL integer or FLOAT gives a non-NULL value. | `0 - x` instead of `-x`. |
| ENG-30503 | IN lists ignore NULL elements: `x NOT IN (NULL, ...)` is TRUE instead of NULL. | As ENG-30500. |
| ENG-30504 | `NOT (b OR c)` over BOOLEAN values fails in the planner (cast error) when it is an operand: `NOT (NOT (b OR c))`, `(NOT (b OR c)) IS NOT NULL`. | NOT is never applied directly to OR. |
| ENG-30505 | `x IN ('...')` on a unique VARCHAR(n) or VARBINARY(n) column fails when a value is longer than n. | String and binary constants are at most one character or byte, as for ENG-30509 and ENG-12656. |
| ENG-30506 | In a parenthesized AND/OR list, an operand after an IN predicate that starts with a function call or a negative literal crashes the parser (NPE). | Function calls are always printed in parentheses, whatever `--avoid-known-bugs` is. |
| ENG-30507 | With an outer join, a WHERE IN predicate on an indexed column is applied as part of the join instead of after it, so it does not remove the rows it should: wrong rows. | No IN predicates when the query has an outer join. |
| ENG-30508 | A join filter of constants only, which VoltDB rejects ("Join with filters that do not depend on joined tables"), fails with an internal NumberFormatException instead when it is an IN with a DECIMAL constant on the left. | The internal error is an expected error, like the rejection it replaces. |
| ENG-30509 | A range scan on a multi-column index compares a VARCHAR(n) column with only the first n characters of a longer value, a constant or another table's column: `c >= '-2'` matches `'-'`, and so does `c LIKE '-2%'`. Wrong rows. | Strings are never compared with `<`, `>`, `<=`, `>=` or BETWEEN; string constants and a LIKE pattern's literal prefix are at most one character (the smallest VARCHAR). |
| ENG-30510 | After turning a RIGHT JOIN into a LEFT JOIN, VoltDB can use an ON condition on the preserved table as that table's index range, dropping rows that should be null-extended. Wrong rows. | No RIGHT JOIN (a LEFT JOIN with the tables swapped is the same). |
| ENG-30511 | An index nested-loop join's range scan on a narrower integer column, with a key from a wider column that is below the narrower type's range, matches every row: `b > s` is true for BIGINT `b = -40000` and SMALLINT `s = 5`. Wrong rows. | Ordered comparisons of numbers have a constant on one side, and BETWEEN on numbers has constant bounds. |
| ENG-30512 | COUNT(*) over an empty index range, such as `x BETWEEN 9 AND 1`, is negative (INDEX COUNT). | BETWEEN bounds are constants in ascending order. |
| ENG-30513 | Comparing a FLOAT with a DECIMAL of magnitude 2^63 or more gives wrong results; the DECIMAL appears to wrap through a 64-bit integer. Such DECIMALs come from the data and from arithmetic. | A numeric comparison, BETWEEN or IN uses only FLOAT operands or only exact ones (integers and DECIMAL). |
| ENG-30514 | A primary key column that is not declared NOT NULL accepts NULL, but the planner assumes it is never NULL: `LEFT JOIN t ON ... WHERE t.pk IS NULL` drops the ON condition. Wrong rows. | Primary key columns are NOT NULL. |
| ENG-30515 | INDEX COUNT clamps a bound above the column type's range to the type's maximum and misses rows holding it: TINYINT `c < 32767` does not count 127. | Constants in ordered numeric comparisons and BETWEEN lie between -126 and 126. |
| ENG-30516 | `WHERE c IS NULL AND c = 5` on an indexed column drops the IS NULL when `c = 5` becomes the index lookup. Wrong rows. | IS NULL is written `NOT (x IS NOT NULL)`. |

The published `voltdbclient` jar does not register its JDBC driver with `java.sql.Driver`, so the provider loads
`org.voltdb.jdbc.Driver` itself.

Expand Down
20 changes: 20 additions & 0 deletions src/sqlancer/voltdb/VoltDBErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ public static boolean isRetryable(SQLException e) {
return false;
}

public static void addExpressionErrors(ExpectedErrors errors) {
// VoltDB does not support a join whose WHERE clause folds to a filter on none of the joined tables; a
// predicate that references a column can still fold to a constant, e.g. (FALSE AND t.c) = FALSE
errors.add("Join with filters that do not depend on joined tables is not supported");
// ENG-30508: the same rejected filter, when it is an IN with a DECIMAL constant on the left, fails with an
// internal error instead
errors.add("NUMERIC constant value type must match a FLOAT, DECIMAL, or integral column, not null");
// run-time arithmetic errors: random operands divide by zero and overflow, and the two NoREC queries may
// evaluate an expression on different rows, so only one of them may hit the error
errors.add("Attempted to divide"); // integer or DECIMAL division by zero
errors.add("data exception: division by zero"); // the same, in constants the planner folds
errors.add("Invalid result value"); // FLOAT result is infinite or NaN
errors.add("will overflow BigInt storage"); // integer +, -, * out of range
errors.add("causing overflow/underflow"); // DECIMAL out of range
errors.add("out of range for the destination type"); // a huge DECIMAL or FLOAT result cast to an integer
// DECIMAL out of range in an expression of constants, which the planner folds; the message quotes a
// truncated value, e.g. "30145.999" for -99999999999999999999999999.999999999999 + -30146
errors.add("Numeric conversion error to type DECIMAL");
}

public static void addInsertErrors(ExpectedErrors errors) {
// a generated key can repeat an existing primary key or unique value
errors.add("CONSTRAINT VIOLATION");
Expand Down
5 changes: 4 additions & 1 deletion src/sqlancer/voltdb/VoltDBOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class VoltDBOptions implements DBMSSpecificOptions<VoltDBOracleFactory> {
public static final int DEFAULT_PORT = 21212;

@Parameter(names = "--oracle", description = "The test oracles to use")
public List<VoltDBOracleFactory> oracles = List.of(VoltDBOracleFactory.SMOKE);
public List<VoltDBOracleFactory> oracles = List.of(VoltDBOracleFactory.NOREC);

@Parameter(names = "--max-num-tables", description = "The maximum number of tables each database creates")
public int maxNumTables = 3;
Expand All @@ -26,6 +26,9 @@ public class VoltDBOptions implements DBMSSpecificOptions<VoltDBOracleFactory> {
@Parameter(names = "--retry-seconds", description = "How long a statement VoltDB rejects because of a concurrent schema change is retried")
public int retrySeconds = 120;

@Parameter(names = "--avoid-known-bugs", description = "Do not generate SQL that hits a known, unfixed VoltDB bug (see the README)", arity = 1)
public boolean avoidKnownBugs = true;

@Override
public List<VoltDBOracleFactory> getTestOracleFactory() {
return oracles;
Expand Down
16 changes: 16 additions & 0 deletions src/sqlancer/voltdb/VoltDBOracleFactory.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package sqlancer.voltdb;

import sqlancer.OracleFactory;
import sqlancer.common.oracle.NoRECOracle;
import sqlancer.common.oracle.TestOracle;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.voltdb.VoltDBProvider.VoltDBGlobalState;
import sqlancer.voltdb.gen.VoltDBExpressionGenerator;
import sqlancer.voltdb.oracle.VoltDBSmokeOracle;

public enum VoltDBOracleFactory implements OracleFactory<VoltDBGlobalState> {
Expand All @@ -16,6 +19,19 @@ public enum VoltDBOracleFactory implements OracleFactory<VoltDBGlobalState> {
public TestOracle<VoltDBGlobalState> create(VoltDBGlobalState globalState) {
return new VoltDBSmokeOracle(globalState);
}
},

/**
* Compares the row count of a query that filters with a random predicate in its WHERE clause with the count of an
* equivalent query that evaluates the predicate per row in its projection, which the planner cannot optimize.
*/
NOREC {
@Override
public TestOracle<VoltDBGlobalState> create(VoltDBGlobalState globalState) {
ExpectedErrors errors = ExpectedErrors.newErrors().build();
VoltDBErrors.addExpressionErrors(errors);
return new NoRECOracle<>(globalState, new VoltDBExpressionGenerator(globalState), errors);
}
}

}
Loading