Search in sources :

Example 36 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class JsonOperators method castToVarchar.

@ScalarOperator(CAST)
@SqlNullable
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToVarchar(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Slice result = currentTokenAsVarchar(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to VARCHAR");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR), e);
    }
}
Also used : Slice(io.airlift.slice.Slice) JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 37 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class JsonOperators method castToBigint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BIGINT)
public static Long castToBigint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsBigint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BIGINT");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BIGINT), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 38 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class JsonOperators method castToSmallint.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(SMALLINT)
public static Long castToSmallint(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Long result = currentTokenAsSmallint(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to SMALLINT");
        return result;
    } catch (PrestoException e) {
        if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
        }
        throw e;
    } catch (IllegalArgumentException | IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), SMALLINT), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 39 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class JsonOperators method castToDouble.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(DOUBLE)
public static Double castToDouble(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Double result = currentTokenAsDouble(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DOUBLE");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), DOUBLE), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.currentTokenAsDouble(com.facebook.presto.util.JsonUtil.currentTokenAsDouble) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 40 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class MultimapFromEntriesFunction method multimapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,array(V))")
@SqlNullable
public Block multimapFromEntries(@TypeParameter("map(K,array(V))") MapType mapType, @SqlType("array(row(K,V))") Block block) {
    Type keyType = mapType.getKeyType();
    Type valueType = ((ArrayType) mapType.getValueType()).getElementType();
    RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
    int entryCount = block.getPositionCount();
    IntList[] entryIndicesList = new IntList[entryCount];
    for (int i = 0; i < entryIndicesList.length; i++) {
        entryIndicesList[i] = new IntArrayList();
    }
    TypedSet keySet = new TypedSet(keyType, entryCount, NAME);
    for (int i = 0; i < entryCount; i++) {
        if (block.isNull(i)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block rowBlock = rowType.getObject(block, i);
        if (rowBlock.isNull(0)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (keySet.contains(rowBlock, 0)) {
            entryIndicesList[keySet.positionOf(rowBlock, 0)].add(i);
        } else {
            keySet.add(rowBlock, 0);
            entryIndicesList[keySet.size() - 1].add(i);
        }
    }
    BlockBuilder multimapBlockBuilder = mapType.createBlockBuilder(null, keySet.size());
    BlockBuilder singleMapWriter = multimapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keySet.size(); i++) {
        keyType.appendTo(rowType.getObject(block, entryIndicesList[i].getInt(0)), 0, singleMapWriter);
        BlockBuilder singleArrayWriter = singleMapWriter.beginBlockEntry();
        for (int entryIndex : entryIndicesList[i]) {
            valueType.appendTo(rowType.getObject(block, entryIndex), 1, singleArrayWriter);
        }
        singleMapWriter.closeEntry();
    }
    multimapBlockBuilder.closeEntry();
    return mapType.getObject(multimapBlockBuilder, multimapBlockBuilder.getPositionCount() - 1);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) SqlType(com.facebook.presto.spi.function.SqlType) RowType(com.facebook.presto.common.type.RowType) RowType(com.facebook.presto.common.type.RowType) TypedSet(com.facebook.presto.operator.aggregation.TypedSet) Block(com.facebook.presto.common.block.Block) PrestoException(com.facebook.presto.spi.PrestoException) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) IntList(it.unimi.dsi.fastutil.ints.IntList) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) SqlNullable(com.facebook.presto.spi.function.SqlNullable) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

SqlNullable (com.facebook.presto.spi.function.SqlNullable)61 SqlType (com.facebook.presto.spi.function.SqlType)61 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)44 Description (com.facebook.presto.spi.function.Description)37 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)29 PrestoException (com.facebook.presto.spi.PrestoException)17 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)15 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)15 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)15 Geometry (org.locationtech.jts.geom.Geometry)15 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)14 JsonParser (com.fasterxml.jackson.core.JsonParser)14 IOException (java.io.IOException)14 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)9 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)8 JsonCastException (com.facebook.presto.util.JsonCastException)8 Slice (io.airlift.slice.Slice)8 Point (com.esri.core.geometry.Point)7 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)7 TypeParameter (com.facebook.presto.spi.function.TypeParameter)7