Exemplo n.º 1
0
  /**
   * Test prepared query.
   *
   * @param qCom the q com
   * @throws Exception the exception
   */
  @Test(dataProvider = "queryCommands")
  public void preparedQuery(LensQueryCommands qCom) throws Exception {
    long submitTime = System.currentTimeMillis();
    String sql = "cube select id, name from test_dim";
    String result =
        qCom.getAllPreparedQueries("testPreparedName", "all", submitTime, Long.MAX_VALUE);

    assertEquals(result, "No prepared queries");
    final String qh = qCom.prepare(sql, "testPreparedName");
    result =
        qCom.getAllPreparedQueries(
            "testPreparedName", "all", submitTime, System.currentTimeMillis());
    assertEquals(qh, result);

    result = qCom.getPreparedStatus(qh);
    assertTrue(result.contains("User query:cube select id, name from test_dim"));
    assertTrue(result.contains(qh));

    result = qCom.executePreparedQuery(qh, false, "testPrepQuery1");

    log.warn("XXXXXX Prepared query sync result is  " + result);
    assertTrue(result.contains("1\tfirst"));

    String handle = qCom.executePreparedQuery(qh, true, "testPrepQuery2");
    log.debug("Perpared query handle is   " + handle);
    while (!qCom.getClient().getQueryStatus(handle).finished()) {
      Thread.sleep(5000);
    }
    String status = qCom.getStatus(handle);
    log.debug("Prepared Query Status is  " + status);
    assertTrue(status.contains("Status : SUCCESSFUL"));

    result = qCom.getQueryResults(handle, null, true);
    log.debug("Prepared Query Result is  " + result);
    assertTrue(result.contains("1\tfirst"));
    // Fetch again.
    result = qCom.getQueryResults(handle, null, true);
    log.debug("Prepared Query Result is  " + result);
    assertTrue(result.contains("1\tfirst"));

    result = qCom.destroyPreparedQuery(qh);

    log.debug("destroy result is " + result);
    assertEquals("Successfully destroyed " + qh, result);
    result = qCom.getAllPreparedQueries("testPreparedName", "all", submitTime, Long.MAX_VALUE);

    assertEquals(result, "No prepared queries");

    final String qh2 = qCom.explainAndPrepare(sql, "testPrepQuery3");
    assertTrue(qh2.contains(explainPlan));
    String handles = qCom.getAllPreparedQueries("testPrepQuery3", "all", -1, Long.MAX_VALUE);
    assertFalse(handles.contains("No prepared queries"), handles);

    String handles2 = qCom.getAllPreparedQueries("testPrepQuery3", "all", -1, submitTime - 1);
    assertFalse(handles2.contains(qh), handles2);
    result = qCom.destroyPreparedQuery(handles);
    assertEquals("Successfully destroyed " + handles, result);
  }
  /**
   * Test execute async query.
   *
   * @param qCom the q com
   * @throws Exception the exception
   */
  private void testExecuteAsyncQuery(LensQueryCommands qCom) throws Exception {
    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    String sql = "cube select id,name from test_dim";
    long submitTime = System.currentTimeMillis();
    String qh = qCom.executeQuery(sql, true, "testQuery1");
    String user =
        qCom.getClient()
            .getLensStatement(new QueryHandle(UUID.fromString(qh)))
            .getQuery()
            .getSubmittedUser();
    String result = qCom.getAllQueries("", "testQuery1", user, -1, Long.MAX_VALUE);
    // this is because previous query has run two query handle will be there
    assertTrue(result.contains(qh), result);
    assertTrue(result.contains("Total number of queries"));
    String[] resultSplits = result.split("\n");
    // assert on the number of queries
    assertEquals(
        String.valueOf(resultSplits.length - 1),
        resultSplits[resultSplits.length - 1].split(": ")[1]);

    QueryStatus queryStatus = client.getQueryStatus(qh);
    while (!queryStatus.finished()) {
      if (queryStatus.launched()) {
        String details = qCom.getDetails(qh);
        assertTrue(details.contains("driverQuery"));
      }
      Thread.sleep(1000);
      queryStatus = client.getQueryStatus(qh);
    }

    // Check that query name searching is 'ilike'
    String result2 = qCom.getAllQueries("", "query", "all", -1, Long.MAX_VALUE);
    assertTrue(result2.contains(qh), result2);

    assertTrue(qCom.getStatus(qh).contains("Status : SUCCESSFUL"));
    String details = qCom.getDetails(qh);
    assertTrue(details.contains("driverQuery"));

    result = qCom.getQueryResults(qh, null, true);
    assertTrue(result.contains("1\tfirst"));

    downloadResult(qCom, qh, result);
    // re-download should also succeed
    downloadResult(qCom, qh, result);

    // Kill query is not tested as there is no deterministic way of killing a query

    result = qCom.getAllQueries("SUCCESSFUL", "", "all", -1, Long.MAX_VALUE);
    assertTrue(result.contains(qh), result);

    result = qCom.getAllQueries("FAILED", "", "all", -1, Long.MAX_VALUE);
    if (!result.contains("No queries")) {
      // Make sure valid query handles are returned
      String[] handles = StringUtils.split(result, "\n");
      for (String handle : handles) {
        if (!handle.contains("Total number of queries")) {
          QueryHandle.fromString(handle.trim());
        }
      }
    }

    String queryName =
        client.getLensStatement(new QueryHandle(UUID.fromString(qh))).getQuery().getQueryName();
    assertTrue("testQuery1".equalsIgnoreCase(queryName), queryName);
    result = qCom.getAllQueries("", "", "", submitTime, System.currentTimeMillis());
    assertTrue(result.contains(qh), result);

    result = qCom.getAllQueries("", "fooBar", "all", submitTime, System.currentTimeMillis());
    assertTrue(result.contains("No queries"), result);

    result = qCom.getAllQueries("SUCCESSFUL", "", "all", submitTime, System.currentTimeMillis());
    assertTrue(result.contains(qh));

    result = qCom.getAllQueries("SUCCESSFUL", "", "all", submitTime - 5000, submitTime - 1);
    // should not give query since its not in the range
    assertFalse(result.contains(qh));

    try {
      // Should fail with bad request since fromDate > toDate
      result = qCom.getAllQueries("SUCCESSFUL", "", "all", submitTime + 5000, submitTime);
      fail("Call should have failed with BadRequestException, instead got " + result);
    } catch (BadRequestException exc) {
      // pass
    }

    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  }