Search in sources :

Example 51 with PhoenixStatement

use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.

the class QueryOptimizerTest method testOrderByOptimizedOut.

@Test
public void testOrderByOptimizedOut() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute("CREATE TABLE foo (k VARCHAR NOT NULL PRIMARY KEY, v VARCHAR) IMMUTABLE_ROWS=true");
    PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
    QueryPlan plan = stmt.optimizeQuery("SELECT * FROM foo ORDER BY k");
    assertEquals(OrderBy.FWD_ROW_KEY_ORDER_BY, plan.getOrderBy());
}
Also used : Connection(java.sql.Connection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 52 with PhoenixStatement

use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.

the class QueryOptimizerTest method testChooseIndexFromOrderByAsc.

@Test
public void testChooseIndexFromOrderByAsc() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute("CREATE TABLE t (k INTEGER NOT NULL PRIMARY KEY DESC, v1 VARCHAR, v2 VARCHAR) IMMUTABLE_ROWS=true");
    conn.createStatement().execute("CREATE INDEX idx ON t(v1, k)");
    PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
    QueryPlan plan = stmt.optimizeQuery("SELECT k FROM t WHERE k > 30 ORDER BY v1, k LIMIT 5");
    assertEquals("IDX", plan.getTableRef().getTable().getTableName().getString());
}
Also used : Connection(java.sql.Connection) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 53 with PhoenixStatement

use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.

the class ParallelIteratorsIT method testServerNameOnScan.

@Test
public void testServerNameOnScan() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES);
    byte[][] splits = new byte[][] { K3, K9, KR };
    createTable(conn, splits);
    PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
    ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + " LIMIT 1");
    rs.next();
    QueryPlan plan = stmt.getQueryPlan();
    List<List<Scan>> nestedScans = plan.getScans();
    assertNotNull(nestedScans);
    for (List<Scan> scans : nestedScans) {
        for (Scan scan : scans) {
            byte[] serverNameBytes = scan.getAttribute(BaseScannerRegionObserver.SCAN_REGION_SERVER);
            assertNotNull(serverNameBytes);
            ServerName serverName = ServerName.parseVersionedServerName(serverNameBytes);
            assertNotNull(serverName.getHostname());
        }
    }
}
Also used : ServerName(org.apache.hadoop.hbase.ServerName) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Scan(org.apache.hadoop.hbase.client.Scan) QueryPlan(org.apache.phoenix.compile.QueryPlan) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test)

Example 54 with PhoenixStatement

use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.

the class PhoenixInputFormat method getQueryPlan.

/**
     * Returns the query plan associated with the select query.
     */
private QueryPlan getQueryPlan(final Configuration configuration, String selectStatement) throws IOException {
    try {
        final String currentScnValue = configuration.get(PhoenixConfigurationUtil.CURRENT_SCN_VALUE);
        final Properties overridingProps = new Properties();
        if (currentScnValue != null) {
            overridingProps.put(PhoenixRuntime.CURRENT_SCN_ATTRIB, currentScnValue);
        }
        final Connection connection = PhoenixConnectionUtil.getInputConnection(configuration, overridingProps);
        Preconditions.checkNotNull(selectStatement);
        final Statement statement = connection.createStatement();
        final PhoenixStatement pstmt = statement.unwrap(PhoenixStatement.class);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Compiled query : " + selectStatement);
        }
        // Optimize the query plan so that we potentially use secondary indexes
        final QueryPlan queryPlan = pstmt.optimizeQuery(selectStatement);
        // Initialize the query plan so it sets up the parallel scans
        queryPlan.iterator(MapReduceParallelScanGrouper.getInstance());
        return queryPlan;
    } catch (Exception exception) {
        LOG.error(String.format("Failed to get the query plan with error [%s]", exception.getMessage()));
        throw new RuntimeException(exception);
    }
}
Also used : PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) HConnection(org.apache.hadoop.hbase.client.HConnection) Properties(java.util.Properties) QueryPlan(org.apache.phoenix.compile.QueryPlan) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) IOException(java.io.IOException)

Example 55 with PhoenixStatement

use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.

the class CreateTableIT method testStartKeyStopKey.

@Test
public void testStartKeyStopKey() throws SQLException {
    long ts = nextTimestamp();
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts));
    Connection conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("CREATE TABLE start_stop_test (pk char(2) not null primary key) SPLIT ON ('EA','EZ')");
    conn.close();
    String query = "select count(*) from start_stop_test where pk >= 'EA' and pk < 'EZ'";
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2));
    conn = DriverManager.getConnection(getUrl(), props);
    Statement statement = conn.createStatement();
    statement.execute(query);
    PhoenixStatement pstatement = statement.unwrap(PhoenixStatement.class);
    List<KeyRange> splits = pstatement.getQueryPlan().getSplits();
    assertTrue(splits.size() > 0);
}
Also used : PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Statement(java.sql.Statement) KeyRange(org.apache.phoenix.query.KeyRange) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) Test(org.junit.Test)

Aggregations

PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)64 Connection (java.sql.Connection)47 Test (org.junit.Test)42 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)34 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)13 ResultSet (java.sql.ResultSet)10 StatementContext (org.apache.phoenix.compile.StatementContext)10 QueryPlan (org.apache.phoenix.compile.QueryPlan)8 PTable (org.apache.phoenix.schema.PTable)8 SQLException (java.sql.SQLException)7 Statement (java.sql.Statement)7 PhoenixResultSet (org.apache.phoenix.jdbc.PhoenixResultSet)7 PColumn (org.apache.phoenix.schema.PColumn)7 ArrayList (java.util.ArrayList)6 Properties (java.util.Properties)6 MutationState (org.apache.phoenix.execute.MutationState)6 PreparedStatement (java.sql.PreparedStatement)5 List (java.util.List)5 Scan (org.apache.hadoop.hbase.client.Scan)5 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)5