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());
}
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());
}
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());
}
}
}
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);
}
}
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);
}
Aggregations