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

io.delta.standalone.internal.expressions.Util Maven / Gradle / Ivy

The newest version!
package io.delta.standalone.internal.expressions;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;

import io.delta.standalone.types.*;

public final class Util {

    public static Comparator createComparator(DataType dataType) {
        if (dataType instanceof IntegerType) {
            return new CastingComparator();
        }

        if (dataType instanceof BooleanType) {
            return new CastingComparator();
        }

        if (dataType instanceof FloatType) {
            return new CastingComparator();
        }

        if (dataType instanceof LongType) {
            return new CastingComparator();
        }

        if (dataType instanceof ByteType) {
            return new CastingComparator();
        }

        if (dataType instanceof ShortType) {
            return new CastingComparator();
        }

        if (dataType instanceof DoubleType) {
            return new CastingComparator();
        }

        if (dataType instanceof DecimalType) {
            return new CastingComparator();
        }

        if (dataType instanceof TimestampType) {
            return new CastingComparator();
        }

        if (dataType instanceof DateType) {
            return new CastingComparator();
        }

        if (dataType instanceof StringType) {
            return new CastingComparator();
        }

        if (dataType instanceof BinaryType) {
            return (o1, o2) -> {
                byte[] one = (byte[]) o1;
                byte[] two = (byte[]) o2;
                int i = 0;
                while (i < one.length && i < two.length) {
                    if (one[i] != two[i]) {
                        return Byte.compare(one[i], two[i]);
                    }
                    i ++;
                }
                return Integer.compare(one.length, two.length);
            };
        }

        // unsupported comparison types: ArrayType, StructType, MapType
        throw new IllegalArgumentException(
            "Couldn't find matching comparator for DataType: " + dataType.getTypeName());
    }
}