Skip to content

Commit

Permalink
fix: avoid logging CREATE TABLE exception (sqlancer#875)
Browse files Browse the repository at this point in the history
* fix: avoid logging CREATE TABLE exception

* Update TestLoggableFactory.java

* Update SQLLoggableFactory.java

We need to check the query in the constructor

* Update SQLLoggableFactory.java

Fix wrong indent.

* fix: check couldAffectSchema before setting false

* fix: revise check query string

---------

Co-authored-by: Yichen Yan <[email protected]>
  • Loading branch information
JensonSung and oraluben authored Aug 14, 2023
1 parent 33d6957 commit 9bcc3ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/sqlancer/common/query/SQLQueryAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public SQLQueryAdapter(String query, boolean couldAffectSchema) {
}

public SQLQueryAdapter(String query, ExpectedErrors expectedErrors) {
this(query, expectedErrors, false);
this(query, expectedErrors, guessAffectSchemaFromQuery(query));
}

private static boolean guessAffectSchemaFromQuery(String query) {
return query.contains("CREATE TABLE") && !query.startsWith("EXPLAIN");
}

public SQLQueryAdapter(String query, ExpectedErrors expectedErrors, boolean couldAffectSchema) {
Expand All @@ -46,8 +50,10 @@ private String canonicalizeString(String s) {
}

private void checkQueryString() {
if (query.contains("CREATE TABLE") && !query.startsWith("EXPLAIN") && !couldAffectSchema) {
throw new AssertionError("CREATE TABLE statements should set couldAffectSchema to true");
if (!couldAffectSchema) {
if (guessAffectSchemaFromQuery(query)) {
throw new AssertionError("CREATE TABLE statements should set couldAffectSchema to true");
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/sqlancer/TestLoggableFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sqlancer;

import org.junit.jupiter.api.Test;
import sqlancer.common.log.SQLLoggableFactory;
import sqlancer.common.query.SQLQueryAdapter;

public class TestLoggableFactory {

@Test
public void testLogCreateTable() {
String query = "CREATE TABLE t1 (c1 INT)";
SQLLoggableFactory logger = new SQLLoggableFactory();
SQLQueryAdapter queryAdapter = logger.getQueryForStateToReproduce(query);
assert (queryAdapter.couldAffectSchema());
}

}

0 comments on commit 9bcc3ff

Please sign in to comment.