-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support partitioned queries + data boost in Connection API (#2540)
* feat: support partitioned queries + data boost in Connection API Adds support for Partitioned Queries and Data Boost in the Connection API. This enables the use of these features in the JDBC driver and PGAdapter. * fix: match the correct group in regex * feat: add more SQL statements for partitioned queries * chore: refactor client side statements to accept the entire parsed statement Refactor the internal interface of client-side statements so these receive the entire parsed statement, including any query parameters in the statement. This allows us to create client-side statements that actually use the query parameters that have been specified by the user. * chore: simplify test * chore: cleanup differences * chore: cleanup unrelated changes * fix: update converter name * test: add more tests * chore: add missing license header * fix: handle empty partitioned queries correctly * fix: do not use any random staleness for partitioned queries * fix: only return false for next() if all have finished * chore: rename to autoPartitionMode * chore: rename sql statements + add tests for empty results * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: address review comments * Batch read connection api native adjustments (#2569) * chore: add ClientSideStatementPartitionExecutor to SpannerFeature * chore: wrap AbstractStatementParser static initialization in try/catch * chore: add ClientSideStatementRunPartitionExecutor to SpannerFeature * chore: add ClientSideStatementRunPartitionedQueryExecutor to SpannerFeature * chore: lint formatting --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Burke Davison <[email protected]>
- Loading branch information
1 parent
b59940c
commit 4e31d04
Showing
42 changed files
with
39,843 additions
and
25,929 deletions.
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
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) { | ||
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.