Search in sources :

Example 1 with FunctionArgument

use of org.apache.phoenix.parse.PFunction.FunctionArgument in project phoenix by apache.

the class MetaDataEndpointImpl method getFunction.

private PFunction getFunction(RegionScanner scanner, final boolean isReplace, long clientTimeStamp, List<Mutation> deleteMutationsForReplace) throws IOException, SQLException {
    List<Cell> results = Lists.newArrayList();
    scanner.next(results);
    if (results.isEmpty()) {
        return null;
    }
    Cell[] functionKeyValues = new Cell[FUNCTION_KV_COLUMNS.size()];
    Cell[] functionArgKeyValues = new Cell[FUNCTION_ARG_KV_COLUMNS.size()];
    // Create PFunction based on KeyValues from scan
    Cell keyValue = results.get(0);
    byte[] keyBuffer = keyValue.getRowArray();
    int keyLength = keyValue.getRowLength();
    int keyOffset = keyValue.getRowOffset();
    long currentTimeMillis = EnvironmentEdgeManager.currentTimeMillis();
    if (isReplace) {
        long deleteTimeStamp = clientTimeStamp == HConstants.LATEST_TIMESTAMP ? currentTimeMillis - 1 : (keyValue.getTimestamp() < clientTimeStamp ? clientTimeStamp - 1 : keyValue.getTimestamp());
        deleteMutationsForReplace.add(new Delete(keyBuffer, keyOffset, keyLength, deleteTimeStamp));
    }
    PName tenantId = newPName(keyBuffer, keyOffset, keyLength);
    int tenantIdLength = (tenantId == null) ? 0 : tenantId.getBytes().length;
    if (tenantIdLength == 0) {
        tenantId = null;
    }
    PName functionName = newPName(keyBuffer, keyOffset + tenantIdLength + 1, keyLength - tenantIdLength - 1);
    int functionNameLength = functionName.getBytes().length + 1;
    int offset = tenantIdLength + functionNameLength + 1;
    long timeStamp = keyValue.getTimestamp();
    int i = 0;
    int j = 0;
    while (i < results.size() && j < FUNCTION_KV_COLUMNS.size()) {
        Cell kv = results.get(i);
        Cell searchKv = FUNCTION_KV_COLUMNS.get(j);
        int cmp = Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(), searchKv.getQualifierArray(), searchKv.getQualifierOffset(), searchKv.getQualifierLength());
        if (cmp == 0) {
            // Find max timestamp of table
            timeStamp = Math.max(timeStamp, kv.getTimestamp());
            // header row
            functionKeyValues[j++] = kv;
            i++;
        } else if (cmp > 0) {
            timeStamp = Math.max(timeStamp, kv.getTimestamp());
            functionKeyValues[j++] = null;
        } else {
            // shouldn't happen - means unexpected KV in system table header row
            i++;
        }
    }
    // CLASS_NAME,NUM_ARGS and JAR_PATH are required.
    if (functionKeyValues[CLASS_NAME_INDEX] == null || functionKeyValues[NUM_ARGS_INDEX] == null) {
        throw new IllegalStateException("Didn't find expected key values for function row in metadata row");
    }
    Cell classNameKv = functionKeyValues[CLASS_NAME_INDEX];
    PName className = newPName(classNameKv.getValueArray(), classNameKv.getValueOffset(), classNameKv.getValueLength());
    Cell jarPathKv = functionKeyValues[JAR_PATH_INDEX];
    PName jarPath = null;
    if (jarPathKv != null) {
        jarPath = newPName(jarPathKv.getValueArray(), jarPathKv.getValueOffset(), jarPathKv.getValueLength());
    }
    Cell numArgsKv = functionKeyValues[NUM_ARGS_INDEX];
    int numArgs = PInteger.INSTANCE.getCodec().decodeInt(numArgsKv.getValueArray(), numArgsKv.getValueOffset(), SortOrder.getDefault());
    Cell returnTypeKv = functionKeyValues[RETURN_TYPE_INDEX];
    PName returnType = returnTypeKv == null ? null : newPName(returnTypeKv.getValueArray(), returnTypeKv.getValueOffset(), returnTypeKv.getValueLength());
    List<FunctionArgument> arguments = Lists.newArrayListWithExpectedSize(numArgs);
    for (int k = 0; k < numArgs; k++) {
        results.clear();
        scanner.next(results);
        if (results.isEmpty()) {
            break;
        }
        Cell typeKv = results.get(0);
        if (isReplace) {
            long deleteTimeStamp = clientTimeStamp == HConstants.LATEST_TIMESTAMP ? currentTimeMillis - 1 : (typeKv.getTimestamp() < clientTimeStamp ? clientTimeStamp - 1 : typeKv.getTimestamp());
            deleteMutationsForReplace.add(new Delete(typeKv.getRowArray(), typeKv.getRowOffset(), typeKv.getRowLength(), deleteTimeStamp));
        }
        int typeKeyLength = typeKv.getRowLength();
        PName typeName = newPName(typeKv.getRowArray(), typeKv.getRowOffset() + offset, typeKeyLength - offset - 3);
        int argPositionOffset = offset + typeName.getBytes().length + 1;
        short argPosition = Bytes.toShort(typeKv.getRowArray(), typeKv.getRowOffset() + argPositionOffset, typeKeyLength - argPositionOffset);
        addArgumentToFunction(results, functionName, typeName, functionArgKeyValues, arguments, argPosition);
    }
    Collections.sort(arguments, new Comparator<FunctionArgument>() {

        @Override
        public int compare(FunctionArgument o1, FunctionArgument o2) {
            return o1.getArgPosition() - o2.getArgPosition();
        }
    });
    return new PFunction(tenantId, functionName.getString(), arguments, returnType.getString(), className.getString(), jarPath == null ? null : jarPath.getString(), timeStamp);
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) PFunction(org.apache.phoenix.parse.PFunction) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) PName(org.apache.phoenix.schema.PName) Cell(org.apache.hadoop.hbase.Cell) FunctionArgument(org.apache.phoenix.parse.PFunction.FunctionArgument)

Example 2 with FunctionArgument

use of org.apache.phoenix.parse.PFunction.FunctionArgument in project phoenix by apache.

the class MetaDataEndpointImpl method addArgumentToFunction.

private void addArgumentToFunction(List<Cell> results, PName functionName, PName type, Cell[] functionKeyValues, List<FunctionArgument> arguments, short argPosition) throws SQLException {
    int i = 0;
    int j = 0;
    while (i < results.size() && j < FUNCTION_ARG_KV_COLUMNS.size()) {
        Cell kv = results.get(i);
        Cell searchKv = FUNCTION_ARG_KV_COLUMNS.get(j);
        int cmp = Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(), searchKv.getQualifierArray(), searchKv.getQualifierOffset(), searchKv.getQualifierLength());
        if (cmp == 0) {
            functionKeyValues[j++] = kv;
            i++;
        } else if (cmp > 0) {
            functionKeyValues[j++] = null;
        } else {
            // shouldn't happen - means unexpected KV in system table column row
            i++;
        }
    }
    Cell isArrayKv = functionKeyValues[IS_ARRAY_INDEX];
    boolean isArrayType = isArrayKv == null ? false : Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(isArrayKv.getValueArray(), isArrayKv.getValueOffset(), isArrayKv.getValueLength()));
    Cell isConstantKv = functionKeyValues[IS_CONSTANT_INDEX];
    boolean isConstant = isConstantKv == null ? false : Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(isConstantKv.getValueArray(), isConstantKv.getValueOffset(), isConstantKv.getValueLength()));
    Cell defaultValueKv = functionKeyValues[DEFAULT_VALUE_INDEX];
    String defaultValue = defaultValueKv == null ? null : (String) PVarchar.INSTANCE.toObject(defaultValueKv.getValueArray(), defaultValueKv.getValueOffset(), defaultValueKv.getValueLength());
    Cell minValueKv = functionKeyValues[MIN_VALUE_INDEX];
    String minValue = minValueKv == null ? null : (String) PVarchar.INSTANCE.toObject(minValueKv.getValueArray(), minValueKv.getValueOffset(), minValueKv.getValueLength());
    Cell maxValueKv = functionKeyValues[MAX_VALUE_INDEX];
    String maxValue = maxValueKv == null ? null : (String) PVarchar.INSTANCE.toObject(maxValueKv.getValueArray(), maxValueKv.getValueOffset(), maxValueKv.getValueLength());
    FunctionArgument arg = new FunctionArgument(type.getString(), isArrayType, isConstant, defaultValue == null ? null : LiteralExpression.newConstant((new LiteralParseNode(defaultValue)).getValue()), minValue == null ? null : LiteralExpression.newConstant((new LiteralParseNode(minValue)).getValue()), maxValue == null ? null : LiteralExpression.newConstant((new LiteralParseNode(maxValue)).getValue()), argPosition);
    arguments.add(arg);
}
Also used : ByteString(com.google.protobuf.ByteString) Cell(org.apache.hadoop.hbase.Cell) FunctionArgument(org.apache.phoenix.parse.PFunction.FunctionArgument) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) LiteralParseNode(org.apache.phoenix.parse.LiteralParseNode)

Example 3 with FunctionArgument

use of org.apache.phoenix.parse.PFunction.FunctionArgument in project phoenix by apache.

the class MetaDataClient method createFunction.

public MutationState createFunction(CreateFunctionStatement stmt) throws SQLException {
    boolean wasAutoCommit = connection.getAutoCommit();
    connection.rollback();
    try {
        PFunction function = new PFunction(stmt.getFunctionInfo(), stmt.isTemporary(), stmt.isReplace());
        connection.setAutoCommit(false);
        String tenantIdStr = connection.getTenantId() == null ? null : connection.getTenantId().getString();
        List<Mutation> functionData = Lists.newArrayListWithExpectedSize(function.getFunctionArguments().size() + 1);
        List<FunctionArgument> args = function.getFunctionArguments();
        try (PreparedStatement argUpsert = connection.prepareStatement(INSERT_FUNCTION_ARGUMENT)) {
            for (int i = 0; i < args.size(); i++) {
                FunctionArgument arg = args.get(i);
                addFunctionArgMutation(function.getFunctionName(), arg, argUpsert, i);
            }
            functionData.addAll(connection.getMutationState().toMutations().next().getSecond());
            connection.rollback();
        }
        try (PreparedStatement functionUpsert = connection.prepareStatement(CREATE_FUNCTION)) {
            functionUpsert.setString(1, tenantIdStr);
            functionUpsert.setString(2, function.getFunctionName());
            functionUpsert.setInt(3, function.getFunctionArguments().size());
            functionUpsert.setString(4, function.getClassName());
            functionUpsert.setString(5, function.getJarPath());
            functionUpsert.setString(6, function.getReturnType());
            functionUpsert.execute();
            functionData.addAll(connection.getMutationState().toMutations(null).next().getSecond());
            connection.rollback();
        }
        MetaDataMutationResult result = connection.getQueryServices().createFunction(functionData, function, stmt.isTemporary());
        MutationCode code = result.getMutationCode();
        switch(code) {
            case FUNCTION_ALREADY_EXISTS:
                if (!function.isReplace()) {
                    throw new FunctionAlreadyExistsException(function.getFunctionName(), result.getFunctions().get(0));
                } else {
                    connection.removeFunction(function.getTenantId(), function.getFunctionName(), result.getMutationTime());
                    addFunctionToCache(result);
                }
            case NEWER_FUNCTION_FOUND:
                // it to this connection as we can't see it.
                throw new NewerFunctionAlreadyExistsException(function.getFunctionName(), result.getFunctions().get(0));
            default:
                List<PFunction> functions = new ArrayList<PFunction>(1);
                functions.add(function);
                result = new MetaDataMutationResult(code, result.getMutationTime(), functions, true);
                if (function.isReplace()) {
                    connection.removeFunction(function.getTenantId(), function.getFunctionName(), result.getMutationTime());
                }
                addFunctionToCache(result);
        }
    } finally {
        connection.setAutoCommit(wasAutoCommit);
    }
    return new MutationState(1, 1000, connection);
}
Also used : PFunction(org.apache.phoenix.parse.PFunction) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IndexKeyConstraint(org.apache.phoenix.parse.IndexKeyConstraint) PrimaryKeyConstraint(org.apache.phoenix.parse.PrimaryKeyConstraint) ColumnDefInPkConstraint(org.apache.phoenix.parse.ColumnDefInPkConstraint) MutationCode(org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode) MutationState(org.apache.phoenix.execute.MutationState) Mutation(org.apache.hadoop.hbase.client.Mutation) FunctionArgument(org.apache.phoenix.parse.PFunction.FunctionArgument) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)

Aggregations

FunctionArgument (org.apache.phoenix.parse.PFunction.FunctionArgument)3 Cell (org.apache.hadoop.hbase.Cell)2 PFunction (org.apache.phoenix.parse.PFunction)2 PSmallint (org.apache.phoenix.schema.types.PSmallint)2 PTinyint (org.apache.phoenix.schema.types.PTinyint)2 ByteString (com.google.protobuf.ByteString)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 Delete (org.apache.hadoop.hbase.client.Delete)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)1 MutationCode (org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode)1 MutationState (org.apache.phoenix.execute.MutationState)1 ColumnDefInPkConstraint (org.apache.phoenix.parse.ColumnDefInPkConstraint)1 IndexKeyConstraint (org.apache.phoenix.parse.IndexKeyConstraint)1 LiteralParseNode (org.apache.phoenix.parse.LiteralParseNode)1 PrimaryKeyConstraint (org.apache.phoenix.parse.PrimaryKeyConstraint)1 PName (org.apache.phoenix.schema.PName)1