use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class ViewIndexIT method assertRowCount.
private void assertRowCount(Connection conn, String fullTableName, String fullBaseName, int expectedCount) throws SQLException {
PhoenixStatement stmt = conn.createStatement().unwrap(PhoenixStatement.class);
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + fullTableName);
assertTrue(rs.next());
assertEquals(expectedCount, rs.getInt(1));
// Ensure that index is being used
rs = stmt.executeQuery("EXPLAIN SELECT COUNT(*) FROM " + fullTableName);
if (fullBaseName != null) {
// Uses index and finds correct number of rows
assertTrue(QueryUtil.getExplainPlan(rs).startsWith("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(Bytes.toBytes(fullBaseName)))));
}
// Force it not to use index and still finds correct number of rows
rs = stmt.executeQuery("SELECT /*+ NO_INDEX */ * FROM " + fullTableName);
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(expectedCount, count);
// Ensure that the table, not index is being used
assertEquals(fullTableName, stmt.getQueryPlan().getContext().getCurrentTable().getTable().getName().getString());
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class RoundRobinResultIteratorWithStatsIT method testRoundRobinBehavior.
@Test
public void testRoundRobinBehavior() throws Exception {
int nRows = 30000;
try (Connection conn = DriverManager.getConnection(getUrl())) {
conn.createStatement().execute("CREATE TABLE " + tableName + "(K VARCHAR PRIMARY KEY)");
PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES(?)");
for (int i = 1; i <= nRows; i++) {
stmt.setString(1, i + "");
stmt.executeUpdate();
if ((i % 2000) == 0) {
conn.commit();
}
}
conn.commit();
conn.createStatement().execute("UPDATE STATISTICS " + tableName);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
MockParallelIteratorFactory parallelIteratorFactory = new MockParallelIteratorFactory();
phxConn.setIteratorFactory(parallelIteratorFactory);
ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
StatementContext ctx = rs.unwrap(PhoenixResultSet.class).getContext();
PTable table = ctx.getResolver().getTables().get(0).getTable();
parallelIteratorFactory.setTable(table);
PhoenixStatement pstmt = stmt.unwrap(PhoenixStatement.class);
int numIterators = pstmt.getQueryPlan().getSplits().size();
assertTrue(numIterators > 1);
int numFetches = 2 * numIterators;
List<String> iteratorOrder = new ArrayList<>(numFetches);
for (int i = 1; i <= numFetches; i++) {
rs.next();
iteratorOrder.add(rs.getString(1));
}
/*
* Because TableResultIterators are created in parallel in multiple threads, their relative order is not
* deterministic. However, once the iterators are assigned to a RoundRobinResultIterator, the order in which
* the next iterator is picked is deterministic - i1, i2, .. i7, i8, i1, i2, .. i7, i8, i1, i2, ..
*/
for (int i = 0; i < numIterators; i++) {
assertEquals(iteratorOrder.get(i), iteratorOrder.get(i + numIterators));
}
}
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class PhoenixQueryTimeoutIT method loadDataAndPrepareQuery.
//-----------------------------------------------------------------
// Private Helper Methods
//-----------------------------------------------------------------
private PreparedStatement loadDataAndPrepareQuery(int timeoutMs, int timeoutSecs) throws Exception, SQLException {
Properties props = new Properties();
props.setProperty(QueryServices.THREAD_TIMEOUT_MS_ATTRIB, String.valueOf(timeoutMs));
Connection conn = DriverManager.getConnection(getUrl(), props);
PreparedStatement ps = conn.prepareStatement("SELECT * FROM " + tableName);
PhoenixStatement phoenixStmt = ps.unwrap(PhoenixStatement.class);
assertEquals(timeoutMs, phoenixStmt.getQueryTimeoutInMillis());
assertEquals(timeoutSecs, phoenixStmt.getQueryTimeout());
return ps;
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class RoundRobinResultIteratorIT method testIteratorsPickedInRoundRobinFashionForSaltedTable.
@Test
public void testIteratorsPickedInRoundRobinFashionForSaltedTable() throws Exception {
try (Connection conn = getConnection()) {
String testTable = "testIteratorsPickedInRoundRobinFashionForSaltedTable".toUpperCase();
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE " + testTable + "(K VARCHAR PRIMARY KEY) SALT_BUCKETS = 8");
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
MockParallelIteratorFactory parallelIteratorFactory = new MockParallelIteratorFactory();
phxConn.setIteratorFactory(parallelIteratorFactory);
ResultSet rs = stmt.executeQuery("SELECT * FROM " + testTable);
StatementContext ctx = rs.unwrap(PhoenixResultSet.class).getContext();
PTable table = ctx.getResolver().getTables().get(0).getTable();
parallelIteratorFactory.setTable(table);
PhoenixStatement pstmt = stmt.unwrap(PhoenixStatement.class);
int numIterators = pstmt.getQueryPlan().getSplits().size();
assertEquals(8, numIterators);
int numFetches = 2 * numIterators;
List<String> iteratorOrder = new ArrayList<>(numFetches);
for (int i = 1; i <= numFetches; i++) {
rs.next();
iteratorOrder.add(rs.getString(1));
}
/*
* Because TableResultIterators are created in parallel in multiple threads, their relative order is not
* deterministic. However, once the iterators are assigned to a RoundRobinResultIterator, the order in which
* the next iterator is picked is deterministic - i1, i2, .. i7, i8, i1, i2, .. i7, i8, i1, i2, ..
*/
for (int i = 0; i < numIterators; i++) {
assertEquals(iteratorOrder.get(i), iteratorOrder.get(i + numIterators));
}
}
}
use of org.apache.phoenix.jdbc.PhoenixStatement in project phoenix by apache.
the class PostLocalIndexDDLCompiler method compile.
public MutationPlan compile(PTable index) throws SQLException {
try (final PhoenixStatement statement = new PhoenixStatement(connection)) {
String query = "SELECT count(*) FROM " + tableName;
final QueryPlan plan = statement.compileQuery(query);
TableRef tableRef = plan.getTableRef();
Scan scan = plan.getContext().getScan();
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
final PTable dataTable = tableRef.getTable();
List<PTable> indexes = Lists.newArrayListWithExpectedSize(1);
for (PTable indexTable : dataTable.getIndexes()) {
if (indexTable.getKey().equals(index.getKey())) {
index = indexTable;
break;
}
}
// Only build newly created index.
indexes.add(index);
IndexMaintainer.serialize(dataTable, ptr, indexes, plan.getContext().getConnection());
// Set attribute on scan that UngroupedAggregateRegionObserver will switch on.
// We'll detect that this attribute was set the server-side and write the index
// rows per region as a result. The value of the attribute will be our persisted
// index maintainers.
// Define the LOCAL_INDEX_BUILD as a new static in BaseScannerRegionObserver
scan.setAttribute(BaseScannerRegionObserver.LOCAL_INDEX_BUILD_PROTO, ByteUtil.copyKeyBytesIfNecessary(ptr));
// By default, we'd use a FirstKeyOnly filter as nothing else needs to be projected for count(*).
// However, in this case, we need to project all of the data columns that contribute to the index.
IndexMaintainer indexMaintainer = index.getIndexMaintainer(dataTable, connection);
for (ColumnReference columnRef : indexMaintainer.getAllColumns()) {
if (index.getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS) {
scan.addFamily(columnRef.getFamily());
} else {
scan.addColumn(columnRef.getFamily(), columnRef.getQualifier());
}
}
// with a connectionless connection (which makes testing easier).
return new BaseMutationPlan(plan.getContext(), Operation.UPSERT) {
@Override
public MutationState execute() throws SQLException {
connection.getMutationState().commitDDLFence(dataTable);
Tuple tuple = plan.iterator().next();
long rowCount = 0;
if (tuple != null) {
Cell kv = tuple.getValue(0);
ImmutableBytesWritable tmpPtr = new ImmutableBytesWritable(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
// A single Cell will be returned with the count(*) - we decode that here
rowCount = PLong.INSTANCE.getCodec().decodeLong(tmpPtr, SortOrder.getDefault());
}
// rows that were added.
return new MutationState(0, 0, connection, rowCount);
}
};
}
}
Aggregations