Search in sources :

Example 11 with MetaDataClient

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

the class CreateIndexCompiler method compile.

public MutationPlan compile(final CreateIndexStatement create) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    final ColumnResolver resolver = FromCompiler.getResolver(create, connection, create.getUdfParseNodes());
    Scan scan = new Scan();
    final StatementContext context = new StatementContext(statement, resolver, scan, new SequenceManager(statement));
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
    List<ParseNode> splitNodes = create.getSplitNodes();
    if (create.getIndexType() == IndexType.LOCAL) {
        if (!splitNodes.isEmpty()) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_SPLIT_LOCAL_INDEX).build().buildException();
        }
        List<Pair<String, Object>> list = create.getProps() != null ? create.getProps().get("") : null;
        if (list != null) {
            for (Pair<String, Object> pair : list) {
                if (pair.getFirst().equals(PhoenixDatabaseMetaData.SALT_BUCKETS)) {
                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.CANNOT_SALT_LOCAL_INDEX).build().buildException();
                }
            }
        }
    }
    final byte[][] splits = new byte[splitNodes.size()][];
    for (int i = 0; i < splits.length; i++) {
        ParseNode node = splitNodes.get(i);
        if (!node.isStateless()) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.SPLIT_POINT_NOT_CONSTANT).setMessage("Node: " + node).build().buildException();
        }
        LiteralExpression expression = (LiteralExpression) node.accept(expressionCompiler);
        splits[i] = expression.getBytes();
    }
    final MetaDataClient client = new MetaDataClient(connection);
    return new BaseMutationPlan(context, operation) {

        @Override
        public MutationState execute() throws SQLException {
            return client.createIndex(create, splits);
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CREATE INDEX"));
        }
    };
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ParseNode(org.apache.phoenix.parse.ParseNode) Scan(org.apache.hadoop.hbase.client.Scan) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) Pair(org.apache.hadoop.hbase.util.Pair)

Example 12 with MetaDataClient

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

the class CreateSchemaCompiler method compile.

public MutationPlan compile(final CreateSchemaStatement create) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    final StatementContext context = new StatementContext(statement);
    final MetaDataClient client = new MetaDataClient(connection);
    return new BaseMutationPlan(context, create.getOperation()) {

        @Override
        public MutationState execute() throws SQLException {
            try {
                return client.createSchema(create);
            } finally {
                if (client.getConnection() != connection) {
                    client.getConnection().close();
                }
            }
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CREATE SCHEMA"));
        }

        @Override
        public StatementContext getContext() {
            return context;
        }
    };
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection)

Example 13 with MetaDataClient

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

the class CreateTableCompiler method compile.

public MutationPlan compile(CreateTableStatement create) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    ColumnResolver resolver = FromCompiler.getResolverForCreation(create, connection);
    PTableType type = create.getTableType();
    PhoenixConnection connectionToBe = connection;
    PTable parentToBe = null;
    ViewType viewTypeToBe = null;
    Scan scan = new Scan();
    final StatementContext context = new StatementContext(statement, resolver, scan, new SequenceManager(statement));
    // TODO: support any statement for a VIEW instead of just a WHERE clause
    ParseNode whereNode = create.getWhereClause();
    String viewStatementToBe = null;
    byte[][] viewColumnConstantsToBe = null;
    BitSet isViewColumnReferencedToBe = null;
    // Check whether column families having local index column family suffix or not if present
    // don't allow creating table.
    // Also validate the default values expressions.
    List<ColumnDef> columnDefs = create.getColumnDefs();
    List<ColumnDef> overideColumnDefs = null;
    PrimaryKeyConstraint pkConstraint = create.getPrimaryKeyConstraint();
    for (int i = 0; i < columnDefs.size(); i++) {
        ColumnDef columnDef = columnDefs.get(i);
        if (columnDef.getColumnDefName().getFamilyName() != null && columnDef.getColumnDefName().getFamilyName().contains(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.UNALLOWED_COLUMN_FAMILY).build().buildException();
        }
        // False means we do not need the default (because it evaluated to null)
        if (!columnDef.validateDefault(context, pkConstraint)) {
            if (overideColumnDefs == null) {
                overideColumnDefs = new ArrayList<>(columnDefs);
            }
            overideColumnDefs.set(i, new ColumnDef(columnDef, null));
        }
    }
    if (overideColumnDefs != null) {
        create = new CreateTableStatement(create, overideColumnDefs);
    }
    final CreateTableStatement finalCreate = create;
    if (type == PTableType.VIEW) {
        TableRef tableRef = resolver.getTables().get(0);
        int nColumns = tableRef.getTable().getColumns().size();
        isViewColumnReferencedToBe = new BitSet(nColumns);
        // Used to track column references in a view
        ExpressionCompiler expressionCompiler = new ColumnTrackingExpressionCompiler(context, isViewColumnReferencedToBe);
        parentToBe = tableRef.getTable();
        viewTypeToBe = parentToBe.getViewType() == ViewType.MAPPED ? ViewType.MAPPED : ViewType.UPDATABLE;
        if (whereNode == null) {
            viewStatementToBe = parentToBe.getViewStatement();
        } else {
            whereNode = StatementNormalizer.normalize(whereNode, resolver);
            if (whereNode.isStateless()) {
                throw new SQLExceptionInfo.Builder(SQLExceptionCode.VIEW_WHERE_IS_CONSTANT).build().buildException();
            }
            // If our parent has a VIEW statement, combine it with this one
            if (parentToBe.getViewStatement() != null) {
                SelectStatement select = new SQLParser(parentToBe.getViewStatement()).parseQuery().combine(whereNode);
                whereNode = select.getWhere();
            }
            Expression where = whereNode.accept(expressionCompiler);
            if (where != null && !LiteralExpression.isTrue(where)) {
                TableName baseTableName = create.getBaseTableName();
                StringBuilder buf = new StringBuilder();
                whereNode.toSQL(resolver, buf);
                viewStatementToBe = QueryUtil.getViewStatement(baseTableName.getSchemaName(), baseTableName.getTableName(), buf.toString());
            }
            if (viewTypeToBe != ViewType.MAPPED) {
                Long scn = connection.getSCN();
                connectionToBe = (scn != null || tableRef.getTable().isTransactional()) ? connection : // clocks being in sync.
                new PhoenixConnection(// on our connection.
                new DelegateConnectionQueryServices(connection.getQueryServices()) {

                    @Override
                    public void addTable(PTable table, long resolvedTime) throws SQLException {
                        connection.addTable(table, resolvedTime);
                    }
                }, connection, tableRef.getTimeStamp() + 1);
                viewColumnConstantsToBe = new byte[nColumns][];
                ViewWhereExpressionVisitor visitor = new ViewWhereExpressionVisitor(parentToBe, viewColumnConstantsToBe);
                where.accept(visitor);
                // If view is not updatable, viewColumnConstants should be empty. We will still
                // inherit our parent viewConstants, but we have no additional ones.
                viewTypeToBe = visitor.isUpdatable() ? ViewType.UPDATABLE : ViewType.READ_ONLY;
                if (viewTypeToBe != ViewType.UPDATABLE) {
                    viewColumnConstantsToBe = null;
                }
            }
        }
    }
    final ViewType viewType = viewTypeToBe;
    final String viewStatement = viewStatementToBe;
    final byte[][] viewColumnConstants = viewColumnConstantsToBe;
    final BitSet isViewColumnReferenced = isViewColumnReferencedToBe;
    List<ParseNode> splitNodes = create.getSplitNodes();
    final byte[][] splits = new byte[splitNodes.size()][];
    ImmutableBytesWritable ptr = context.getTempPtr();
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
    for (int i = 0; i < splits.length; i++) {
        ParseNode node = splitNodes.get(i);
        if (node instanceof BindParseNode) {
            context.getBindManager().addParamMetaData((BindParseNode) node, VARBINARY_DATUM);
        }
        if (node.isStateless()) {
            Expression expression = node.accept(expressionCompiler);
            if (expression.evaluate(null, ptr)) {
                ;
                splits[i] = ByteUtil.copyKeyBytesIfNecessary(ptr);
                continue;
            }
        }
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.SPLIT_POINT_NOT_CONSTANT).setMessage("Node: " + node).build().buildException();
    }
    final MetaDataClient client = new MetaDataClient(connectionToBe);
    final PTable parent = parentToBe;
    return new BaseMutationPlan(context, operation) {

        @Override
        public MutationState execute() throws SQLException {
            try {
                return client.createTable(finalCreate, splits, parent, viewStatement, viewType, viewColumnConstants, isViewColumnReferenced);
            } finally {
                if (client.getConnection() != connection) {
                    client.getConnection().close();
                }
            }
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CREATE TABLE"));
        }
    };
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) PTable(org.apache.phoenix.schema.PTable) SelectStatement(org.apache.phoenix.parse.SelectStatement) BindParseNode(org.apache.phoenix.parse.BindParseNode) ColumnParseNode(org.apache.phoenix.parse.ColumnParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) MetaDataClient(org.apache.phoenix.schema.MetaDataClient) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PTableType(org.apache.phoenix.schema.PTableType) CreateTableStatement(org.apache.phoenix.parse.CreateTableStatement) BitSet(java.util.BitSet) ColumnDef(org.apache.phoenix.parse.ColumnDef) PrimaryKeyConstraint(org.apache.phoenix.parse.PrimaryKeyConstraint) PrimaryKeyConstraint(org.apache.phoenix.parse.PrimaryKeyConstraint) TableName(org.apache.phoenix.parse.TableName) SQLParser(org.apache.phoenix.parse.SQLParser) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) Expression(org.apache.phoenix.expression.Expression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) IsNullExpression(org.apache.phoenix.expression.IsNullExpression) AndExpression(org.apache.phoenix.expression.AndExpression) ComparisonExpression(org.apache.phoenix.expression.ComparisonExpression) Scan(org.apache.hadoop.hbase.client.Scan) ViewType(org.apache.phoenix.schema.PTable.ViewType) TableRef(org.apache.phoenix.schema.TableRef) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 14 with MetaDataClient

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

the class OpenStatementCompiler method compile.

public MutationPlan compile(final OpenStatement open) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    final StatementContext context = new StatementContext(statement);
    final MetaDataClient client = new MetaDataClient(connection);
    return new BaseMutationPlan(context, operation) {

        @Override
        public MutationState execute() throws SQLException {
            return client.open(open);
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("OPEN CURSOR"));
        }
    };
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection)

Example 15 with MetaDataClient

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

the class CloseStatementCompiler method compile.

public MutationPlan compile(final CloseStatement close) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    final StatementContext context = new StatementContext(statement);
    final MetaDataClient client = new MetaDataClient(connection);
    return new BaseMutationPlan(context, operation) {

        @Override
        public MutationState execute() throws SQLException {
            return client.close(close);
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CLOSE CURSOR"));
        }
    };
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection)

Aggregations

MetaDataClient (org.apache.phoenix.schema.MetaDataClient)18 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 PTable (org.apache.phoenix.schema.PTable)8 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)6 ParseNode (org.apache.phoenix.parse.ParseNode)5 Scan (org.apache.hadoop.hbase.client.Scan)4 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)4 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)4 SQLException (java.sql.SQLException)3 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)3 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)3 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)3 SelectStatement (org.apache.phoenix.parse.SelectStatement)3 PColumn (org.apache.phoenix.schema.PColumn)3 TableRef (org.apache.phoenix.schema.TableRef)3 BitSet (java.util.BitSet)2 Map (java.util.Map)2 Cell (org.apache.hadoop.hbase.Cell)2 Pair (org.apache.hadoop.hbase.util.Pair)2 AggregatePlan (org.apache.phoenix.execute.AggregatePlan)2