Skip to content

Commit

Permalink
DRILL-6946: Implement java.sql.Connection setSchema and getSchema met…
Browse files Browse the repository at this point in the history
…hods in DrillConnectionImpl

closes #1596
  • Loading branch information
arina-ielchiieva authored and Hanumath Maduri committed Jan 7, 2019
1 parent 7a25d9d commit 814e9f0
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 178 deletions.
196 changes: 130 additions & 66 deletions contrib/native/client/src/protobuf/User.pb.cc

Large diffs are not rendered by default.

87 changes: 86 additions & 1 deletion contrib/native/client/src/protobuf/User.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -138,7 +138,8 @@ public void run() {
.setIdentifierQuoteString(config.quoting().string)
.setIdentifierCasing(getIdentifierCasing(config.unquotedCasing(), config.caseSensitive()))
.setQuotedIdentifierCasing(getIdentifierCasing(config.quotedCasing(), config.caseSensitive()))
.addAllSqlKeywords(Splitter.on(",").split(metadata.getJdbcKeywords()));
.addAllSqlKeywords(Splitter.on(",").split(metadata.getJdbcKeywords()))
.setCurrentSchema(session.getDefaultSchemaPath());
respBuilder.setServerMeta(metaBuilder);
respBuilder.setStatus(RequestStatus.OK);
} catch(Throwable t) {
Expand Down
Expand Up @@ -21,28 +21,43 @@
import static org.junit.Assert.assertNotNull;

import org.apache.calcite.avatica.util.Quoting;
import org.apache.drill.test.BaseTestQuery;
import org.apache.drill.exec.proto.UserProtos.GetServerMetaResp;
import org.apache.drill.exec.proto.UserProtos.RequestStatus;
import org.apache.drill.exec.proto.UserProtos.ServerMeta;
import org.apache.drill.test.ClusterFixture;
import org.apache.drill.test.ClusterFixtureBuilder;
import org.apache.drill.test.ClusterTest;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Tests for server metadata provider APIs.
*/
public class TestServerMetaProvider extends BaseTestQuery {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TestServerMetaProvider.class);
public class TestServerMetaProvider extends ClusterTest {

@BeforeClass
public static void setup() throws Exception {
ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher);
startCluster(builder);
}

@Test
public void testServerMeta() throws Exception {
GetServerMetaResp resp = client.getServerMeta().get();
assertNotNull(resp);
assertEquals(RequestStatus.OK, resp.getStatus());
assertNotNull(resp.getServerMeta());
GetServerMetaResp response = client.client().getServerMeta().get();
assertNotNull(response);
assertEquals(RequestStatus.OK, response.getStatus());
assertNotNull(response.getServerMeta());

assertEquals(Quoting.BACK_TICK.string, response.getServerMeta().getIdentifierQuoteString());
}

@Test
public void testCurrentSchema() throws Exception {
GetServerMetaResp response = client.client().getServerMeta().get();
assertEquals(RequestStatus.OK, response.getStatus());
assertEquals("", response.getServerMeta().getCurrentSchema());

ServerMeta serverMeta = resp.getServerMeta();
logger.trace("Server metadata: {}", serverMeta);
queryBuilder().sql("use dfs.tmp").run();

assertEquals(Quoting.BACK_TICK.string, serverMeta.getIdentifierQuoteString());
response = client.client().getServerMeta().get();
assertEquals(RequestStatus.OK, response.getStatus());
assertEquals("dfs.tmp", response.getServerMeta().getCurrentSchema());
}

}
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

import org.apache.calcite.avatica.AvaticaConnection;
Expand All @@ -44,6 +45,7 @@
import org.apache.calcite.avatica.NoSuchStatementException;
import org.apache.calcite.avatica.QueryState;
import org.apache.calcite.avatica.UnregisteredDriver;
import org.apache.commons.lang3.StringUtils;
import org.apache.drill.common.config.DrillConfig;
import org.apache.drill.common.exceptions.DrillRuntimeException;
import org.apache.drill.common.exceptions.UserException;
Expand All @@ -52,6 +54,8 @@
import org.apache.drill.exec.exception.OutOfMemoryException;
import org.apache.drill.exec.memory.BufferAllocator;
import org.apache.drill.exec.memory.RootAllocatorFactory;
import org.apache.drill.exec.proto.UserBitShared;
import org.apache.drill.exec.proto.UserProtos;
import org.apache.drill.exec.rpc.RpcException;
import org.apache.drill.exec.server.Drillbit;
import org.apache.drill.exec.server.RemoteServiceSet;
Expand Down Expand Up @@ -582,6 +586,33 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep
}
}

@Override
public void setSchema(String schema) throws SQLException {
checkOpen();
try {
client.runQuery(UserBitShared.QueryType.SQL, String.format("use %s", schema));
} catch (RpcException e) {
throw new SQLException("Error when setting schema", e);
}
}

@Override
public String getSchema() throws SQLException {
checkOpen();
try {
UserProtos.GetServerMetaResp response = client.getServerMeta().get();
if (response.getStatus() != UserProtos.RequestStatus.OK) {
UserBitShared.DrillPBError drillError = response.getError();
throw new SQLException("Error when getting server meta: " + drillError.getMessage());
}
UserProtos.ServerMeta serverMeta = response.getServerMeta();
String currentSchema = serverMeta.hasCurrentSchema() ? serverMeta.getCurrentSchema() : null;
return StringUtils.isEmpty(currentSchema) ? null : currentSchema;
} catch (InterruptedException | ExecutionException e) {
throw new SQLException("Error when getting server meta", e);
}
}

@Override
public void abort(Executor executor) throws SQLException {
checkOpen();
Expand Down
Expand Up @@ -20,55 +20,80 @@
import org.apache.calcite.avatica.util.Quoting;
import org.apache.drill.categories.JdbcTest;
import org.apache.drill.categories.SlowTest;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

/**
* Test for Drill's Properties in the JDBC URL connection string
*/
@Category({SlowTest.class, JdbcTest.class})
public class ConnectionInfoTest extends JdbcTestBase {
private static Connection connection;
private static DatabaseMetaData dbmd;

@Rule
public ExpectedException thrown = ExpectedException.none();

@After
public void tearDown() {
reset();
}

@Test
public void testQuotingIdentifiersProperty() throws Exception {
Connection connection = connect("jdbc:drill:zk=local;quoting_identifiers='\"'");
DatabaseMetaData dbmd = connection.getMetaData();
assertThat(dbmd.getIdentifierQuoteString(), equalTo(Quoting.DOUBLE_QUOTE.string));

reset();

connection = connect("jdbc:drill:zk=local;quoting_identifiers=[");
dbmd = connection.getMetaData();
assertThat(dbmd.getIdentifierQuoteString(), equalTo(Quoting.BRACKET.string));
}

@Test
public void testIncorrectCharacterForQuotingIdentifiers() throws Exception {
thrown.expect(SQLException.class);
thrown.expectMessage(containsString("Option planner.parser.quoting_identifiers must be one of: [`, \", []"));

connect("jdbc:drill:zk=local;quoting_identifiers=&");
}

@Test
public void testSetSchemaUsingConnectionMethod() throws Exception {
Connection connection = connect("jdbc:drill:zk=local");
assertNull(connection.getSchema());

connection.setSchema("dfs.tmp");
assertEquals("dfs.tmp", connection.getSchema());
}

@Test
public void testQuotingIdentifiersProperty() throws SQLException {
try {
// Test DoubleQuotes for the DrillProperty#QUOTING_IDENTIFIERS in connection URL
connection = connect("jdbc:drill:zk=local;quoting_identifiers='\"'");
dbmd = connection.getMetaData();
assertThat(dbmd.getIdentifierQuoteString(), equalTo(Quoting.DOUBLE_QUOTE.string));
reset();

// Test Brackets for the DrillProperty#QUOTING_IDENTIFIERS in connection URL
connection = connect("jdbc:drill:zk=local;quoting_identifiers=[");
dbmd = connection.getMetaData();
assertThat(dbmd.getIdentifierQuoteString(), equalTo(Quoting.BRACKET.string));
} finally {
reset();
}
public void testIncorrectlySetSchema() throws Exception {
Connection connection = connect("jdbc:drill:zk=local");

thrown.expect(SQLException.class);
thrown.expectMessage("Error when setting schema");

connection.setSchema("ABC");
}

@Test(expected = SQLException.class)
public void testIncorrectCharacterForQuotingIdentifiers() throws SQLException {
try {
connection = connect("jdbc:drill:zk=local;quoting_identifiers=&");
}
catch (SQLException e) {
// Check exception text message
assertThat(e.getMessage(), containsString("Option planner.parser.quoting_identifiers " +
"must be one of: [`, \", []"));
throw e;
} finally {
reset();
}
@Test
public void testSchemaInConnectionString() throws Exception {
Connection connection = connect("jdbc:drill:zk=local;schema=sys");
assertEquals("sys", connection.getSchema());
}

}
Expand Up @@ -4078,6 +4078,8 @@ public void writeTo(com.dyuproject.protostuff.Output output, org.apache.drill.ex
output.writeBool(48, message.getTransactionSupported(), false);
for(org.apache.drill.exec.proto.UserProtos.UnionSupport unionSupport : message.getUnionSupportList())
output.writeEnum(49, unionSupport.getNumber(), true);
if(message.hasCurrentSchema())
output.writeString(50, message.getCurrentSchema(), false);
}
public boolean isInitialized(org.apache.drill.exec.proto.UserProtos.ServerMeta message)
{
Expand Down Expand Up @@ -4265,6 +4267,9 @@ public void mergeFrom(com.dyuproject.protostuff.Input input, org.apache.drill.ex
case 49:
builder.addUnionSupport(org.apache.drill.exec.proto.UserProtos.UnionSupport.valueOf(input.readEnum()));
break;
case 50:
builder.setCurrentSchema(input.readString());
break;
default:
input.handleUnknownField(number, this);
}
Expand Down Expand Up @@ -4354,6 +4359,7 @@ public static java.lang.String getFieldName(int number)
case 47: return "tableTerm";
case 48: return "transactionSupported";
case 49: return "unionSupport";
case 50: return "currentSchema";
default: return null;
}
}
Expand Down Expand Up @@ -4414,6 +4420,7 @@ public static int getFieldNumber(java.lang.String name)
fieldMap.put("tableTerm", 47);
fieldMap.put("transactionSupported", 48);
fieldMap.put("unionSupport", 49);
fieldMap.put("currentSchema", 50);
}
}

Expand Down

0 comments on commit 814e9f0

Please sign in to comment.