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
Original file line number Diff line number Diff line change
Expand Up @@ -1035,4 +1035,30 @@ public void testLastStatement_Transaction_BatchDml() throws SQLException {
assertFalse(
mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class).get(0).getLastStatements());
}

@Test
public void testSetProtoDescriptorsFilePathUnsupported() throws SQLException {
if (dialect == Dialect.GOOGLE_STANDARD_SQL) {
try (Connection connection = createJdbcConnection();
Statement statement = connection.createStatement()) {
SQLException setException =
assertThrows(
SQLException.class,
() -> statement.execute("SET PROTO_DESCRIPTORS_FILE_PATH = 'descriptors.pb'"));
assertEquals(Code.INVALID_ARGUMENT.getNumber(), setException.getErrorCode());
assertTrue(
setException.getMessage().contains("SET PROTO_DESCRIPTORS_FILE_PATH is not supported"));

SQLException showException =
assertThrows(
SQLException.class,
() -> statement.execute("SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH"));
assertEquals(Code.INVALID_ARGUMENT.getNumber(), showException.getErrorCode());
assertTrue(
showException
.getMessage()
.contains("SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH is not supported"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,15 @@ START BATCH DDL;
ABORT BATCH;

NEW_CONNECTION;
-- Set proto descriptors using relative path to the descriptors.pb file. This gets applied for next DDL statement
-- Setting proto descriptors via file path is not supported and should throw an exception
@EXPECT EXCEPTION INVALID_ARGUMENT
SET PROTO_DESCRIPTORS_FILE_PATH = 'src/test/resources/com/google/cloud/spanner/jdbc/it/descriptors.pb';
-- Check if Proto descriptors is set
@EXPECT RESULT_SET 'PROTO_DESCRIPTORS'
SHOW VARIABLE PROTO_DESCRIPTORS;

@EXPECT EXCEPTION INVALID_ARGUMENT
SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH;

-- Set proto descriptors using base64 string. This gets applied for next DDL statement
SET PROTO_DESCRIPTORS = 'CvYCCgxzaW5nZXIucHJvdG8SFmV4YW1wbGVzLnNwYW5uZXIubXVzaWMi6gEKClNpbmdlckluZm8SIAoJc2luZ2VyX2lkGAEgASgDSABSCHNpbmdlcklkiAEBEiIKCmJpcnRoX2RhdGUYAiABKAlIAVIJYmlydGhEYXRliAEBEiUKC25hdGlvbmFsaXR5GAMgASgJSAJSC25hdGlvbmFsaXR5iAEBEjgKBWdlbnJlGAQgASgOMh0uZXhhbXBsZXMuc3Bhbm5lci5tdXNpYy5HZW5yZUgDUgVnZW5yZYgBAUIMCgpfc2luZ2VyX2lkQg0KC19iaXJ0aF9kYXRlQg4KDF9uYXRpb25hbGl0eUIICgZfZ2VucmUqLgoFR2VucmUSBwoDUE9QEAASCAoESkFaWhABEggKBEZPTEsQAhIICgRST0NLEANCKQoYY29tLmdvb2dsZS5jbG91ZC5zcGFubmVyQgtTaW5nZXJQcm90b1AAYgZwcm90bzM=';

CREATE PROTO BUNDLE (examples.spanner.music.Genre);
-- Check if Proto descriptors is reset to null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,25 +763,6 @@ public byte[] convert(String value) {
}
}

/** Converter for converting String that take in file path as input to String */
static class ProtoDescriptorsFileConverter implements ClientSideStatementValueConverter<String> {

public ProtoDescriptorsFileConverter(String allowedValues) {}

@Override
public Class<String> getParameterClass() {
return String.class;
}

@Override
public String convert(String filePath) {
if (Strings.isNullOrEmpty(filePath)) {
return null;
}
return filePath;
}
}

static class CredentialsProviderConverter
implements ClientSideStatementValueConverter<CredentialsProvider> {
static final CredentialsProviderConverter INSTANCE = new CredentialsProviderConverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.core.GaxProperties;
import com.google.cloud.ByteArray;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.AsyncResultSet;
import com.google.cloud.spanner.BatchClient;
Expand Down Expand Up @@ -105,9 +104,6 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
Expand Down Expand Up @@ -303,7 +299,6 @@ static UnitOfWorkType of(TransactionMode transactionMode) {
private RequestOptions.ClientContext clientContext;
private boolean excludeTxnFromChangeStreams;
private byte[] protoDescriptors;
private String protoDescriptorsFilePath;

/** Create a connection and register it in the SpannerPool. */
ConnectionImpl(ConnectionOptions options) {
Expand Down Expand Up @@ -537,7 +532,6 @@ private void reset(Context context, boolean inTransaction) {
this.connectionState.resetValue(DIRECTED_READ, context, inTransaction);
this.connectionState.resetValue(SAVEPOINT_SUPPORT, context, inTransaction);
this.protoDescriptors = null;
this.protoDescriptorsFilePath = null;
this.clientContext = null;

if (!isTransactionStarted()) {
Expand Down Expand Up @@ -1025,22 +1019,6 @@ public void setExcludeTxnFromChangeStreams(boolean excludeTxnFromChangeStreams)
@Override
public byte[] getProtoDescriptors() {
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
if (this.protoDescriptors == null && this.protoDescriptorsFilePath != null) {
// Read from file if filepath is valid
try {
File protoDescriptorsFile = new File(this.protoDescriptorsFilePath);
if (!protoDescriptorsFile.isFile()) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
String.format(
"File %s is not a valid proto descriptors file", this.protoDescriptorsFilePath));
}
InputStream pdStream = Files.newInputStream(protoDescriptorsFile.toPath());
this.protoDescriptors = ByteArray.copyFrom(pdStream).toByteArray();
} catch (Exception exception) {
throw SpannerExceptionFactory.newSpannerException(exception);
}
}
return this.protoDescriptors;
}

Expand All @@ -1051,21 +1029,6 @@ public void setProtoDescriptors(@Nonnull byte[] protoDescriptors) {
ConnectionPreconditions.checkState(
!isBatchActive(), "Proto descriptors cannot be set when a batch is active");
this.protoDescriptors = protoDescriptors;
this.protoDescriptorsFilePath = null;
}

void setProtoDescriptorsFilePath(@Nonnull String protoDescriptorsFilePath) {
Preconditions.checkNotNull(protoDescriptorsFilePath);
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ConnectionPreconditions.checkState(
!isBatchActive(), "Proto descriptors file path cannot be set when a batch is active");
this.protoDescriptorsFilePath = protoDescriptorsFilePath;
this.protoDescriptors = null;
}

String getProtoDescriptorsFilePath() {
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
return this.protoDescriptorsFilePath;
}

/**
Expand Down Expand Up @@ -2439,7 +2402,6 @@ private ApiFuture<Void> executeDdlAsync(CallType callType, ParsedStatement ddl)
ApiFuture<Void> result = getOrStartDdlUnitOfWork(ddl).executeDdlAsync(callType, ddl);
// reset proto descriptors after executing a DDL statement
this.protoDescriptors = null;
this.protoDescriptorsFilePath = null;
return result;
}

Expand Down Expand Up @@ -2551,7 +2513,6 @@ public ApiFuture<long[]> runBatchAsync() {
if (isDdlBatchActive()) {
// reset proto descriptors after executing a DDL batch
this.protoDescriptors = null;
this.protoDescriptorsFilePath = null;
}
this.batchMode = BatchMode.NONE;
setDefaultTransactionOptions(getDefaultIsolationLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_OPTIMIZER_STATISTICS_PACKAGE;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_OPTIMIZER_VERSION;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_PROTO_DESCRIPTORS;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_PROTO_DESCRIPTORS_FILE_PATH;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_READONLY;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_READ_LOCK_MODE;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SET_READ_ONLY_STALENESS;
Expand Down Expand Up @@ -75,7 +74,6 @@
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_OPTIMIZER_STATISTICS_PACKAGE;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_OPTIMIZER_VERSION;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_PROTO_DESCRIPTORS;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_PROTO_DESCRIPTORS_FILE_PATH;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_READONLY;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_READ_LOCK_MODE;
import static com.google.cloud.spanner.connection.StatementResult.ClientSideStatementType.SHOW_READ_ONLY_STALENESS;
Expand Down Expand Up @@ -794,9 +792,12 @@ public StatementResult statementSetProtoDescriptors(byte[] protoDescriptors) {

@Override
public StatementResult statementSetProtoDescriptorsFilePath(String filePath) {
Preconditions.checkNotNull(filePath);
getConnection().setProtoDescriptorsFilePath(filePath);
return noResult(SET_PROTO_DESCRIPTORS_FILE_PATH);
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"SET PROTO_DESCRIPTORS_FILE_PATH is not supported. To set proto descriptors, encode the"
+ " descriptors file as a base64 string and use the SET PROTO_DESCRIPTORS ="
+ " '<base64_encoded_proto_descriptors>' statement instead, or pass the descriptors"
+ " bytes directly using the Connection#setProtoDescriptors(byte[]) API.");
}

@Override
Expand All @@ -809,10 +810,10 @@ public StatementResult statementShowProtoDescriptors() {

@Override
public StatementResult statementShowProtoDescriptorsFilePath() {
return resultSet(
String.format("%sPROTO_DESCRIPTORS_FILE_PATH", getNamespace(connection.getDialect())),
getConnection().getProtoDescriptorsFilePath(),
SHOW_PROTO_DESCRIPTORS_FILE_PATH);
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH is not supported. Use SHOW VARIABLE"
+ " PROTO_DESCRIPTORS instead.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@
"propertyName": "PROTO_DESCRIPTORS_FILE_PATH",
"separator": "=",
"allowedValues": "'((\\S+)|())'",
"converterName": "ClientSideStatementValueConverters$ProtoDescriptorsFileConverter"
"converterName": "ClientSideStatementValueConverters$StringValueConverter"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,27 +271,40 @@ public static void closeLog() {
}
}

private static void logValid(
ClientSideStatementImpl statement, String sql, boolean unsupportedStatement) {
if (unsupportedStatement) {
log(statement.getExamplePrerequisiteStatements(), sql, ErrorCode.INVALID_ARGUMENT);
} else {
log(statement.getExamplePrerequisiteStatements(), sql);
}
}

/** Generates test statements for all {@link ClientSideStatement}s */
private static void generateTestStatements(
AbstractStatementParser parser, ClientSideStatementImpl statement) {
boolean unsupportedStatement =
statement.getStatementType() == ClientSideStatementType.SET_PROTO_DESCRIPTORS_FILE_PATH
|| statement.getStatementType()
== ClientSideStatementType.SHOW_PROTO_DESCRIPTORS_FILE_PATH;
for (String sql : statement.getExampleStatements()) {
log(statement.getExamplePrerequisiteStatements(), sql);
logValid(statement, sql, unsupportedStatement);
if (statement.getStatementType() != ClientSideStatementType.RUN_PARTITION
&& statement.getStatementType() != ClientSideStatementType.SET_DIRECTED_READ) {
// Partition ids are case-sensitive.
// DirectedReadOptions are case-sensitive.
log(statement.getExamplePrerequisiteStatements(), upper(sql));
log(statement.getExamplePrerequisiteStatements(), lower(sql));
logValid(statement, upper(sql), unsupportedStatement);
logValid(statement, lower(sql), unsupportedStatement);
}
log(statement.getExamplePrerequisiteStatements(), withLeadingSpaces(sql));
log(statement.getExamplePrerequisiteStatements(), withLeadingTabs(sql));
log(statement.getExamplePrerequisiteStatements(), withLeadingLinefeeds(sql));
log(statement.getExamplePrerequisiteStatements(), withTrailingSpaces(sql));
log(statement.getExamplePrerequisiteStatements(), withTrailingTabs(sql));
log(statement.getExamplePrerequisiteStatements(), withTrailingLinefeeds(sql));
log(statement.getExamplePrerequisiteStatements(), withSpaces(sql));
log(statement.getExamplePrerequisiteStatements(), withTabs(sql));
log(statement.getExamplePrerequisiteStatements(), withLinefeeds(sql));
logValid(statement, withLeadingSpaces(sql), unsupportedStatement);
logValid(statement, withLeadingTabs(sql), unsupportedStatement);
logValid(statement, withLeadingLinefeeds(sql), unsupportedStatement);
logValid(statement, withTrailingSpaces(sql), unsupportedStatement);
logValid(statement, withTrailingTabs(sql), unsupportedStatement);
logValid(statement, withTrailingLinefeeds(sql), unsupportedStatement);
logValid(statement, withSpaces(sql), unsupportedStatement);
logValid(statement, withTabs(sql), unsupportedStatement);
logValid(statement, withLinefeeds(sql), unsupportedStatement);

log(
statement.getExamplePrerequisiteStatements(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2054,40 +2054,35 @@ UnitOfWork getCurrentUnitOfWorkOrStartNewUnitOfWork(
connection.execute(Statement.of("SELECT FOO FROM BAR"));
assertArrayEquals(protoDescriptors, connection.getProtoDescriptors());

// proto descriptor file path should reset after executing a DDL statement
connection.setProtoDescriptorsFilePath(protoDescriptorsFilePath);
assertArrayEquals(protoDescriptors, connection.getProtoDescriptors());
connection.execute(Statement.of("CREATE PROTO BUNDLE (examples.spanner.music.SingerInfo)"));
assertNull(connection.getProtoDescriptors());
assertNull(connection.getProtoDescriptorsFilePath());

// proto descriptor file path should not reset if the statement is not a DDL statement
connection.setProtoDescriptorsFilePath(protoDescriptorsFilePath);
assertArrayEquals(protoDescriptors, connection.getProtoDescriptors());
connection.execute(Statement.of("SELECT FOO FROM BAR"));
assertArrayEquals(protoDescriptors, connection.getProtoDescriptors());
assertEquals(protoDescriptorsFilePath, connection.getProtoDescriptorsFilePath());

// test proto descriptor file path as input
connection.setProtoDescriptorsFilePath(protoDescriptorsFilePath);
assertArrayEquals(protoDescriptors, connection.getProtoDescriptors());
connection.execute(Statement.of("CREATE PROTO BUNDLE (examples.spanner.music.SingerInfo)"));
assertNull(connection.getProtoDescriptors());

// proto descriptor set through file path should overwrite the proto descriptor set from
// byte[]
connection.setProtoDescriptors("protoDescriptors".getBytes());
connection.setProtoDescriptorsFilePath(protoDescriptorsFilePath);
assertArrayEquals(protoDescriptors, connection.getProtoDescriptors());
connection.execute(Statement.of("CREATE PROTO BUNDLE (examples.spanner.music.SingerInfo)"));
assertNull(connection.getProtoDescriptors());

// proto descriptor set through byte[] should overwrite the proto descriptor from file path
connection.setProtoDescriptorsFilePath(protoDescriptorsFilePath);
connection.setProtoDescriptors("protoDescriptors".getBytes());
assertArrayEquals("protoDescriptors".getBytes(), connection.getProtoDescriptors());
connection.execute(Statement.of("CREATE PROTO BUNDLE (examples.spanner.music.SingerInfo)"));
assertNull(connection.getProtoDescriptors());
// SET PROTO_DESCRIPTORS_FILE_PATH should throw an unsupported exception explaining how to use
// SET PROTO_DESCRIPTORS
SpannerException setException =
assertThrows(
SpannerException.class,
() ->
connection.execute(
Statement.of(
String.format(
"SET PROTO_DESCRIPTORS_FILE_PATH = '%s'",
protoDescriptorsFilePath))));
assertEquals(ErrorCode.INVALID_ARGUMENT, setException.getErrorCode());
assertTrue(
setException.getMessage().contains("SET PROTO_DESCRIPTORS_FILE_PATH is not supported"));
assertTrue(
setException
.getMessage()
.contains("SET PROTO_DESCRIPTORS = '<base64_encoded_proto_descriptors>'"));

// SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH should throw an unsupported exception
SpannerException showException =
assertThrows(
SpannerException.class,
() -> connection.execute(Statement.of("SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH")));
assertEquals(ErrorCode.INVALID_ARGUMENT, showException.getErrorCode());
assertTrue(
showException
.getMessage()
.contains("SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH is not supported"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,11 @@ public void testStatementSetProtoDescriptors() {
@Test
public void testStatementSetProtoDescriptorsFilePath() {
String filePath = "com/google/cloud/spanner/descriptors.pb";
subject.statementSetProtoDescriptorsFilePath(filePath);
verify(connection).setProtoDescriptorsFilePath(filePath);
SpannerException exception =
assertThrows(
SpannerException.class, () -> subject.statementSetProtoDescriptorsFilePath(filePath));
assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode());
assertTrue(exception.getMessage().contains("SET PROTO_DESCRIPTORS_FILE_PATH is not supported"));
}

@Test
Expand All @@ -286,8 +289,13 @@ public void testStatementGetProtoDescriptors() {

@Test
public void testStatementGetProtoDescriptorsFilePath() {
subject.statementShowProtoDescriptorsFilePath();
verify(connection).getProtoDescriptorsFilePath();
SpannerException exception =
assertThrows(SpannerException.class, () -> subject.statementShowProtoDescriptorsFilePath());
assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode());
assertTrue(
exception
.getMessage()
.contains("SHOW VARIABLE PROTO_DESCRIPTORS_FILE_PATH is not supported"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,6 @@ public void testReset() {
"descriptor".getBytes(StandardCharsets.UTF_8),
connection::setProtoDescriptors,
connection::getProtoDescriptors);
assertResetProperty(
connection,
null,
"filename",
connection::setProtoDescriptorsFilePath,
connection::getProtoDescriptorsFilePath);

// Test the AutocommitDmlMode property that is only supported in auto-commit mode.
connection.rollback();
Expand Down
Loading
Loading