use of org.apache.phoenix.schema.PName 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.PName in project phoenix by apache.
the class UpgradeIT method testMapTableToNamespaceDuringUpgrade.
@Test
public void testMapTableToNamespaceDuringUpgrade() throws SQLException, IOException, IllegalArgumentException, InterruptedException {
String[] strings = new String[] { "a", "b", "c", "d" };
try (Connection conn = DriverManager.getConnection(getUrl())) {
String schemaName = "TEST";
String phoenixFullTableName = schemaName + "." + generateUniqueName();
String indexName = "IDX_" + generateUniqueName();
String localIndexName = "LIDX_" + generateUniqueName();
String viewName = "VIEW_" + generateUniqueName();
String viewIndexName = "VIDX_" + generateUniqueName();
String[] tableNames = new String[] { phoenixFullTableName, schemaName + "." + indexName, schemaName + "." + localIndexName, "diff." + viewName, "test." + viewName, viewName };
String[] viewIndexes = new String[] { "diff." + viewIndexName, "test." + viewIndexName };
conn.createStatement().execute("CREATE TABLE " + phoenixFullTableName + "(k VARCHAR PRIMARY KEY, v INTEGER, f INTEGER, g INTEGER NULL, h INTEGER NULL)");
PreparedStatement upsertStmt = conn.prepareStatement("UPSERT INTO " + phoenixFullTableName + " VALUES(?, ?, 0, 0, 0)");
int i = 1;
for (String str : strings) {
upsertStmt.setString(1, str);
upsertStmt.setInt(2, i++);
upsertStmt.execute();
}
conn.commit();
// creating local index
conn.createStatement().execute("create local index " + localIndexName + " on " + phoenixFullTableName + "(K)");
// creating global index
conn.createStatement().execute("create index " + indexName + " on " + phoenixFullTableName + "(k)");
// creating view in schema 'diff'
conn.createStatement().execute("CREATE VIEW diff." + viewName + " (col VARCHAR) AS SELECT * FROM " + phoenixFullTableName);
// creating view in schema 'test'
conn.createStatement().execute("CREATE VIEW test." + viewName + " (col VARCHAR) AS SELECT * FROM " + phoenixFullTableName);
conn.createStatement().execute("CREATE VIEW " + viewName + "(col VARCHAR) AS SELECT * FROM " + phoenixFullTableName);
// Creating index on views
conn.createStatement().execute("create index " + viewIndexName + " on diff." + viewName + "(col)");
conn.createStatement().execute("create index " + viewIndexName + " on test." + viewName + "(col)");
// validate data
for (String tableName : tableNames) {
ResultSet rs = conn.createStatement().executeQuery("select * from " + tableName);
for (String str : strings) {
assertTrue(rs.next());
assertEquals(str, rs.getString(1));
}
}
// validate view Index data
for (String viewIndex : viewIndexes) {
ResultSet rs = conn.createStatement().executeQuery("select * from " + viewIndex);
for (String str : strings) {
assertTrue(rs.next());
assertEquals(str, rs.getString(2));
}
}
HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
assertTrue(admin.tableExists(phoenixFullTableName));
assertTrue(admin.tableExists(schemaName + QueryConstants.NAME_SEPARATOR + indexName));
assertTrue(admin.tableExists(MetaDataUtil.getViewIndexPhysicalName(Bytes.toBytes(phoenixFullTableName))));
Properties props = new Properties();
props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(true));
props.setProperty(QueryServices.IS_SYSTEM_TABLE_MAPPED_TO_NAMESPACE, Boolean.toString(false));
admin.close();
PhoenixConnection phxConn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
UpgradeUtil.upgradeTable(phxConn, phoenixFullTableName);
UpgradeUtil.mapChildViewsToNamespace(phxConn, phoenixFullTableName, props);
phxConn.close();
props = new Properties();
phxConn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
// purge MetaDataCache except for system tables
phxConn.getMetaDataCache().pruneTables(new PMetaData.Pruner() {
@Override
public boolean prune(PTable table) {
return table.getType() != PTableType.SYSTEM;
}
@Override
public boolean prune(PFunction function) {
return false;
}
});
admin = phxConn.getQueryServices().getAdmin();
String hbaseTableName = SchemaUtil.getPhysicalTableName(Bytes.toBytes(phoenixFullTableName), true).getNameAsString();
assertTrue(admin.tableExists(hbaseTableName));
assertTrue(admin.tableExists(Bytes.toBytes(hbaseTableName)));
assertTrue(admin.tableExists(schemaName + QueryConstants.NAMESPACE_SEPARATOR + indexName));
assertTrue(admin.tableExists(MetaDataUtil.getViewIndexPhysicalName(Bytes.toBytes(hbaseTableName))));
i = 0;
// validate data
for (String tableName : tableNames) {
ResultSet rs = phxConn.createStatement().executeQuery("select * from " + tableName);
for (String str : strings) {
assertTrue(rs.next());
assertEquals(str, rs.getString(1));
}
}
// validate view Index data
for (String viewIndex : viewIndexes) {
ResultSet rs = conn.createStatement().executeQuery("select * from " + viewIndex);
for (String str : strings) {
assertTrue(rs.next());
assertEquals(str, rs.getString(2));
}
}
PName tenantId = phxConn.getTenantId();
PName physicalName = PNameFactory.newName(hbaseTableName);
String oldSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(PNameFactory.newName(phoenixFullTableName), false);
String newSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(physicalName, true);
String newSequenceName = MetaDataUtil.getViewIndexSequenceName(physicalName, tenantId, true);
ResultSet rs = phxConn.createStatement().executeQuery("SELECT " + PhoenixDatabaseMetaData.CURRENT_VALUE + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + PhoenixDatabaseMetaData.TENANT_ID + " IS NULL AND " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + newSchemaName + "' AND " + PhoenixDatabaseMetaData.SEQUENCE_NAME + "='" + newSequenceName + "'");
assertTrue(rs.next());
assertEquals("-32765", rs.getString(1));
rs = phxConn.createStatement().executeQuery("SELECT " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + PhoenixDatabaseMetaData.CURRENT_VALUE + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + PhoenixDatabaseMetaData.TENANT_ID + " IS NULL AND " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + oldSchemaName + "'");
assertFalse(rs.next());
phxConn.close();
admin.close();
}
}
use of org.apache.phoenix.schema.PName 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());
}
}
use of org.apache.phoenix.schema.PName in project phoenix by apache.
the class DropColumnIT method testDroppingIndexedColDropsIndex.
@Test
public void testDroppingIndexedColDropsIndex() throws Exception {
String indexTableName = generateUniqueName();
String dataTableFullName = SchemaUtil.getTableName(SCHEMA_NAME, generateUniqueName());
String localIndexTableName1 = "LOCAL_" + indexTableName + "_1";
String localIndexTableName2 = "LOCAL_" + indexTableName + "_2";
try (Connection conn = getConnection()) {
conn.setAutoCommit(false);
conn.createStatement().execute("CREATE TABLE " + dataTableFullName + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) " + tableDDLOptions);
// create one regular and two local indexes
conn.createStatement().execute("CREATE INDEX " + indexTableName + " ON " + dataTableFullName + " (v2) INCLUDE (v1)");
conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName1 + " ON " + dataTableFullName + " (v2) INCLUDE (v1)");
conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName2 + " ON " + dataTableFullName + " (k) INCLUDE (v1)");
// upsert a single row
PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + dataTableFullName + " VALUES(?,?,?)");
stmt.setString(1, "a");
stmt.setString(2, "x");
stmt.setString(3, "1");
stmt.execute();
conn.commit();
// verify the indexes were created
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
PTable dataTable = pconn.getTable(new PTableKey(null, dataTableFullName));
assertEquals("Unexpected number of indexes ", 3, dataTable.getIndexes().size());
PTable indexTable = dataTable.getIndexes().get(0);
byte[] indexTablePhysicalName = indexTable.getPhysicalName().getBytes();
PName localIndexTablePhysicalName = dataTable.getIndexes().get(1).getPhysicalName();
// drop v2 which causes the regular index and first local index to be dropped
conn.createStatement().execute("ALTER TABLE " + dataTableFullName + " DROP COLUMN v2 ");
// verify the both of the indexes' metadata were dropped
conn.createStatement().execute("SELECT * FROM " + dataTableFullName);
try {
conn.createStatement().execute("SELECT * FROM " + indexTableName);
fail("Index should have been dropped");
} catch (TableNotFoundException e) {
}
pconn = conn.unwrap(PhoenixConnection.class);
dataTable = pconn.getTable(new PTableKey(null, dataTableFullName));
try {
pconn.getTable(new PTableKey(null, indexTableName));
fail("index should have been dropped");
} catch (TableNotFoundException e) {
}
try {
pconn.getTable(new PTableKey(null, localIndexTableName1));
fail("index should have been dropped");
} catch (TableNotFoundException e) {
}
assertEquals("Unexpected number of indexes ", 1, dataTable.getIndexes().size());
// verify that the regular index physical table was dropped
try {
conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(indexTablePhysicalName);
fail("Index table should have been dropped");
} catch (TableNotFoundException e) {
}
// verify that the local index physical table was *not* dropped
conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(localIndexTablePhysicalName.getBytes());
PTable localIndex2 = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, localIndexTableName2));
// there should be a single row belonging to localIndexTableName2
Scan scan = new Scan();
scan.addFamily(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES);
HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(localIndexTablePhysicalName.getBytes());
ResultScanner results = table.getScanner(scan);
Result result = results.next();
assertNotNull(result);
String indexColumnName = IndexUtil.getIndexColumnName(QueryConstants.DEFAULT_COLUMN_FAMILY, "V1");
PColumn localIndexCol = localIndex2.getColumnForColumnName(indexColumnName);
byte[] colValue;
if (!mutable && columnEncoded) {
KeyValueColumnExpression colExpression = new SingleCellColumnExpression(localIndexCol, indexColumnName, localIndex2.getEncodingScheme(), localIndex2.getImmutableStorageScheme());
ImmutableBytesPtr ptr = new ImmutableBytesPtr();
colExpression.evaluate(new ResultTuple(result), ptr);
colValue = ptr.copyBytesIfNecessary();
} else {
colValue = result.getValue(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES, localIndexCol.getColumnQualifierBytes());
}
assertNotNull("localIndexTableName2 row is missing", colValue);
assertNull(results.next());
}
}
use of org.apache.phoenix.schema.PName in project phoenix by apache.
the class SchemaUtil method getColumnDisplayName.
public static String getColumnDisplayName(PColumn column) {
PName columnName = column.getFamilyName();
String cf = columnName == null ? null : columnName.getString();
return getName(cf == null || cf.isEmpty() ? null : cf, column.getName().getString(), false);
}
Aggregations