use of org.apache.phoenix.schema.PColumn 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.schema.PColumn in project phoenix by apache.
the class PhoenixRuntime method getColumn.
private static PColumn getColumn(PTable table, @Nullable String familyName, String columnName) throws SQLException {
if (table == null) {
throw new SQLException("Table must not be null.");
}
if (columnName == null) {
throw new SQLException("columnName must not be null.");
}
// normalize and remove quotes from family and column names before looking up.
familyName = SchemaUtil.normalizeIdentifier(familyName);
columnName = SchemaUtil.normalizeIdentifier(columnName);
// we're dealing with an index table.
if (table.getType() == PTableType.INDEX) {
columnName = IndexUtil.getIndexColumnName(familyName, columnName);
}
PColumn pColumn = null;
if (familyName != null) {
PColumnFamily family = table.getColumnFamily(familyName);
pColumn = family.getPColumnForColumnName(columnName);
} else {
pColumn = table.getColumnForColumnName(columnName);
}
return pColumn;
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class WhereCompilerTest method testAndPKAndNotPK.
@Test
public void testAndPKAndNotPK() throws SQLException {
String query = "select * from bugTable where ID = 'i2' and company = 'c3'";
PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
pconn.createStatement().execute("create table bugTable(ID varchar primary key,company varchar)");
PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
QueryPlan plan = pstmt.optimizeQuery();
Scan scan = plan.getContext().getScan();
Filter filter = scan.getFilter();
PColumn column = plan.getTableRef().getTable().getColumnForColumnName("COMPANY");
assertEquals(singleKVFilter(constantComparison(CompareOp.EQUAL, new KeyValueColumnExpression(column), "c3")), filter);
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class ColumnExpressionTest method testSerializationWithNullScale.
@Test
public void testSerializationWithNullScale() throws Exception {
int maxLen = 30;
PName colName = PNameFactory.newName("c1");
PColumn column = new PColumnImpl(colName, PNameFactory.newName("f1"), PBinary.INSTANCE, maxLen, null, true, 20, SortOrder.getDefault(), 0, null, false, null, false, false, colName.getBytes());
ColumnExpression colExp = new KeyValueColumnExpression(column);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dOut = new DataOutputStream(baos);
colExp.write(dOut);
dOut.flush();
ColumnExpression colExp2 = new KeyValueColumnExpression();
byte[] bytes = baos.toByteArray();
DataInputStream dIn = new DataInputStream(new ByteArrayInputStream(bytes, 0, bytes.length));
colExp2.readFields(dIn);
assertEquals(maxLen, colExp2.getMaxLength().intValue());
assertNull(colExp2.getScale());
assertEquals(PBinary.INSTANCE, colExp2.getDataType());
}
use of org.apache.phoenix.schema.PColumn in project phoenix by apache.
the class AlterMultiTenantTableWithViewsIT method getIndexOfPkColumn.
private int getIndexOfPkColumn(PhoenixConnection conn, String columnName, String tableName) throws SQLException {
String normalizedTableName = SchemaUtil.normalizeIdentifier(tableName);
PTable table = conn.getTable(new PTableKey(conn.getTenantId(), normalizedTableName));
List<PColumn> pkCols = table.getPKColumns();
String normalizedColumnName = SchemaUtil.normalizeIdentifier(columnName);
int i = 0;
for (PColumn pkCol : pkCols) {
if (pkCol.getName().getString().equals(normalizedColumnName)) {
return i;
}
i++;
}
return -1;
}
Aggregations