Java Code Examples for io.airlift.slice.Slices#utf8Slice()

The following examples show how to use io.airlift.slice.Slices#utf8Slice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestArrayBlock.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithVariableWidthBlock()
{
    Slice[][] expectedValues = new Slice[ARRAY_SIZES.length][];
    for (int i = 0; i < ARRAY_SIZES.length; i++) {
        expectedValues[i] = new Slice[ARRAY_SIZES[i]];
        for (int j = 0; j < ARRAY_SIZES[i]; j++) {
            expectedValues[i][j] = Slices.utf8Slice(format("%d.%d", i, j));
        }
    }

    BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);

    assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
    assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
    assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
    assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);

    Slice[][] expectedValuesWithNull = alternatingNullValues(expectedValues);
    BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
    assertBlock(blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
    assertBlock(blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
    assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
    assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
}
 
Example 2
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 6 votes vote down vote up
private static Slice getSliceExpressedValue(Object value, Type type) {
    Slice sliceValue;
    if (value instanceof String) {
        sliceValue = Slices.utf8Slice((String) value);
    } else if (value instanceof byte[]) {
        sliceValue = Slices.wrappedBuffer((byte[]) value);
    } else if (value instanceof Integer) {
        sliceValue = Slices.utf8Slice(value.toString());
    } else {
        throw new IllegalStateException("unsupported string field type: " + value.getClass().getName());
    }
    if (isVarcharType(type)) {
        sliceValue = truncateToLength(sliceValue, type);
    }
    if (isCharType(type)) {
        sliceValue = truncateToLengthAndTrimSpaces(sliceValue, type);
    }

    return sliceValue;
}
 
Example 3
Source File: TestColumnarMap.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void test()
{
    Slice[][][] expectedValues = new Slice[MAP_SIZES.length][][];
    for (int mapIndex = 0; mapIndex < MAP_SIZES.length; mapIndex++) {
        expectedValues[mapIndex] = new Slice[MAP_SIZES[mapIndex]][];
        for (int entryIndex = 0; entryIndex < MAP_SIZES[mapIndex]; entryIndex++) {
            Slice[] entry = new Slice[2];
            entry[0] = Slices.utf8Slice(format("key.%d.%d", mapIndex, entryIndex));
            if (entryIndex % 3 != 1) {
                entry[1] = Slices.utf8Slice(format("value.%d.%d", mapIndex, entryIndex));
            }
            expectedValues[mapIndex][entryIndex] = entry;
        }
    }
    BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
    verifyBlock(blockBuilder, expectedValues);
    verifyBlock(blockBuilder.build(), expectedValues);

    Slice[][][] expectedValuesWithNull = alternatingNullValues(expectedValues);
    BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
    verifyBlock(blockBuilderWithNull, expectedValuesWithNull);
    verifyBlock(blockBuilderWithNull.build(), expectedValuesWithNull);
}
 
Example 4
Source File: UDFRe2JRegexpExtractAll.java    From hive-third-functions with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    String source = (String) arguments[0].get();
    String pattern = (String) arguments[1].get();
    Long groupIndex = 0L;
    if (arguments.length == 3) {
        groupIndex = (Long) arguments[2].get();
    }

    if (source == null) {
        return null;
    }

    if (re2JRegexp == null) {
        re2JRegexp = new Re2JRegexp(Integer.MAX_VALUE, 5, Slices.utf8Slice(pattern));
    }

    result.clear();
    result.addAll(re2JRegexp.extractAll(Slices.utf8Slice(source), groupIndex));

    return result;
}
 
Example 5
Source File: MatchQueryFunction.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@ScalarFunction("match_phrase")
@Description("es match_phrase")
@SqlType(StandardTypes.VARCHAR)
@SqlNullable
public static Slice matchPhrase(
        @SqlType(StandardTypes.VARCHAR) Slice filter)
{
    if (filter == null) {
        return null;
    }
    String filterStr = filter.toStringUtf8();

    QueryBuilder builder = QueryBuilders.matchPhraseQuery(MATCH_COLUMN_SEP, filterStr);
    return Slices.utf8Slice(builder.toString());
}
 
Example 6
Source File: MatchQueryFunction.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@ScalarFunction("match_query")
@Description("es match_query")
@SqlType(StandardTypes.VARCHAR)
@SqlNullable
public static Slice matchQuery(
        @SqlType(StandardTypes.VARCHAR) Slice filter)
{
    if (filter == null) {
        return null;
    }
    String filterStr = filter.toStringUtf8();

    QueryBuilder builder = QueryBuilders.matchQuery(MATCH_COLUMN_SEP, filterStr);
    return Slices.utf8Slice(builder.toString());
}
 
Example 7
Source File: TeradataStringFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Returns the hexadecimal representation of the UTF-16BE encoding of the argument")
@ScalarFunction("char2hexint")
@SqlType(StandardTypes.VARCHAR)
public static Slice char2HexInt(@SqlType(StandardTypes.VARCHAR) Slice string)
{
    Slice utf16 = Slices.wrappedBuffer(UTF_16BE.encode(string.toStringUtf8()));
    String encoded = BaseEncoding.base16().encode(utf16.getBytes());
    return Slices.utf8Slice(encoded);
}
 
Example 8
Source File: SetDigestFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice hashCounts(@SqlType(SetDigestType.NAME) Slice slice)
{
    SetDigest digest = SetDigest.newInstance(slice);

    try {
        return Slices.utf8Slice(OBJECT_MAPPER.writeValueAsString(digest.getHashCounts()));
    }
    catch (JsonProcessingException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 9
Source File: TestLikeFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Slice offsetHeapSlice(String value)
{
    Slice source = Slices.utf8Slice(value);
    Slice result = Slices.allocate(source.length() + 5);
    result.setBytes(2, source);
    return result.slice(2, source.length());
}
 
Example 10
Source File: MatchQueryFunction.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@ScalarFunction("match_phrase")
@Description("es match_phrase")
@SqlType(StandardTypes.VARCHAR)
@SqlNullable
public static Slice matchPhrase(
        @SqlType(StandardTypes.VARCHAR) Slice filter)
{
    if (filter == null) {
        return null;
    }
    String filterStr = filter.toStringUtf8();

    QueryBuilder builder = QueryBuilders.matchPhraseQuery(MATCH_COLUMN_SEP, filterStr);
    return Slices.utf8Slice(builder.toString());
}
 
Example 11
Source File: MatchQueryFunction.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@ScalarFunction("match_phrase")
@Description("es match_phrase")
@SqlType(StandardTypes.VARCHAR)
@SqlNullable
public static Slice matchPhrase(
        @SqlType(StandardTypes.VARCHAR) Slice filter)
{
    if (filter == null) {
        return null;
    }
    String filterStr = filter.toStringUtf8();

    QueryBuilder builder = QueryBuilders.matchPhraseQuery(MATCH_COLUMN_SEP, filterStr);
    return Slices.utf8Slice(builder.toString());
}
 
Example 12
Source File: MLFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@ScalarFunction("classify")
@SqlType(StandardTypes.VARCHAR)
public static Slice varcharClassify(@SqlType(MAP_BIGINT_DOUBLE) Block featuresMap, @SqlType("Classifier(varchar)") Slice modelSlice)
{
    FeatureVector features = ModelUtils.toFeatures(featuresMap);
    Model model = getOrLoadModel(modelSlice);
    checkArgument(model.getType().equals(VARCHAR_CLASSIFIER), "model is not a Classifier(varchar)");
    Classifier<String> varcharClassifier = (Classifier<String>) model;
    return Slices.utf8Slice(varcharClassifier.classify(features));
}
 
Example 13
Source File: AccumuloRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public Slice getSlice(int field)
{
    Type type = getType(field);
    if (type instanceof VarbinaryType) {
        return Slices.wrappedBuffer(serializer.getVarbinary(fieldToColumnName[field]));
    }
    if (type instanceof VarcharType) {
        return Slices.utf8Slice(serializer.getVarchar(fieldToColumnName[field]));
    }
    throw new PrestoException(NOT_SUPPORTED, "Unsupported type " + type);
}
 
Example 14
Source File: TestVarcharType.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
protected Object getGreaterValue(Object value)
{
    return Slices.utf8Slice(((Slice) value).toStringUtf8() + "_");
}
 
Example 15
Source File: Re2JRegexpType.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public void writeObject(BlockBuilder blockBuilder, Object value)
{
    Slice pattern = Slices.utf8Slice(((Re2JRegexp) value).pattern());
    blockBuilder.writeBytes(pattern, 0, pattern.length()).closeEntry();
}
 
Example 16
Source File: ListBasedRecordSet.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public Slice getSlice(int field)
{
    return Slices.utf8Slice(keys.get(field).get(position));
}
 
Example 17
Source File: VarcharDecoder.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public void decode(Supplier<Object> getter, BlockBuilder output)
{
    Slice slice = Slices.utf8Slice(getter.get().toString());
    output.writeBytes(slice, 0, slice.length()).closeEntry();
}
 
Example 18
Source File: BenchmarkTransformValue.java    From presto with Apache License 2.0 4 votes vote down vote up
@Setup
public void setup()
{
    Metadata metadata = createTestMetadataManager();
    ExpressionCompiler compiler = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0));
    ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder();
    Type elementType;
    Object compareValue;
    switch (type) {
        case "BIGINT":
            elementType = BIGINT;
            compareValue = 0L;
            break;
        case "DOUBLE":
            elementType = DOUBLE;
            compareValue = 0.0d;
            break;
        case "VARCHAR":
            elementType = VARCHAR;
            compareValue = Slices.utf8Slice("0");
            break;
        default:
            throw new UnsupportedOperationException();
    }
    MapType mapType = mapType(elementType, elementType);
    MapType returnType = mapType(elementType, BOOLEAN);
    ResolvedFunction resolvedFunction = metadata.resolveFunction(
            QualifiedName.of(name),
            fromTypes(mapType, new FunctionType(ImmutableList.of(elementType), elementType)));
    ResolvedFunction greaterThan = metadata.resolveOperator(GREATER_THAN, ImmutableList.of(elementType, elementType));
    projectionsBuilder.add(call(resolvedFunction, returnType, ImmutableList.of(
            field(0, mapType),
            new LambdaDefinitionExpression(
                    ImmutableList.of(elementType, elementType),
                    ImmutableList.of("x", "y"),
                    call(greaterThan, BOOLEAN, ImmutableList.of(
                            new VariableReferenceExpression("y", elementType),
                            constant(compareValue, elementType)))))));
    Block block = createChannel(POSITIONS, mapType, elementType);

    ImmutableList<RowExpression> projections = projectionsBuilder.build();
    pageProcessor = compiler.compilePageProcessor(Optional.empty(), projections).get();
    page = new Page(block);
}
 
Example 19
Source File: TpchRecordSet.java    From presto with Apache License 2.0 4 votes vote down vote up
private Slice getSlice(TpchColumn<E> tpchColumn)
{
    return Slices.utf8Slice(tpchColumn.getString(row));
}
 
Example 20
Source File: PrometheusRecordCursor.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public Slice getSlice(int field)
{
    checkFieldType(field, createUnboundedVarcharType());
    return Slices.utf8Slice((String) getFieldValue(field));
}