Java Code Examples for org.apache.phoenix.schema.PTable#getColumns()

The following examples show how to use org.apache.phoenix.schema.PTable#getColumns() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable)
        throws SQLException {
    if (!dynColumns.isEmpty()) {
        List<PColumn> allcolumns = new ArrayList<PColumn>();
        List<PColumn> existingColumns = theTable.getColumns();
        // Need to skip the salting column, as it's added in the makePTable call below
        allcolumns.addAll(theTable.getBucketNum() == null ? existingColumns : existingColumns.subList(1, existingColumns.size()));
        // Position still based on with the salting columns
        int position = existingColumns.size();
        PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable));
        for (ColumnDef dynColumn : dynColumns) {
            PName familyName = defaultFamilyName;
            PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName());
            String family = dynColumn.getColumnDefName().getFamilyName();
            if (family != null) {
                theTable.getColumnFamily(family); // Verifies that column family exists
                familyName = PNameFactory.newName(family);
            }
            allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(),
                    dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression()));
            position++;
        }
        theTable = PTableImpl.makePTable(theTable, allcolumns);
    }
    return theTable;
}
 
Example 2
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public ProjectedPTableWrapper createProjectedTable(RowProjector rowProjector) throws SQLException {
    assert(isSubselect());
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    List<Expression> sourceExpressions = new ArrayList<Expression>();
    ListMultimap<String, String> columnNameMap = ArrayListMultimap.<String, String>create();
    PTable table = tableRef.getTable();
    for (PColumn column : table.getColumns()) {
        String colName = getProjectedColumnName(null, tableRef.getTableAlias(), column.getName().getString());
        Expression sourceExpression = rowProjector.getColumnProjector(column.getPosition()).getExpression();
        PColumnImpl projectedColumn = new PColumnImpl(PNameFactory.newName(colName), PNameFactory.newName(TupleProjector.VALUE_COLUMN_FAMILY),
                sourceExpression.getDataType(), sourceExpression.getMaxLength(), sourceExpression.getScale(), sourceExpression.isNullable(),
                column.getPosition(), sourceExpression.getSortOrder(), column.getArraySize(), column.getViewConstant(), column.isViewReferenced(), column.getExpressionStr());
        projectedColumns.add(projectedColumn);
        sourceExpressions.add(sourceExpression);
    }
    PTable t = PTableImpl.makePTable(table.getTenantId(), PNameFactory.newName(PROJECTED_TABLE_SCHEMA), table.getName(), PTableType.JOIN,
                table.getIndexState(), table.getTimeStamp(), table.getSequenceNumber(), table.getPKName(),
                null, projectedColumns, table.getParentSchemaName(),
                table.getParentTableName(), table.getIndexes(), table.isImmutableRows(), Collections.<PName>emptyList(), null, null,
                table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(), table.getViewType(),
                table.getViewIndexId(), table.getIndexType());
    return new ProjectedPTableWrapper(t, columnNameMap, sourceExpressions);
}
 
Example 3
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static ColumnResolver getResolverForCompiledDerivedTable(PhoenixConnection connection, TableRef tableRef, RowProjector projector)
        throws SQLException {
    List<PColumn> projectedColumns = new ArrayList<PColumn>();
    List<Expression> sourceExpressions = new ArrayList<Expression>();
    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());
        projectedColumns.add(projectedColumn);
        sourceExpressions.add(sourceExpression);
    }
    PTable t = PTableImpl.makePTable(table, projectedColumns);
    return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols()));
}
 
Example 4
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, QueryConstants.EMPTY_COLUMN_BYTES.length, 0);
	return rowSize;
}
 
Example 5
Source File: FromCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
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(),
            column.getTimestamp());
        projectedColumns.add(projectedColumn);
    }
    PTable t = PTableImpl.builderWithColumns(table, projectedColumns)
            .build();
    return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols()));
}
 
Example 6
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Imperfect estimate of row size given a PTable
 * TODO: keep row count in stats table and use total size / row count instead
 * @param table
 * @return estimate of size in bytes of a row
 */
public static long estimateRowSize(PTable table) {
	int keyLength = estimateKeyLength(table);
	long rowSize = 0;
	for (PColumn column : table.getColumns()) {
		if (!SchemaUtil.isPKColumn(column)) {
            PDataType type = column.getDataType();
            Integer maxLength = column.getMaxLength();
            int valueLength = !type.isFixedWidth() ? VAR_KV_LENGTH_ESTIMATE : maxLength == null ? type.getByteSize() : maxLength;
			rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, column.getFamilyName().getBytes().length, column.getName().getBytes().length, valueLength);
		}
	}
	byte[] emptyKeyValueKV = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
	// Empty key value
	rowSize += KeyValue.getKeyValueDataStructureSize(keyLength, getEmptyColumnFamily(table).length, emptyKeyValueKV.length, 0);
	return rowSize;
}
 
Example 7
Source File: MetaDataEndpointImplIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void assertColumnNamesEqual(PTable table, String... cols) {
    List<String> actual = Lists.newArrayList();
    for (PColumn column : table.getColumns()) {
        actual.add(column.getName().getString().trim());
    }
    List<String> expected = Arrays.asList(cols);
    assertEquals(Joiner.on(", ").join(expected), Joiner.on(", ").join(actual));
}
 
Example 8
Source File: ViewMetadataIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void validateCols(PTable table) {
    final String prefix = table.getType() == PTableType.INDEX ? "0:" : "";
    Predicate<PColumn> predicate = new Predicate<PColumn>() {
        @Override
        public boolean apply(PColumn col) {
            return col.getName().getString().equals(prefix + "V3")
                    || col.getName().getString().equals(prefix + "V2");
        }
    };
    List<PColumn> colList = table.getColumns();
    Collection<PColumn> filteredCols = Collections2.filter(colList, predicate);
    assertEquals(1, filteredCols.size());
    assertEquals(prefix + "V3", filteredCols.iterator().next().getName().getString());
}
 
Example 9
Source File: ScanUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static boolean hasDynamicColumns(PTable table) {
    for (PColumn col : table.getColumns()) {
        if (col.isDynamic()) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: SortMergeJoinPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    if (table != null) {
        for (PColumn column : table.getColumns()) {
            if (!SchemaUtil.isPKColumn(column)) {
                builder.addField(column);
            }
        }
    }
    return builder.build();
}
 
Example 11
Source File: TupleProjector.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public TupleProjector(PTable projectedTable) throws SQLException {
    Preconditions.checkArgument(projectedTable.getType() == PTableType.PROJECTED);
	List<PColumn> columns = projectedTable.getColumns();
	this.expressions = new Expression[columns.size() - projectedTable.getPKColumns().size()];
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	int i = 0;
    for (PColumn column : columns) {
    	if (!SchemaUtil.isPKColumn(column)) {
    		builder.addField(column);
    		expressions[i++] = ((ProjectedColumn) column).getSourceColumnRef().newColumnExpression();
    	}
    }
    schema = builder.build();
    valueSet = ValueBitSet.newInstance(schema);
}
 
Example 12
Source File: CorrelatePlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
    KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
    if (table != null) {
        for (PColumn column : table.getColumns()) {
            if (!SchemaUtil.isPKColumn(column)) {
                builder.addField(column);
            }
        }
    }
    return builder.build();
}
 
Example 13
Source File: HashJoinInfo.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	if (table != null) {
	    for (PColumn column : table.getColumns()) {
	        if (!SchemaUtil.isPKColumn(column)) {
	            builder.addField(column);
	        }
	    }
	}
    return builder.build();
}
 
Example 14
Source File: JoinCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public PTable createProjectedTable(RowProjector rowProjector) throws SQLException {
    assert(isSubselect());
    TableRef tableRef = FromCompiler.getResolverForCompiledDerivedTable(statement.getConnection(), this.tableRef, rowProjector).getTables().get(0);
    List<ColumnRef> sourceColumns = new ArrayList<ColumnRef>();
    PTable table = tableRef.getTable();
    for (PColumn column : table.getColumns()) {
        sourceColumns.add(new ColumnRef(tableRef, column.getPosition()));
    }
    return TupleProjectionCompiler.createProjectedTable(tableRef, sourceColumns, false);
}
 
Example 15
Source File: FromCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
protected PTable addDynamicColumns(List<ColumnDef> dynColumns, PTable theTable)
        throws SQLException {
    if (!dynColumns.isEmpty()) {
        List<PColumn> existingColumns = theTable.getColumns();
        // Need to skip the salting column, as it's handled in the PTable builder call below
        List<PColumn> allcolumns = new ArrayList<>(
                theTable.getBucketNum() == null ? existingColumns :
                        existingColumns.subList(1, existingColumns.size()));
        // Position still based on with the salting columns
        int position = existingColumns.size();
        PName defaultFamilyName = PNameFactory.newName(SchemaUtil.getEmptyColumnFamily(theTable));
        for (ColumnDef dynColumn : dynColumns) {
            PName familyName = defaultFamilyName;
            PName name = PNameFactory.newName(dynColumn.getColumnDefName().getColumnName());
            String family = dynColumn.getColumnDefName().getFamilyName();
            if (family != null) {
                theTable.getColumnFamily(family); // Verifies that column family exists
                familyName = PNameFactory.newName(family);
            }
            allcolumns.add(new PColumnImpl(name, familyName, dynColumn.getDataType(), dynColumn.getMaxLength(),
                    dynColumn.getScale(), dynColumn.isNull(), position, dynColumn.getSortOrder(), dynColumn.getArraySize(), null, false, dynColumn.getExpression(), false, true, Bytes.toBytes(dynColumn.getColumnDefName().getColumnName()),
                HConstants.LATEST_TIMESTAMP));
            position++;
        }
        theTable = PTableImpl.builderWithColumns(theTable, allcolumns)
                .build();
    }
    return theTable;
}
 
Example 16
Source File: MetaDataEndpointImplIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void assertColumnNamesAndDefinitionsEqual(PTable table, Map<String, String> expected) {
    Map<String, String> actual = Maps.newHashMap();
    for (PColumn column : table.getColumns()) {
        actual.put(column.getName().getString().trim(), column.getDataType().getSqlTypeName());
    }
    assertEquals(expected, actual);
}
 
Example 17
Source File: HashJoinInfo.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static KeyValueSchema buildSchema(PTable table) {
	KeyValueSchemaBuilder builder = new KeyValueSchemaBuilder(0);
	if (table != null) {
	    for (PColumn column : table.getColumns()) {
	        if (!SchemaUtil.isPKColumn(column)) {
	            builder.addField(column);
	        }
	    }
	}
    return builder.build();
}
 
Example 18
Source File: ProjectedColumnExpression.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public ProjectedColumnExpression(PColumn column, PTable table, String displayName) {
	this(column, table.getColumns(), column.getPosition() - table.getPKColumns().size(), displayName);
}
 
Example 19
Source File: ProjectedColumnExpression.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public ProjectedColumnExpression(PColumn column, PTable table, String displayName) {
	this(column, table.getColumns(), column.getPosition() - table.getPKColumns().size(), displayName);
}
 
Example 20
Source File: AppendOnlySchemaIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private void testAddColumns(boolean sameClient) throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    try (Connection conn1 = DriverManager.getConnection(getUrl(), props);
            Connection conn2 = sameClient ? conn1 : DriverManager.getConnection(getUrl(), props)) {

        String metricTableName = generateUniqueName();
        String viewName = generateUniqueName();
        String metricIdSeqTableName = generateUniqueName();

        // create sequence for auto partition
        conn1.createStatement().execute("CREATE SEQUENCE " + metricIdSeqTableName + " CACHE 1");
        // create base table
        conn1.createStatement().execute("CREATE TABLE " + metricTableName + " (metricId INTEGER NOT NULL, metricVal1 DOUBLE, CONSTRAINT PK PRIMARY KEY(metricId))"
                + " APPEND_ONLY_SCHEMA = true, UPDATE_CACHE_FREQUENCY=1, AUTO_PARTITION_SEQ=" + metricIdSeqTableName);
        // create view
        String ddl =
                "CREATE VIEW IF NOT EXISTS "
                        + viewName + "( hostName varchar NOT NULL,"
                        + " CONSTRAINT HOSTNAME_PK PRIMARY KEY (hostName))"
                        + " AS SELECT * FROM " + metricTableName
                        + " UPDATE_CACHE_FREQUENCY=300000";
        conn1.createStatement().execute(ddl);
        
        conn1.createStatement().execute("UPSERT INTO " + viewName + "(hostName, metricVal1) VALUES('host1', 1.0)");
        conn1.commit();

        // execute ddl that creates that same view with an additional pk column and regular column
        // and also changes the order of the pk columns (which is not respected since we only 
        // allow appending columns)
        ddl =
                "CREATE VIEW IF NOT EXISTS "
                        + viewName + "( instanceName varchar, hostName varchar, metricVal2 double, metricVal1 double"
                        + " CONSTRAINT HOSTNAME_PK PRIMARY KEY (instancename, hostName))"
                        + " AS SELECT * FROM " + metricTableName
                        + " UPDATE_CACHE_FREQUENCY=300000";
        conn2.createStatement().execute(ddl);

        conn2.createStatement().execute(
            "UPSERT INTO " + viewName + "(hostName, instanceName, metricVal1, metricval2) VALUES('host2', 'instance2', 21.0, 22.0)");
        conn2.commit();
        
        conn1.createStatement().execute("UPSERT INTO " + viewName + "(hostName, metricVal1) VALUES('host3', 3.0)");
        conn1.commit();
        
        // verify data exists
        ResultSet rs = conn2.createStatement().executeQuery("SELECT * from " + viewName);
        
        // verify the two columns were added correctly
        PTable table =
                conn2.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, viewName));
        List<PColumn> pkColumns = table.getPKColumns();
        assertEquals(3,table.getPKColumns().size());
        // even though the second create view statement changed the order of the pk, the original order is maintained
        PColumn metricId = pkColumns.get(0);
        assertEquals("METRICID", metricId.getName().getString());
        assertFalse(metricId.isNullable());
        PColumn hostName = pkColumns.get(1);
        assertEquals("HOSTNAME", hostName.getName().getString());
        // hostname name is not nullable even though the second create statement changed it to nullable
        // since we only allow appending columns
        assertFalse(hostName.isNullable());
        PColumn instanceName = pkColumns.get(2);
        assertEquals("INSTANCENAME", instanceName.getName().getString());
        assertTrue(instanceName.isNullable());
        List<PColumn> columns = table.getColumns();
        assertEquals("METRICID", columns.get(0).getName().getString());
        assertEquals("METRICVAL1", columns.get(1).getName().getString());
        assertEquals("HOSTNAME", columns.get(2).getName().getString());
        assertEquals("INSTANCENAME", columns.get(3).getName().getString());
        assertEquals("METRICVAL2", columns.get(4).getName().getString());
        
        // verify the data
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(1.0, rs.getDouble(2), 1e-6);
        assertEquals("host1", rs.getString(3));
        assertEquals(null, rs.getString(4));
        assertEquals(0.0, rs.getDouble(5), 1e-6);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(21.0, rs.getDouble(2), 1e-6);
        assertEquals("host2", rs.getString(3));
        assertEquals("instance2", rs.getString(4));
        assertEquals(22.0, rs.getDouble(5), 1e-6);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(3.0, rs.getDouble(2), 1e-6);
        assertEquals("host3", rs.getString(3));
        assertEquals(null, rs.getString(4));
        assertEquals(0.0, rs.getDouble(5), 1e-6);
        assertFalse(rs.next());
    }
}