Skip to content
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

feat: Support query hints for DML statements #1030

Merged
merged 3 commits into from Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -315,6 +315,10 @@ public boolean isQuery(String sql) {
*/
@InternalApi
public boolean isUpdateStatement(String sql) {
// Skip any query hints at the beginning of the query.
if (sql.startsWith("@")) {
sql = removeStatementHint(sql);
}
return statementStartsWith(sql, dmlStatements);
}

Expand Down Expand Up @@ -460,6 +464,12 @@ static String removeStatementHint(String sql) {
startQueryIndex = upperCaseSql.indexOf(keyword);
if (startQueryIndex > -1) break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I know this was not changed in this PR, but please apply the following as well, as the Google style guide dictates that all if statements should always use curly braces.

Suggested change
if (startQueryIndex > -1) break;
if (startQueryIndex > -1) {
break;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
if (startQueryIndex <= -1) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this if statement
you can probably make changes in the previous for-loop as follows:

import com.google.common.collect.Sets;

...

Set<String> selectAndDmlStatements = Sets.union(selectStatements, dmlStatements).immutableCopy();
for (String keyword : selectAndDmlStatements) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply this suggestion, as there is no need to do this in two separate loops. Also, please update the comment on line 460 to reflect the fact that statement hints are also supported for DML statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated both the for-loop and comment.

for (String keyword : dmlStatements) {
startQueryIndex = upperCaseSql.indexOf(keyword);
if (startQueryIndex > -1) break;
}
}
if (startQueryIndex > -1) {
int endStatementHintIndex = sql.substring(0, startQueryIndex).lastIndexOf('}');
if (startStatementHintIndex == -1 || startStatementHintIndex > endStatementHintIndex) {
Expand Down
Expand Up @@ -317,7 +317,7 @@ public void testIsQuery() {
}

@Test
public void testQueryHints() {
public void testIsQuery_QueryHints() {
// Valid query hints.
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@ {JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
Expand Down Expand Up @@ -358,9 +358,51 @@ public void testQueryHints() {
assertFalse(parser.isQuery("@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
assertFalse(parser.isQuery("@JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertFalse(parser.isQuery("@JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
}

@Test
public void testIsUpdate_QueryHints() {
// Valid query hints.
assertTrue(
parser.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive} UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@ {LOCK_SCANNED_RANGES=exclusive} UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@{ LOCK_SCANNED_RANGES=exclusive} UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive } UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive}\nUPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@{\nLOCK_SCANNED_RANGES = exclusive \t}\n\t UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive}\n -- Single line comment\nUPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(
parser.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive}\n /* Multi line comment\n with more comments\n */UPDATE FOO SET NAME='foo' WHERE ID=1"));

// Multiple query hints.
assertTrue(
StatementParser.INSTANCE.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive} @{USE_ADDITIONAL_PARALLELISM=TRUE} UPDATE FOO SET NAME='foo' WHERE ID=1"));

// Invalid query hints.
assertFalse(
StatementParser.INSTANCE.isQuery(
"@{FORCE_INDEX=index_name} @{JOIN_METHOD=HASH_JOIN} UPDATE tbl set FOO=1 WHERE ID=2"));
parser.isUpdateStatement(
"@{LOCK_SCANNED_RANGES=exclusive UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertFalse(
parser.isUpdateStatement(
"@LOCK_SCANNED_RANGES=exclusive} UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertFalse(
parser.isUpdateStatement(
"@LOCK_SCANNED_RANGES=exclusive UPDATE FOO SET NAME='foo' WHERE ID=1"));
}

@Test
Expand Down