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 DropColumnIT method helpTestDroppingIndexedColDropsViewIndex.
public void helpTestDroppingIndexedColDropsViewIndex(boolean isMultiTenant) throws Exception {
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
props.setProperty(TENANT_ID_ATTRIB, TENANT_ID);
try (Connection conn = getConnection();
Connection viewConn = isMultiTenant ? getConnection(props) : conn) {
String tableWithView = generateUniqueName();
String viewOfTable = generateUniqueName();
String viewIndex1 = generateUniqueName();
String viewIndex2 = generateUniqueName();
conn.setAutoCommit(false);
viewConn.setAutoCommit(false);
String ddlFormat = "CREATE TABLE " + tableWithView + " (%s k VARCHAR NOT NULL, v1 VARCHAR, v2 VARCHAR, v3 VARCHAR, v4 VARCHAR CONSTRAINT PK PRIMARY KEY(%s k))%s";
String ddl = String.format(ddlFormat, isMultiTenant ? "TENANT_ID VARCHAR NOT NULL, " : "", isMultiTenant ? "TENANT_ID, " : "", isMultiTenant ? "MULTI_TENANT=true" : "");
conn.createStatement().execute(ddl);
viewConn.createStatement().execute("CREATE VIEW " + viewOfTable + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableWithView);
// create an index with the column that will be dropped
viewConn.createStatement().execute("CREATE INDEX " + viewIndex1 + " ON " + viewOfTable + "(v2) INCLUDE (v4)");
// create an index without the column that will be dropped
viewConn.createStatement().execute("CREATE INDEX " + viewIndex2 + " ON " + viewOfTable + "(v1) INCLUDE (v4)");
// verify index was created
try {
viewConn.createStatement().execute("SELECT * FROM " + viewIndex1);
} catch (TableNotFoundException e) {
fail("Index on view was not created");
}
// upsert a single row
PreparedStatement stmt = viewConn.prepareStatement("UPSERT INTO " + viewOfTable + " VALUES(?,?,?,?,?,?,?)");
stmt.setString(1, "a");
stmt.setString(2, "b");
stmt.setString(3, "c");
stmt.setString(4, "d");
stmt.setString(5, "e");
stmt.setInt(6, 1);
stmt.setString(7, "g");
stmt.execute();
viewConn.commit();
// verify the index was created
PhoenixConnection pconn = viewConn.unwrap(PhoenixConnection.class);
PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
PTable view = pconn.getTable(new PTableKey(tenantId, viewOfTable));
PTable viewIndex = pconn.getTable(new PTableKey(tenantId, viewIndex1));
byte[] viewIndexPhysicalTable = viewIndex.getPhysicalName().getBytes();
assertNotNull("Can't find view index", viewIndex);
assertEquals("Unexpected number of indexes ", 2, view.getIndexes().size());
assertEquals("Unexpected index ", viewIndex1, view.getIndexes().get(0).getName().getString());
assertEquals("Unexpected index ", viewIndex2, view.getIndexes().get(1).getName().getString());
// drop two columns
conn.createStatement().execute("ALTER TABLE " + tableWithView + " DROP COLUMN v2, v3 ");
// verify columns were dropped
try {
conn.createStatement().execute("SELECT v2 FROM " + tableWithView);
fail("Column should have been dropped");
} catch (ColumnNotFoundException e) {
}
try {
conn.createStatement().execute("SELECT v3 FROM " + tableWithView);
fail("Column should have been dropped");
} catch (ColumnNotFoundException e) {
}
// verify index metadata was dropped
try {
viewConn.createStatement().execute("SELECT * FROM " + viewIndex1);
fail("Index metadata should have been dropped");
} catch (TableNotFoundException e) {
}
pconn = viewConn.unwrap(PhoenixConnection.class);
view = pconn.getTable(new PTableKey(tenantId, viewOfTable));
try {
viewIndex = pconn.getTable(new PTableKey(tenantId, viewIndex1));
fail("View index should have been dropped");
} catch (TableNotFoundException e) {
}
assertEquals("Unexpected number of indexes ", 1, view.getIndexes().size());
assertEquals("Unexpected index ", viewIndex2, view.getIndexes().get(0).getName().getString());
// verify that the physical index view table is *not* dropped
conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(viewIndexPhysicalTable);
// scan the physical table and verify there is a single row for the second local index
Scan scan = new Scan();
HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(viewIndexPhysicalTable);
ResultScanner results = table.getScanner(scan);
Result result = results.next();
assertNotNull(result);
PTable viewIndexPTable = pconn.getTable(new PTableKey(pconn.getTenantId(), viewIndex2));
PColumn column = viewIndexPTable.getColumnForColumnName(IndexUtil.getIndexColumnName(QueryConstants.DEFAULT_COLUMN_FAMILY, "V4"));
byte[] cq = column.getColumnQualifierBytes();
// there should be a single row belonging to VIEWINDEX2
assertNotNull(viewIndex2 + " row is missing", result.getValue(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, cq));
assertNull(results.next());
}
}
Aggregations