use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ParameterizedTransactionIT method testRowConflicts.
private void testRowConflicts(String fullTableName) throws Exception {
try (Connection conn1 = DriverManager.getConnection(getUrl());
Connection conn2 = DriverManager.getConnection(getUrl())) {
conn1.setAutoCommit(false);
conn2.setAutoCommit(false);
String selectSql = "SELECT * FROM " + fullTableName;
conn1.setAutoCommit(false);
ResultSet rs = conn1.createStatement().executeQuery(selectSql);
boolean immutableRows = conn1.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, fullTableName)).isImmutableRows();
assertFalse(rs.next());
// upsert row using conn1
String upsertSql = "UPSERT INTO " + fullTableName + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk, a.int_col1) VALUES(?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = conn1.prepareStatement(upsertSql);
TestUtil.setRowKeyColumns(stmt, 1);
stmt.setInt(7, 10);
stmt.execute();
// upsert row using conn2
upsertSql = "UPSERT INTO " + fullTableName + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk, b.int_col2) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn2.prepareStatement(upsertSql);
TestUtil.setRowKeyColumns(stmt, 1);
stmt.setInt(7, 11);
stmt.execute();
conn1.commit();
// second commit should fail
try {
conn2.commit();
if (!immutableRows)
fail();
} catch (SQLException e) {
if (immutableRows)
fail();
assertEquals(e.getErrorCode(), SQLExceptionCode.TRANSACTION_CONFLICT_EXCEPTION.getErrorCode());
}
}
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class ParameterizedTransactionIT method testCreateTableToBeTransactional.
@Test
public void testCreateTableToBeTransactional() throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(getUrl(), props);
String t1 = generateUniqueName();
String t2 = generateUniqueName();
String ddl = "CREATE TABLE " + t1 + " (k varchar primary key) " + tableDDLOptions;
conn.createStatement().execute(ddl);
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
PTable table = pconn.getTable(new PTableKey(null, t1));
HTableInterface htable = pconn.getQueryServices().getTable(Bytes.toBytes(t1));
assertTrue(table.isTransactional());
assertTrue(htable.getTableDescriptor().getCoprocessors().contains(PhoenixTransactionalProcessor.class.getName()));
try {
ddl = "ALTER TABLE " + t1 + " SET transactional=false";
conn.createStatement().execute(ddl);
fail();
} catch (SQLException e) {
assertEquals(SQLExceptionCode.TX_MAY_NOT_SWITCH_TO_NON_TX.getErrorCode(), e.getErrorCode());
}
HBaseAdmin admin = pconn.getQueryServices().getAdmin();
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(t2));
desc.addFamily(new HColumnDescriptor(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES));
admin.createTable(desc);
ddl = "CREATE TABLE " + t2 + " (k varchar primary key) transactional=true";
conn.createStatement().execute(ddl);
HTableDescriptor htableDescriptor = admin.getTableDescriptor(TableName.valueOf(t2));
String str = htableDescriptor.getValue(PhoenixTransactionContext.READ_NON_TX_DATA);
assertEquals(Boolean.TRUE.toString(), str);
// Should be ok, as HBase metadata should match existing metadata.
ddl = "CREATE TABLE IF NOT EXISTS " + t1 + " (k varchar primary key)";
try {
conn.createStatement().execute(ddl);
fail();
} catch (SQLException e) {
assertEquals(SQLExceptionCode.TX_MAY_NOT_SWITCH_TO_NON_TX.getErrorCode(), e.getErrorCode());
}
ddl += " transactional=true";
conn.createStatement().execute(ddl);
table = pconn.getTable(new PTableKey(null, t1));
htable = pconn.getQueryServices().getTable(Bytes.toBytes(t1));
assertTrue(table.isTransactional());
assertTrue(htable.getTableDescriptor().getCoprocessors().contains(PhoenixTransactionalProcessor.class.getName()));
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class NonTxIndexBuilderTest method getTestIndexMaintainer.
private IndexMaintainer getTestIndexMaintainer() throws Exception {
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
// disable column encoding, makes debugging easier
props.put(QueryServices.DEFAULT_COLUMN_ENCODED_BYTES_ATRRIB, "0");
Connection conn = DriverManager.getConnection(getUrl(), props);
try {
conn.setAutoCommit(true);
conn.createStatement().execute(TEST_TABLE_DDL);
conn.createStatement().execute(TEST_TABLE_INDEX_DDL);
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), TEST_TABLE_STRING));
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
table.getIndexMaintainers(ptr, pconn);
List<IndexMaintainer> indexMaintainerList = IndexMaintainer.deserialize(ptr, GenericKeyValueBuilder.INSTANCE, true);
assertEquals(1, indexMaintainerList.size());
IndexMaintainer indexMaintainer = indexMaintainerList.get(0);
return indexMaintainer;
} finally {
conn.close();
}
}
use of org.apache.phoenix.schema.PTableKey in project phoenix by apache.
the class IndexColumnNamesTest method testGetDynamicColPrecision.
/**
* Tests that col types with a precision are outputted correctly in the dynamic columns
* @throws SQLException
*/
@Test
public void testGetDynamicColPrecision() throws SQLException {
conn.createStatement().execute(DYNAMIC_COL_DDL);
conn.createStatement().execute(DYNAMIC_COL_IDX_DDL);
PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
pDataTable = pconn.getTable(new PTableKey(pconn.getTenantId(), "PRECISION_NAME_TEST"));
pIndexTable = pconn.getTable(new PTableKey(pconn.getTenantId(), "PRECISION_NAME_IDX_TEST"));
IndexColumnNames indexColumnNames = new IndexColumnNames(pDataTable, pIndexTable);
assertEquals("[\"CHAR_TEST\" CHAR(15), \"VARCHAR_TEST\" VARCHAR(1), \"DECIMAL_TEST\" DECIMAL(10,2), \"BINARY_TEST\" BINARY(11), \"VARCHAR_UNSPEC\" VARCHAR, \"DEC_UNSPEC\" DECIMAL]", indexColumnNames.getDynamicDataCols().toString());
assertEquals("[\":CHAR_TEST\" CHAR(15), \"0:VARCHAR_TEST\" VARCHAR(1), \"0:DECIMAL_TEST\" DECIMAL(10,2), \"0:BINARY_TEST\" BINARY(11), \"0:VARCHAR_UNSPEC\" VARCHAR, \"0:DEC_UNSPEC\" DECIMAL]", indexColumnNames.getDynamicIndexCols().toString());
}
Aggregations