use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class PhoenixRuntime method getPkColumns.
private static List<PColumn> getPkColumns(PTable ptable, Connection conn) throws SQLException {
PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
List<PColumn> pkColumns = ptable.getPKColumns();
// Skip the salting column and the view index id column if present.
// Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
// get a sublist of pkColumns by skipping the offset columns.
pkColumns = pkColumns.subList(offset, pkColumns.size());
if (ptable.getType() == PTableType.INDEX) {
// index tables have the same schema name as their parent/data tables.
String fullDataTableName = ptable.getParentName().getString();
// Get the corresponding columns of the data table.
List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
pkColumns = dataColumns;
}
return pkColumns;
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class PhoenixRuntime method getTable.
/**
*
* @param conn
* @param name requires a pre-normalized table name or a pre-normalized schema and table name
* @return
* @throws SQLException
*/
public static PTable getTable(Connection conn, String name) throws SQLException {
PTable table = null;
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
try {
table = pconn.getTable(new PTableKey(pconn.getTenantId(), name));
} catch (TableNotFoundException e) {
String schemaName = SchemaUtil.getSchemaNameFromFullName(name);
String tableName = SchemaUtil.getTableNameFromFullName(name);
MetaDataMutationResult result = new MetaDataClient(pconn).updateCache(schemaName, tableName);
if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
throw e;
}
table = result.getTable();
}
return table;
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class ViewCompilerTest method assertViewType.
public void assertViewType(String[] viewNames, String[] viewDDLs, ViewType viewType) throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
String ct = "CREATE TABLE t (k1 INTEGER NOT NULL, k2 VARCHAR, v VARCHAR, CONSTRAINT pk PRIMARY KEY (k1,k2))";
conn.createStatement().execute(ct);
for (String viewDDL : viewDDLs) {
conn.createStatement().execute(viewDDL);
}
StringBuilder buf = new StringBuilder();
int count = 0;
for (String view : viewNames) {
PTable table = conn.getTable(new PTableKey(null, view));
assertEquals(viewType, table.getViewType());
conn.createStatement().execute("DROP VIEW " + table.getName().getString());
buf.append(' ');
buf.append(table.getName().getString());
count++;
}
assertEquals("Expected " + viewDDLs.length + ", but got " + count + ":" + buf.toString(), viewDDLs.length, count);
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class ViewCompilerTest method testViewInvalidation.
@Test
public void testViewInvalidation() throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
String ct = "CREATE TABLE s1.t (k1 INTEGER NOT NULL, k2 VARCHAR, v VARCHAR, CONSTRAINT pk PRIMARY KEY (k1,k2))";
conn.createStatement().execute(ct);
conn.createStatement().execute("CREATE VIEW s2.v3 AS SELECT * FROM s1.t WHERE v = 'bar'");
// TODO: should it be an error to remove columns from a VIEW that we're defined there?
conn.createStatement().execute("ALTER VIEW s2.v3 DROP COLUMN v");
try {
conn.createStatement().executeQuery("SELECT * FROM s2.v3");
fail();
} catch (ColumnNotFoundException e) {
}
// No error, as v still exists in t
conn.createStatement().execute("CREATE VIEW s2.v4 AS SELECT * FROM s1.t WHERE v = 'bas'");
// No error, even though view is invalid
conn.createStatement().execute("DROP VIEW s2.v3");
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class WhereCompilerTest method testPaddedRowKeyFilter.
@Test
public void testPaddedRowKeyFilter() throws SQLException {
String keyPrefix = "fo";
String query = "select * from atable where entity_id=?";
List<Object> binds = Arrays.<Object>asList(keyPrefix);
PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
bindParams(pstmt, binds);
QueryPlan plan = pstmt.optimizeQuery();
Scan scan = plan.getContext().getScan();
assertEquals(0, scan.getStartRow().length);
assertEquals(0, scan.getStopRow().length);
assertNotNull(scan.getFilter());
}
Aggregations