-
Notifications
You must be signed in to change notification settings - Fork 397
[Hive] Support TLP oracle #1201
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4faa1c9
[Hive] Support TLP oracle
liyxiris 6e86698
[Hive] Remove redundant lines
liyxiris ab731ec
[Hive] refactor Hive TLP oracle using generic approaches
liyxiris b9c13c3
[Hive] upgrade Hive dependencies & remove expected 'Invalid Constrain…
liyxiris 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
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
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,40 @@ | ||
| package sqlancer.hive; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import sqlancer.common.query.ExpectedErrors; | ||
|
|
||
| public class HiveErrors { | ||
|
|
||
| private HiveErrors() { | ||
| } | ||
|
|
||
| public static List<String> getExpressionErrors() { | ||
| ArrayList<String> errors = new ArrayList<>(); | ||
|
|
||
| errors.add("cannot recognize input near"); | ||
| errors.add("Argument type mismatch"); | ||
| errors.add("Error while compiling statement"); | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| public static void addExpressionErrors(ExpectedErrors errors) { | ||
| errors.addAll(getExpressionErrors()); | ||
| } | ||
|
|
||
| public static List<String> getInsertErrors() { | ||
| ArrayList<String> errors = new ArrayList<>(); | ||
|
|
||
| errors.add("Either CHECK or NOT NULL constraint violated!"); | ||
| errors.add("Error running query"); | ||
| errors.add("is different from preceding arguments"); | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| public static void addInsertErrors(ExpectedErrors errors) { | ||
| errors.addAll(getInsertErrors()); | ||
| } | ||
| } | ||
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,11 @@ | ||
| package sqlancer.hive; | ||
|
|
||
| import sqlancer.SQLGlobalState; | ||
|
|
||
| public class HiveGlobalState extends SQLGlobalState<HiveOptions, HiveSchema> { | ||
|
|
||
| @Override | ||
| protected HiveSchema readSchema() throws Exception { | ||
| return HiveSchema.fromConnection(getConnection(), getDatabaseName()); | ||
| } | ||
| } |
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,43 @@ | ||
| package sqlancer.hive; | ||
|
|
||
| import com.beust.jcommander.Parameter; | ||
| import com.beust.jcommander.Parameters; | ||
|
|
||
| import sqlancer.common.oracle.TLPWhereOracle; | ||
| import sqlancer.common.oracle.TestOracle; | ||
| import sqlancer.common.query.ExpectedErrors; | ||
| import sqlancer.DBMSSpecificOptions; | ||
| import sqlancer.hive.gen.HiveExpressionGenerator; | ||
| import sqlancer.OracleFactory; | ||
|
|
||
| import java.sql.SQLException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| @Parameters(separators = "=", commandDescription = "Hive (default port: " + HiveOptions.DEFAULT_PORT | ||
| + ", default host: " + HiveOptions.DEFAULT_HOST + ")") | ||
| public class HiveOptions implements DBMSSpecificOptions<HiveOptions.HiveOracleFactory> { | ||
| public static final String DEFAULT_HOST = "localhost"; | ||
| public static final int DEFAULT_PORT = 10000; | ||
|
|
||
| @Parameter(names = "--oracle") | ||
| public List<HiveOracleFactory> oracle = Arrays.asList(HiveOracleFactory.TLPWhere); | ||
|
|
||
| public enum HiveOracleFactory implements OracleFactory<HiveGlobalState> { | ||
| TLPWhere { | ||
| @Override | ||
| public TestOracle<HiveGlobalState> create(HiveGlobalState globalState) throws SQLException { | ||
| HiveExpressionGenerator gen = new HiveExpressionGenerator(globalState); | ||
| ExpectedErrors expectedErrors = ExpectedErrors.newErrors() | ||
| .with(HiveErrors.getExpressionErrors()).build(); | ||
|
|
||
| return new TLPWhereOracle<>(globalState, gen, expectedErrors); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public List<HiveOracleFactory> getTestOracleFactory() { | ||
| return oracle; | ||
| } | ||
| } |
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 @@ | ||
| package sqlancer.hive; | ||
|
|
||
| import sqlancer.AbstractAction; | ||
| import sqlancer.DatabaseProvider; | ||
| import sqlancer.IgnoreMeException; | ||
| import sqlancer.MainOptions; | ||
| import sqlancer.Randomly; | ||
| import sqlancer.SQLConnection; | ||
| import sqlancer.SQLProviderAdapter; | ||
| import sqlancer.StatementExecutor; | ||
| import sqlancer.common.query.SQLQueryAdapter; | ||
| import sqlancer.common.query.SQLQueryProvider; | ||
| import sqlancer.hive.gen.HiveInsertGenerator; | ||
| import sqlancer.hive.gen.HiveTableGenerator; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
|
|
||
| @AutoService(DatabaseProvider.class) | ||
| public class HiveProvider extends SQLProviderAdapter<HiveGlobalState, HiveOptions> { | ||
|
|
||
| public HiveProvider() { | ||
| super(HiveGlobalState.class, HiveOptions.class); | ||
| } | ||
|
|
||
| public enum Action implements AbstractAction<HiveGlobalState> { | ||
|
|
||
| INSERT(HiveInsertGenerator::getQuery); | ||
|
|
||
| private final SQLQueryProvider<HiveGlobalState> sqlQueryProvider; | ||
|
|
||
| Action(SQLQueryProvider<HiveGlobalState> sqlQueryProvider) { | ||
| this.sqlQueryProvider = sqlQueryProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLQueryAdapter getQuery(HiveGlobalState state) throws Exception { | ||
| return sqlQueryProvider.getQuery(state); | ||
| } | ||
| } | ||
|
|
||
| private static int mapActions(HiveGlobalState globalState, Action a) { | ||
| Randomly r = globalState.getRandomly(); | ||
| switch (a) { | ||
| case INSERT: | ||
| return r.getInteger(0, globalState.getOptions().getMaxNumberInserts()); | ||
| default: | ||
| throw new AssertionError(a); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void generateDatabase(HiveGlobalState globalState) throws Exception { | ||
| for (int i = 0; i < Randomly.fromOptions(1, 2); i++) { | ||
| boolean success; | ||
| do { | ||
| String tableName = globalState.getSchema().getFreeTableName(); | ||
| SQLQueryAdapter qt = HiveTableGenerator.generate(globalState, tableName); | ||
| success = globalState.executeStatement(qt); | ||
| } while(!success); | ||
| } | ||
| if (globalState.getSchema().getDatabaseTables().isEmpty()) { | ||
| throw new IgnoreMeException(); // TODO | ||
| } | ||
|
|
||
| StatementExecutor<HiveGlobalState, Action> se = new StatementExecutor<HiveGlobalState, Action>( | ||
| globalState, Action.values(), | ||
| HiveProvider::mapActions, (q) -> { | ||
| if (globalState.getSchema().getDatabaseTables().isEmpty()) { | ||
| throw new IgnoreMeException(); | ||
| } | ||
| }); | ||
| se.executeStatements(); | ||
| } | ||
|
|
||
| @Override | ||
| public SQLConnection createDatabase(HiveGlobalState globalState) throws SQLException { | ||
| String username = globalState.getOptions().getUserName(); | ||
| String password = globalState.getOptions().getPassword(); | ||
| String host = globalState.getOptions().getHost(); | ||
| int port = globalState.getOptions().getPort(); | ||
| if (host == null) { | ||
| host = HiveOptions.DEFAULT_HOST; | ||
| } | ||
| if (port == MainOptions.NO_SET_PORT) { | ||
| port = HiveOptions.DEFAULT_PORT; | ||
| } | ||
|
|
||
| String databaseName = globalState.getDatabaseName(); | ||
|
|
||
| String url = String.format("jdbc:hive2://%s:%d/%s", host, port, "default"); | ||
| Connection con = DriverManager.getConnection(url, username, password); | ||
| globalState.getState().logStatement("DROP DATABASE IF EXISTS " + databaseName + " CASCADE"); | ||
| globalState.getState().logStatement("CREATE DATABASE " + databaseName); | ||
| globalState.getState().logStatement("USE " + databaseName); | ||
| try (Statement s = con.createStatement()) { | ||
| s.execute("DROP DATABASE IF EXISTS " + databaseName + " CASCADE"); | ||
| } | ||
| try (Statement s = con.createStatement()) { | ||
| s.execute("CREATE DATABASE " + databaseName); | ||
| } | ||
| try (Statement s = con.createStatement()) { | ||
| s.execute("USE " + databaseName); | ||
| } | ||
| con.close(); | ||
| con = DriverManager.getConnection( | ||
| String.format("jdbc:hive2://%s:%d/%s", host, port, databaseName, | ||
| username, password)); | ||
|
|
||
| return new SQLConnection(con); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDBMSName() { | ||
| return "hive"; | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an expected error or could it be an actual bug (e.g., like an internal error)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These errors are expected since I use an untyped expression generator for Hive, and syntactically invalid statements will cause these errors so they're ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick follow up question on this: for an untyped expression generator, I would expected semantically invalid statements, but not syntactically invalid ones. Would it be possible to show an example where this causes such an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for responding. Yes, Agreed! Does the "Invalid Constraint syntax" below also relate to an actual semantic error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh thanks for mentioning, "Invalid Constraint syntax" also relates to mismatched types, but I believe it's not needed anymore since it will be covered by the above HiveErrors. It's removed in the latest commit.