Search in sources :

Example 1 with MetaDataClient

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

the class CreateFunctionCompiler method compile.

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

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

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CREATE" + (create.getFunctionInfo().isReplace() ? " OR REPLACE" : "") + " FUNCTION"));
        }

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

Example 2 with MetaDataClient

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

the class CreateSequenceCompiler method compile.

public MutationPlan compile(final CreateSequenceStatement sequence) throws SQLException {
    ParseNode startsWithNode = sequence.getStartWith();
    ParseNode incrementByNode = sequence.getIncrementBy();
    ParseNode maxValueNode = sequence.getMaxValue();
    ParseNode minValueNode = sequence.getMinValue();
    ParseNode cacheNode = sequence.getCacheSize();
    // validate parse nodes
    if (startsWithNode != null) {
        validateNodeIsStateless(sequence, startsWithNode, SQLExceptionCode.START_WITH_MUST_BE_CONSTANT);
    }
    validateNodeIsStateless(sequence, incrementByNode, SQLExceptionCode.INCREMENT_BY_MUST_BE_CONSTANT);
    validateNodeIsStateless(sequence, maxValueNode, SQLExceptionCode.MAXVALUE_MUST_BE_CONSTANT);
    validateNodeIsStateless(sequence, minValueNode, SQLExceptionCode.MINVALUE_MUST_BE_CONSTANT);
    if (cacheNode != null) {
        validateNodeIsStateless(sequence, cacheNode, SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT);
    }
    final PhoenixConnection connection = statement.getConnection();
    final StatementContext context = new StatementContext(statement);
    // add param meta data if required
    if (startsWithNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) startsWithNode, LONG_DATUM);
    }
    if (incrementByNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) incrementByNode, LONG_DATUM);
    }
    if (maxValueNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) maxValueNode, LONG_DATUM);
    }
    if (minValueNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) minValueNode, LONG_DATUM);
    }
    if (cacheNode instanceof BindParseNode) {
        context.getBindManager().addParamMetaData((BindParseNode) cacheNode, INTEGER_DATUM);
    }
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(context);
    final long incrementBy = evalExpression(sequence, context, incrementByNode.accept(expressionCompiler), SQLExceptionCode.INCREMENT_BY_MUST_BE_CONSTANT);
    if (incrementBy == 0) {
        throw SequenceUtil.getException(sequence.getSequenceName().getSchemaName(), sequence.getSequenceName().getTableName(), SQLExceptionCode.INCREMENT_BY_MUST_NOT_BE_ZERO);
    }
    final long maxValue = evalExpression(sequence, context, maxValueNode.accept(expressionCompiler), SQLExceptionCode.MAXVALUE_MUST_BE_CONSTANT);
    final long minValue = evalExpression(sequence, context, minValueNode.accept(expressionCompiler), SQLExceptionCode.MINVALUE_MUST_BE_CONSTANT);
    if (minValue > maxValue) {
        TableName sequenceName = sequence.getSequenceName();
        throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), SQLExceptionCode.MINVALUE_MUST_BE_LESS_THAN_OR_EQUAL_TO_MAXVALUE);
    }
    long startsWithValue;
    if (startsWithNode == null) {
        startsWithValue = incrementBy > 0 ? minValue : maxValue;
    } else {
        startsWithValue = evalExpression(sequence, context, startsWithNode.accept(expressionCompiler), SQLExceptionCode.START_WITH_MUST_BE_CONSTANT);
        if (startsWithValue < minValue || startsWithValue > maxValue) {
            TableName sequenceName = sequence.getSequenceName();
            throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), SQLExceptionCode.STARTS_WITH_MUST_BE_BETWEEN_MIN_MAX_VALUE);
        }
    }
    final long startsWith = startsWithValue;
    long cacheSizeValue;
    if (cacheNode == null) {
        cacheSizeValue = connection.getQueryServices().getProps().getLong(QueryServices.SEQUENCE_CACHE_SIZE_ATTRIB, QueryServicesOptions.DEFAULT_SEQUENCE_CACHE_SIZE);
    } else {
        cacheSizeValue = evalExpression(sequence, context, cacheNode.accept(expressionCompiler), SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT);
        if (cacheSizeValue < 0) {
            TableName sequenceName = sequence.getSequenceName();
            throw SequenceUtil.getException(sequenceName.getSchemaName(), sequenceName.getTableName(), SQLExceptionCode.CACHE_MUST_BE_NON_NEGATIVE_CONSTANT);
        }
    }
    final long cacheSize = Math.max(1L, cacheSizeValue);
    final MetaDataClient client = new MetaDataClient(connection);
    return new BaseMutationPlan(context, operation) {

        @Override
        public MutationState execute() throws SQLException {
            return client.createSequence(sequence, startsWith, incrementBy, cacheSize, minValue, maxValue);
        }

        @Override
        public ExplainPlan getExplainPlan() throws SQLException {
            return new ExplainPlan(Collections.singletonList("CREATE SEQUENCE"));
        }
    };
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) TableName(org.apache.phoenix.parse.TableName) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) BindParseNode(org.apache.phoenix.parse.BindParseNode) ParseNode(org.apache.phoenix.parse.ParseNode) BindParseNode(org.apache.phoenix.parse.BindParseNode)

Example 3 with MetaDataClient

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

the class DeclareCursorCompiler method compile.

public MutationPlan compile(final DeclareCursorStatement declare) throws SQLException {
    if (declare.getBindCount() != 0) {
        throw new SQLException("Cannot declare cursor, internal SELECT statement contains bindings!");
    }
    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.declareCursor(declare, queryPlan);
        }

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

Example 4 with MetaDataClient

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

the class DropSequenceCompiler method compile.

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

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

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

Example 5 with MetaDataClient

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

the class PhoenixRuntime method getTable.

/**
 * Returns the table if it is found in the connection metadata cache. If the metadata of this
 * table has changed since it was put in the cache these changes will not necessarily be
 * reflected in the returned table. If the table is not found, makes a call to the server to
 * fetch the latest metadata of the table. This is different than how a table is resolved when
 * it is referenced from a query (a call is made to the server to fetch the latest metadata of the table
 * depending on the UPDATE_CACHE_FREQUENCY property)
 * See https://issues.apache.org/jira/browse/PHOENIX-4475
 * @param conn
 * @param name requires a pre-normalized table name or a pre-normalized schema and table name
 * @return
 * @throws SQLException
 */
public static PTable getTable(Connection conn, String name) throws SQLException {
    PTable table = null;
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    try {
        table = pconn.getTable(new PTableKey(pconn.getTenantId(), name));
    } catch (TableNotFoundException e) {
        String schemaName = SchemaUtil.getSchemaNameFromFullName(name);
        String tableName = SchemaUtil.getTableNameFromFullName(name);
        MetaDataMutationResult result = new MetaDataClient(pconn).updateCache(schemaName, tableName);
        if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
            throw e;
        }
        table = result.getTable();
    }
    return table;
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) PTable(org.apache.phoenix.schema.PTable)

Aggregations

MetaDataClient (org.apache.phoenix.schema.MetaDataClient)20 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)16 PTable (org.apache.phoenix.schema.PTable)9 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)6 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)6 PColumn (org.apache.phoenix.schema.PColumn)4 ParseNode (org.apache.phoenix.parse.ParseNode)3 Map (java.util.Map)2 Scan (org.apache.hadoop.hbase.client.Scan)2 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)2 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)2 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)2 PhoenixIndexBuilder (org.apache.phoenix.index.PhoenixIndexBuilder)2 BindParseNode (org.apache.phoenix.parse.BindParseNode)2 TableName (org.apache.phoenix.parse.TableName)2 TableRef (org.apache.phoenix.schema.TableRef)2 PLong (org.apache.phoenix.schema.types.PLong)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1