use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class Field method toString.
@Override
public String toString() {
if (value == null) {
return "null";
}
if (Types.isArrayType(type)) {
Type elementType = Types.getElementType(type);
StringBuilder builder = new StringBuilder("ARRAY [");
for (Object element : AccumuloRowSerializer.getArrayFromBlock(elementType, this.getArray())) {
if (Types.isArrayType(elementType)) {
Type elementElementType = Types.getElementType(elementType);
builder.append(new Field(AccumuloRowSerializer.getBlockFromArray(elementElementType, (List<?>) element), elementType)).append(',');
} else if (Types.isMapType(elementType)) {
builder.append(new Field(AccumuloRowSerializer.getBlockFromMap(elementType, (Map<?, ?>) element), elementType)).append(',');
} else {
builder.append(new Field(element, elementType)).append(',');
}
}
return builder.deleteCharAt(builder.length() - 1).append("]").toString();
}
if (Types.isMapType(type)) {
StringBuilder builder = new StringBuilder("MAP(");
StringBuilder keys = new StringBuilder("ARRAY [");
StringBuilder values = new StringBuilder("ARRAY [");
for (Entry<Object, Object> entry : AccumuloRowSerializer.getMapFromBlock(type, this.getMap()).entrySet()) {
Type keyType = Types.getKeyType(type);
if (Types.isArrayType(keyType)) {
keys.append(new Field(AccumuloRowSerializer.getBlockFromArray(Types.getElementType(keyType), (List<?>) entry.getKey()), keyType)).append(',');
} else if (Types.isMapType(keyType)) {
keys.append(new Field(AccumuloRowSerializer.getBlockFromMap(keyType, (Map<?, ?>) entry.getKey()), keyType)).append(',');
} else {
keys.append(new Field(entry.getKey(), keyType)).append(',');
}
Type valueType = Types.getValueType(type);
if (Types.isArrayType(valueType)) {
values.append(new Field(AccumuloRowSerializer.getBlockFromArray(Types.getElementType(valueType), (List<?>) entry.getValue()), valueType)).append(',');
} else if (Types.isMapType(valueType)) {
values.append(new Field(AccumuloRowSerializer.getBlockFromMap(valueType, (Map<?, ?>) entry.getValue()), valueType)).append(',');
} else {
values.append(new Field(entry.getValue(), valueType)).append(',');
}
}
keys.deleteCharAt(keys.length() - 1).append(']');
values.deleteCharAt(values.length() - 1).append(']');
return builder.append(keys).append(", ").append(values).append(")").toString();
}
// Validate the object is the given type
if (type.equals(BIGINT) || type.equals(BOOLEAN) || type.equals(DOUBLE) || type.equals(INTEGER) || type.equals(REAL) || type.equals(TINYINT) || type.equals(SMALLINT)) {
return value.toString();
} else if (type.equals(DATE)) {
return "DATE '" + value.toString() + "'";
} else if (type.equals(TIME)) {
return "TIME '" + value.toString() + "'";
} else if (type.equals(TIMESTAMP)) {
return "TIMESTAMP '" + value.toString() + "'";
} else if (type.equals(VARBINARY)) {
return "CAST('" + new String((byte[]) value, UTF_8).replaceAll("'", "''") + "' AS VARBINARY)";
} else if (type instanceof VarcharType) {
return "'" + value.toString().replaceAll("'", "''") + "'";
} else {
throw new PrestoException(NOT_SUPPORTED, "Unsupported PrestoType " + type);
}
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class Row method valueFromString.
/**
* Converts the given String into a Java object based on the given Presto type
*
* @param str String to convert
* @param type Presto Type
* @return Java object
* @throws PrestoException If the type is not supported by this function
*/
public static Object valueFromString(String str, Type type) {
if (str == null || str.isEmpty()) {
return null;
} else if (Types.isArrayType(type)) {
Type elementType = Types.getElementType(type);
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
for (String element : Splitter.on(',').split(str)) {
listBuilder.add(valueFromString(element, elementType));
}
return AccumuloRowSerializer.getBlockFromArray(elementType, listBuilder.build());
} else if (Types.isMapType(type)) {
Type keyType = Types.getKeyType(type);
Type valueType = Types.getValueType(type);
ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder();
for (String element : Splitter.on(',').split(str)) {
ImmutableList.Builder<String> builder = ImmutableList.builder();
List<String> keyValue = builder.addAll(Splitter.on("->").split(element)).build();
checkArgument(keyValue.size() == 2, format("Map element %s has %d entries, not 2", element, keyValue.size()));
mapBuilder.put(valueFromString(keyValue.get(0), keyType), valueFromString(keyValue.get(1), valueType));
}
return AccumuloRowSerializer.getBlockFromMap(type, mapBuilder.build());
} else if (type.equals(BIGINT)) {
return Long.parseLong(str);
} else if (type.equals(BOOLEAN)) {
return Boolean.parseBoolean(str);
} else if (type.equals(DATE)) {
return Date.valueOf(LocalDate.parse(str, DATE_PARSER));
} else if (type.equals(DOUBLE)) {
return Double.parseDouble(str);
} else if (type.equals(INTEGER)) {
return Integer.parseInt(str);
} else if (type.equals(REAL)) {
return Float.parseFloat(str);
} else if (type.equals(SMALLINT)) {
return Short.parseShort(str);
} else if (type.equals(TIME)) {
return Time.valueOf(LocalTime.parse(str, TIME_PARSER));
} else if (type.equals(TIMESTAMP)) {
return Timestamp.valueOf(LocalDateTime.parse(str, TIMESTAMP_PARSER));
} else if (type.equals(TINYINT)) {
return Byte.valueOf(str);
} else if (type.equals(VARBINARY)) {
return str.getBytes(UTF_8);
} else if (type instanceof VarcharType) {
return str;
} else {
throw new PrestoException(NOT_SUPPORTED, "Unsupported type " + type);
}
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class PinotPushdownUtils method getLiteralAsString.
// Copied from com.facebook.presto.sql.planner.LiteralInterpreter.evaluate
public static String getLiteralAsString(ConstantExpression node) {
Type type = node.getType();
if (node.getValue() == null) {
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), String.format("Null constant expression %s with value of type %s", node, type));
}
if (type instanceof BooleanType) {
return String.valueOf(((Boolean) node.getValue()).booleanValue());
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
Number number = (Number) node.getValue();
return format("%d", number.longValue());
}
if (type instanceof DoubleType) {
return node.getValue().toString();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return format("%f", intBitsToFloat(number.intValue()));
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(node.getValue() instanceof Long);
return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType).toString();
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType).toString();
}
if (type instanceof VarcharType || type instanceof CharType) {
return "'" + ((Slice) node.getValue()).toStringUtf8() + "'";
}
if (type instanceof TimestampType || type instanceof DateType) {
return node.getValue().toString();
}
if (type instanceof TimestampWithTimeZoneType) {
Long millisUtc = DateTimeEncoding.unpackMillisUtc((Long) node.getValue());
return millisUtc.toString();
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), String.format("Cannot handle the constant expression %s with value of type %s", node, type));
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class RcFileTester method preprocessWriteValueOld.
private static Object preprocessWriteValueOld(Type type, Object value) {
if (value == null) {
return null;
}
if (type.equals(BOOLEAN)) {
return value;
} else if (type.equals(TINYINT)) {
return ((Number) value).byteValue();
} else if (type.equals(SMALLINT)) {
return ((Number) value).shortValue();
} else if (type.equals(INTEGER)) {
return ((Number) value).intValue();
} else if (type.equals(BIGINT)) {
return ((Number) value).longValue();
} else if (type.equals(REAL)) {
return ((Number) value).floatValue();
} else if (type.equals(DOUBLE)) {
return ((Number) value).doubleValue();
} else if (type instanceof VarcharType) {
return value;
} else if (type.equals(VARBINARY)) {
return ((SqlVarbinary) value).getBytes();
} else if (type.equals(DATE)) {
int days = ((SqlDate) value).getDays();
LocalDate localDate = LocalDate.ofEpochDay(days);
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
long millis = zonedDateTime.toEpochSecond() * 1000;
Date date = new Date(0);
// mills must be set separately to avoid masking
date.setTime(millis);
return date;
} else if (type.equals(TIMESTAMP)) {
long millisUtc = (int) ((SqlTimestamp) value).getMillisUtc();
return new Timestamp(millisUtc);
} else if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
} else if (type.getTypeSignature().getBase().equals(ARRAY)) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueOld(elementType, element)).collect(toList());
} else if (type.getTypeSignature().getBase().equals(MAP)) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Map<Object, Object> newMap = new HashMap<>();
for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
newMap.put(preprocessWriteValueOld(keyType, entry.getKey()), preprocessWriteValueOld(valueType, entry.getValue()));
}
return newMap;
} else if (type.getTypeSignature().getBase().equals(ROW)) {
List<?> fieldValues = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
List<Object> newStruct = new ArrayList<>();
for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
newStruct.add(preprocessWriteValueOld(fieldTypes.get(fieldId), fieldValues.get(fieldId)));
}
return newStruct;
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class H2QueryRunner method insertRows.
private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data) {
List<ColumnMetadata> columns = tableMetadata.getColumns().stream().filter(columnMetadata -> !columnMetadata.isHidden()).collect(toImmutableList());
String vars = Joiner.on(',').join(nCopies(columns.size(), "?"));
String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);
RecordCursor cursor = data.cursor();
while (true) {
// insert 1000 rows at a time
PreparedBatch batch = handle.prepareBatch(sql);
for (int row = 0; row < 1000; row++) {
if (!cursor.advanceNextPosition()) {
if (batch.size() > 0) {
batch.execute();
}
return;
}
for (int column = 0; column < columns.size(); column++) {
Type type = columns.get(column).getType();
if (BOOLEAN.equals(type)) {
batch.bind(column, cursor.getBoolean(column));
} else if (BIGINT.equals(type)) {
batch.bind(column, cursor.getLong(column));
} else if (INTEGER.equals(type)) {
batch.bind(column, (int) cursor.getLong(column));
} else if (DOUBLE.equals(type)) {
batch.bind(column, cursor.getDouble(column));
} else if (type instanceof VarcharType) {
batch.bind(column, cursor.getSlice(column).toStringUtf8());
} else if (DATE.equals(type)) {
long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
// H2 expects dates in to be millis at midnight in the JVM timezone
long localMillis = DateTimeZone.UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millisUtc);
batch.bind(column, new Date(localMillis));
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
batch.add();
}
batch.execute();
}
}
Aggregations