Skip to content
Closed
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 @@ -17,6 +17,7 @@
package com.google.cloud.spanner.connection;

import static com.google.cloud.spanner.connection.SimpleParser.isValidIdentifierChar;
import static com.google.cloud.spanner.connection.SimpleParser.isValidIdentifierFirstChar;
import static com.google.cloud.spanner.connection.StatementHintParser.convertHintsToOptions;

import com.google.api.core.InternalApi;
Expand Down Expand Up @@ -570,15 +571,16 @@ ParsedStatement parse(Statement statement, QueryOptions defaultQueryOptions) {
}

ParsedStatement internalParse(Statement statement, QueryOptions defaultQueryOptions) {
String sql = statement.getSql();
StatementHintParser statementHintParser = new StatementHintParser(getDialect(), sql);
StatementHintParser statementHintParser =
new StatementHintParser(getDialect(), statement.getSql());
ReadQueryUpdateTransactionOption[] optionsFromHints = EMPTY_OPTIONS;
if (statementHintParser.hasStatementHints()
&& !statementHintParser.getClientSideStatementHints().isEmpty()) {
statement =
statement.toBuilder().replace(statementHintParser.getSqlWithoutClientSideHints()).build();
optionsFromHints = convertHintsToOptions(statementHintParser.getClientSideStatementHints());
}
String sql = statement.getSql();
// Create a supplier that will actually remove all comments and hints from the SQL string to be
// backwards compatible with anything that really needs the SQL string without comments.
Supplier<String> sqlWithoutCommentsSupplier =
Expand Down Expand Up @@ -941,12 +943,13 @@ public boolean checkReturningClause(String sql) {
String parseDollarQuotedString(String sql, int index) {
// Look ahead to the next dollar sign (if any). Everything in between is the quote tag.
StringBuilder tag = new StringBuilder();
int startIndex = index;
while (index < sql.length()) {
char c = sql.charAt(index);
if (c == DOLLAR) {
return tag.toString();
}
if (!isValidIdentifierChar(c)) {
if (index == startIndex ? !isValidIdentifierFirstChar(c) : !isValidIdentifierChar(c)) {
break;
}
tag.append(c);
Expand All @@ -970,7 +973,9 @@ int skip(String sql, int currentIndex, @Nullable StringBuilder result) {
|| (supportsBacktickQuote() && currentChar == BACKTICK_QUOTE)) {
appendIfNotNull(result, currentChar);
return skipQuoted(sql, currentIndex, currentChar, result);
} else if (supportsDollarQuotedStrings() && currentChar == DOLLAR) {
} else if (supportsDollarQuotedStrings()
&& currentChar == DOLLAR
&& (currentIndex == 0 || !isValidIdentifierChar(sql.charAt(currentIndex - 1)))) {
String dollarTag = parseDollarQuotedString(sql, currentIndex + 1);
if (dollarTag != null) {
appendIfNotNull(result, currentChar, dollarTag, currentChar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,7 @@ private StatementResult internalExecute(
internalExecuteQuery(CallType.SYNC, parsedStatement, AnalyzeMode.NONE));
case UPDATE:
if (parsedStatement.hasReturningClause()) {
checkDmlReturningAllowed(parsedStatement);
return StatementResultImpl.of(
internalExecuteQuery(CallType.SYNC, parsedStatement, AnalyzeMode.NONE));
}
Expand Down Expand Up @@ -1574,6 +1575,7 @@ public AsyncStatementResult executeAsync(Statement statement) {
internalExecuteQueryAsync(CallType.ASYNC, parsedStatement, AnalyzeMode.NONE));
case UPDATE:
if (parsedStatement.hasReturningClause()) {
checkDmlReturningAllowed(parsedStatement);
return AsyncStatementResultImpl.of(
internalExecuteQueryAsync(CallType.ASYNC, parsedStatement, AnalyzeMode.NONE));
}
Expand Down Expand Up @@ -1780,16 +1782,7 @@ private ResultSet parseAndExecuteQuery(
return internalExecuteQuery(callType, parsedStatement, analyzeMode, options);
case UPDATE:
if (parsedStatement.hasReturningClause()) {
// Cannot execute DML statement with returning clause in read-only mode or in
// READ_ONLY_TRANSACTION transaction mode.
if (this.isReadOnly()
|| (this.isInTransaction()
&& this.getTransactionMode() == TransactionMode.READ_ONLY_TRANSACTION)) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.FAILED_PRECONDITION,
"DML statement with returning clause cannot be executed in read-only mode: "
+ parsedStatement.getSql());
}
checkDmlReturningAllowed(parsedStatement);
return internalExecuteQuery(callType, parsedStatement, analyzeMode, options);
}
case DDL:
Expand All @@ -1802,6 +1795,19 @@ private ResultSet parseAndExecuteQuery(
"Statement is not a query or DML with returning clause: " + parsedStatement.getSql());
}

private void checkDmlReturningAllowed(ParsedStatement parsedStatement) {
// Cannot execute DML statement with returning clause in read-only mode or in
// READ_ONLY_TRANSACTION transaction mode.
if (this.isReadOnly()
|| (this.isInTransaction()
&& this.getTransactionMode() == TransactionMode.READ_ONLY_TRANSACTION)) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.FAILED_PRECONDITION,
"DML statement with returning clause cannot be executed in read-only mode: "
+ parsedStatement.getSql());
}
}

private AsyncResultSet parseAndExecuteQueryAsync(Statement query, QueryOption... options) {
Preconditions.checkNotNull(query);
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
Expand All @@ -1821,16 +1827,7 @@ private AsyncResultSet parseAndExecuteQueryAsync(Statement query, QueryOption...
CallType.ASYNC, parsedStatement, AnalyzeMode.NONE, options);
case UPDATE:
if (parsedStatement.hasReturningClause()) {
// Cannot execute DML statement with returning clause in read-only mode or in
// READ_ONLY_TRANSACTION transaction mode.
if (this.isReadOnly()
|| (this.isInTransaction()
&& this.getTransactionMode() == TransactionMode.READ_ONLY_TRANSACTION)) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.FAILED_PRECONDITION,
"DML statement with returning clause cannot be executed in read-only mode: "
+ parsedStatement.getSql());
}
checkDmlReturningAllowed(parsedStatement);
return internalExecuteQueryAsync(
CallType.ASYNC, parsedStatement, AnalyzeMode.NONE, options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.cloud.spanner.connection.AbstractStatementParser.SLASH;

import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.SpannerException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.util.Objects;
Expand Down Expand Up @@ -201,24 +202,33 @@ Result eatIdentifier() {
return Result.found(sql.substring(startPos, pos));
}

/**
* Eats a single-quoted string. This implementation currently does not support escape sequences.
*/
/** Eats a single-quoted string using the dialect's quote-escaping rules. */
Result eatSingleQuotedString() {
if (!eatToken('\'')) {
return Result.NOT_FOUND;
}
int startPos = pos;
while (pos < sql.length() && sql.charAt(pos) != '\'') {
if (sql.charAt(pos) == '\n') {
return Result.NOT_FOUND;
}
pos++;
if (statementParser.supportsTripleQuotedStrings()
&& pos + 1 < sql.length()
&& sql.charAt(pos) == '\''
&& sql.charAt(pos + 1) == '\'') {
pos = sql.length();
return Result.NOT_FOUND;
}
if (pos == sql.length()) {
final int endPos;
try {
endPos = statementParser.skipQuoted(sql, pos - 1, '\'', null, null);
} catch (SpannerException e) {
pos = sql.length();
return Result.NOT_FOUND;
}
String value = sql.substring(pos, endPos - 1);
if (value.indexOf('\n') != -1
|| (treatHintCommentsAsTokens && (value.contains("/*") || value.contains("*/")))) {
pos = sql.length();
return Result.NOT_FOUND;
}
return Result.found(sql.substring(startPos, pos++));
pos = endPos;
return Result.found(value);
}

boolean peekTokens(char... tokens) {
Expand Down Expand Up @@ -394,13 +404,16 @@ boolean skipMultiLineComment() {
&& sql.length() > (pos + 1)
&& sql.charAt(pos + 1) == ASTERISK) {
level++;
pos += 2;
continue;
}
if (sql.charAt(pos) == ASTERISK && sql.length() > (pos + 1) && sql.charAt(pos + 1) == SLASH) {
level--;
pos += 2;
if (level == 0) {
pos += 2;
return true;
}
continue;
}
pos++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public ApiFuture<ResultSet> executeQueryAsync(
&& (analyzeMode != AnalyzeMode.NONE || statement.hasReturningClause())),
"The statement must be a query, or the statement must be DML and AnalyzeMode must be PLAN"
+ " or PROFILE");
if (statement.isUpdate()) {
ConnectionPreconditions.checkState(
!isReadOnly(), "Update statements are not allowed in read-only mode");
}
try (Scope ignore = span.makeCurrent()) {
checkAndMarkUsed();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ private static Tuple<String, Map<String, String>> extract(
return Tuple.of(updatedSql, builder.build());
}

/** Eats a hint literal. This is a literal that could be a quoted string, or an identifier. */
/**
* Eats a hint literal. This is a literal that could be a single-quoted string (with dialect
* escape handling) or an identifier.
*/
private static Result eatHintLiteral(SimpleParser parser) {
if (parser.peekToken('\'')) {
return parser.eatSingleQuotedString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2087,4 +2087,79 @@ UnitOfWork getCurrentUnitOfWorkOrStartNewUnitOfWork(
assertNull(connection.getProtoDescriptors());
}
}

@Test
public void testDmlReturningNotAllowedInReadOnlyMode() {
String dmlReturning = "INSERT INTO FOO (ID, NAME) VALUES (1, 'One') THEN RETURN ID";
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build())) {
subject.setReadOnly(true);
for (boolean autocommit : new boolean[] {true, false}) {
subject.setAutocommit(autocommit);
SpannerException ex1 =
assertThrows(SpannerException.class, () -> subject.execute(Statement.of(dmlReturning)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex1.getErrorCode());

SpannerException ex2 =
assertThrows(
SpannerException.class, () -> subject.executeAsync(Statement.of(dmlReturning)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex2.getErrorCode());

SpannerException ex3 =
assertThrows(
SpannerException.class, () -> subject.executeQuery(Statement.of(dmlReturning)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex3.getErrorCode());

SpannerException ex4 =
assertThrows(
SpannerException.class,
() -> subject.executeQueryAsync(Statement.of(dmlReturning)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex4.getErrorCode());
}

// Also verify when connection is read-write, but active transaction is READ_ONLY_TRANSACTION.
subject.setReadOnly(false);
subject.setAutocommit(false);
subject.setTransactionMode(TransactionMode.READ_ONLY_TRANSACTION);
SpannerException ex5 =
assertThrows(SpannerException.class, () -> subject.execute(Statement.of(dmlReturning)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex5.getErrorCode());

SpannerException ex6 =
assertThrows(
SpannerException.class, () -> subject.executeAsync(Statement.of(dmlReturning)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex6.getErrorCode());
}
}

@Test
public void testHintWithEscapedBackslashDoesNotBypassReadOnly() {
String hintWithEscapedSingleQuote =
"@{statement_tag='\\'} INSERT INTO FOO (ID) VALUES (1) -- '} SELECT 1";
String hintWithEscapedBackslash =
"@{statement_tag='\\\\'} INSERT INTO FOO (ID) VALUES (1) -- '} SELECT 1";
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build())) {
subject.setReadOnly(true);
AbstractStatementParser.ParsedStatement parsedEscapedQuote =
AbstractStatementParser.getInstance(subject.getDialect())
.parse(Statement.of(hintWithEscapedSingleQuote));
assertEquals(StatementType.QUERY, parsedEscapedQuote.getType());
assertEquals(" SELECT 1", parsedEscapedQuote.getStatement().getSql());

SpannerException ex =
assertThrows(
SpannerException.class,
() -> subject.execute(Statement.of(hintWithEscapedBackslash)));
assertEquals(ErrorCode.FAILED_PRECONDITION, ex.getErrorCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,54 @@ static String skipHint(String sql) {
parser.skipHint();
return parser.getSql().substring(parser.getPos());
}

@Test(timeout = 5000)
public void testEatSingleQuotedStringEscapesAndCommentTokens() {
if (dialect == Dialect.GOOGLE_STANDARD_SQL) {
assertEquals(found("foo\\'bar"), parserFor("'foo\\'bar'").eatSingleQuotedString());
assertEquals(found("foo\\\\"), parserFor("'foo\\\\'").eatSingleQuotedString());
assertEquals(
found("\\'} INSERT INTO t VALUES (1) -- "),
parserFor("'\\'} INSERT INTO t VALUES (1) -- '}").eatSingleQuotedString());
assertEquals(NOT_FOUND, parserFor("'''triple'''").eatSingleQuotedString());
assertEquals(found("foo"), parserFor("'foo''bar'").eatSingleQuotedString());
} else {
assertEquals(found("foo''bar"), parserFor("'foo''bar'").eatSingleQuotedString());
assertEquals(found("foo\\bar"), parserFor("'foo\\bar'").eatSingleQuotedString());
assertEquals(found("''foo''"), parserFor("'''foo'''").eatSingleQuotedString());
assertEquals(
NOT_FOUND,
new SimpleParser(Dialect.POSTGRESQL, "'has */ inside'", 0, true).eatSingleQuotedString());
assertEquals(
NOT_FOUND,
new SimpleParser(Dialect.POSTGRESQL, "'has /* inside'", 0, true).eatSingleQuotedString());
assertEquals(
NOT_FOUND,
new SimpleParser(Dialect.POSTGRESQL, "'line1\nline2'", 0, false).eatSingleQuotedString());
}
}

@Test(timeout = 5000)
public void testSkipMultiLineCommentOverlappingAndNested() {
SimpleParser singleLineWithCommentToken = parserFor("-- /* x\nSELECT 1");
singleLineWithCommentToken.skipWhitespaces();
assertEquals("-- /* x\n".length(), singleLineWithCommentToken.getPos());

SimpleParser trailingCloseComment = parserFor("/* a */*/ SELECT 1");
assertTrue(trailingCloseComment.skipMultiLineComment());
assertEquals("/* a */".length(), trailingCloseComment.getPos());

SimpleParser overlappingNested = parserFor("/* /*/ */ SELECT 1");
if (dialect == Dialect.POSTGRESQL) {
assertFalse(overlappingNested.skipMultiLineComment());
assertEquals("/* /*/ */ SELECT 1".length(), overlappingNested.getPos());

SimpleParser balancedOverlapping = parserFor("/* /*/ */ */ SELECT 1");
assertTrue(balancedOverlapping.skipMultiLineComment());
assertEquals("/* /*/ */ */".length(), balancedOverlapping.getPos());
} else {
assertTrue(overlappingNested.skipMultiLineComment());
assertEquals("/* /*/".length(), overlappingNested.getPos());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -894,4 +894,39 @@ public void testGetCommitResponseAfterDdl() {
}
assertNull(transaction.getCommitResponseOrNull());
}

@Test
public void testExecuteQueryAsync_UpdateInReadOnlyMode() {
ParsedStatement updateWithReturning = createParsedUpdate(VALID_UPDATE);
when(updateWithReturning.hasReturningClause()).thenReturn(true);

SingleUseTransaction readOnlyTx = createReadOnlySubject(TimestampBound.strong());
try {
get(readOnlyTx.executeQueryAsync(CallType.SYNC, updateWithReturning, AnalyzeMode.NONE));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.FAILED_PRECONDITION, e.getErrorCode());
assertThat(e.getMessage()).contains("Update statements are not allowed in read-only mode");
}
// Verify the single-use transaction was not marked as used when the precondition check failed.
assertNotNull(
get(
readOnlyTx.executeQueryAsync(
CallType.SYNC, createParsedQuery(VALID_QUERY), AnalyzeMode.NONE)));

SingleUseTransaction readOnlyPlanTx = createReadOnlySubject(TimestampBound.strong());
try {
get(
readOnlyPlanTx.executeQueryAsync(
CallType.SYNC, createParsedUpdate(VALID_UPDATE), AnalyzeMode.PLAN));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.FAILED_PRECONDITION, e.getErrorCode());
assertThat(e.getMessage()).contains("Update statements are not allowed in read-only mode");
}
assertNotNull(
get(
readOnlyPlanTx.executeQueryAsync(
CallType.SYNC, createParsedQuery(VALID_QUERY), AnalyzeMode.NONE)));
}
}
Loading
Loading