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
Show file tree
Hide file tree
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
Next Next commit
Added the code for handling the explain
  • Loading branch information
gauravsnj committed Apr 21, 2022
commit 1ea42ddfd2938cf50b4bd50b5fc90d230f4513af
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.ResultSet;
import com.google.cloud.spanner.TimestampBound;
import com.google.protobuf.Duration;
import com.google.spanner.v1.RequestOptions.Priority;
Expand Down Expand Up @@ -111,4 +112,6 @@ StatementResult statementSetPgSessionCharacteristicsTransactionMode(
StatementResult statementShowRPCPriority();

StatementResult statementShowTransactionIsolationLevel();

ResultSet statementExplain(String sql);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@
import com.google.cloud.spanner.CommitStats;
import com.google.cloud.spanner.Dialect;
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.Statement;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.Type;
Expand Down Expand Up @@ -442,4 +444,24 @@ public StatementResult statementShowRPCPriority() {
public StatementResult statementShowTransactionIsolationLevel() {
return resultSet("transaction_isolation", "serializable", SHOW_TRANSACTION_ISOLATION_LEVEL);
}

@Override
public ResultSet statementExplain(String sql){
sql = sql.trim();
String firstWord = sql.split(" ")[0];

if(firstWord.equalsIgnoreCase("analyze")) {
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);

}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ enum ClientSideStatementType {
ABORT_BATCH,
SET_RPC_PRIORITY,
SHOW_RPC_PRIORITY,
SHOW_TRANSACTION_ISOLATION_LEVEL
SHOW_TRANSACTION_ISOLATION_LEVEL,
EXPLAIN
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@
"method": "statementShowTransactionIsolationLevel",
"exampleStatements": ["show transaction isolation level","show variable transaction isolation level"]
},
{
"name": "EXPLAIN '<sql>'",
"executorName": "ClientSideStatementNoParamExecutor",
Copy link
Collaborator

Choose a reason for hiding this comment

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

This won't work, as the method that you have defined has a parameter; the sql string that is to be explained is the input parameter in this case. You have two options for this:

  1. You could reuse the ClientSideStatementSetExecutor for this. That executor is normally used for statements like SET <variable> = <value> where the value is the input parameter. In this case, the input parameter would be the entire sql string that follows EXPLAIN.
  2. You could create a custom executor specifically for this method. See ClientSideStatementPgBeginExecutor for an example for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think creating a new executor class will be better as ClientSideStatementSetExecutor is for specific type of commands and EXPLAIN clearly doesn't fit in that type. What do you say?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that would be the neatest solution for this.

"resultType": "RESULT_SET",
"regex": "(?is)\\A\\s*explain\\s+(.*)\\z",
"method": "statementExplain",
"exampleStatements": ["explain select"]
},
{
"name": "{START | BEGIN} [TRANSACTION | WORK] [{ (READ ONLY|READ WRITE) | (ISOLATION LEVEL (DEFAULT|SERIALIZABLE)) }]",
"executorName": "ClientSideStatementPgBeginExecutor",
Expand Down