Skip to content
Open
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
12 changes: 11 additions & 1 deletion google-cloud-spanner/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -790,5 +790,15 @@
<className>com/google/cloud/spanner/connection/Connection</className>
<method>boolean isAutoBatchDmlUpdateCountVerification()</method>
</difference>

<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/connection/Connection</className>
<method>com.google.cloud.spanner.Dialect getConnectionDialect()</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/connection/Connection</className>
<method>void setConnectionDialect(com.google.cloud.spanner.Dialect)</method>
</difference>

</differences>
2 changes: 1 addition & 1 deletion google-cloud-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>6.81.2</version><!-- {x-version-update:google-cloud-spanner:current} -->
<version>6.81.2-SNAPSHOT</version><!-- {x-version-update:google-cloud-spanner:current} -->
<packaging>jar</packaging>
<name>Google Cloud Spanner</name>
<url>https://github.com/googleapis/java-spanner</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,14 @@ RequestOptions buildRequestOptions(Options options) {

ExecuteSqlRequest.Builder getExecuteSqlRequestBuilder(
Statement statement, QueryMode queryMode, Options options, boolean withTransactionSelector) {
String sql = statement.getSql();
// FIXME: This should be a request / query option parameter instead
if (options.hasDialect() && options.dialect() == Dialect.GOOGLE_STANDARD_SQL) {
sql = "/* GOOGLESQL */ " + sql;
}
ExecuteSqlRequest.Builder builder =
ExecuteSqlRequest.newBuilder()
.setSql(statement.getSql())
.setSql(sql)
.setQueryMode(queryMode)
.setSession(session.getName());
addParameters(builder, statement.getParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public static ReadQueryUpdateTransactionOption priority(RpcPriority priority) {
return new PriorityOption(priority);
}

public static ReadQueryUpdateTransactionOption dialect(Dialect dialect) {
return new DialectOption(dialect);
}

public static TransactionOption maxCommitDelay(Duration maxCommitDelay) {
Preconditions.checkArgument(!maxCommitDelay.isNegative(), "maxCommitDelay should be positive");
return new MaxCommitDelayOption(maxCommitDelay);
Expand Down Expand Up @@ -377,6 +381,20 @@ void appendToOptions(Options options) {
}
}

static final class DialectOption extends InternalOption
implements ReadQueryUpdateTransactionOption {
private final Dialect dialect;

DialectOption(Dialect dialect) {
this.dialect = dialect;
}

@Override
void appendToOptions(Options options) {
options.dialect = dialect;
}
}

static final class TagOption extends InternalOption implements ReadQueryUpdateTransactionOption {
private final String tag;

Expand Down Expand Up @@ -460,6 +478,7 @@ void appendToOptions(Options options) {
private String pageToken;
private String filter;
private RpcPriority priority;
private Dialect dialect;
private String tag;
private String etag;
private Boolean validateOnly;
Expand Down Expand Up @@ -541,6 +560,14 @@ Priority priority() {
return priority == null ? null : priority.proto;
}

boolean hasDialect() {
return dialect != null;
}

Dialect dialect() {
return dialect;
}

boolean hasTag() {
return tag != null;
}
Expand Down Expand Up @@ -693,6 +720,7 @@ public boolean equals(Object o) {
&& Objects.equals(pageToken(), that.pageToken())
&& Objects.equals(filter(), that.filter())
&& Objects.equals(priority(), that.priority())
&& Objects.equals(dialect(), that.dialect())
&& Objects.equals(tag(), that.tag())
&& Objects.equals(etag(), that.etag())
&& Objects.equals(validateOnly(), that.validateOnly())
Expand Down Expand Up @@ -760,6 +788,9 @@ public int hashCode() {
if (orderBy != null) {
result = 31 * result + orderBy.hashCode();
}
if (dialect != null) {
result = 31 * result + dialect.hashCode();
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,9 @@ static class DialectConverter implements ClientSideStatementValueConverter<Diale

private DialectConverter() {}

/** Constructor needed for reflection. */
public DialectConverter(String allowedValues) {}

@Override
public Class<Dialect> getParameterClass() {
return Dialect.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,14 @@ default Dialect getDialect() {
throw new UnsupportedOperationException("Not implemented");
}

default Dialect getConnectionDialect() {
throw new UnsupportedOperationException("Not implemented");
}

default void setConnectionDialect(Dialect dialect) {
throw new UnsupportedOperationException("Not implemented");
}

/** The {@link DatabaseClient} that is used by this {@link Connection}. */
@InternalApi
default DatabaseClient getDatabaseClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.cloud.spanner.connection.ConnectionProperties.DATA_BOOST_ENABLED;
import static com.google.cloud.spanner.connection.ConnectionProperties.DDL_IN_TRANSACTION_MODE;
import static com.google.cloud.spanner.connection.ConnectionProperties.DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE;
import static com.google.cloud.spanner.connection.ConnectionProperties.DIALECT;
import static com.google.cloud.spanner.connection.ConnectionProperties.DIRECTED_READ;
import static com.google.cloud.spanner.connection.ConnectionProperties.KEEP_TRANSACTION_ALIVE;
import static com.google.cloud.spanner.connection.ConnectionProperties.MAX_COMMIT_DELAY;
Expand Down Expand Up @@ -103,6 +104,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.Stack;
import java.util.UUID;
Expand All @@ -118,6 +120,7 @@

/** Implementation for {@link Connection}, the generic Spanner connection API (not JDBC). */
class ConnectionImpl implements Connection {

private static final String INSTRUMENTATION_SCOPE = "cloud.google.com/java";
private static final String SINGLE_USE_TRANSACTION = "SingleUseTransaction";
private static final String READ_ONLY_TRANSACTION = "ReadOnlyTransaction";
Expand Down Expand Up @@ -160,6 +163,7 @@ class ConnectionImpl implements Connection {
private static final ParsedStatement RELEASE_STATEMENT =
AbstractStatementParser.getInstance(Dialect.GOOGLE_STANDARD_SQL)
.parse(Statement.of("RELEASE s1"));
public static final String GOOGLESQL_DIALECT_HINT = "/* GOOGLESQL */ ";

/**
* Exception that is used to register the stacktrace of the code that opened a {@link Connection}.
Expand Down Expand Up @@ -383,7 +387,10 @@ private DdlClient createDdlClient() {

private AbstractStatementParser getStatementParser() {
if (this.statementParser == null) {
this.statementParser = AbstractStatementParser.getInstance(dbClient.getDialect());
Dialect dbDialect = dbClient.getDialect();
Dialect connDialect = this.getConnectionPropertyValue(DIALECT);
Dialect parserDialect = Optional.ofNullable(connDialect).orElse(dbDialect);
this.statementParser = AbstractStatementParser.getInstance(parserDialect);
}
return this.statementParser;
}
Expand Down Expand Up @@ -478,6 +485,7 @@ private void reset(Context context, boolean inTransaction) {
this.connectionState.resetValue(OPTIMIZER_VERSION, context, inTransaction);
this.connectionState.resetValue(OPTIMIZER_STATISTICS_PACKAGE, context, inTransaction);
this.connectionState.resetValue(RPC_PRIORITY, context, inTransaction);
this.connectionState.resetValue(DIALECT, context, inTransaction);
this.connectionState.resetValue(DDL_IN_TRANSACTION_MODE, context, inTransaction);
this.connectionState.resetValue(RETURN_COMMIT_STATS, context, inTransaction);
this.connectionState.resetValue(
Expand Down Expand Up @@ -527,6 +535,27 @@ public Dialect getDialect() {
return dbClient.getDialect();
}

@Override
public Dialect getConnectionDialect() {
return Optional.ofNullable(getConnectionPropertyValue(DIALECT))
.orElse(getDialect());
}

@Override
public void setConnectionDialect(Dialect dialect) {
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ConnectionPreconditions.checkState(!isBatchActive(), "Cannot set dialect while in a batch");
ConnectionPreconditions.checkState(
!isTransactionStarted(), "Cannot set dialect while a transaction is active");
ConnectionPreconditions.checkState(
!(isAutocommit() && isInTransaction()),
"Cannot set dialect while in a temporary transaction");
ConnectionPreconditions.checkState(
!transactionBeginMarked, "Cannot set dialect when a transaction has begun");
setConnectionPropertyValue(DIALECT, dialect);
clearLastTransactionAndSetDefaultTransactionOptions();
}

@Override
public DatabaseClient getDatabaseClient() {
return dbClient;
Expand Down Expand Up @@ -1576,6 +1605,16 @@ public PartitionedQueryResultSet runPartitionedQuery(
return new MergedResultSet(this, partitionIds, getMaxPartitionedParallelism());
}

// FIXME: This is added on all GoogleSQL dialect queries at the moment. This is only necessary for
// PostgreSQL database queries with GoogleSQL dialect override.
private Statement addDialectHintIfRequired(Statement stmt) {
Dialect connDialect = getConnectionPropertyValue(DIALECT);
if (connDialect == Dialect.GOOGLE_STANDARD_SQL) {
return stmt.toBuilder().replace(GOOGLESQL_DIALECT_HINT + stmt.getSql()).build();
}
return stmt;
}

/**
* Parses the given statement as a query and executes it. Throws a {@link SpannerException} if the
* statement is not a query.
Expand All @@ -1585,7 +1624,8 @@ private ResultSet parseAndExecuteQuery(
Preconditions.checkNotNull(query);
Preconditions.checkNotNull(analyzeMode);
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ParsedStatement parsedStatement = getStatementParser().parse(query, buildQueryOptions());
Statement updatedQuery = addDialectHintIfRequired(query);
ParsedStatement parsedStatement = getStatementParser().parse(updatedQuery, buildQueryOptions());
if (parsedStatement.isQuery() || parsedStatement.isUpdate()) {
switch (parsedStatement.getType()) {
case CLIENT_SIDE:
Expand Down Expand Up @@ -1624,7 +1664,8 @@ private AsyncResultSet parseAndExecuteQueryAsync(
CallType callType, Statement query, AnalyzeMode analyzeMode, QueryOption... options) {
Preconditions.checkNotNull(query);
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ParsedStatement parsedStatement = getStatementParser().parse(query, buildQueryOptions());
Statement updatedQuery = addDialectHintIfRequired(query);
ParsedStatement parsedStatement = getStatementParser().parse(updatedQuery, buildQueryOptions());
if (parsedStatement.isQuery() || parsedStatement.isUpdate()) {
switch (parsedStatement.getType()) {
case CLIENT_SIDE:
Expand Down Expand Up @@ -1678,7 +1719,8 @@ private boolean isInternalMetadataQuery(QueryOption... options) {
public long executeUpdate(Statement update) {
Preconditions.checkNotNull(update);
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ParsedStatement parsedStatement = getStatementParser().parse(update);
Statement updatedQuery = addDialectHintIfRequired(update);
ParsedStatement parsedStatement = getStatementParser().parse(updatedQuery);
if (parsedStatement.isUpdate()) {
switch (parsedStatement.getType()) {
case UPDATE:
Expand Down Expand Up @@ -1706,7 +1748,8 @@ public long executeUpdate(Statement update) {
public ApiFuture<Long> executeUpdateAsync(Statement update) {
Preconditions.checkNotNull(update);
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ParsedStatement parsedStatement = getStatementParser().parse(update);
Statement updatedQuery = addDialectHintIfRequired(update);
ParsedStatement parsedStatement = getStatementParser().parse(updatedQuery);
if (parsedStatement.isUpdate()) {
switch (parsedStatement.getType()) {
case UPDATE:
Expand Down Expand Up @@ -1855,6 +1898,9 @@ private QueryOption[] mergeQueryStatementTag(QueryOption... options) {

private QueryOption[] mergeQueryRequestOptions(
ParsedStatement parsedStatement, QueryOption... options) {
if (getConnectionPropertyValue(DIALECT) != null) {
options = appendQueryOption(options, Options.dialect(getConnectionPropertyValue(DIALECT)));
}
if (getConnectionPropertyValue(RPC_PRIORITY) != null) {
options =
appendQueryOption(options, Options.priority(getConnectionPropertyValue(RPC_PRIORITY)));
Expand Down Expand Up @@ -1903,6 +1949,14 @@ private UpdateOption[] mergeUpdateRequestOptions(UpdateOption... options) {
options[options.length - 1] = Options.priority(getConnectionPropertyValue(RPC_PRIORITY));
}
}
if (getConnectionPropertyValue(DIALECT) != null) {
if (options == null ||options.length == 0) {
options = new UpdateOption[] {Options.dialect(getConnectionPropertyValue(DIALECT))};
} else {
options = Arrays.copyOf(options, options.length + 1);
options[options.length - 1] = Options.dialect(getConnectionPropertyValue(DIALECT));
}
}
return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ class ConnectionProperties {
create(
DIALECT_PROPERTY_NAME,
"Sets the dialect to use for new databases that are created by this connection.",
Dialect.GOOGLE_STANDARD_SQL,
null,
DialectConverter.INSTANCE,
Context.STARTUP);
Context.USER);
static final ConnectionProperty<Boolean> TRACK_SESSION_LEAKS =
create(
TRACK_SESSION_LEAKS_PROPERTY_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner.connection;

import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.Options.RpcPriority;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.TimestampBound;
Expand Down Expand Up @@ -44,6 +45,10 @@ interface ConnectionStatementExecutor {

StatementResult statementShowReadOnly();

StatementResult statementSetDialect(Dialect dialect);

StatementResult statementShowDialect();

StatementResult statementSetRetryAbortsInternally(Boolean retryAbortsInternally);

StatementResult statementShowRetryAbortsInternally();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_DATA_BOOST_ENABLED;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_DEFAULT_TRANSACTION_ISOLATION;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_DIALECT;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_DIRECTED_READ;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_EXCLUDE_TXN_FROM_CHANGE_STREAMS;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_KEEP_TRANSACTION_ALIVE;
Expand Down Expand Up @@ -62,6 +63,7 @@
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_COMMIT_TIMESTAMP;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DATA_BOOST_ENABLED;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DIALECT;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_DIRECTED_READ;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_EXCLUDE_TXN_FROM_CHANGE_STREAMS;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_KEEP_TRANSACTION_ALIVE;
Expand Down Expand Up @@ -180,6 +182,21 @@ public StatementResult statementShowReadOnly() {
SHOW_READONLY);
}

@Override
public StatementResult statementSetDialect(Dialect dialect) {
Preconditions.checkNotNull(dialect);
getConnection().setConnectionDialect(dialect);
return noResult(SET_DIALECT);
}

@Override
public StatementResult statementShowDialect() {
return StatementResultImpl.resultSet(
String.format("%sDIALECT", getNamespace(connection.getDialect())),
getConnection().getConnectionDialect(),
SHOW_DIALECT);
}

@Override
public StatementResult statementSetRetryAbortsInternally(Boolean retryAbortsInternally) {
Preconditions.checkNotNull(retryAbortsInternally);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ enum ClientSideStatementType {
SET_AUTOCOMMIT,
SHOW_READONLY,
SET_READONLY,
SHOW_DIALECT,
SET_DIALECT,
SHOW_RETRY_ABORTS_INTERNALLY,
SET_RETRY_ABORTS_INTERNALLY,
SHOW_AUTOCOMMIT_DML_MODE,
Expand Down
Loading
Loading