Search in sources :

Example 16 with ColumnDesc

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);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 17 with ColumnDesc

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"))));
}
Also used : QueryResult(co.cask.cdap.proto.QueryResult) ColumnDesc(co.cask.cdap.proto.ColumnDesc) File(java.io.File) URL(java.net.URL)

Example 18 with ColumnDesc

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());
}
Also used : ExploreClient(co.cask.cdap.explore.client.ExploreClient) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) SQLException(java.sql.SQLException) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) List(java.util.List) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Test(org.junit.Test)

Example 19 with ColumnDesc

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
    }
}
Also used : MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ExploreClient(co.cask.cdap.explore.client.ExploreClient) QueryResult(co.cask.cdap.proto.QueryResult) SQLException(java.sql.SQLException) MockExploreClient(co.cask.cdap.explore.client.MockExploreClient) ResultSet(java.sql.ResultSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Test(org.junit.Test)

Example 20 with ColumnDesc

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);
    }
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) QueryHandle(co.cask.cdap.proto.QueryHandle) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Callable(java.util.concurrent.Callable) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ColumnDesc (co.cask.cdap.proto.ColumnDesc)38 QueryResult (co.cask.cdap.proto.QueryResult)23 Test (org.junit.Test)21 DatasetId (co.cask.cdap.proto.id.DatasetId)14 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)13 TimePartitionedFileSet (co.cask.cdap.api.dataset.lib.TimePartitionedFileSet)8 PartitionedFileSet (co.cask.cdap.api.dataset.lib.PartitionedFileSet)7 SQLException (java.sql.SQLException)7 Location (org.apache.twill.filesystem.Location)7 FileSet (co.cask.cdap.api.dataset.lib.FileSet)6 Schema (co.cask.cdap.api.data.schema.Schema)4 ExploreClient (co.cask.cdap.explore.client.ExploreClient)4 MockExploreClient (co.cask.cdap.explore.client.MockExploreClient)4 QueryStatus (co.cask.cdap.proto.QueryStatus)4 StreamId (co.cask.cdap.proto.id.StreamId)4 ResultSet (java.sql.ResultSet)4 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)3 PartitionedFileSetProperties (co.cask.cdap.api.dataset.lib.PartitionedFileSetProperties)3 Table (co.cask.cdap.api.dataset.table.Table)3 QueryHandle (co.cask.cdap.proto.QueryHandle)3