Example #1
0
        public static Converter GetConverter(LogicalType logicalType, int scale)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return((Converter)(Delegate)(LogicalRead <TPhysical, TPhysical> .Converter)((s, dl, d, nl) => ConvertNative(s, d)));
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return((Converter)(Delegate)(LogicalRead <TPhysical?, TPhysical> .Converter)ConvertNative);
            }

            if (typeof(TLogical) == typeof(sbyte))
            {
                return((Converter)(Delegate)(LogicalRead <sbyte, int> .Converter)((s, dl, d, nl) => ConvertInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(sbyte?))
            {
                return((Converter)(Delegate)(LogicalRead <sbyte?, int> .Converter)ConvertInt8);
            }

            if (typeof(TLogical) == typeof(byte))
            {
                return((Converter)(Delegate)(LogicalRead <byte, int> .Converter)((s, dl, d, nl) => ConvertUInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(byte?))
            {
                return((Converter)(Delegate)(LogicalRead <byte?, int> .Converter)ConvertUInt8);
            }

            if (typeof(TLogical) == typeof(short))
            {
                return((Converter)(Delegate)(LogicalRead <short, int> .Converter)((s, dl, d, nl) => ConvertInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(short?))
            {
                return((Converter)(Delegate)(LogicalRead <short?, int> .Converter)ConvertInt16);
            }

            if (typeof(TLogical) == typeof(ushort))
            {
                return((Converter)(Delegate)(LogicalRead <ushort, int> .Converter)((s, dl, d, nl) => ConvertUInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(ushort?))
            {
                return((Converter)(Delegate)(LogicalRead <ushort?, int> .Converter)ConvertUInt16);
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return((Converter)(Delegate)(LogicalRead <uint, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, uint>(s), d)));
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return((Converter)(Delegate)(LogicalRead <uint?, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, uint>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return((Converter)(Delegate)(LogicalRead <ulong, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, ulong>(s), d)));
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return((Converter)(Delegate)(LogicalRead <ulong?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, ulong>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalRead <decimal, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, d, multiplier)));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalRead <decimal?, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, dl, d, multiplier, nl)));
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return((Converter)(Delegate)(LogicalRead <Date, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, Date>(s), d)));
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return((Converter)(Delegate)(LogicalRead <Date?, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, Date>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(DateTime))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMillis(s, d)));

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos))
            {
                return((Converter)(Delegate)(LogicalRead <DateTimeNanos, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, DateTimeNanos>(s), d)));
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <DateTime?, long> .Converter)ConvertDateTimeMillis);

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <DateTime?, long> .Converter)ConvertDateTimeMicros);

                case TimeUnit.Nanos:
                    return((Converter)(Delegate)(LogicalRead <TPhysical?, TPhysical> .Converter)ConvertNative);
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos?))
            {
                return((Converter)(Delegate)(LogicalRead <DateTimeNanos?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, DateTimeNanos>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                switch (((TimeLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan, int> .Converter)((s, dl, d, nl) => ConvertTimeSpanMillis(s, d)));

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan, long> .Converter)((s, dl, d, nl) => ConvertTimeSpanMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos))
            {
                return((Converter)(Delegate)(LogicalRead <TimeSpanNanos, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, TimeSpanNanos>(s), d)));
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                var timeLogicalType = (TimeLogicalType)logicalType;
                var timeUnit        = timeLogicalType.TimeUnit;

                switch (timeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan?, int> .Converter)ConvertTimeSpanMillis);

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan?, long> .Converter)ConvertTimeSpanMicros);
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos?))
            {
                return((Converter)(Delegate)(LogicalRead <TimeSpanNanos?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, TimeSpanNanos>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(string))
            {
                return((Converter)(Delegate)(LogicalRead <string, ByteArray> .Converter)ConvertString);
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                return((Converter)(Delegate)(LogicalRead <byte[], ByteArray> .Converter)ConvertByteArray);
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }
Example #2
0
        public static Converter GetConverter(LogicalType logicalType, int scale, ByteArrayReaderCache <TPhysical, TLogical> byteArrayCache)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return((Converter)(Delegate)(LogicalRead <TPhysical, TPhysical> .Converter)((s, dl, d, nl) => ConvertNative(s, d)));
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return((Converter)(Delegate)(LogicalRead <TPhysical?, TPhysical> .Converter)ConvertNative);
            }

            if (typeof(TLogical) == typeof(sbyte))
            {
                return((Converter)(Delegate)(LogicalRead <sbyte, int> .Converter)((s, dl, d, nl) => ConvertInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(sbyte?))
            {
                return((Converter)(Delegate)(LogicalRead <sbyte?, int> .Converter)ConvertInt8);
            }

            if (typeof(TLogical) == typeof(byte))
            {
                return((Converter)(Delegate)(LogicalRead <byte, int> .Converter)((s, dl, d, nl) => ConvertUInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(byte?))
            {
                return((Converter)(Delegate)(LogicalRead <byte?, int> .Converter)ConvertUInt8);
            }

            if (typeof(TLogical) == typeof(short))
            {
                return((Converter)(Delegate)(LogicalRead <short, int> .Converter)((s, dl, d, nl) => ConvertInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(short?))
            {
                return((Converter)(Delegate)(LogicalRead <short?, int> .Converter)ConvertInt16);
            }

            if (typeof(TLogical) == typeof(ushort))
            {
                return((Converter)(Delegate)(LogicalRead <ushort, int> .Converter)((s, dl, d, nl) => ConvertUInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(ushort?))
            {
                return((Converter)(Delegate)(LogicalRead <ushort?, int> .Converter)ConvertUInt16);
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return((Converter)(Delegate)(LogicalRead <uint, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, uint>(s), d)));
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return((Converter)(Delegate)(LogicalRead <uint?, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, uint>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return((Converter)(Delegate)(LogicalRead <ulong, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, ulong>(s), d)));
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return((Converter)(Delegate)(LogicalRead <ulong?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, ulong>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalRead <decimal, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, d, multiplier)));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalRead <decimal?, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, dl, d, multiplier, nl)));
            }

            if (typeof(TLogical) == typeof(Guid))
            {
                return((Converter)(Delegate)(LogicalRead <Guid, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertUuid(s, d)));
            }

            if (typeof(TLogical) == typeof(Guid?))
            {
                return((Converter)(Delegate)(LogicalRead <Guid?, FixedLenByteArray> .Converter)ConvertUuid);
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return((Converter)(Delegate)(LogicalRead <Date, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, Date>(s), d)));
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return((Converter)(Delegate)(LogicalRead <Date?, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, Date>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(DateTime))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMillis(s, d)));

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos))
            {
                return((Converter)(Delegate)(LogicalRead <DateTimeNanos, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, DateTimeNanos>(s), d)));
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <DateTime?, long> .Converter)ConvertDateTimeMillis);

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <DateTime?, long> .Converter)ConvertDateTimeMicros);

                case TimeUnit.Nanos:
                    return((Converter)(Delegate)(LogicalRead <TPhysical?, TPhysical> .Converter)ConvertNative);
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos?))
            {
                return((Converter)(Delegate)(LogicalRead <DateTimeNanos?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, DateTimeNanos>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                switch (((TimeLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan, int> .Converter)((s, dl, d, nl) => ConvertTimeSpanMillis(s, d)));

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan, long> .Converter)((s, dl, d, nl) => ConvertTimeSpanMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos))
            {
                return((Converter)(Delegate)(LogicalRead <TimeSpanNanos, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, TimeSpanNanos>(s), d)));
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                var timeLogicalType = (TimeLogicalType)logicalType;
                var timeUnit        = timeLogicalType.TimeUnit;

                switch (timeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan?, int> .Converter)ConvertTimeSpanMillis);

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan?, long> .Converter)ConvertTimeSpanMicros);
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos?))
            {
                return((Converter)(Delegate)(LogicalRead <TimeSpanNanos?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, TimeSpanNanos>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(string))
            {
                return(byteArrayCache.IsUsable
                    ? (Converter)(Delegate)(LogicalRead <string, ByteArray> .Converter)((s, dl, d, nl) => ConvertString(s, dl, d, nl, (ByteArrayReaderCache <ByteArray, string>)(object) byteArrayCache))
                    : (Converter)(Delegate)(LogicalRead <string, ByteArray> .Converter)ConvertString);
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                // Do not reuse byte[] instances, as they are not immutable.
                // Perhaps an optional optimisation if there is demand for it?

                //return byteArrayCache.IsUsable
                //    ? (Converter) (Delegate) (LogicalRead<byte[], ByteArray>.Converter) ((s, dl, d, nl) => ConvertByteArray(s, dl, d, nl, (ByteArrayReaderCache<ByteArray, byte[]>) (object) byteArrayCache))
                //    : (Converter) (Delegate) (LogicalRead<byte[], ByteArray>.Converter) ConvertByteArray;

                return((Converter)(Delegate)(LogicalRead <byte[], ByteArray> .Converter)ConvertByteArray);
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }
Example #3
0
        public static Delegate GetConverter(ColumnDescriptor columnDescriptor, ColumnChunkMetaData columnChunkMetaData)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return(LogicalRead.GetNativeConverter <TPhysical, TPhysical>());
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return(LogicalRead.GetNullableNativeConverter <TPhysical, TPhysical>());
            }

            if (typeof(TLogical) == typeof(sbyte))
            {
                return((LogicalRead <sbyte, int> .Converter)((s, _, d, _) => LogicalRead.ConvertInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(sbyte?))
            {
                return((LogicalRead <sbyte?, int> .Converter)LogicalRead.ConvertInt8);
            }

            if (typeof(TLogical) == typeof(byte))
            {
                return((LogicalRead <byte, int> .Converter)((s, _, d, _) => LogicalRead.ConvertUInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(byte?))
            {
                return((LogicalRead <byte?, int> .Converter)LogicalRead.ConvertUInt8);
            }

            if (typeof(TLogical) == typeof(short))
            {
                return((LogicalRead <short, int> .Converter)((s, _, d, _) => LogicalRead.ConvertInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(short?))
            {
                return((LogicalRead <short?, int> .Converter)LogicalRead.ConvertInt16);
            }

            if (typeof(TLogical) == typeof(ushort))
            {
                return((LogicalRead <ushort, int> .Converter)((s, _, d, _) => LogicalRead.ConvertUInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(ushort?))
            {
                return((LogicalRead <ushort?, int> .Converter)LogicalRead.ConvertUInt16);
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return(LogicalRead.GetNativeConverter <uint, int>());
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return(LogicalRead.GetNullableNativeConverter <uint, int>());
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return(LogicalRead.GetNativeConverter <ulong, long>());
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return(LogicalRead.GetNullableNativeConverter <ulong, long>());
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                var multiplier = Decimal128.GetScaleMultiplier(columnDescriptor.TypeScale);
                return((LogicalRead <decimal, FixedLenByteArray> .Converter)((s, _, d, _) => LogicalRead.ConvertDecimal128(s, d, multiplier)));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                var multiplier = Decimal128.GetScaleMultiplier(columnDescriptor.TypeScale);
                return((LogicalRead <decimal?, FixedLenByteArray> .Converter)((s, dl, d, del) => LogicalRead.ConvertDecimal128(s, dl, d, multiplier, del)));
            }

            if (typeof(TLogical) == typeof(Guid))
            {
                return((LogicalRead <Guid, FixedLenByteArray> .Converter)((s, _, d, _) => LogicalRead.ConvertUuid(s, d)));
            }

            if (typeof(TLogical) == typeof(Guid?))
            {
                return((LogicalRead <Guid?, FixedLenByteArray> .Converter)LogicalRead.ConvertUuid);
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return(LogicalRead.GetNativeConverter <Date, int>());
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return(LogicalRead.GetNullableNativeConverter <Date, int>());
            }

            var logicalType = columnDescriptor.LogicalType;

            if (typeof(TLogical) == typeof(DateTime))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <DateTime, long> .Converter)((s, _, d, _) => LogicalRead.ConvertDateTimeMillis(s, d)));

                case TimeUnit.Micros:
                    return((LogicalRead <DateTime, long> .Converter)((s, _, d, _) => LogicalRead.ConvertDateTimeMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos))
            {
                return(LogicalRead.GetNativeConverter <DateTimeNanos, long>());
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <DateTime?, long> .Converter)LogicalRead.ConvertDateTimeMillis);

                case TimeUnit.Micros:
                    return((LogicalRead <DateTime?, long> .Converter)LogicalRead.ConvertDateTimeMicros);

                case TimeUnit.Nanos:
                    return((LogicalRead <TPhysical?, TPhysical> .Converter)LogicalRead.ConvertNative);
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos?))
            {
                return(LogicalRead.GetNullableNativeConverter <DateTimeNanos, long>());
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                switch (((TimeLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <TimeSpan, int> .Converter)((s, _, d, _) => LogicalRead.ConvertTimeSpanMillis(s, d)));

                case TimeUnit.Micros:
                    return((LogicalRead <TimeSpan, long> .Converter)((s, _, d, _) => LogicalRead.ConvertTimeSpanMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos))
            {
                return(LogicalRead.GetNativeConverter <TimeSpanNanos, long>());
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                var timeLogicalType = (TimeLogicalType)logicalType;
                var timeUnit        = timeLogicalType.TimeUnit;

                switch (timeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <TimeSpan?, int> .Converter)LogicalRead.ConvertTimeSpanMillis);

                case TimeUnit.Micros:
                    return((LogicalRead <TimeSpan?, long> .Converter)LogicalRead.ConvertTimeSpanMicros);
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos?))
            {
                return(LogicalRead.GetNullableNativeConverter <TimeSpanNanos, long>());
            }

            if (typeof(TLogical) == typeof(string))
            {
                var byteArrayCache = new ByteArrayReaderCache <TPhysical, TLogical>(columnChunkMetaData);

                return(byteArrayCache.IsUsable
                    ? (LogicalRead <string?, ByteArray> .Converter)((s, dl, d, del) => LogicalRead.ConvertString(s, dl, d, del, (ByteArrayReaderCache <ByteArray, string>)(object) byteArrayCache))
                    : LogicalRead.ConvertString);
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                // Do not reuse byte[] instances, as they are not immutable.
                // Perhaps an optional optimisation if there is demand for it?

                //return byteArrayCache.IsUsable
                //    ? (LogicalRead<byte[], ByteArray>.Converter) ((s, dl, d, nl) => ConvertByteArray(s, dl, d, nl, (ByteArrayReaderCache<ByteArray, byte[]>) (object) byteArrayCache))
                //    : (LogicalRead<byte[], ByteArray>.Converter) ConvertByteArray;

                return((LogicalRead <byte[]?, ByteArray> .Converter)LogicalRead.ConvertByteArray);
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }
Example #4
0
        public static Delegate GetConverter(ColumnDescriptor columnDescriptor, ByteBuffer? byteBuffer)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return LogicalWrite.GetNativeConverter<TPhysical, TPhysical>();
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return LogicalWrite.GetNullableNativeConverter<TPhysical, TPhysical>();
            }

            if (typeof(TLogical) == typeof(sbyte))
            {
                return (LogicalWrite<sbyte, int>.Converter) ((s, _, d, _) => LogicalWrite.ConvertInt8(s, d));
            }

            if (typeof(TLogical) == typeof(sbyte?))
            {
                return (LogicalWrite<sbyte?, int>.Converter) LogicalWrite.ConvertInt8;
            }

            if (typeof(TLogical) == typeof(byte))
            {
                return (LogicalWrite<byte, int>.Converter) ((s, _, d, _) => LogicalWrite.ConvertUInt8(s, d));
            }

            if (typeof(TLogical) == typeof(byte?))
            {
                return (LogicalWrite<byte?, int>.Converter) LogicalWrite.ConvertUInt8;
            }

            if (typeof(TLogical) == typeof(short))
            {
                return (LogicalWrite<short, int>.Converter) ((s, _, d, _) => LogicalWrite.ConvertInt16(s, d));
            }

            if (typeof(TLogical) == typeof(short?))
            {
                return (LogicalWrite<short?, int>.Converter) LogicalWrite.ConvertInt16;
            }

            if (typeof(TLogical) == typeof(ushort))
            {
                return (LogicalWrite<ushort, int>.Converter) ((s, _, d, _) => LogicalWrite.ConvertUInt16(s, d));
            }

            if (typeof(TLogical) == typeof(ushort?))
            {
                return (LogicalWrite<ushort?, int>.Converter) LogicalWrite.ConvertUInt16;
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return LogicalWrite.GetNativeConverter<uint, int>();
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return LogicalWrite.GetNullableNativeConverter<uint, int>();
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return LogicalWrite.GetNativeConverter<ulong, long>();
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return LogicalWrite.GetNullableNativeConverter<ulong, long>();
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                if (byteBuffer == null) throw new ArgumentNullException(nameof(byteBuffer));
                var multiplier = Decimal128.GetScaleMultiplier(columnDescriptor.TypeScale);
                return (LogicalWrite<decimal, FixedLenByteArray>.Converter) ((s, _, d, _) => LogicalWrite.ConvertDecimal128(s, d, multiplier, byteBuffer));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                if (byteBuffer == null) throw new ArgumentNullException(nameof(byteBuffer));
                var multiplier = Decimal128.GetScaleMultiplier(columnDescriptor.TypeScale);
                return (LogicalWrite<decimal?, FixedLenByteArray>.Converter) ((s, dl, d, nl) => LogicalWrite.ConvertDecimal128(s, dl, d, multiplier, nl, byteBuffer));
            }

            if (typeof(TLogical) == typeof(Guid))
            {
                if (byteBuffer == null) throw new ArgumentNullException(nameof(byteBuffer));
                return (LogicalWrite<Guid, FixedLenByteArray>.Converter) ((s, _, d, _) => LogicalWrite.ConvertUuid(s, d, byteBuffer));
            }

            if (typeof(TLogical) == typeof(Guid?))
            {
                if (byteBuffer == null) throw new ArgumentNullException(nameof(byteBuffer));
                return (LogicalWrite<Guid?, FixedLenByteArray>.Converter) ((s, dl, d, nl) => LogicalWrite.ConvertUuid(s, dl, d, nl, byteBuffer));
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return LogicalWrite.GetNativeConverter<Date, int>();
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return LogicalWrite.GetNullableNativeConverter<Date, int>();
            }

            var logicalType = columnDescriptor.LogicalType;

            if (typeof(TLogical) == typeof(DateTime))
            {
                switch (((TimestampLogicalType) logicalType).TimeUnit)
                {
                    case TimeUnit.Millis:
                        return (LogicalWrite<DateTime, long>.Converter) ((s, _, d, _) => LogicalWrite.ConvertDateTimeMillis(s, d));
                    case TimeUnit.Micros:
                        return (LogicalWrite<DateTime, long>.Converter) ((s, _, d, _) => LogicalWrite.ConvertDateTimeMicros(s, d));
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos))
            {
                return LogicalWrite.GetNativeConverter<DateTimeNanos, long>();
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                switch (((TimestampLogicalType) logicalType).TimeUnit)
                {
                    case TimeUnit.Millis:
                        return (LogicalWrite<DateTime?, long>.Converter) LogicalWrite.ConvertDateTimeMillis;
                    case TimeUnit.Micros:
                        return (LogicalWrite<DateTime?, long>.Converter) LogicalWrite.ConvertDateTimeMicros;
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos?))
            {
                return LogicalWrite.GetNullableNativeConverter<DateTimeNanos, long>();
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                switch (((TimeLogicalType) logicalType).TimeUnit)
                {
                    case TimeUnit.Millis:
                        return (LogicalWrite<TimeSpan, int>.Converter) ((s, _, d, _) => LogicalWrite.ConvertTimeSpanMillis(s, d));
                    case TimeUnit.Micros:
                        return (LogicalWrite<TimeSpan, long>.Converter) ((s, _, d, _) => LogicalWrite.ConvertTimeSpanMicros(s, d));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos))
            {
                return LogicalWrite.GetNativeConverter<TimeSpanNanos, long>();
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                switch (((TimeLogicalType) logicalType).TimeUnit)
                {
                    case TimeUnit.Millis:
                        return (LogicalWrite<TimeSpan?, int>.Converter) LogicalWrite.ConvertTimeSpanMillis;
                    case TimeUnit.Micros:
                        return (LogicalWrite<TimeSpan?, long>.Converter) LogicalWrite.ConvertTimeSpanMicros;
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos?))
            {
                return LogicalWrite.GetNullableNativeConverter<TimeSpanNanos, long>();
            }

            if (typeof(TLogical) == typeof(string))
            {
                if (byteBuffer == null) throw new ArgumentNullException(nameof(byteBuffer));
                return (LogicalWrite<string, ByteArray>.Converter) ((s, dl, d, nl) => LogicalWrite.ConvertString(s, dl, d, nl, byteBuffer));
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                if (byteBuffer == null) throw new ArgumentNullException(nameof(byteBuffer));
                return (LogicalWrite<byte[], ByteArray>.Converter) ((s, dl, d, nl) => LogicalWrite.ConvertByteArray(s, dl, d, nl, byteBuffer));
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }
Example #5
0
        public static Converter GetConverter(LogicalType logicalType, int scale, ByteBuffer byteBuffer)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return((Converter)(Delegate)(LogicalWrite <TPhysical, TPhysical> .Converter)((s, dl, d, nl) => ConvertNative(s, d)));
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return((Converter)(Delegate)(LogicalWrite <TPhysical?, TPhysical> .Converter)ConvertNative);
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return((Converter)(Delegate)(LogicalWrite <uint, int> .Converter)((s, dl, d, nl) => ConvertNative(s, MemoryMarshal.Cast <int, uint>(d))));
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return((Converter)(Delegate)(LogicalWrite <uint?, int> .Converter)((s, dl, d, nl) => ConvertNative(s, dl, MemoryMarshal.Cast <int, uint>(d), nl)));
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return((Converter)(Delegate)(LogicalWrite <ulong, long> .Converter)((s, dl, d, nl) => ConvertNative(s, MemoryMarshal.Cast <long, ulong>(d))));
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return((Converter)(Delegate)(LogicalWrite <ulong?, long> .Converter)((s, dl, d, nl) => ConvertNative(s, dl, MemoryMarshal.Cast <long, ulong>(d), nl)));
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalWrite <decimal, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, d, multiplier, byteBuffer)));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalWrite <decimal?, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, dl, d, multiplier, nl, byteBuffer)));
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return((Converter)(Delegate)(LogicalWrite <Date, int> .Converter)((s, dl, d, nl) => ConvertNative(s, MemoryMarshal.Cast <int, Date>(d))));
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return((Converter)(Delegate)(LogicalWrite <Date?, int> .Converter)((s, dl, d, nl) => ConvertNative(s, dl, MemoryMarshal.Cast <int, Date>(d), nl)));
            }

            if (typeof(TLogical) == typeof(DateTime))
            {
                if (logicalType == LogicalType.TimestampMicros)
                {
                    return((Converter)(Delegate)(LogicalWrite <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMicros(s, d)));
                }

                if (logicalType == LogicalType.TimestampMillis)
                {
                    return((Converter)(Delegate)(LogicalWrite <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMillis(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                if (logicalType == LogicalType.TimestampMicros)
                {
                    return((Converter)(Delegate)(LogicalWrite <DateTime?, long> .Converter)ConvertDateTimeMicros);
                }

                if (logicalType == LogicalType.TimestampMillis)
                {
                    return((Converter)(Delegate)(LogicalWrite <DateTime?, long> .Converter)ConvertDateTimeMillis);
                }
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                if (logicalType == LogicalType.TimeMicros)
                {
                    return((Converter)(Delegate)(LogicalWrite <TimeSpan, long> .Converter)((s, dl, d, nl) => ConvertTimeSpanMicros(s, d)));
                }

                if (logicalType == LogicalType.TimeMillis)
                {
                    return((Converter)(Delegate)(LogicalWrite <TimeSpan, int> .Converter)((s, dl, d, nl) => ConvertTimeSpanMillis(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                if (logicalType == LogicalType.TimeMicros)
                {
                    return((Converter)(Delegate)(LogicalWrite <TimeSpan?, long> .Converter)ConvertTimeSpanMicros);
                }

                if (logicalType == LogicalType.TimeMillis)
                {
                    return((Converter)(Delegate)(LogicalWrite <TimeSpan?, int> .Converter)ConvertTimeSpanMillis);
                }
            }

            if (typeof(TLogical) == typeof(string))
            {
                return((Converter)(Delegate)(LogicalWrite <string, ByteArray> .Converter)((s, dl, d, nl) => ConvertString(s, dl, d, nl, byteBuffer)));
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                return((Converter)(Delegate)(LogicalWrite <byte[], ByteArray> .Converter)((s, dl, d, nl) => ConvertByteArray(s, dl, d, nl, byteBuffer)));
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }