All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.plugin.hive.util.DecimalUtils Maven / Gradle / Ivy

Go to download

This is a Databricks build of Trino's Hive plugin which includes support for HTTP based transport for it's Hive metastore thrift interface.

The newest version!
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.plugin.hive.util;

import io.trino.spi.type.Int128;
import io.trino.spi.type.Int128Math;
import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;

import static io.trino.spi.type.Decimals.rescale;

public final class DecimalUtils
{
    private DecimalUtils() {}

    public static long getShortDecimalValue(HiveDecimalWritable writable, int columnScale)
    {
        byte[] bytes = writable.getInternalStorage();
        long value = getShortDecimalValue(bytes);
        value = rescale(value, writable.getScale(), columnScale);
        return value;
    }

    public static long getShortDecimalValue(byte[] bytes)
    {
        long value = 0;
        if ((bytes[0] & 0x80) != 0) {
            for (int i = 0; i < 8 - bytes.length; ++i) {
                value |= 0xFFL << (8 * (7 - i));
            }
        }

        for (int i = 0; i < bytes.length; i++) {
            value |= ((long) bytes[bytes.length - i - 1] & 0xFFL) << (8 * i);
        }

        return value;
    }

    public static Int128 getLongDecimalValue(HiveDecimalWritable writable, int columnScale)
    {
        Int128 value = Int128.fromBigEndian(writable.getInternalStorage());
        return Int128Math.rescale(value, columnScale - writable.getScale());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy