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());
}
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class FromCompiler method getResolverForCompiledDerivedTable.
public static ColumnResolver getResolverForCompiledDerivedTable(PhoenixConnection connection, TableRef tableRef, RowProjector projector) throws SQLException {
List<PColumn> projectedColumns = new ArrayList<PColumn>();
PTable table = tableRef.getTable();
for (PColumn column : table.getColumns()) {
Expression sourceExpression = projector.getColumnProjector(column.getPosition()).getExpression();
PColumnImpl projectedColumn = new PColumnImpl(column.getName(), column.getFamilyName(), sourceExpression.getDataType(), sourceExpression.getMaxLength(), sourceExpression.getScale(), sourceExpression.isNullable(), column.getPosition(), sourceExpression.getSortOrder(), column.getArraySize(), column.getViewConstant(), column.isViewReferenced(), column.getExpressionStr(), column.isRowTimestamp(), column.isDynamic(), column.getColumnQualifierBytes());
projectedColumns.add(projectedColumn);
}
PTable t = PTableImpl.makePTable(table, projectedColumns);
return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols()));
}
use of org.apache.phoenix.schema.PTable in project phoenix by apache.
the class ProjectionCompiler method compile.
/**
* Builds the projection for the scan
* @param context query context kept between compilation of different query clauses
* @param statement TODO
* @param groupBy compiled GROUP BY clause
* @param targetColumns list of columns, parallel to aliasedNodes, that are being set for an
* UPSERT SELECT statement. Used to coerce expression types to the expected target type.
* @return projector used to access row values during scan
* @throws SQLException
*/
public static RowProjector compile(StatementContext context, SelectStatement statement, GroupBy groupBy, List<? extends PDatum> targetColumns, Expression where) throws SQLException {
List<KeyValueColumnExpression> arrayKVRefs = new ArrayList<KeyValueColumnExpression>();
List<ProjectedColumnExpression> arrayProjectedColumnRefs = new ArrayList<ProjectedColumnExpression>();
List<Expression> arrayKVFuncs = new ArrayList<Expression>();
List<Expression> arrayOldFuncs = new ArrayList<Expression>();
Map<Expression, Integer> arrayExpressionCounts = new HashMap<>();
List<AliasedNode> aliasedNodes = statement.getSelect();
// Setup projected columns in Scan
SelectClauseVisitor selectVisitor = new SelectClauseVisitor(context, groupBy, arrayKVRefs, arrayKVFuncs, arrayExpressionCounts, arrayProjectedColumnRefs, arrayOldFuncs, statement);
List<ExpressionProjector> projectedColumns = new ArrayList<ExpressionProjector>();
ColumnResolver resolver = context.getResolver();
TableRef tableRef = context.getCurrentTable();
PTable table = tableRef.getTable();
boolean resolveColumn = !tableRef.equals(resolver.getTables().get(0));
boolean isWildcard = false;
Scan scan = context.getScan();
int index = 0;
List<Expression> projectedExpressions = Lists.newArrayListWithExpectedSize(aliasedNodes.size());
List<byte[]> projectedFamilies = Lists.newArrayListWithExpectedSize(aliasedNodes.size());
for (AliasedNode aliasedNode : aliasedNodes) {
ParseNode node = aliasedNode.getNode();
// TODO: visitor?
if (node instanceof WildcardParseNode) {
if (statement.isAggregate()) {
ExpressionCompiler.throwNonAggExpressionInAggException(node.toString());
}
if (tableRef == TableRef.EMPTY_TABLE_REF) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.NO_TABLE_SPECIFIED_FOR_WILDCARD_SELECT).build().buildException();
}
isWildcard = true;
if (tableRef.getTable().getType() == PTableType.INDEX && ((WildcardParseNode) node).isRewrite()) {
projectAllIndexColumns(context, tableRef, resolveColumn, projectedExpressions, projectedColumns, targetColumns);
} else {
projectAllTableColumns(context, tableRef, resolveColumn, projectedExpressions, projectedColumns, targetColumns);
}
} else if (node instanceof TableWildcardParseNode) {
TableName tName = ((TableWildcardParseNode) node).getTableName();
TableRef tRef = resolver.resolveTable(tName.getSchemaName(), tName.getTableName());
if (tRef.equals(tableRef)) {
isWildcard = true;
}
if (tRef.getTable().getType() == PTableType.INDEX && ((TableWildcardParseNode) node).isRewrite()) {
projectAllIndexColumns(context, tRef, true, projectedExpressions, projectedColumns, targetColumns);
} else {
projectAllTableColumns(context, tRef, true, projectedExpressions, projectedColumns, targetColumns);
}
} else if (node instanceof FamilyWildcardParseNode) {
if (tableRef == TableRef.EMPTY_TABLE_REF) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.NO_TABLE_SPECIFIED_FOR_WILDCARD_SELECT).build().buildException();
}
// Project everything for SELECT cf.*
String cfName = ((FamilyWildcardParseNode) node).getName();
// Delay projecting to scan, as when any other column in the column family gets
// added to the scan, it overwrites that we want to project the entire column
// family. Instead, we do the projection at the end.
// TODO: consider having a ScanUtil.addColumn and ScanUtil.addFamily to work
// around this, as this code depends on this function being the last place where
// columns are projected (which is currently true, but could change).
projectedFamilies.add(Bytes.toBytes(cfName));
if (tableRef.getTable().getType() == PTableType.INDEX && ((FamilyWildcardParseNode) node).isRewrite()) {
projectIndexColumnFamily(context, cfName, tableRef, resolveColumn, projectedExpressions, projectedColumns);
} else {
projectTableColumnFamily(context, cfName, tableRef, resolveColumn, projectedExpressions, projectedColumns);
}
} else {
Expression expression = node.accept(selectVisitor);
projectedExpressions.add(expression);
expression = coerceIfNecessary(index, targetColumns, expression);
if (node instanceof BindParseNode) {
context.getBindManager().addParamMetaData((BindParseNode) node, expression);
}
if (!node.isStateless()) {
if (!selectVisitor.isAggregate() && statement.isAggregate()) {
ExpressionCompiler.throwNonAggExpressionInAggException(expression.toString());
}
}
String columnAlias = aliasedNode.getAlias() != null ? aliasedNode.getAlias() : SchemaUtil.normalizeIdentifier(aliasedNode.getNode().getAlias());
boolean isCaseSensitive = aliasedNode.getAlias() != null ? aliasedNode.isCaseSensitve() : (columnAlias != null ? SchemaUtil.isCaseSensitive(aliasedNode.getNode().getAlias()) : selectVisitor.isCaseSensitive);
String name = columnAlias == null ? expression.toString() : columnAlias;
projectedColumns.add(new ExpressionProjector(name, tableRef.getTableAlias() == null ? (table.getName() == null ? "" : table.getName().getString()) : tableRef.getTableAlias(), expression, isCaseSensitive));
}
selectVisitor.reset();
index++;
}
for (int i = arrayProjectedColumnRefs.size() - 1; i >= 0; i--) {
Expression expression = arrayProjectedColumnRefs.get(i);
Integer count = arrayExpressionCounts.get(expression);
if (count != 0) {
arrayKVRefs.remove(i);
arrayKVFuncs.remove(i);
arrayOldFuncs.remove(i);
}
}
if (arrayKVFuncs.size() > 0 && arrayKVRefs.size() > 0) {
serailizeArrayIndexInformationAndSetInScan(context, arrayKVFuncs, arrayKVRefs);
KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
for (Expression expression : arrayKVRefs) {
builder.addField(expression);
}
KeyValueSchema kvSchema = builder.build();
ValueBitSet arrayIndexesBitSet = ValueBitSet.newInstance(kvSchema);
builder = new KeyValueSchemaBuilder(0);
for (Expression expression : arrayKVFuncs) {
builder.addField(expression);
}
KeyValueSchema arrayIndexesSchema = builder.build();
Map<Expression, Expression> replacementMap = new HashMap<>();
for (int i = 0; i < arrayOldFuncs.size(); i++) {
Expression function = arrayKVFuncs.get(i);
replacementMap.put(arrayOldFuncs.get(i), new ArrayIndexExpression(i, function.getDataType(), arrayIndexesBitSet, arrayIndexesSchema));
}
ReplaceArrayFunctionExpressionVisitor visitor = new ReplaceArrayFunctionExpressionVisitor(replacementMap);
for (int i = 0; i < projectedColumns.size(); i++) {
ExpressionProjector projector = projectedColumns.get(i);
projectedColumns.set(i, new ExpressionProjector(projector.getName(), tableRef.getTableAlias() == null ? (table.getName() == null ? "" : table.getName().getString()) : tableRef.getTableAlias(), projector.getExpression().accept(visitor), projector.isCaseSensitive()));
}
}
// TODO make estimatedByteSize more accurate by counting the joined columns.
int estimatedKeySize = table.getRowKeySchema().getEstimatedValueLength();
int estimatedByteSize = 0;
for (Map.Entry<byte[], NavigableSet<byte[]>> entry : scan.getFamilyMap().entrySet()) {
PColumnFamily family = table.getColumnFamily(entry.getKey());
if (entry.getValue() == null) {
for (PColumn column : family.getColumns()) {
Integer maxLength = column.getMaxLength();
int byteSize = column.getDataType().isFixedWidth() ? maxLength == null ? column.getDataType().getByteSize() : maxLength : RowKeySchema.ESTIMATED_VARIABLE_LENGTH_SIZE;
estimatedByteSize += SizedUtil.KEY_VALUE_SIZE + estimatedKeySize + byteSize;
}
} else {
for (byte[] cq : entry.getValue()) {
//if (!Bytes.equals(cq, ByteUtil.EMPTY_BYTE_ARRAY) || cq.length > 0) {
PColumn column = family.getPColumnForColumnQualifier(cq);
Integer maxLength = column.getMaxLength();
int byteSize = column.getDataType().isFixedWidth() ? maxLength == null ? column.getDataType().getByteSize() : maxLength : RowKeySchema.ESTIMATED_VARIABLE_LENGTH_SIZE;
estimatedByteSize += SizedUtil.KEY_VALUE_SIZE + estimatedKeySize + byteSize;
}
//}
}
}
boolean isProjectEmptyKeyValue = false;
if (isWildcard) {
projectAllColumnFamilies(table, scan);
} else {
isProjectEmptyKeyValue = where == null || LiteralExpression.isTrue(where) || where.requiresFinalEvaluation();
for (byte[] family : projectedFamilies) {
projectColumnFamily(table, scan, family);
}
}
return new RowProjector(projectedColumns, estimatedByteSize, isProjectEmptyKeyValue, resolver.hasUDFs(), isWildcard);
}
Aggregations