Skip to content

Commit 6dbff63

Browse files
committed
Merge branch 'main' into qualify-without-removing-comments
2 parents f19389c + a2780ed commit 6dbff63

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

google-cloud-spanner/clirr-ignored-differences.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,5 +927,23 @@
927927
<className>com/google/cloud/spanner/connection/ConnectionOptions</className>
928928
<field>VALID_PROPERTIES</field>
929929
</difference>
930+
931+
<!-- Remove supportsExplain() from the parser -->
932+
<difference>
933+
<differenceType>7002</differenceType>
934+
<className>com/google/cloud/spanner/connection/AbstractStatementParser</className>
935+
<method>boolean supportsExplain()</method>
936+
</difference>
937+
<difference>
938+
<differenceType>7002</differenceType>
939+
<className>com/google/cloud/spanner/connection/PostgreSQLStatementParser</className>
940+
<method>boolean supportsExplain()</method>
941+
</difference>
942+
<difference>
943+
<differenceType>7002</differenceType>
944+
<className>com/google/cloud/spanner/connection/SpannerStatementParser</className>
945+
<method>boolean supportsExplain()</method>
946+
</difference>
947+
930948

931949
</differences>

google-cloud-spanner/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,19 @@
517517
</execution>
518518
</executions>
519519
</plugin>
520+
<plugin>
521+
<groupId>org.apache.maven.plugins</groupId>
522+
<artifactId>maven-compiler-plugin</artifactId>
523+
<configuration>
524+
<annotationProcessorPaths>
525+
<path>
526+
<groupId>org.openjdk.jmh</groupId>
527+
<artifactId>jmh-generator-annprocess</artifactId>
528+
<version>1.37</version>
529+
</path>
530+
</annotationProcessorPaths>
531+
</configuration>
532+
</plugin>
520533
</plugins>
521534
</build>
522535
</profile>

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Statement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode;
2323
import com.google.common.base.Preconditions;
24+
import com.google.common.collect.ImmutableMap;
2425
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
2526
import java.io.Serializable;
2627
import java.util.Collections;
@@ -140,7 +141,7 @@ Builder handle(Value value) {
140141

141142
/** Creates a {@code Statement} with the given SQL text {@code sql}. */
142143
public static Statement of(String sql) {
143-
return newBuilder(sql).build();
144+
return new Statement(sql, ImmutableMap.of(), /*queryOptions=*/ null);
144145
}
145146

146147
/** Creates a new statement builder with the SQL text {@code sql}. */

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/AbstractStatementParser.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.cloud.spanner.connection.UnitOfWork.CallType;
3333
import com.google.common.annotations.VisibleForTesting;
3434
import com.google.common.base.Preconditions;
35+
import com.google.common.base.Splitter;
3536
import com.google.common.base.Suppliers;
3637
import com.google.common.cache.Cache;
3738
import com.google.common.cache.CacheBuilder;
@@ -44,6 +45,7 @@
4445
import java.util.Collection;
4546
import java.util.Collections;
4647
import java.util.HashMap;
48+
import java.util.Iterator;
4749
import java.util.Map;
4850
import java.util.Objects;
4951
import java.util.Set;
@@ -663,20 +665,16 @@ public boolean isUpdateStatement(String sql) {
663665
return statementStartsWith(sql, dmlStatements);
664666
}
665667

666-
protected abstract boolean supportsExplain();
667-
668668
private boolean statementStartsWith(String sql, Iterable<String> checkStatements) {
669669
Preconditions.checkNotNull(sql);
670-
String[] tokens = sql.split("\\s+", 2);
671-
int checkIndex = 0;
672-
if (supportsExplain() && tokens[0].equalsIgnoreCase("EXPLAIN")) {
673-
checkIndex = 1;
670+
Iterator<String> tokens = Splitter.onPattern("\\s+").split(sql).iterator();
671+
if (!tokens.hasNext()) {
672+
return false;
674673
}
675-
if (tokens.length > checkIndex) {
676-
for (String check : checkStatements) {
677-
if (tokens[checkIndex].equalsIgnoreCase(check)) {
678-
return true;
679-
}
674+
String token = tokens.next();
675+
for (String check : checkStatements) {
676+
if (token.equalsIgnoreCase(check)) {
677+
return true;
680678
}
681679
}
682680
return false;
@@ -982,7 +980,8 @@ int skipQuoted(
982980
appendIfNotNull(result, startQuote);
983981
appendIfNotNull(result, startQuote);
984982
}
985-
while (currentIndex < sql.length()) {
983+
int length = sql.length();
984+
while (currentIndex < length) {
986985
char currentChar = sql.charAt(currentIndex);
987986
if (currentChar == startQuote) {
988987
if (supportsDollarQuotedStrings() && currentChar == DOLLAR) {
@@ -993,7 +992,7 @@ int skipQuoted(
993992
return currentIndex + tag.length() + 2;
994993
}
995994
} else if (supportsEscapeQuoteWithQuote()
996-
&& sql.length() > currentIndex + 1
995+
&& length > currentIndex + 1
997996
&& sql.charAt(currentIndex + 1) == startQuote) {
998997
// This is an escaped quote (e.g. 'foo''bar')
999998
appendIfNotNull(result, currentChar);
@@ -1002,7 +1001,7 @@ int skipQuoted(
10021001
continue;
10031002
} else if (isTripleQuoted) {
10041003
// Check if this is the end of the triple-quoted string.
1005-
if (sql.length() > currentIndex + 2
1004+
if (length > currentIndex + 2
10061005
&& sql.charAt(currentIndex + 1) == startQuote
10071006
&& sql.charAt(currentIndex + 2) == startQuote) {
10081007
appendIfNotNull(result, currentChar);
@@ -1016,7 +1015,7 @@ int skipQuoted(
10161015
}
10171016
} else if (supportsBackslashEscape()
10181017
&& currentChar == BACKSLASH
1019-
&& sql.length() > currentIndex + 1
1018+
&& length > currentIndex + 1
10201019
&& sql.charAt(currentIndex + 1) == startQuote) {
10211020
// This is an escaped quote (e.g. 'foo\'bar').
10221021
// Note that in raw strings, the \ officially does not start an escape sequence, but the

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/PostgreSQLStatementParser.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ Dialect getDialect() {
4646
return Dialect.POSTGRESQL;
4747
}
4848

49-
/**
50-
* Indicates whether the parser supports the {@code EXPLAIN} clause. The PostgreSQL parser does
51-
* not support it.
52-
*/
53-
@Override
54-
protected boolean supportsExplain() {
55-
return false;
56-
}
57-
5849
@Override
5950
boolean supportsNestedComments() {
6051
return true;
@@ -125,7 +116,8 @@ String removeCommentsAndTrimInternal(String sql) {
125116
int multiLineCommentStartIdx = -1;
126117
StringBuilder res = new StringBuilder(sql.length());
127118
int index = 0;
128-
while (index < sql.length()) {
119+
int length = sql.length();
120+
while (index < length) {
129121
char c = sql.charAt(index);
130122
if (isInSingleLineComment) {
131123
if (c == '\n') {
@@ -134,34 +126,34 @@ String removeCommentsAndTrimInternal(String sql) {
134126
res.append(c);
135127
}
136128
} else if (multiLineCommentLevel > 0) {
137-
if (sql.length() > index + 1 && c == ASTERISK && sql.charAt(index + 1) == SLASH) {
129+
if (length > index + 1 && c == ASTERISK && sql.charAt(index + 1) == SLASH) {
138130
multiLineCommentLevel--;
139131
if (multiLineCommentLevel == 0) {
140-
if (!whitespaceBeforeOrAfterMultiLineComment && (sql.length() > index + 2)) {
132+
if (!whitespaceBeforeOrAfterMultiLineComment && (length > index + 2)) {
141133
whitespaceBeforeOrAfterMultiLineComment =
142134
Character.isWhitespace(sql.charAt(index + 2));
143135
}
144136
// If the multiline comment does not have any whitespace before or after it, and it is
145137
// neither at the start nor at the end of SQL string, append an extra space.
146138
if (!whitespaceBeforeOrAfterMultiLineComment
147139
&& (multiLineCommentStartIdx != 0)
148-
&& (index != sql.length() - 2)) {
140+
&& (index != length - 2)) {
149141
res.append(' ');
150142
}
151143
}
152144
index++;
153-
} else if (sql.length() > index + 1 && c == SLASH && sql.charAt(index + 1) == ASTERISK) {
145+
} else if (length > index + 1 && c == SLASH && sql.charAt(index + 1) == ASTERISK) {
154146
multiLineCommentLevel++;
155147
index++;
156148
}
157149
} else {
158150
// Check for -- which indicates the start of a single-line comment.
159-
if (sql.length() > index + 1 && c == HYPHEN && sql.charAt(index + 1) == HYPHEN) {
151+
if (length > index + 1 && c == HYPHEN && sql.charAt(index + 1) == HYPHEN) {
160152
// This is a single line comment.
161153
isInSingleLineComment = true;
162154
index += 2;
163155
continue;
164-
} else if (sql.length() > index + 1 && c == SLASH && sql.charAt(index + 1) == ASTERISK) {
156+
} else if (length > index + 1 && c == SLASH && sql.charAt(index + 1) == ASTERISK) {
165157
multiLineCommentLevel++;
166158
if (index >= 1) {
167159
whitespaceBeforeOrAfterMultiLineComment = Character.isWhitespace(sql.charAt(index - 1));

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerStatementParser.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ Dialect getDialect() {
4646
return Dialect.GOOGLE_STANDARD_SQL;
4747
}
4848

49-
/**
50-
* Indicates whether the parser supports the {@code EXPLAIN} clause. The Spanner parser does
51-
* support it.
52-
*/
53-
@Override
54-
protected boolean supportsExplain() {
55-
return true;
56-
}
57-
5849
@Override
5950
boolean supportsNestedComments() {
6051
return false;

0 commit comments

Comments
 (0)