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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations