1616
1717package com .google .cloud .spanner .connection .it ;
1818
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertNotNull ;
1921
22+ import com .google .cloud .spanner .Dialect ;
2023import com .google .cloud .spanner .Mutation ;
2124import com .google .cloud .spanner .ParallelIntegrationTest ;
2225import com .google .cloud .spanner .ResultSet ;
2326import com .google .cloud .spanner .Statement ;
27+ import com .google .cloud .spanner .connection .Connection ;
2428import com .google .cloud .spanner .connection .ITAbstractSpannerTest ;
2529import java .util .Arrays ;
30+ import java .util .Collections ;
31+ import org .junit .Before ;
32+ import org .junit .BeforeClass ;
2633import org .junit .Test ;
2734import org .junit .experimental .categories .Category ;
2835import org .junit .runner .RunWith ;
@@ -36,9 +43,27 @@ public void appendConnectionUri(StringBuilder uri) {
3643 uri .append (";autocommit=false" );
3744 }
3845
39- @ Override
40- public boolean doCreateDefaultTestTable () {
41- return true ;
46+ @ BeforeClass
47+ public static void setupPostgreSQL (){
48+ database = env .getTestHelper ().createTestDatabase (Dialect .POSTGRESQL , Collections .emptyList ());;
49+ }
50+
51+
52+ @ Before
53+ public void createTestTable () {
54+
55+ try (Connection connection = createConnection ()) {
56+ connection .setAutocommit (true );
57+ if (!tableExists (connection , "TEST" )) {
58+ connection .setAutocommit (false );
59+ connection .startBatchDdl ();
60+ connection .execute (
61+ Statement .of (
62+ "CREATE TABLE TEST (ID INT NOT NULL PRIMARY KEY, NAME VARCHAR(100) NOT NULL)" ));
63+ connection .runBatch ();
64+ }
65+ }
66+
4267 }
4368
4469 @ Test
@@ -52,6 +77,30 @@ public void testExplainStatement() {
5277
5378 ResultSet resultSet =
5479 connection .execute (Statement .of ("EXPLAIN SELECT * from TEST" )).getResultSet ();
80+ while (resultSet .next ()){
81+ assertNotNull (resultSet .getString ("QUERY PLAN" ));
82+ }
83+ assertEquals (1 , resultSet .getColumnCount ());
84+ }
85+ }
86+
87+ @ Test
88+ public void testExplainAnalyzeStatement () {
89+ try (ITConnection connection = createConnection ()) {
90+ connection .bufferedWrite (
91+ Arrays .asList (
92+ Mutation .newInsertBuilder ("TEST" ).set ("ID" ).to (1L ).set ("NAME" ).to ("TEST-1" ).build (),
93+ Mutation .newInsertBuilder ("TEST" ).set ("ID" ).to (2L ).set ("NAME" ).to ("TEST-2" ).build ()));
94+ connection .commit ();
95+
96+ ResultSet resultSet =
97+ connection .execute (Statement .of ("EXPLAIN ANALYZE SELECT * from TEST" )).getResultSet ();
98+ while (resultSet .next ()){
99+ assertNotNull (resultSet .getString ("QUERY PLAN" ));
100+ assertNotNull (resultSet .getString ("EXECUTION STATS" ));
101+ }
102+ assertEquals (2 , resultSet .getColumnCount ());
55103 }
56104 }
105+
57106}
0 commit comments