use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class CreateTableIT method assertColumnEncodingMetadata.
private void assertColumnEncodingMetadata(QualifierEncodingScheme expectedEncodingScheme, ImmutableStorageScheme expectedStorageScheme, String tableName, Connection conn) throws Exception {
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable table = phxConn.getTable(new PTableKey(null, tableName));
assertEquals(expectedEncodingScheme, table.getEncodingScheme());
assertEquals(expectedStorageScheme, table.getImmutableStorageScheme());
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class ColumnEncodedBytesPropIT method testValidateProperty.
@Test
public void testValidateProperty() throws SQLException {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
String dataTableFullName1 = SchemaUtil.getTableName("", generateUniqueName());
String dataTableFullName2 = SchemaUtil.getTableName("", generateUniqueName());
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
Statement stmt = conn.createStatement();
String ddl = "CREATE TABLE " + dataTableFullName1 + " (id varchar not null, val varchar " + " CONSTRAINT pk PRIMARY KEY (id)) COLUMN_ENCODED_BYTES=4";
stmt.execute(ddl);
ddl = "CREATE TABLE " + dataTableFullName2 + " (id varchar not null, val varchar " + " CONSTRAINT pk PRIMARY KEY (id)) COLUMN_ENCODED_BYTES=NONE";
stmt.execute(ddl);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable dataTable1 = phxConn.getTable(new PTableKey(null, dataTableFullName1));
assertEquals("Encoding scheme set incorrectly", QualifierEncodingScheme.FOUR_BYTE_QUALIFIERS, dataTable1.getEncodingScheme());
PTable dataTable2 = phxConn.getTable(new PTableKey(null, dataTableFullName2));
assertEquals("Encoding scheme set incorrectly", QualifierEncodingScheme.NON_ENCODED_QUALIFIERS, dataTable2.getEncodingScheme());
}
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class AlterMultiTenantTableWithViewsIT method checkColumnPartOfPk.
private boolean checkColumnPartOfPk(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);
for (PColumn pkCol : pkCols) {
if (pkCol.getName().getString().equals(normalizedColumnName)) {
return true;
}
}
return false;
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class AutoPartitionViewsIT method testValidateAttributes.
@Test
public void testValidateAttributes() throws SQLException {
try (Connection conn = DriverManager.getConnection(getUrl());
Connection viewConn1 = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : DriverManager.getConnection(getUrl());
Connection viewConn2 = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : DriverManager.getConnection(getUrl())) {
String tableName = generateUniqueName();
String autoSeqName = generateUniqueName();
try {
String ddl = String.format("CREATE TABLE " + tableName + " (%s metricId VARCHAR, val1 DOUBLE, val2 DOUBLE CONSTRAINT PK PRIMARY KEY( %s metricId)) %s", isMultiTenant ? "tenantId VARCHAR, " : "", isMultiTenant ? "tenantId, " : "", String.format(tableDDLOptions, autoSeqName));
conn.createStatement().execute(ddl);
fail("Sequence value must be castable to the auto partition id column data type");
} catch (SQLException e) {
assertEquals(SQLExceptionCode.SEQUENCE_NOT_CASTABLE_TO_AUTO_PARTITION_ID_COLUMN.getErrorCode(), e.getErrorCode());
}
String ddl = String.format("CREATE TABLE " + tableName + " (%s metricId INTEGER NOT NULL, val1 DOUBLE, val2 DOUBLE CONSTRAINT PK PRIMARY KEY( %s metricId)) %s", isMultiTenant ? "tenantId VARCHAR NOT NULL, " : "", isMultiTenant ? "tenantId, " : "", String.format(tableDDLOptions, autoSeqName));
conn.createStatement().execute(ddl);
String baseViewName = generateUniqueName();
String metricView1 = baseViewName + "_VIEW1";
String metricView2 = baseViewName + "_VIEW2";
String metricView3 = baseViewName + "_VIEW3";
String metricView4 = baseViewName + "_VIEW4";
try {
viewConn1.createStatement().execute("CREATE VIEW " + metricView1 + " AS SELECT * FROM " + tableName);
fail("Auto-partition sequence must be created before view is created");
} catch (SequenceNotFoundException e) {
}
conn.createStatement().execute("CREATE SEQUENCE " + autoSeqName + " start with " + (Integer.MAX_VALUE - 2) + " cache 1");
viewConn1.createStatement().execute("CREATE VIEW " + metricView1 + " AS SELECT * FROM " + tableName + " WHERE val2=1.2");
// create a view without a where clause
viewConn1.createStatement().execute("CREATE VIEW " + metricView2 + " AS SELECT * FROM " + tableName);
// create a view with a complex where clause
viewConn1.createStatement().execute("CREATE VIEW " + metricView3 + " AS SELECT * FROM " + tableName + " WHERE val1=1.0 OR val2=2.0");
try {
viewConn1.createStatement().execute("CREATE VIEW " + metricView4 + " AS SELECT * FROM " + tableName);
fail("Creating a view with a partition id that is too large should fail");
} catch (SQLException e) {
assertEquals(SQLExceptionCode.CANNOT_COERCE_AUTO_PARTITION_ID.getErrorCode(), e.getErrorCode());
}
if (isMultiTenant) {
// load tables into cache
viewConn1.createStatement().execute("SELECT * FROM " + metricView1);
viewConn1.createStatement().execute("SELECT * FROM " + metricView2);
viewConn1.createStatement().execute("SELECT * FROM " + metricView3);
}
PhoenixConnection pconn = viewConn1.unwrap(PhoenixConnection.class);
PTable view1 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView1));
PTable view2 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView2));
PTable view3 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView3));
// verify the view statement was set correctly
String expectedViewStatement1 = "SELECT * FROM \"" + tableName + "\" WHERE VAL2 = 1.2 AND METRICID = " + (Integer.MAX_VALUE - 2);
String expectedViewStatement2 = "SELECT * FROM \"" + tableName + "\" WHERE METRICID = " + (Integer.MAX_VALUE - 1);
String expectedViewStatement3 = "SELECT * FROM \"" + tableName + "\" WHERE (VAL1 = 1.0 OR VAL2 = 2.0) AND METRICID = " + Integer.MAX_VALUE;
assertEquals("Unexpected view statement", expectedViewStatement1, view1.getViewStatement());
assertEquals("Unexpected view statement", expectedViewStatement2, view2.getViewStatement());
assertEquals("Unexpected view statement", expectedViewStatement3, view3.getViewStatement());
// verify isViewReferenced was set correctly
int expectedParitionColIndex = isMultiTenant ? 1 : 0;
PColumn partitionCol1 = view1.getColumns().get(expectedParitionColIndex);
PColumn partitionCol2 = view2.getColumns().get(expectedParitionColIndex);
PColumn partitionCol3 = view3.getColumns().get(expectedParitionColIndex);
assertTrue("Partition column view referenced attribute should be true ", partitionCol1.isViewReferenced());
assertTrue("Partition column view referenced attribute should be true ", partitionCol2.isViewReferenced());
assertTrue("Partition column view referenced attribute should be true ", partitionCol3.isViewReferenced());
// verify viewConstant was set correctly
byte[] expectedPartition1 = new byte[Bytes.SIZEOF_INT + 1];
PInteger.INSTANCE.toBytes(Integer.MAX_VALUE - 2, expectedPartition1, 0);
byte[] expectedPartition2 = new byte[Bytes.SIZEOF_INT + 1];
PInteger.INSTANCE.toBytes(Integer.MAX_VALUE - 1, expectedPartition2, 0);
byte[] expectedPartition3 = new byte[Bytes.SIZEOF_INT + 1];
PInteger.INSTANCE.toBytes(Integer.MAX_VALUE, expectedPartition3, 0);
assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition1, partitionCol1.getViewConstant());
assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition2, partitionCol2.getViewConstant());
assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition3, partitionCol3.getViewConstant());
// verify that the table was created correctly on the server
viewConn2.createStatement().execute("SELECT * FROM " + metricView1);
viewConn2.createStatement().execute("SELECT * FROM " + metricView2);
viewConn2.createStatement().execute("SELECT * FROM " + metricView3);
pconn = viewConn2.unwrap(PhoenixConnection.class);
view1 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView1));
view2 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView2));
view3 = pconn.getTable(new PTableKey(pconn.getTenantId(), metricView3));
// verify the view statement was set correctly
assertEquals("Unexpected view statement", expectedViewStatement1, view1.getViewStatement());
assertEquals("Unexpected view statement", expectedViewStatement2, view2.getViewStatement());
assertEquals("Unexpected view statement", expectedViewStatement3, view3.getViewStatement());
// verify isViewReferenced was set correctly
partitionCol1 = view1.getColumns().get(expectedParitionColIndex);
partitionCol2 = view2.getColumns().get(expectedParitionColIndex);
partitionCol3 = view3.getColumns().get(expectedParitionColIndex);
assertTrue("Partition column view referenced attribute should be true ", partitionCol1.isViewReferenced());
assertTrue("Partition column view referenced attribute should be true ", partitionCol2.isViewReferenced());
assertTrue("Partition column view referenced attribute should be true ", partitionCol3.isViewReferenced());
// verify viewConstant was set correctly
assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition1, partitionCol1.getViewConstant());
assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition2, partitionCol2.getViewConstant());
assertArrayEquals("Unexpected Partition column view constant attribute", expectedPartition3, partitionCol3.getViewConstant());
}
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class ImmutableTablePropertiesIT method testImmutableProperty.
@Test
public void testImmutableProperty() throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
String immutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
String mutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
Statement stmt = conn.createStatement();
// create table with immutable table property set to true
String ddl = "CREATE TABLE " + immutableDataTableFullName + " (a_string varchar not null, col1 integer" + " CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=true";
stmt.execute(ddl);
// create table with immutable table property set to false
ddl = "CREATE TABLE " + mutableDataTableFullName + " (a_string varchar not null, col1 integer" + " CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=false";
stmt.execute(ddl);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable immutableTable = phxConn.getTable(new PTableKey(null, immutableDataTableFullName));
assertTrue("IMMUTABLE_ROWS should be set to true", immutableTable.isImmutableRows());
PTable mutableTable = phxConn.getTable(new PTableKey(null, mutableDataTableFullName));
assertFalse("IMMUTABLE_ROWS should be set to false", mutableTable.isImmutableRows());
}
}
Aggregations