use of co.cask.cdap.proto.ColumnDesc in project cdap by caskdata.
the class BaseHiveExploreService method getStatus.
@Override
public QueryStatus getStatus(QueryHandle handle) throws ExploreException, HandleNotFoundException, SQLException {
startAndWait();
InactiveOperationInfo inactiveOperationInfo = inactiveHandleCache.getIfPresent(handle);
if (inactiveOperationInfo != null) {
// Operation has been made inactive, so return the saved status.
LOG.trace("Returning saved status for inactive handle {}", handle);
return inactiveOperationInfo.getStatus();
}
try {
// Fetch status from Hive
QueryStatus status = fetchStatus(getActiveOperationInfo(handle));
LOG.trace("Status of handle {} is {}", handle, status);
// No results or error, so can be timed out aggressively
if (status.getStatus() == QueryStatus.OpStatus.FINISHED && !status.hasResults()) {
// In case of a query that writes to a Dataset, we will always fall into this condition,
// and timing out aggressively will also close the transaction and make the writes visible
timeoutAggressively(handle, getResultSchema(handle), status);
} else if (status.getStatus() == QueryStatus.OpStatus.ERROR) {
// getResultSchema will fail if the query is in error
timeoutAggressively(handle, ImmutableList.<ColumnDesc>of(), status);
}
return status;
} catch (HiveSQLException e) {
throw getSqlException(e);
}
}
use of co.cask.cdap.proto.ColumnDesc in project cdap by caskdata.
the class InMemoryExploreServiceTest method runNamespacedTest.
private void runNamespacedTest(String namespace) throws Exception {
URL loadFileUrl = getClass().getResource("/test_table.dat");
Assert.assertNotNull(loadFileUrl);
// Should have no tables
runCommand(namespace, "show tables", true, Lists.newArrayList(new ColumnDesc("tab_name", "STRING", 1, "from deserializer")), ImmutableList.<QueryResult>of());
runCommand(namespace, "create table test (first INT, second STRING) ROW FORMAT " + "DELIMITED FIELDS TERMINATED BY '\\t'", false, ImmutableList.<ColumnDesc>of(), ImmutableList.<QueryResult>of());
runCommand(namespace, "show tables", true, Lists.newArrayList(new ColumnDesc("tab_name", "STRING", 1, "from deserializer")), Lists.newArrayList(new QueryResult(Lists.<Object>newArrayList("test"))));
runCommand(namespace, "describe test", true, Lists.newArrayList(new ColumnDesc("col_name", "STRING", 1, "from deserializer"), new ColumnDesc("data_type", "STRING", 2, "from deserializer"), new ColumnDesc("comment", "STRING", 3, "from deserializer")), Lists.newArrayList(new QueryResult(Lists.<Object>newArrayList("first", "int", "")), new QueryResult(Lists.<Object>newArrayList("second", "string", ""))));
// Should have no data
runCommand(namespace, "select * from test", true, Lists.newArrayList(new ColumnDesc("test.first", "INT", 1, null), new ColumnDesc("test.second", "STRING", 2, null)), ImmutableList.<QueryResult>of());
runCommand(namespace, "LOAD DATA LOCAL INPATH '" + new File(loadFileUrl.toURI()).getAbsolutePath() + "' INTO TABLE test", false, ImmutableList.<ColumnDesc>of(), ImmutableList.<QueryResult>of());
runCommand(namespace, "select first, second from test", true, Lists.newArrayList(new ColumnDesc("first", "INT", 1, null), new ColumnDesc("second", "STRING", 2, null)), Lists.newArrayList(new QueryResult(Lists.<Object>newArrayList("1", "one")), new QueryResult(Lists.<Object>newArrayList("2", "two")), new QueryResult(Lists.<Object>newArrayList("3", "three")), new QueryResult(Lists.<Object>newArrayList("4", "four")), new QueryResult(Lists.<Object>newArrayList("5", "five"))));
runCommand(namespace, "select * from test", true, Lists.newArrayList(new ColumnDesc("test.first", "INT", 1, null), new ColumnDesc("test.second", "STRING", 2, null)), Lists.newArrayList(new QueryResult(Lists.<Object>newArrayList("1", "one")), new QueryResult(Lists.<Object>newArrayList("2", "two")), new QueryResult(Lists.<Object>newArrayList("3", "three")), new QueryResult(Lists.<Object>newArrayList("4", "four")), new QueryResult(Lists.<Object>newArrayList("5", "five"))));
}
use of co.cask.cdap.proto.ColumnDesc in project cdap by caskdata.
the class ExplorePreparedStatementTest method executeTest.
@Test
public void executeTest() throws Exception {
ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("SELECT * FROM table WHERE id=100, name='foo'", (List<ColumnDesc>) Lists.newArrayList(new ColumnDesc("column1", "STRING", 1, ""), new ColumnDesc("column2", "int", 2, ""))), ImmutableMap.of("SELECT * FROM table WHERE id=100, name='foo'", (List<QueryResult>) Lists.<QueryResult>newArrayList()));
String ns = "ns1";
ExplorePreparedStatement statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE id=?, name=?", ns);
statement.setInt(1, 100);
try {
statement.execute();
Assert.fail();
} catch (SQLException e) {
// Parameter 2 has not been set
}
statement.setString(2, "foo");
Assert.assertEquals("SELECT * FROM table WHERE id=100, name='foo'", statement.updateSql());
Assert.assertTrue(statement.execute());
ResultSet rs = statement.getResultSet();
Assert.assertNotNull(rs);
Assert.assertFalse(rs.isClosed());
Assert.assertFalse(rs.next());
statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name='?'", ns);
Assert.assertEquals("SELECT * FROM table WHERE name='?'", statement.updateSql());
statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name='?', id=?", ns);
statement.setInt(1, 100);
Assert.assertEquals("SELECT * FROM table WHERE name='?', id=100", statement.updateSql());
statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"?\", id=?", ns);
statement.setInt(1, 100);
Assert.assertEquals("SELECT * FROM table WHERE name=\"?\", id=100", statement.updateSql());
statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"'?'\", id=?", ns);
statement.setInt(1, 100);
Assert.assertEquals("SELECT * FROM table WHERE name=\"'?'\", id=100", statement.updateSql());
statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"'?\", id=?", ns);
statement.setInt(1, 100);
Assert.assertEquals("SELECT * FROM table WHERE name=\"'?\", id=100", statement.updateSql());
statement = new ExplorePreparedStatement(null, exploreClient, "SELECT * FROM table WHERE name=\"\\\"?\\\"\", id=?", ns);
statement.setInt(1, 100);
Assert.assertEquals("SELECT * FROM table WHERE name=\"\\\"?\\\"\", id=100", statement.updateSql());
}
use of co.cask.cdap.proto.ColumnDesc in project cdap by caskdata.
the class ExploreResultSetTest method sameNamedColumns.
@Test
public void sameNamedColumns() throws Exception {
ExploreClient exploreClient = new MockExploreClient(ImmutableMap.of("mock_query", (List<ColumnDesc>) Lists.newArrayList(new ColumnDesc("column1", "STRING", 2, ""), new ColumnDesc("column1", "int", 1, ""))), ImmutableMap.of("mock_query", (List<QueryResult>) Lists.newArrayList(new QueryResult(ImmutableList.<Object>of(1, "value1")))));
ResultSet resultSet = new ExploreResultSet(exploreClient.submit(new NamespaceId(ns), "mock_query").get(), new ExploreStatement(null, exploreClient, ns), 0);
Assert.assertTrue(resultSet.next());
Assert.assertEquals(1, resultSet.findColumn("column1"));
Assert.assertEquals(1, resultSet.getObject("column1"));
// Can't call get... after resultSet is closed, nor findColumn
resultSet.close();
try {
resultSet.getObject(1);
} catch (SQLException e) {
// Expected
}
try {
resultSet.findColumn("column1");
} catch (SQLException e) {
// Expected
}
}
use of co.cask.cdap.proto.ColumnDesc in project cdap by caskdata.
the class ExploreQueryExecutorHttpHandler method getQueryResultsSchema.
@GET
@Path("data/explore/queries/{id}/schema")
public void getQueryResultsSchema(HttpRequest request, HttpResponder responder, @PathParam("id") String id) throws ExploreException {
try {
final QueryHandle handle = QueryHandle.fromId(id);
List<ColumnDesc> schema;
if (!handle.equals(QueryHandle.NO_OP)) {
schema = doAs(handle, new Callable<List<ColumnDesc>>() {
@Override
public List<ColumnDesc> call() throws Exception {
return exploreService.getResultSchema(handle);
}
});
} else {
schema = Lists.newArrayList();
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(schema));
} catch (IllegalArgumentException e) {
LOG.debug("Got exception:", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
} catch (SQLException e) {
LOG.debug("Got exception:", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
} catch (HandleNotFoundException e) {
responder.sendStatus(HttpResponseStatus.NOT_FOUND);
}
}
Aggregations