Skip to content

feat: Add support for Explain feature #1852

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 51 commits into from
May 19, 2022
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
1ea42dd
Added the code for handling the explain
gauravsnj Apr 21, 2022
8c7a5b0
Update ConnectionStatementExecutorImpl.java
gauravsnj Apr 21, 2022
177f07f
Added tests
gauravsnj Apr 25, 2022
242e351
Update PG_ClientSideStatements.json
gauravsnj Apr 25, 2022
053dd92
resolved the comments
gauravsnj Apr 25, 2022
0677368
Update google-cloud-spanner/src/main/resources/com/google/cloud/spann…
gauravsnj Apr 25, 2022
87069ed
Update ConnectionStatementExecutorTest.java
gauravsnj Apr 25, 2022
436775b
Merge remote-tracking branch 'origin/explain-feature' into explain-fe…
gauravsnj Apr 25, 2022
5047608
Update PG_ClientSideStatements.json
gauravsnj Apr 25, 2022
f09c6e3
Update PG_ClientSideStatements.json
gauravsnj Apr 25, 2022
411ca77
Formatted the files for ci lint
gauravsnj Apr 25, 2022
f663865
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/co…
gauravsnj Apr 26, 2022
4b7127d
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/co…
gauravsnj Apr 26, 2022
e3614be
resolved some comments
gauravsnj Apr 26, 2022
e125c42
formatted the code
gauravsnj Apr 26, 2022
af79b7b
added some code
gauravsnj Apr 27, 2022
7a1937a
added support for "explain (format ) foo" kind of statements
gauravsnj Apr 28, 2022
d0c6abf
resolved some comments
gauravsnj Apr 28, 2022
9f084ba
Update ConnectionStatementExecutorImpl.java
gauravsnj Apr 28, 2022
0f76847
fixed a small bug
gauravsnj Apr 29, 2022
daddd3e
Added the code for formatting query plan for export
gauravsnj May 6, 2022
e38e917
Update ConnectionStatementExecutorImpl.java
gauravsnj May 6, 2022
60ad94e
Update ConnectionStatementExecutorImpl.java
gauravsnj May 6, 2022
e2813ab
Update ConnectionStatementExecutorImpl.java
gauravsnj May 6, 2022
32fc9de
Update ConnectionStatementExecutorImpl.java
gauravsnj May 6, 2022
e57da64
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/co…
gauravsnj May 6, 2022
b4101ad
Changed assertThat to assertEquals
gauravsnj May 6, 2022
c59e964
Merge remote-tracking branch 'origin/explain-feature' into explain-fe…
gauravsnj May 6, 2022
a767158
removed unnecessary lines
gauravsnj May 6, 2022
1a8fd85
Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/co…
gauravsnj May 6, 2022
50808f1
Changed assertThat to assertEquals
gauravsnj May 6, 2022
1f4aa32
Update ConnectionStatementExecutorImpl.java
gauravsnj May 6, 2022
0610cae
Update ConnectionStatementExecutorImpl.java
gauravsnj May 6, 2022
faaa7cd
Added tests
gauravsnj May 12, 2022
dc9dab2
format
gauravsnj May 12, 2022
4177cdd
Update PartitionedDmlTransaction.java
gauravsnj May 12, 2022
105d8a5
generated sql script
gauravsnj May 12, 2022
6162363
resolved comments
gauravsnj May 13, 2022
d58acc0
resolved comments
gauravsnj May 13, 2022
638ae76
Update PG_ClientSideStatements.json
gauravsnj May 13, 2022
07e521c
Merge branch 'main' into explain-feature
gauravsnj May 13, 2022
dd9e83e
Update PG_ClientSideStatements.json
gauravsnj May 13, 2022
55f1736
Update PG_ClientSideStatements.json
gauravsnj May 13, 2022
2d7649f
Create ITExplainTest.java
gauravsnj May 16, 2022
40b3f89
added Integration tests
gauravsnj May 16, 2022
c272165
reformatted
gauravsnj May 17, 2022
10f06e8
changed region
gauravsnj May 17, 2022
8695453
Revert "changed region"
gauravsnj May 17, 2022
0b66c53
Update ITExplainTest.java
gauravsnj May 17, 2022
a08c293
Update ITExplainTest.java
gauravsnj May 17, 2022
9996f1c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 17, 2022
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
Prev Previous commit
Next Next commit
Update ConnectionStatementExecutorImpl.java
  • Loading branch information
gauravsnj committed Apr 21, 2022
commit 8c7a5b07237c5f52c054140a8b393610965ddf10
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@
import com.google.cloud.spanner.CommitResponse;
import com.google.cloud.spanner.CommitStats;
import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Options.RpcPriority;
import com.google.cloud.spanner.ReadContext.QueryAnalyzeMode;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.ResultSets;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TimestampBound;
Expand All @@ -72,19 +74,26 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Duration;
import com.google.spanner.v1.RequestOptions;
import com.google.spanner.v1.RequestOptions.Priority;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;


/**
* The methods in this class are called by the different {@link ClientSideStatement}s. These method
* calls are then forwarded into a {@link Connection}.
*/
class ConnectionStatementExecutorImpl implements ConnectionStatementExecutor {


private final Set<String> explainOptions = ImmutableSet.of("VERBOSE", "COSTS", "SETTINGS", "BUFFERS", "WAL", "TIMING", "SUMMARY", "FORMAT");

static final class StatementTimeoutGetter implements DurationValueGetter {
private final Connection connection;

Expand Down Expand Up @@ -447,18 +456,23 @@ public StatementResult statementShowTransactionIsolationLevel() {

@Override
public ResultSet statementExplain(String sql){

sql = sql.trim();
String firstWord = sql.split(" ")[0];
String firstString = sql.split(" ")[0].toLowerCase();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of improvements that can be added to sql.split(..) here:

  1. In this specific case, we only need the first word, so we should add a limit to the split invocation to prevent the entire string from being analyzed. Also; as we need the second keyword further below, it would be better to call split only once to get the two first keywords instead of calling split twice.
  2. The keywords can be separated by any type of whitespace characters (so space, tab, linefeed, ...), and there could be more than one whitespace between keywords, so you should use split("\\s+")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggesting these improvements. I've added them in the code


if(explainOptions.contains(firstString)){
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.UNIMPLEMENTED, String.format("%s is not implemented yet", firstString));
}

if(firstWord.equalsIgnoreCase("analyze")) {
if(firstString.equals("analyze") || firstString.equals("analyse")) {
sql = sql.split(" ",2)[1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also use "\\s+" to split the string, and consider combining the two calls to split into one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Statement statement = Statement.newBuilder(sql).build();
return getConnection().analyzeQuery(statement, QueryAnalyzeMode.PROFILE);
}
else{
Statement statement = Statement.newBuilder(sql).build();
return getConnection().analyzeQuery(statement, QueryAnalyzeMode.PLAN);

}


Expand Down