Search in sources :

Example 71 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class ProjectionCompiler method projectTableColumnFamily.

private static void projectTableColumnFamily(StatementContext context, String cfName, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns) throws SQLException {
    PTable table = tableRef.getTable();
    PColumnFamily pfamily = table.getColumnFamily(cfName);
    for (PColumn column : pfamily.getColumns()) {
        ColumnRef ref = new ColumnRef(tableRef, column.getPosition());
        if (resolveColumn) {
            ref = context.getResolver().resolveColumn(table.getTableName().getString(), cfName, column.getName().getString());
        }
        Expression expression = ref.newColumnExpression();
        projectedExpressions.add(expression);
        String colName = column.getName().toString();
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) BaseTerminalExpression(org.apache.phoenix.expression.BaseTerminalExpression) Expression(org.apache.phoenix.expression.Expression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ColumnRef(org.apache.phoenix.schema.ColumnRef) LocalIndexDataColumnRef(org.apache.phoenix.schema.LocalIndexDataColumnRef) PColumnFamily(org.apache.phoenix.schema.PColumnFamily) PTable(org.apache.phoenix.schema.PTable)

Example 72 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class ProjectionCompiler method projectAllTableColumns.

private static void projectAllTableColumns(StatementContext context, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns, List<? extends PDatum> targetColumns) throws SQLException {
    ColumnResolver resolver = context.getResolver();
    PTable table = tableRef.getTable();
    int projectedOffset = projectedExpressions.size();
    int posOffset = table.getBucketNum() == null ? 0 : 1;
    int minPKOffset = getMinPKOffset(table, context.getConnection().getTenantId());
    for (int i = posOffset, j = posOffset; i < table.getColumns().size(); i++) {
        PColumn column = table.getColumns().get(i);
        // Skip tenant ID column (which may not be the first column, but is the first PK column)
        if (SchemaUtil.isPKColumn(column) && j++ < minPKOffset) {
            posOffset++;
            continue;
        }
        ColumnRef ref = new ColumnRef(tableRef, i);
        String colName = ref.getColumn().getName().getString();
        String tableAlias = tableRef.getTableAlias();
        if (resolveColumn) {
            try {
                if (tableAlias != null) {
                    ref = resolver.resolveColumn(null, tableAlias, colName);
                } else {
                    String schemaName = table.getSchemaName().getString();
                    ref = resolver.resolveColumn(schemaName.length() == 0 ? null : schemaName, table.getTableName().getString(), colName);
                }
            } catch (AmbiguousColumnException e) {
                if (column.getFamilyName() != null) {
                    ref = resolver.resolveColumn(tableAlias != null ? tableAlias : table.getTableName().getString(), column.getFamilyName().getString(), colName);
                } else {
                    throw e;
                }
            }
        }
        Expression expression = ref.newColumnExpression();
        expression = coerceIfNecessary(i - posOffset + projectedOffset, targetColumns, expression);
        ImmutableBytesWritable ptr = context.getTempPtr();
        if (IndexUtil.getViewConstantValue(column, ptr)) {
            expression = LiteralExpression.newConstant(column.getDataType().toObject(ptr), expression.getDataType());
        }
        projectedExpressions.add(expression);
        boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
        projectedColumns.add(new ExpressionProjector(colName, tableRef.getTableAlias() == null ? table.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive));
    }
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) BaseTerminalExpression(org.apache.phoenix.expression.BaseTerminalExpression) Expression(org.apache.phoenix.expression.Expression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) ProjectedColumnExpression(org.apache.phoenix.expression.ProjectedColumnExpression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ColumnRef(org.apache.phoenix.schema.ColumnRef) LocalIndexDataColumnRef(org.apache.phoenix.schema.LocalIndexDataColumnRef) AmbiguousColumnException(org.apache.phoenix.schema.AmbiguousColumnException) PTable(org.apache.phoenix.schema.PTable)

Example 73 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class UpsertCompiler method prependTenantAndViewConstants.

private static SelectStatement prependTenantAndViewConstants(PTable table, SelectStatement select, String tenantId, Set<PColumn> addViewColumns, boolean useServerTimestamp) {
    if ((!table.isMultiTenant() || tenantId == null) && table.getViewIndexId() == null && addViewColumns.isEmpty() && !useServerTimestamp) {
        return select;
    }
    List<AliasedNode> selectNodes = newArrayListWithCapacity(select.getSelect().size() + 1 + addViewColumns.size());
    if (table.getViewIndexId() != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(table.getViewIndexId())));
    }
    if (table.isMultiTenant() && tenantId != null) {
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(tenantId)));
    }
    selectNodes.addAll(select.getSelect());
    for (PColumn column : addViewColumns) {
        byte[] byteValue = column.getViewConstant();
        Object value = column.getDataType().toObject(byteValue, 0, byteValue.length - 1);
        selectNodes.add(new AliasedNode(null, new LiteralParseNode(value)));
    }
    if (useServerTimestamp) {
        PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
        selectNodes.add(new AliasedNode(null, getNodeForRowTimestampColumn(rowTimestampCol)));
    }
    return SelectStatement.create(select, selectNodes);
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) AliasedNode(org.apache.phoenix.parse.AliasedNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode)

Example 74 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class ColumnParseNode method toSQL.

@Override
public void toSQL(ColumnResolver resolver, StringBuilder buf) {
    // If resolver is not null, then resolve to get fully qualified name
    String tableName = null;
    if (resolver == null) {
        if (this.tableName != null) {
            tableName = this.tableName.getTableName();
        }
    } else {
        try {
            ColumnRef ref = resolver.resolveColumn(this.getSchemaName(), this.getTableName(), this.getName());
            PColumn column = ref.getColumn();
            if (!SchemaUtil.isPKColumn(column)) {
                PTable table = ref.getTable();
                String defaultFamilyName = table.getDefaultFamilyName() == null ? QueryConstants.DEFAULT_COLUMN_FAMILY : table.getDefaultFamilyName().getString();
                // Translate to the data table column name
                String dataFamilyName = column.getFamilyName().getString();
                tableName = defaultFamilyName.equals(dataFamilyName) ? null : dataFamilyName;
            }
        } catch (SQLException e) {
            // Already resolved, so not possible
            throw new RuntimeException(e);
        }
    }
    if (tableName != null) {
        if (isTableNameCaseSensitive()) {
            buf.append('"');
            buf.append(tableName);
            buf.append('"');
        } else {
            buf.append(tableName);
        }
        buf.append('.');
    }
    toSQL(buf);
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) SQLException(java.sql.SQLException) ColumnRef(org.apache.phoenix.schema.ColumnRef) PTable(org.apache.phoenix.schema.PTable)

Example 75 with PColumn

use of org.apache.phoenix.schema.PColumn in project phoenix by apache.

the class HavingCompilerTest method testOrAggFuncInHaving.

@Test
public void testOrAggFuncInHaving() throws SQLException {
    String query = "select count(1) from atable group by a_string having count(1) >= 1 or a_string = 'foo'";
    List<Object> binds = Collections.emptyList();
    Expressions expressions = compileStatement(query, binds);
    PColumn aCol = ATABLE.getColumnForColumnName("A_STRING");
    Expression h = or(constantComparison(CompareOp.GREATER_OR_EQUAL, new CountAggregateFunction(), 1L), constantComparison(CompareOp.EQUAL, new // a_string comes from group by key in this case
    RowKeyColumnExpression(// a_string comes from group by key in this case
    aCol, new RowKeyValueAccessor(Arrays.<PColumn>asList(aCol), 0)), "foo"));
    assertNull(expressions.whereClause);
    assertEquals(h, expressions.havingClause);
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) CountAggregateFunction(org.apache.phoenix.expression.function.CountAggregateFunction) RoundDateExpression(org.apache.phoenix.expression.function.RoundDateExpression) Expression(org.apache.phoenix.expression.Expression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowKeyValueAccessor(org.apache.phoenix.schema.RowKeyValueAccessor) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

PColumn (org.apache.phoenix.schema.PColumn)101 PTable (org.apache.phoenix.schema.PTable)59 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)26 Expression (org.apache.phoenix.expression.Expression)21 TableRef (org.apache.phoenix.schema.TableRef)20 ArrayList (java.util.ArrayList)19 PName (org.apache.phoenix.schema.PName)18 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)17 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)17 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)17 ColumnRef (org.apache.phoenix.schema.ColumnRef)17 Hint (org.apache.phoenix.parse.HintNode.Hint)14 PTableKey (org.apache.phoenix.schema.PTableKey)14 ColumnNotFoundException (org.apache.phoenix.schema.ColumnNotFoundException)13 PColumnFamily (org.apache.phoenix.schema.PColumnFamily)13 PSmallint (org.apache.phoenix.schema.types.PSmallint)13 SQLException (java.sql.SQLException)12 ProjectedColumnExpression (org.apache.phoenix.expression.ProjectedColumnExpression)12 PColumnImpl (org.apache.phoenix.schema.PColumnImpl)12 Map (java.util.Map)11