Search in sources :

Example 11 with TableName

use of org.apache.phoenix.parse.TableName in project phoenix by apache.

the class SubqueryRewriter method getJoinConditionNode.

private ParseNode getJoinConditionNode(ParseNode lhs, List<AliasedNode> rhs, String rhsTableAlias) throws SQLException {
    List<ParseNode> lhsNodes;
    if (lhs instanceof RowValueConstructorParseNode) {
        lhsNodes = ((RowValueConstructorParseNode) lhs).getChildren();
    } else {
        lhsNodes = Collections.singletonList(lhs);
    }
    if (lhsNodes.size() != (rhs.size() - 1))
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.SUBQUERY_RETURNS_DIFFERENT_NUMBER_OF_FIELDS).build().buildException();
    int count = lhsNodes.size();
    TableName rhsTableName = NODE_FACTORY.table(null, rhsTableAlias);
    List<ParseNode> equalNodes = Lists.newArrayListWithExpectedSize(count);
    for (int i = 0; i < count; i++) {
        ParseNode rhsNode = NODE_FACTORY.column(rhsTableName, rhs.get(i + 1).getAlias(), null);
        equalNodes.add(NODE_FACTORY.equal(lhsNodes.get(i), rhsNode));
    }
    return count == 1 ? equalNodes.get(0) : NODE_FACTORY.and(equalNodes);
}
Also used : TableName(org.apache.phoenix.parse.TableName) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) ExistsParseNode(org.apache.phoenix.parse.ExistsParseNode) SubqueryParseNode(org.apache.phoenix.parse.SubqueryParseNode) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) CompoundParseNode(org.apache.phoenix.parse.CompoundParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) InParseNode(org.apache.phoenix.parse.InParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) RowValueConstructorParseNode(org.apache.phoenix.parse.RowValueConstructorParseNode) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 12 with TableName

use of org.apache.phoenix.parse.TableName in project phoenix by apache.

the class FromCompiler method getResolverForCreation.

public static ColumnResolver getResolverForCreation(final CreateTableStatement statement, final PhoenixConnection connection) throws SQLException {
    TableName baseTable = statement.getBaseTableName();
    String schemaName;
    if (baseTable == null) {
        if (SchemaUtil.isSchemaCheckRequired(statement.getTableType(), connection.getQueryServices().getProps())) {
            schemaName = statement.getTableName().getSchemaName();
            if (schemaName != null) {
                new SchemaResolver(connection, statement.getTableName().getSchemaName(), true);
            } else if (connection.getSchema() != null) {
                // To ensure schema set through properties or connection string exists before creating table
                new SchemaResolver(connection, connection.getSchema(), true);
            }
        }
        return EMPTY_TABLE_RESOLVER;
    }
    NamedTableNode tableNode = NamedTableNode.create(null, baseTable, Collections.<ColumnDef>emptyList());
    // Always use non-tenant-specific connection here
    try {
        // We need to always get the latest meta data for the parent table of a create view call to ensure that
        // that we're copying the current table meta data as of when the view is created. Once we no longer
        // copy the parent meta data, but store only the local diffs (PHOENIX-3534), we will no longer need
        // to do this.
        SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, tableNode, true, true);
        return visitor;
    } catch (TableNotFoundException e) {
        // A tenant-specific connection may not create a mapped VIEW.
        if (connection.getTenantId() == null && statement.getTableType() == PTableType.VIEW) {
            ConnectionQueryServices services = connection.getQueryServices();
            byte[] fullTableName = SchemaUtil.getPhysicalName(SchemaUtil.getTableNameAsBytes(baseTable.getSchemaName(), baseTable.getTableName()), connection.getQueryServices().getProps()).getName();
            HTableInterface htable = null;
            try {
                htable = services.getTable(fullTableName);
            } catch (UnsupportedOperationException ignore) {
                // For Connectionless
                throw e;
            } finally {
                if (htable != null)
                    Closeables.closeQuietly(htable);
            }
            tableNode = NamedTableNode.create(null, baseTable, statement.getColumnDefs());
            return new SingleTableColumnResolver(connection, tableNode, e.getTimeStamp(), new HashMap<String, UDFParseNode>(1), false);
        }
        throw e;
    }
}
Also used : TableName(org.apache.phoenix.parse.TableName) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) HashMap(java.util.HashMap) NamedTableNode(org.apache.phoenix.parse.NamedTableNode) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices)

Example 13 with TableName

use of org.apache.phoenix.parse.TableName in project phoenix by apache.

the class IndexStatementRewriter method visit.

@Override
public ParseNode visit(ColumnParseNode node) throws SQLException {
    ColumnRef dataColRef = getResolver().resolveColumn(node.getSchemaName(), node.getTableName(), node.getName());
    PColumn dataCol = dataColRef.getColumn();
    TableRef dataTableRef = dataColRef.getTableRef();
    // view constants if based on an UPDATABLE view
    if (dataCol.getViewConstant() != null) {
        byte[] viewConstant = dataCol.getViewConstant();
        // Ignore last byte, as it's only there so we can have a way to differentiate null
        // from the absence of a value.
        ptr.set(viewConstant, 0, viewConstant.length - 1);
        Object literal = dataCol.getDataType().toObject(ptr);
        return new LiteralParseNode(literal, dataCol.getDataType());
    }
    TableName tName = getReplacedTableName(dataTableRef);
    if (multiTableRewriteMap != null && tName == null)
        return node;
    String indexColName = IndexUtil.getIndexColumnName(dataCol);
    ParseNode indexColNode = new ColumnParseNode(tName, '"' + indexColName + '"', node.getAlias());
    PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
    PDataType dataColType = dataColRef.getColumn().getDataType();
    // TODO: test case for this
    if (!isTopLevel() && indexColType != dataColType) {
        indexColNode = FACTORY.cast(indexColNode, dataColType, null, null);
    }
    return indexColNode;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) TableName(org.apache.phoenix.parse.TableName) PDataType(org.apache.phoenix.schema.types.PDataType) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) FamilyWildcardParseNode(org.apache.phoenix.parse.FamilyWildcardParseNode) ColumnRef(org.apache.phoenix.schema.ColumnRef) TableRef(org.apache.phoenix.schema.TableRef) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode)

Example 14 with TableName

use of org.apache.phoenix.parse.TableName in project phoenix by apache.

the class JoinCompiler method optimize.

public static SelectStatement optimize(PhoenixStatement statement, SelectStatement select, final ColumnResolver resolver) throws SQLException {
    TableRef groupByTableRef = null;
    TableRef orderByTableRef = null;
    if (select.getGroupBy() != null && !select.getGroupBy().isEmpty()) {
        ColumnRefParseNodeVisitor groupByVisitor = new ColumnRefParseNodeVisitor(resolver, statement.getConnection());
        for (ParseNode node : select.getGroupBy()) {
            node.accept(groupByVisitor);
        }
        Set<TableRef> set = groupByVisitor.getTableRefSet();
        if (set.size() == 1) {
            groupByTableRef = set.iterator().next();
        }
    } else if (select.getOrderBy() != null && !select.getOrderBy().isEmpty()) {
        ColumnRefParseNodeVisitor orderByVisitor = new ColumnRefParseNodeVisitor(resolver, statement.getConnection());
        for (OrderByNode node : select.getOrderBy()) {
            node.getNode().accept(orderByVisitor);
        }
        Set<TableRef> set = orderByVisitor.getTableRefSet();
        if (set.size() == 1) {
            orderByTableRef = set.iterator().next();
        }
    }
    JoinTable join = compile(statement, select, resolver);
    if (groupByTableRef != null || orderByTableRef != null) {
        QueryCompiler compiler = new QueryCompiler(statement, select, resolver, false);
        List<Object> binds = statement.getParameters();
        StatementContext ctx = new StatementContext(statement, resolver, new Scan(), new SequenceManager(statement));
        QueryPlan plan = compiler.compileJoinQuery(ctx, binds, join, false, false, null);
        TableRef table = plan.getTableRef();
        if (groupByTableRef != null && !groupByTableRef.equals(table)) {
            groupByTableRef = null;
        }
        if (orderByTableRef != null && !orderByTableRef.equals(table)) {
            orderByTableRef = null;
        }
    }
    final Map<TableRef, TableRef> replacement = new HashMap<TableRef, TableRef>();
    for (Table table : join.getTables()) {
        if (table.isSubselect())
            continue;
        TableRef tableRef = table.getTableRef();
        List<ParseNode> groupBy = tableRef.equals(groupByTableRef) ? select.getGroupBy() : null;
        List<OrderByNode> orderBy = tableRef.equals(orderByTableRef) ? select.getOrderBy() : null;
        SelectStatement stmt = getSubqueryForOptimizedPlan(select.getHint(), table.getDynamicColumns(), tableRef, join.getColumnRefs(), table.getPreFiltersCombined(), groupBy, orderBy, table.isWildCardSelect(), select.hasSequence(), select.getUdfParseNodes());
        QueryPlan plan = statement.getConnection().getQueryServices().getOptimizer().optimize(statement, stmt);
        if (!plan.getTableRef().equals(tableRef)) {
            replacement.put(tableRef, plan.getTableRef());
        }
    }
    if (replacement.isEmpty())
        return select;
    TableNode from = select.getFrom();
    TableNode newFrom = from.accept(new TableNodeVisitor<TableNode>() {

        private TableRef resolveTable(String alias, TableName name) throws SQLException {
            if (alias != null)
                return resolver.resolveTable(null, alias);
            return resolver.resolveTable(name.getSchemaName(), name.getTableName());
        }

        private TableName getReplacedTableName(TableRef tableRef) {
            String schemaName = tableRef.getTable().getSchemaName().getString();
            return TableName.create(schemaName.length() == 0 ? null : schemaName, tableRef.getTable().getTableName().getString());
        }

        @Override
        public TableNode visit(BindTableNode boundTableNode) throws SQLException {
            TableRef tableRef = resolveTable(boundTableNode.getAlias(), boundTableNode.getName());
            TableRef replaceRef = replacement.get(tableRef);
            if (replaceRef == null)
                return boundTableNode;
            String alias = boundTableNode.getAlias();
            return NODE_FACTORY.bindTable(alias == null ? null : '"' + alias + '"', getReplacedTableName(replaceRef));
        }

        @Override
        public TableNode visit(JoinTableNode joinNode) throws SQLException {
            TableNode lhs = joinNode.getLHS();
            TableNode rhs = joinNode.getRHS();
            TableNode lhsReplace = lhs.accept(this);
            TableNode rhsReplace = rhs.accept(this);
            if (lhs == lhsReplace && rhs == rhsReplace)
                return joinNode;
            return NODE_FACTORY.join(joinNode.getType(), lhsReplace, rhsReplace, joinNode.getOnNode(), joinNode.isSingleValueOnly());
        }

        @Override
        public TableNode visit(NamedTableNode namedTableNode) throws SQLException {
            TableRef tableRef = resolveTable(namedTableNode.getAlias(), namedTableNode.getName());
            TableRef replaceRef = replacement.get(tableRef);
            if (replaceRef == null)
                return namedTableNode;
            String alias = namedTableNode.getAlias();
            return NODE_FACTORY.namedTable(alias == null ? null : '"' + alias + '"', getReplacedTableName(replaceRef), namedTableNode.getDynamicColumns());
        }

        @Override
        public TableNode visit(DerivedTableNode subselectNode) throws SQLException {
            return subselectNode;
        }
    });
    SelectStatement indexSelect = IndexStatementRewriter.translate(NODE_FACTORY.select(select, newFrom), resolver, replacement);
    for (TableRef indexTableRef : replacement.values()) {
        // replace expressions with corresponding matching columns for functional indexes
        indexSelect = ParseNodeRewriter.rewrite(indexSelect, new IndexExpressionParseNodeRewriter(indexTableRef.getTable(), indexTableRef.getTableAlias(), statement.getConnection(), indexSelect.getUdfParseNodes()));
    }
    return indexSelect;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) OrderByNode(org.apache.phoenix.parse.OrderByNode) SelectStatement(org.apache.phoenix.parse.SelectStatement) BindTableNode(org.apache.phoenix.parse.BindTableNode) UDFParseNode(org.apache.phoenix.parse.UDFParseNode) ComparisonParseNode(org.apache.phoenix.parse.ComparisonParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) AndParseNode(org.apache.phoenix.parse.AndParseNode) WildcardParseNode(org.apache.phoenix.parse.WildcardParseNode) TableWildcardParseNode(org.apache.phoenix.parse.TableWildcardParseNode) EqualParseNode(org.apache.phoenix.parse.EqualParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) PTable(org.apache.phoenix.schema.PTable) JoinTableNode(org.apache.phoenix.parse.JoinTableNode) TableName(org.apache.phoenix.parse.TableName) DerivedTableNode(org.apache.phoenix.parse.DerivedTableNode) TableNode(org.apache.phoenix.parse.TableNode) JoinTableNode(org.apache.phoenix.parse.JoinTableNode) NamedTableNode(org.apache.phoenix.parse.NamedTableNode) BindTableNode(org.apache.phoenix.parse.BindTableNode) DerivedTableNode(org.apache.phoenix.parse.DerivedTableNode) NamedTableNode(org.apache.phoenix.parse.NamedTableNode) Scan(org.apache.hadoop.hbase.client.Scan) IndexExpressionParseNodeRewriter(org.apache.phoenix.parse.IndexExpressionParseNodeRewriter) TableRef(org.apache.phoenix.schema.TableRef)

Example 15 with TableName

use of org.apache.phoenix.parse.TableName in project phoenix by apache.

the class CreateSequenceCompiler method evalExpression.

private long evalExpression(CreateSequenceStatement sequence, StatementContext context, Expression expression, SQLExceptionCode code) throws SQLException {
    ImmutableBytesWritable ptr = context.getTempPtr();
    expression.evaluate(null, ptr);
    if (ptr.getLength() == 0 || !expression.getDataType().isCoercibleTo(PLong.INSTANCE)) {
        TableName sequenceName = sequence.getSequenceName();
        throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), code);
    }
    return (Long) PLong.INSTANCE.toObject(ptr, expression.getDataType());
}
Also used : TableName(org.apache.phoenix.parse.TableName) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PLong(org.apache.phoenix.schema.types.PLong)

Aggregations

TableName (org.apache.phoenix.parse.TableName)17 ParseNode (org.apache.phoenix.parse.ParseNode)11 ColumnParseNode (org.apache.phoenix.parse.ColumnParseNode)8 TableWildcardParseNode (org.apache.phoenix.parse.TableWildcardParseNode)6 WildcardParseNode (org.apache.phoenix.parse.WildcardParseNode)6 HashMap (java.util.HashMap)5 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)5 ComparisonParseNode (org.apache.phoenix.parse.ComparisonParseNode)5 ArrayList (java.util.ArrayList)4 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)4 AndParseNode (org.apache.phoenix.parse.AndParseNode)4 PLong (org.apache.phoenix.schema.types.PLong)4 Map (java.util.Map)3 Scan (org.apache.hadoop.hbase.client.Scan)3 ColumnResolver (org.apache.phoenix.compile.ColumnResolver)3 MutationState (org.apache.phoenix.execute.MutationState)3 Expression (org.apache.phoenix.expression.Expression)3 AliasedNode (org.apache.phoenix.parse.AliasedNode)3 ColumnDef (org.apache.phoenix.parse.ColumnDef)3 NamedTableNode (org.apache.phoenix.parse.NamedTableNode)3