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 partitioned queries + data boost in Connection API #2540
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
052688a
feat: support partitioned queries + data boost in Connection API
olavloite 9817586
fix: match the correct group in regex
olavloite dc91c9a
Merge branch 'main' into batch-read-connection-api
olavloite bca9b12
feat: add more SQL statements for partitioned queries
olavloite bc5b691
chore: refactor client side statements to accept the entire parsed st…
olavloite 56dfb75
Merge branch 'parsed-statement-to-client-executors' into batch-read-c…
olavloite f0e9f5b
chore: simplify test
olavloite af211d3
Merge branch 'parsed-statement-to-client-executors' into batch-read-c…
olavloite 090868c
chore: cleanup differences
olavloite 31ba134
chore: cleanup unrelated changes
olavloite 6960e11
fix: update converter name
olavloite 549e870
test: add more tests
olavloite 0b5269e
chore: add missing license header
olavloite d5248cc
fix: handle empty partitioned queries correctly
olavloite a461c39
fix: do not use any random staleness for partitioned queries
olavloite 13541b1
fix: only return false for next() if all have finished
olavloite 3ae95e3
chore: rename to autoPartitionMode
olavloite ed6a34e
chore: rename sql statements + add tests for empty results
olavloite 1b39e8c
Merge branch 'main' into batch-read-connection-api
olavloite 9e6dc6e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 89889ee
chore: address review comments
olavloite 1658a51
Batch read connection api native adjustments (#2569)
burkedavison File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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
63 changes: 63 additions & 0 deletions
63
...c/main/java/com/google/cloud/spanner/connection/ClientSideStatementPartitionExecutor.java
This file contains 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,63 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.connection; | ||
|
||
import com.google.cloud.spanner.ErrorCode; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.Statement; | ||
import com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement; | ||
import com.google.cloud.spanner.connection.ClientSideStatementImpl.CompileException; | ||
import java.lang.reflect.Method; | ||
import java.util.regex.Matcher; | ||
|
||
/** Executor for <code>PARTITION <sql></code> statements. */ | ||
class ClientSideStatementPartitionExecutor implements ClientSideStatementExecutor { | ||
private final ClientSideStatementImpl statement; | ||
private final Method method; | ||
|
||
ClientSideStatementPartitionExecutor(ClientSideStatementImpl statement) throws CompileException { | ||
try { | ||
this.statement = statement; | ||
this.method = | ||
ConnectionStatementExecutor.class.getDeclaredMethod( | ||
statement.getMethodName(), Statement.class); | ||
} catch (Exception e) { | ||
throw new CompileException(e, statement); | ||
} | ||
} | ||
|
||
@Override | ||
public StatementResult execute( | ||
ConnectionStatementExecutor connection, ParsedStatement parsedStatement) throws Exception { | ||
String sql = getParameterValue(parsedStatement); | ||
return (StatementResult) | ||
method.invoke(connection, parsedStatement.getStatement().toBuilder().replace(sql).build()); | ||
} | ||
|
||
String getParameterValue(ParsedStatement parsedStatement) { | ||
Matcher matcher = statement.getPattern().matcher(parsedStatement.getSqlWithoutComments()); | ||
if (matcher.find() && matcher.groupCount() >= 2) { | ||
String space = matcher.group(1); | ||
String value = matcher.group(2); | ||
return (space + value).trim(); | ||
} | ||
throw SpannerExceptionFactory.newSpannerException( | ||
ErrorCode.INVALID_ARGUMENT, | ||
String.format( | ||
"Invalid argument for PARTITION: %s", parsedStatement.getStatement().getSql())); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...ain/java/com/google/cloud/spanner/connection/ClientSideStatementRunPartitionExecutor.java
This file contains 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,81 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.connection; | ||
|
||
import com.google.cloud.spanner.ErrorCode; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.Value; | ||
import com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement; | ||
import com.google.cloud.spanner.connection.ClientSideStatementImpl.CompileException; | ||
import com.google.common.base.Strings; | ||
import java.lang.reflect.Method; | ||
import java.util.regex.Matcher; | ||
|
||
/** Executor for <code>RUN PARTITION <partition_id></code> statements. */ | ||
class ClientSideStatementRunPartitionExecutor implements ClientSideStatementExecutor { | ||
private final ClientSideStatementImpl statement; | ||
private final Method method; | ||
|
||
ClientSideStatementRunPartitionExecutor(ClientSideStatementImpl statement) | ||
throws CompileException { | ||
try { | ||
this.statement = statement; | ||
this.method = | ||
ConnectionStatementExecutor.class.getDeclaredMethod( | ||
statement.getMethodName(), String.class); | ||
} catch (Exception e) { | ||
throw new CompileException(e, statement); | ||
} | ||
} | ||
|
||
@Override | ||
public StatementResult execute( | ||
ConnectionStatementExecutor connection, ParsedStatement parsedStatement) throws Exception { | ||
String partitionId = getParameterValue(parsedStatement); | ||
if (partitionId == null) { | ||
throw SpannerExceptionFactory.newSpannerException( | ||
ErrorCode.INVALID_ARGUMENT, | ||
"No valid partition id found in statement: " + parsedStatement.getStatement().getSql()); | ||
} | ||
return (StatementResult) method.invoke(connection, partitionId); | ||
} | ||
|
||
String getParameterValue(ParsedStatement parsedStatement) { | ||
// The statement has the form `RUN PARTITION ['partition-id']`. | ||
// The regex that is defined for this statement is (simplified) `run\s+partition(?:\s*'(.*)')?` | ||
// This regex has one capturing group, which captures the partition-id inside the single quotes. | ||
// That capturing group is however inside a non-capturing optional group. | ||
// That means that: | ||
// 1. If the matcher matches and returns one or more groups, we know that we have a partition-id | ||
// in the SQL statement itself, as that is the only thing that can be in a capturing group. | ||
// 2. If the matcher matches and returns zero groups, we know that the statement is valid, but | ||
// that it does not contain a partition-id in the SQL statement. The partition-id must then | ||
// be included in the statement as a query parameter. | ||
Matcher matcher = statement.getPattern().matcher(parsedStatement.getSqlWithoutComments()); | ||
if (matcher.find() && matcher.groupCount() >= 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Added some commentary to explain what is going on with this regex. |
||
String value = matcher.group(1); | ||
if (!Strings.isNullOrEmpty(value)) { | ||
return value; | ||
} | ||
} | ||
if (parsedStatement.getStatement().getParameters().size() == 1) { | ||
Value value = parsedStatement.getStatement().getParameters().values().iterator().next(); | ||
return value.getAsString(); | ||
} | ||
return null; | ||
} | ||
} |
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.
Suggestion -
StringBuilder
would be a bit more optimal as compared to+
operation.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.
The Java compiler automatically optimizes this internally, as it is not dynamic concatenation (e.g. in a loop or other hard-to-understand construct), so for these simple cases you should keep it as is. You will see that if you change this to using a StringBuilder, IntelliJ will even give you a warning.