Skip to content

chore: include client statement type in parsed statement #1849

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 5 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -270,6 +272,16 @@ public boolean isDdl() {
return false;
}

/**
* Returns the {@link ClientSideStatementType} of this statement. This method may only be called
* on statements of type {@link StatementType#CLIENT_SIDE}.
*/
@InternalApi
public ClientSideStatementType getClientSideStatementType() {
Preconditions.checkState(type == StatementType.CLIENT_SIDE);
return clientSideStatement.getStatementType();
}

Statement getStatement() {
return statement;
}
Expand Down Expand Up @@ -314,7 +326,12 @@ ClientSideStatement getClientSideStatement() {
private final Set<ClientSideStatementImpl> statements;

AbstractStatementParser(Set<ClientSideStatementImpl> statements) {
this.statements = statements;
this.statements = Collections.unmodifiableSet(statements);
}

@VisibleForTesting
Set<ClientSideStatementImpl> getClientSideStatements() {
return statements;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.spanner.connection;

import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType;
import java.util.List;

/**
Expand Down Expand Up @@ -47,6 +48,9 @@ interface ClientSideStatement {
/** @return <code>true</code> if this {@link ClientSideStatement} will return an update count. */
boolean isUpdate();

/** @return the statement type */
ClientSideStatementType getStatementType();

/**
* Execute this {@link ClientSideStatement} on the given {@link ConnectionStatementExecutor}. The
* executor calls the appropriate method(s) on the {@link Connection}. The statement argument is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.spanner.connection;

import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType;
import com.google.cloud.spanner.connection.StatementResult.ResultType;
import com.google.common.base.Preconditions;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -106,6 +107,8 @@ public String getMessage() {
/** The result type of this statement. */
private ResultType resultType;

private ClientSideStatementType statementType;

/** The regular expression that should be used to recognize this class of statements. */
private String regex;

Expand Down Expand Up @@ -183,6 +186,11 @@ public boolean isUpdate() {
return resultType == ResultType.UPDATE_COUNT;
}

@Override
public ClientSideStatementType getStatementType() {
return statementType;
}

boolean matches(String statement) {
Preconditions.checkState(pattern != null, "This statement has not been compiled");
return pattern.matcher(statement).matches();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"name": "SHOW VARIABLE AUTOCOMMIT",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_AUTOCOMMIT",
"regex": "(?is)\\A\\s*show\\s+variable\\s+autocommit\\s*\\z",
"method": "statementShowAutocommit",
"exampleStatements": ["show variable autocommit"]
Expand All @@ -13,6 +14,7 @@
"name": "SHOW VARIABLE READONLY",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_READONLY",
"regex": "(?is)\\A\\s*show\\s+variable\\s+readonly\\s*\\z",
"method": "statementShowReadOnly",
"exampleStatements": ["show variable readonly"]
Expand All @@ -21,6 +23,7 @@
"name": "SHOW VARIABLE RETRY_ABORTS_INTERNALLY",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_RETRY_ABORTS_INTERNALLY",
"regex": "(?is)\\A\\s*show\\s+variable\\s+retry_aborts_internally\\s*\\z",
"method": "statementShowRetryAbortsInternally",
"exampleStatements": ["show variable retry_aborts_internally"],
Expand All @@ -30,6 +33,7 @@
"name": "SHOW VARIABLE AUTOCOMMIT_DML_MODE",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_AUTOCOMMIT_DML_MODE",
"regex": "(?is)\\A\\s*show\\s+variable\\s+autocommit_dml_mode\\s*\\z",
"method": "statementShowAutocommitDmlMode",
"exampleStatements": ["show variable autocommit_dml_mode"]
Expand All @@ -38,6 +42,7 @@
"name": "SHOW VARIABLE STATEMENT_TIMEOUT",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_STATEMENT_TIMEOUT",
"regex": "(?is)\\A\\s*show\\s+variable\\s+statement_timeout\\s*\\z",
"method": "statementShowStatementTimeout",
"exampleStatements": ["show variable statement_timeout"]
Expand All @@ -46,6 +51,7 @@
"name": "SHOW VARIABLE READ_TIMESTAMP",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_READ_TIMESTAMP",
"regex": "(?is)\\A\\s*show\\s+variable\\s+read_timestamp\\s*\\z",
"method": "statementShowReadTimestamp",
"exampleStatements": ["show variable read_timestamp"],
Expand All @@ -55,6 +61,7 @@
"name": "SHOW VARIABLE COMMIT_TIMESTAMP",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_COMMIT_TIMESTAMP",
"regex": "(?is)\\A\\s*show\\s+variable\\s+commit_timestamp\\s*\\z",
"method": "statementShowCommitTimestamp",
"exampleStatements": ["show variable commit_timestamp"],
Expand All @@ -64,6 +71,7 @@
"name": "SHOW VARIABLE READ_ONLY_STALENESS",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_READ_ONLY_STALENESS",
"regex": "(?is)\\A\\s*show\\s+variable\\s+read_only_staleness\\s*\\z",
"method": "statementShowReadOnlyStaleness",
"exampleStatements": ["show variable read_only_staleness"]
Expand All @@ -72,6 +80,7 @@
"name": "SHOW VARIABLE OPTIMIZER_VERSION",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_OPTIMIZER_VERSION",
"regex": "(?is)\\A\\s*show\\s+variable\\s+optimizer_version\\s*\\z",
"method": "statementShowOptimizerVersion",
"exampleStatements": ["show variable optimizer_version"]
Expand All @@ -80,6 +89,7 @@
"name": "SHOW VARIABLE OPTIMIZER_STATISTICS_PACKAGE",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_OPTIMIZER_STATISTICS_PACKAGE",
"regex": "(?is)\\A\\s*show\\s+variable\\s+optimizer_statistics_package\\s*\\z",
"method": "statementShowOptimizerStatisticsPackage",
"exampleStatements": ["show variable optimizer_statistics_package"]
Expand All @@ -88,6 +98,7 @@
"name": "SHOW VARIABLE RETURN_COMMIT_STATS",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_RETURN_COMMIT_STATS",
"regex": "(?is)\\A\\s*show\\s+variable\\s+return_commit_stats\\s*\\z",
"method": "statementShowReturnCommitStats",
"exampleStatements": ["show variable return_commit_stats"]
Expand All @@ -96,6 +107,7 @@
"name": "SHOW VARIABLE COMMIT_RESPONSE",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_COMMIT_RESPONSE",
"regex": "(?is)\\A\\s*show\\s+variable\\s+commit_response\\s*\\z",
"method": "statementShowCommitResponse",
"exampleStatements": ["show variable commit_response"],
Expand All @@ -105,6 +117,7 @@
"name": "SHOW VARIABLE STATEMENT_TAG",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_STATEMENT_TAG",
"regex": "(?is)\\A\\s*show\\s+variable\\s+statement_tag\\s*\\z",
"method": "statementShowStatementTag",
"exampleStatements": ["show variable statement_tag"]
Expand All @@ -113,6 +126,7 @@
"name": "SHOW VARIABLE TRANSACTION_TAG",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_TRANSACTION_TAG",
"regex": "(?is)\\A\\s*show\\s+variable\\s+transaction_tag\\s*\\z",
"method": "statementShowTransactionTag",
"exampleStatements": ["show variable transaction_tag"]
Expand All @@ -121,6 +135,7 @@
"name": "SHOW VARIABLE RPC_PRIORITY",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "RESULT_SET",
"statementType": "SHOW_TRANSACTION_TAG",
"regex": "(?is)\\A\\s*show\\s+variable\\s+rpc_priority\\s*\\z",
"method": "statementShowRPCPriority",
"exampleStatements": ["show variable rpc_priority"]
Expand All @@ -129,6 +144,7 @@
"name": "BEGIN TRANSACTION",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "BEGIN",
"regex": "(?is)\\A\\s*(?:begin|start)(?:\\s+transaction)?\\s*\\z",
"method": "statementBeginTransaction",
"exampleStatements": ["begin", "start", "begin transaction", "start transaction"]
Expand All @@ -137,6 +153,7 @@
"name": "COMMIT TRANSACTION",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "COMMIT",
"regex": "(?is)\\A\\s*(?:commit)(?:\\s+transaction)?\\s*\\z",
"method": "statementCommit",
"exampleStatements": ["commit", "commit transaction"],
Expand All @@ -146,6 +163,7 @@
"name": "ROLLBACK TRANSACTION",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "ROLLBACK",
"regex": "(?is)\\A\\s*(?:rollback)(?:\\s+transaction)?\\s*\\z",
"method": "statementRollback",
"exampleStatements": ["rollback", "rollback transaction"],
Expand All @@ -155,6 +173,7 @@
"name": "START BATCH DDL",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "START_BATCH_DDL",
"regex": "(?is)\\A\\s*(?:start)(?:\\s+batch)(?:\\s+ddl)\\s*\\z",
"method": "statementStartBatchDdl",
"exampleStatements": ["start batch ddl"]
Expand All @@ -163,6 +182,7 @@
"name": "START BATCH DML",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "START_BATCH_DML",
"regex": "(?is)\\A\\s*(?:start)(?:\\s+batch)(?:\\s+dml)\\s*\\z",
"method": "statementStartBatchDml",
"exampleStatements": ["start batch dml"]
Expand All @@ -171,6 +191,7 @@
"name": "RUN BATCH",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "RUN_BATCH",
"regex": "(?is)\\A\\s*(?:run)(?:\\s+batch)\\s*\\z",
"method": "statementRunBatch",
"exampleStatements": ["run batch"],
Expand All @@ -180,6 +201,7 @@
"name": "ABORT BATCH",
"executorName": "ClientSideStatementNoParamExecutor",
"resultType": "NO_RESULT",
"statementType": "ABORT_BATCH",
"regex": "(?is)\\A\\s*(?:abort)(?:\\s+batch)\\s*\\z",
"method": "statementAbortBatch",
"exampleStatements": ["abort batch"],
Expand All @@ -189,6 +211,7 @@
"name": "SET AUTOCOMMIT = TRUE|FALSE",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_AUTOCOMMIT",
"regex": "(?is)\\A\\s*set\\s+autocommit\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetAutocommit",
"exampleStatements": ["set autocommit = true", "set autocommit = false"],
Expand All @@ -203,6 +226,7 @@
"name": "SET READONLY = TRUE|FALSE",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_READONLY",
"regex": "(?is)\\A\\s*set\\s+readonly\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetReadOnly",
"exampleStatements": ["set readonly = true", "set readonly = false"],
Expand All @@ -217,6 +241,7 @@
"name": "SET RETRY_ABORTS_INTERNALLY = TRUE|FALSE",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_RETRY_ABORTS_INTERNALLY",
"regex": "(?is)\\A\\s*set\\s+retry_aborts_internally\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetRetryAbortsInternally",
"exampleStatements": ["set retry_aborts_internally = true", "set retry_aborts_internally = false"],
Expand All @@ -232,6 +257,7 @@
"name": "SET AUTOCOMMIT_DML_MODE = 'PARTITIONED_NON_ATOMIC'|'TRANSACTIONAL'",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_AUTOCOMMIT_DML_MODE",
"regex": "(?is)\\A\\s*set\\s+autocommit_dml_mode\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetAutocommitDmlMode",
"exampleStatements": ["set autocommit_dml_mode='PARTITIONED_NON_ATOMIC'", "set autocommit_dml_mode='TRANSACTIONAL'"],
Expand All @@ -246,6 +272,7 @@
"name": "SET STATEMENT_TIMEOUT = '<duration>'|NULL",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_STATEMENT_TIMEOUT",
"regex": "(?is)\\A\\s*set\\s+statement_timeout\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetStatementTimeout",
"exampleStatements": ["set statement_timeout=null", "set statement_timeout='1s'", "set statement_timeout='100ms'", "set statement_timeout='10000us'", "set statement_timeout='9223372036854775807ns'"],
Expand All @@ -260,6 +287,7 @@
"name": "SET TRANSACTION READ ONLY|READ WRITE",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_TRANSACTION_MODE",
"regex": "(?is)\\A\\s*set\\s+transaction\\s*(?:\\s+)\\s*(.*)\\z",
"method": "statementSetTransactionMode",
"exampleStatements": ["set transaction read only", "set transaction read write"],
Expand All @@ -275,6 +303,7 @@
"name": "SET READ_ONLY_STALENESS = 'STRONG' | 'MIN_READ_TIMESTAMP <timestamp>' | 'READ_TIMESTAMP <timestamp>' | 'MAX_STALENESS <int64>s|ms|us|ns' | 'EXACT_STALENESS (<int64>s|ms|us|ns)'",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_READ_ONLY_STALENESS",
"regex": "(?is)\\A\\s*set\\s+read_only_staleness\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetReadOnlyStaleness",
"exampleStatements": ["set read_only_staleness='STRONG'",
Expand Down Expand Up @@ -303,6 +332,7 @@
"name": "SET OPTIMIZER_VERSION = '<version>'|'LATEST'|''",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_OPTIMIZER_VERSION",
"regex": "(?is)\\A\\s*set\\s+optimizer_version\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetOptimizerVersion",
"exampleStatements": ["set optimizer_version='1'", "set optimizer_version='200'", "set optimizer_version='LATEST'", "set optimizer_version=''"],
Expand All @@ -317,6 +347,7 @@
"name": "SET OPTIMIZER_STATISTICS_PACKAGE = '<package>'|''",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_OPTIMIZER_STATISTICS_PACKAGE",
"regex": "(?is)\\A\\s*set\\s+optimizer_statistics_package\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetOptimizerStatisticsPackage",
"exampleStatements": ["set optimizer_statistics_package='auto_20191128_14_47_22UTC'", "set optimizer_statistics_package=''"],
Expand All @@ -331,6 +362,7 @@
"name": "SET RETURN_COMMIT_STATS = TRUE|FALSE",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_RETURN_COMMIT_STATS",
"regex": "(?is)\\A\\s*set\\s+return_commit_stats\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetReturnCommitStats",
"exampleStatements": ["set return_commit_stats = true", "set return_commit_stats = false"],
Expand All @@ -345,6 +377,7 @@
"name": "SET STATEMENT_TAG = '<tag>'",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_STATEMENT_TAG",
"regex": "(?is)\\A\\s*set\\s+statement_tag\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetStatementTag",
"exampleStatements": ["set statement_tag='tag1'", "set statement_tag='tag2'", "set statement_tag=''"],
Expand All @@ -359,6 +392,7 @@
"name": "SET TRANSACTION_TAG = '<tag>'",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_TRANSACTION_TAG",
"regex": "(?is)\\A\\s*set\\s+transaction_tag\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetTransactionTag",
"exampleStatements": ["set transaction_tag='tag1'", "set transaction_tag='tag2'", "set transaction_tag=''"],
Expand All @@ -374,6 +408,7 @@
"name": "SET RPC_PRIORITY = 'HIGH'|'MEDIUM'|'LOW'|'NULL'",
"executorName": "ClientSideStatementSetExecutor",
"resultType": "NO_RESULT",
"statementType": "SET_RPC_PRIORITY",
"regex": "(?is)\\A\\s*set\\s+rpc_priority\\s*(?:=)\\s*(.*)\\z",
"method": "statementSetRPCPriority",
"exampleStatements": [
Expand Down
Loading