use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.
the class TypeUtils method prestoNativeToJdbcObject.
private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative) {
if (prestoNative == null) {
return null;
}
if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
return prestoNative;
}
if (prestoType instanceof DecimalType) {
DecimalType decimalType = (DecimalType) prestoType;
if (decimalType.isShort()) {
BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
BigInteger unscaledValue = decodeUnscaledValue((Slice) prestoNative);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
if (REAL.equals(prestoType)) {
return intBitsToFloat(toIntExact((long) prestoNative));
}
if (TINYINT.equals(prestoType)) {
return SignedBytes.checkedCast((long) prestoNative);
}
if (SMALLINT.equals(prestoType)) {
return Shorts.checkedCast((long) prestoNative);
}
if (INTEGER.equals(prestoType)) {
return toIntExact((long) prestoNative);
}
if (DATE.equals(prestoType)) {
// convert to midnight in default time zone
long millis = DAYS.toMillis((long) prestoNative);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
}
if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
return ((Slice) prestoNative).toStringUtf8();
}
if (prestoType instanceof ArrayType) {
// process subarray of multi-dimensional array
return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
}
throw new PrestoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.
the class TypeUtils method jdbcObjectToPrestoNative.
private static Object jdbcObjectToPrestoNative(ConnectorSession session, Object jdbcObject, Type prestoType) {
if (jdbcObject == null) {
return null;
}
if (BOOLEAN.equals(prestoType) || TINYINT.equals(prestoType) || SMALLINT.equals(prestoType) || INTEGER.equals(prestoType) || BIGINT.equals(prestoType) || DOUBLE.equals(prestoType)) {
return jdbcObject;
}
if (prestoType instanceof ArrayType) {
return jdbcObjectArrayToBlock(session, ((ArrayType) prestoType).getElementType(), (Object[]) jdbcObject);
}
if (prestoType instanceof DecimalType) {
DecimalType decimalType = (DecimalType) prestoType;
BigDecimal value = (BigDecimal) jdbcObject;
if (decimalType.isShort()) {
return encodeShortScaledValue(value, decimalType.getScale());
}
return encodeScaledValue(value, decimalType.getScale());
}
if (REAL.equals(prestoType)) {
return floatToRawIntBits((float) jdbcObject);
}
if (DATE.equals(prestoType)) {
long localMillis = ((Date) jdbcObject).getTime();
// Convert it to a ~midnight in UTC.
long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
// convert to days
return MILLISECONDS.toDays(utcMillis);
}
if (TIMESTAMP.equals(prestoType)) {
Timestamp timestamp = (Timestamp) jdbcObject;
return timestamp.toLocalDateTime().atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
}
if (prestoType instanceof VarcharType) {
return utf8Slice((String) jdbcObject);
}
if (prestoType instanceof CharType) {
return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", prestoType, jdbcObject.getClass()));
}
use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.
the class MySqlRowExpressionConverter method visitCall.
@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
// remote udf verify
FunctionHandle functionHandle = call.getFunctionHandle();
if (!isDefaultFunction(call)) {
Optional<String> result = mySqlApplyRemoteFunctionPushDown.rewriteRemoteFunction(call, this, context);
if (result.isPresent()) {
return result.get();
}
throw new PrestoException(NOT_SUPPORTED, String.format("MySql connector does not support remote function: %s.%s", call.getDisplayName(), call.getFunctionHandle().getFunctionNamespace()));
}
if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support array constructor");
}
if (standardFunctionResolution.isSubscriptFunction(functionHandle)) {
throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support subscript expression");
}
if (standardFunctionResolution.isCastFunction(functionHandle)) {
// deal with literal, when generic literal expression translate to rowExpression, it will be
// translated to a 'CAST' rowExpression with a varchar type 'CONSTANT' rowExpression, in some
// case, 'CAST' is superfluous
RowExpression argument = call.getArguments().get(0);
Type type = call.getType();
if (argument instanceof ConstantExpression && argument.getType().equals(VARCHAR)) {
String value = argument.accept(this, context);
if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType || type instanceof DecimalType || type instanceof RealType || type instanceof DoubleType) {
return value;
}
}
if (call.getType().getDisplayName().equals(LIKE_PATTERN_NAME)) {
return call.getArguments().get(0).accept(this, context);
}
return getCastExpression(call.getArguments().get(0).accept(this, context), call.getType());
}
return super.visitCall(call, context);
}
use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.
the class TypeUtils method getActualValue.
public static Object getActualValue(Type type, Object value) {
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
return value;
} else if (type instanceof BooleanType) {
return value;
} else if (type instanceof DoubleType) {
return value;
} else if (type instanceof DateType) {
// keep the `long` representation of date
return value;
} else if (type instanceof RealType) {
Long number = (Long) value;
return intBitsToFloat(number.intValue());
} else if (type instanceof VarcharType || type instanceof CharType) {
if (value instanceof Slice) {
return ((Slice) value).toStringUtf8();
}
return value;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(value instanceof Long);
return new BigDecimal(BigInteger.valueOf((Long) value), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
Slice slice;
if (value instanceof String) {
slice = Slices.utf8Slice((String) value);
} else {
checkState(value instanceof Slice);
slice = (Slice) value;
}
return new BigDecimal(decodeUnscaledValue(slice), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
} else if (type instanceof TimestampType) {
Long time = (Long) value;
return new Timestamp(time);
}
throw new UnsupportedOperationException("Not Implemented Exception: " + value + "->" + type);
}
use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.
the class OrcTester method testRow.
// later we can extend for multiple columns
private static boolean testRow(List<Type> types, List<?> values, int row, Map<Integer, TupleDomainFilter> columnFilters) {
for (int column = 0; column < types.size(); column++) {
TupleDomainFilter filter = columnFilters.get(column);
if (filter == null) {
continue;
}
Object value = values.get(row);
if (value == null) {
if (!filter.testNull()) {
return false;
}
} else {
Type type = types.get(column);
if (type == BOOLEAN) {
if (!filter.testBoolean((Boolean) value)) {
return false;
}
} else if (type == BIGINT || type == INTEGER || type == SMALLINT) {
if (!filter.testLong(((Number) value).longValue())) {
return false;
}
} else if (type == DATE) {
if (!filter.testLong(((SqlDate) value).getDays())) {
return false;
}
} else if (type == TIMESTAMP) {
return filter.testLong(((SqlTimestamp) value).getMillis());
} else if (type == VARCHAR) {
return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
} else if (type instanceof CharType) {
String charString = String.valueOf(value);
return filter.testBytes(charString.getBytes(StandardCharsets.UTF_8), 0, charString.length());
} else if (type == VARBINARY) {
byte[] binary = ((SqlVarbinary) value).getBytes();
return filter.testBytes(binary, 0, binary.length);
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
if (decimalType.isShort()) {
return filter.testLong(bigDecimal.unscaledValue().longValue());
}
} else if (type == DOUBLE) {
if (!filter.testDouble((double) value)) {
return false;
}
} else {
fail("Unsupported type: " + type);
}
}
}
return true;
}
Aggregations