use of io.prestosql.spi.type.DecimalType in project carbondata by apache.
the class DecimalSliceStreamReader method decimalBlockWriter.
private void decimalBlockWriter(BigDecimal value) {
if (isShortDecimal(type)) {
long rescaledDecimal = Decimals.rescale(value.unscaledValue().longValue(), value.scale(), ((DecimalType) type).getScale());
type.writeLong(builder, rescaledDecimal);
} else {
Slice slice = getSlice(value, type);
type.writeSlice(builder, parseSlice((DecimalType) type, slice, slice.length()));
}
}
use of io.prestosql.spi.type.DecimalType 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.DecimalType 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.DecimalType in project hetu-core by openlookeng.
the class MySqlClient method getColumns.
@SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session));
PreparedStatement statement = connection.prepareStatement(sql)) {
ResultSetMetaData metaData = statement.getMetaData();
ImmutableMap.Builder<String, ColumnHandle> columnBuilder = new ImmutableMap.Builder<>();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnName = metaData.getColumnLabel(i);
String typeName = metaData.getColumnTypeName(i);
int precision = metaData.getPrecision(i);
int dataType = metaData.getColumnType(i);
int scale = metaData.getScale(i);
// extracted from logical plan during pre-processing
if (dataType == Types.DECIMAL && (precision > MAX_PRECISION || scale < 0)) {
// Covert MySql Decimal type to Presto type
Type type = types.get(columnName.toLowerCase(ENGLISH));
if (type instanceof AbstractType) {
TypeSignature signature = type.getTypeSignature();
typeName = signature.getBase().toUpperCase(ENGLISH);
dataType = JDBCType.valueOf(typeName).getVendorTypeNumber();
if (type instanceof DecimalType) {
precision = ((DecimalType) type).getPrecision();
scale = ((DecimalType) type).getScale();
}
}
}
boolean isNullable = metaData.isNullable(i) != ResultSetMetaData.columnNoNulls;
JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
Optional<ColumnMapping> columnMapping;
try {
columnMapping = toPrestoType(session, connection, typeHandle);
} catch (UnsupportedOperationException ex) {
throw new PrestoException(JDBC_UNSUPPORTED_EXPRESSION, format("Data type [%s] is not support", typeHandle.getJdbcTypeName()));
}
// skip unsupported column types
if (columnMapping.isPresent()) {
Type type = columnMapping.get().getType();
JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullable);
columnBuilder.put(columnName.toLowerCase(ENGLISH), handle);
} else {
return Collections.emptyMap();
}
}
return columnBuilder.build();
} catch (SQLException | PrestoException e) {
throw new PrestoException(JDBC_QUERY_GENERATOR_FAILURE, String.format("Query generator failed for [%s]", e.getMessage()));
}
}
use of io.prestosql.spi.type.DecimalType 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);
}
Aggregations