Ejemplo n.º 1
0
 internal ColumnReader(ParquetHandle handle, RowGroupReader rowGroupReader, ColumnChunkMetaData columnChunkMetaData, int columnIndex)
 {
     Handle              = handle;
     RowGroupReader      = rowGroupReader;
     ColumnChunkMetaData = columnChunkMetaData;
     ColumnIndex         = columnIndex;
 }
Ejemplo n.º 2
0
 public ByteArrayReaderCache(ColumnChunkMetaData columnChunkMetaData)
 {
     // If dictionary encoding is used, it's worth caching repeated values for byte arrays.
     _map = columnChunkMetaData.Encodings.Any(e => e == Encoding.PlainDictionary || e == Encoding.RleDictionary) &&
            (typeof(TPhysical) == typeof(ByteArray) || typeof(TPhysical) == typeof(FixedLenByteArray))
         ? new Dictionary <TPhysical, TLogical>()
         : null;
 }
Ejemplo n.º 3
0
        internal static ColumnReader Create(IntPtr handle, ColumnChunkMetaData columnChunkMetaData)
        {
            var parquetHandle = new ParquetHandle(handle, ColumnReader_Free);

            try
            {
                var type = ExceptionInfo.Return <PhysicalType>(handle, ColumnReader_Type);

                switch (type)
                {
                case PhysicalType.Boolean:
                    return(new ColumnReader <bool>(parquetHandle, columnChunkMetaData));

                case PhysicalType.Int32:
                    return(new ColumnReader <int>(parquetHandle, columnChunkMetaData));

                case PhysicalType.Int64:
                    return(new ColumnReader <long>(parquetHandle, columnChunkMetaData));

                case PhysicalType.Int96:
                    return(new ColumnReader <Int96>(parquetHandle, columnChunkMetaData));

                case PhysicalType.Float:
                    return(new ColumnReader <float>(parquetHandle, columnChunkMetaData));

                case PhysicalType.Double:
                    return(new ColumnReader <double>(parquetHandle, columnChunkMetaData));

                case PhysicalType.ByteArray:
                    return(new ColumnReader <ByteArray>(parquetHandle, columnChunkMetaData));

                case PhysicalType.FixedLenByteArray:
                    return(new ColumnReader <FixedLenByteArray>(parquetHandle, columnChunkMetaData));

                default:
                    throw new NotSupportedException($"Physical type {type} is not supported");
                }
            }

            catch
            {
                parquetHandle.Dispose();
                throw;
            }
        }
Ejemplo n.º 4
0
 internal ColumnReader(ParquetHandle handle, ColumnChunkMetaData columnChunkMetaData)
 {
     Handle = handle;
     ColumnChunkMetaData = columnChunkMetaData;
 }
 /// <summary>
 /// Return a converter delegate that converts a TPhysical readonly-span to a TLogical span.
 /// </summary>
 /// <returns>
 /// A delegate of type LogicalRead&lt;TLogical, TPhysical&gt;.Converter
 /// </returns>
 /// <param name="columnDescriptor">The descriptor of the column to be converted.</param>
 /// <param name="columnChunkMetaData">The metadata of the column-chunk to be converted.</param>
 public virtual Delegate GetConverter <TLogical, TPhysical>(ColumnDescriptor columnDescriptor, ColumnChunkMetaData columnChunkMetaData)
     where TPhysical : unmanaged
 {
     return(LogicalRead <TLogical, TPhysical> .GetConverter(columnDescriptor, columnChunkMetaData));
 }
Ejemplo n.º 6
0
 public void Dispose()
 {
     ColumnChunkMetaData.Dispose();
     Handle.Dispose();
 }
Ejemplo n.º 7
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}");
        }