Example #1
0
        public async Task ArrowFlight()
        {
            var flightTicket = new FlightTicket(Sql);
            var stream       = flightClient.GetStream(flightTicket);

            await foreach (var recordBatch in stream.ResponseStream)
            {
                for (int i = 0; i < recordBatch.ColumnCount; i++)
                {
                    var array = recordBatch.Arrays.ElementAt(i);
                    switch (recordBatch.Schema.GetFieldByIndex(i).DataType.TypeId)
                    {
                    case ArrowTypeId.Int64:
                        Int64Array int64Array = (Int64Array)array;
                        Sum(int64Array);
                        break;

                    case ArrowTypeId.String:
                        StringArray stringArray = (StringArray)array;
                        for (int z = 0; z < stringArray.Length; z++)
                        {
                            stringArray.GetString(z);
                        }
                        break;

                    case ArrowTypeId.Timestamp:
                        TimestampArray timestampArray = (TimestampArray)array;

                        for (int z = 0; z < timestampArray.Length; z++)
                        {
                            timestampArray.GetTimestamp(z);
                        }
                        break;
                    }
                }

                //nop
            }
        }
Example #2
0
 public void Visit(TimestampArray array) => CompareArrays(array);
Example #3
0
 public void Visit(TimestampArray array) => CreateBuffers(array);
Example #4
0
        public static Func <int, T> GetGetter <T>(IArrowArray array)
        {
            if (array is null)
            {
                return(null);
            }

            // TODO: determine fastest way to read out a value from the array.

            if (typeof(T) == typeof(bool))
            {
                var booleanArray = new BooleanArray(array.Data);
                return((Func <int, T>)(object) new Func <int, bool>(
                           index => booleanArray.GetBoolean(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(bool?))
            {
                var booleanArray = new BooleanArray(array.Data);
                return((Func <int, T>)(object) new Func <int, bool?>(booleanArray.GetBoolean));
            }

            if (typeof(T) == typeof(sbyte))
            {
                var int8Array = new Int8Array(array.Data);
                return((Func <int, T>)(object) new Func <int, sbyte>(
                           index => int8Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(sbyte?))
            {
                var int8Array = new Int8Array(array.Data);
                return((Func <int, T>)(object) new Func <int, sbyte?>(int8Array.GetValue));
            }

            if (typeof(T) == typeof(byte))
            {
                var uint8Array = new UInt8Array(array.Data);
                return((Func <int, T>)(object) new Func <int, byte>(
                           index => uint8Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(byte?))
            {
                var uint8Array = new UInt8Array(array.Data);
                return((Func <int, T>)(object) new Func <int, byte?>(uint8Array.GetValue));
            }

            if (typeof(T) == typeof(short))
            {
                var int16Array = new Int16Array(array.Data);
                return((Func <int, T>)(object) new Func <int, short>(
                           index => int16Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(short?))
            {
                var int16Array = new Int16Array(array.Data);
                return((Func <int, T>)(object) new Func <int, short?>(int16Array.GetValue));
            }

            if (typeof(T) == typeof(ushort))
            {
                var uint16Array = new UInt16Array(array.Data);
                return((Func <int, T>)(object) new Func <int, ushort>(
                           index => uint16Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(ushort?))
            {
                var uint16Array = new UInt16Array(array.Data);
                return((Func <int, T>)(object) new Func <int, ushort?>(uint16Array.GetValue));
            }

            if (typeof(T) == typeof(int))
            {
                var int32Array = new Int32Array(array.Data);
                return((Func <int, T>)(object) new Func <int, int>(
                           index => int32Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(int?))
            {
                var int32Array = new Int32Array(array.Data);
                return((Func <int, T>)(object) new Func <int, int?>(int32Array.GetValue));
            }

            if (typeof(T) == typeof(uint))
            {
                var uint32Array = new UInt32Array(array.Data);
                return((Func <int, T>)(object) new Func <int, uint>(
                           index => uint32Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(uint?))
            {
                var uint32Array = new UInt32Array(array.Data);
                return((Func <int, T>)(object) new Func <int, uint?>(uint32Array.GetValue));
            }

            if (typeof(T) == typeof(long))
            {
                var int64Array = new Int64Array(array.Data);
                return((Func <int, T>)(object) new Func <int, long>(
                           index => int64Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(long?))
            {
                var int64Array = new Int64Array(array.Data);
                return((Func <int, T>)(object) new Func <int, long?>(int64Array.GetValue));
            }

            if (typeof(T) == typeof(ulong))
            {
                var uint64Array = new UInt64Array(array.Data);
                return((Func <int, T>)(object) new Func <int, ulong>(
                           index => uint64Array.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(ulong?))
            {
                var uint64Array = new UInt64Array(array.Data);
                return((Func <int, T>)(object) new Func <int, ulong?>(uint64Array.GetValue));
            }

            if (typeof(T) == typeof(double))
            {
                var doubleArray = new DoubleArray(array.Data);
                return((Func <int, T>)(object) new Func <int, double>(
                           index => doubleArray.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(double?))
            {
                var doubleArray = new DoubleArray(array.Data);
                return((Func <int, T>)(object) new Func <int, double?>(doubleArray.GetValue));
            }

            if (typeof(T) == typeof(float))
            {
                var floatArray = new FloatArray(array.Data);
                return((Func <int, T>)(object) new Func <int, float>(
                           index => floatArray.GetValue(index).GetValueOrDefault()));
            }
            if (typeof(T) == typeof(float?))
            {
                var floatArray = new FloatArray(array.Data);
                return((Func <int, T>)(object) new Func <int, float?>(floatArray.GetValue));
            }

            if (typeof(T) == typeof(DateTime))
            {
                if (array.Data.DataType.TypeId == ArrowTypeId.Date32)
                {
                    var date32Array = new Date32Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTime>(
                               index => date32Array.GetDate(index).GetValueOrDefault().DateTime));
                }
                else if (array.Data.DataType.TypeId == ArrowTypeId.Date64)
                {
                    var date64Array = new Date64Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTime>(
                               index => date64Array.GetDate(index).GetValueOrDefault().DateTime));
                }
            }
            if (typeof(T) == typeof(DateTime?))
            {
                if (array.Data.DataType.TypeId == ArrowTypeId.Date32)
                {
                    var date32Array = new Date32Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTime?>(
                               index => date32Array.GetDate(index)?.DateTime));
                }
                else if (array.Data.DataType.TypeId == ArrowTypeId.Date64)
                {
                    var date64Array = new Date64Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTime?>(
                               index => date64Array.GetDate(index)?.DateTime));
                }
            }

            if (typeof(T) == typeof(DateTimeOffset))
            {
                if (array.Data.DataType.TypeId == ArrowTypeId.Date32)
                {
                    var date32Array = new Date32Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTimeOffset>(
                               index => date32Array.GetDate(index).GetValueOrDefault()));
                }
                else if (array.Data.DataType.TypeId == ArrowTypeId.Date64)
                {
                    var date64Array = new Date64Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTimeOffset>(
                               index => date64Array.GetDate(index).GetValueOrDefault()));
                }
            }
            if (typeof(T) == typeof(DateTimeOffset?))
            {
                if (array.Data.DataType.TypeId == ArrowTypeId.Date32)
                {
                    var date32Array = new Date32Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTimeOffset?>(
                               date32Array.GetDate));
                }
                else if (array.Data.DataType.TypeId == ArrowTypeId.Date64)
                {
                    var date64Array = new Date64Array(array.Data);
                    return((Func <int, T>)(object) new Func <int, DateTimeOffset?>(
                               date64Array.GetDate));
                }
            }

            if (typeof(T) == typeof(TimeSpan))
            {
                var timestampArray = new TimestampArray(array.Data);
                return((Func <int, T>)(object) new Func <int, TimeSpan>(
                           index => timestampArray.GetTimestamp(index).GetValueOrDefault().TimeOfDay));
            }
            if (typeof(T) == typeof(TimeSpan?))
            {
                var timestampArray = new TimestampArray(array.Data);
                return((Func <int, T>)(object) new Func <int, TimeSpan?>(
                           index => timestampArray.GetTimestamp(index)?.TimeOfDay));
            }

            if (typeof(T) == typeof(byte[]))
            {
                var binaryArray = new BinaryArray(array.Data);
                return((Func <int, T>)(object) new Func <int, byte[]>(
                           // TODO: how to avoid this allocation/copy?
                           index => binaryArray.GetBytes(index).ToArray()));
            }

            if (typeof(T) == typeof(string))
            {
                var stringArray = new StringArray(array.Data);
                return((Func <int, T>)(object) new Func <int, string>(
                           index => stringArray.GetString(index)));
            }

            // It's something else we don't yet support.
            switch (array.Data.DataType.TypeId)
            {
            case ArrowTypeId.Decimal:
            case ArrowTypeId.Dictionary:
            case ArrowTypeId.FixedSizedBinary:
            case ArrowTypeId.HalfFloat:
            case ArrowTypeId.Interval:
            case ArrowTypeId.List:
            case ArrowTypeId.Map:
            case ArrowTypeId.Null:
            case ArrowTypeId.Struct:
            case ArrowTypeId.Time32:
            case ArrowTypeId.Time64:
            case ArrowTypeId.Union:
            default:
                // TODO: support additional types?
                throw new NotSupportedException(
                          $"Not supported array type: {array.Data.DataType.TypeId}");
            }
        }
Example #5
0
    public int pushRecord_i()
    {
        // reset the position of the buffer and do a full batch read;
        outputBuffer.Position = 0;

        RecordBatch currentBatch = reader.ReadNextRecordBatch();

        for (int i = 0; i < currentBatch.Length; i++)
        {
            var newInputRow = new object[this.schema.Ordinals.Count];

            for (int j = 0; j < this.schema.Ordinals.Count; j++)
            {
                var array_j = currentBatch.Arrays.ElementAt(j);
                switch (array_j.Data.DataType.TypeId)
                {
                case ArrowTypeId.Int32:
                    newInputRow[j] = (Int64)(((Int32Array)array_j).Values[i]);
                    break;

                case ArrowTypeId.Int64:
                    newInputRow[j] = ((Int64Array)array_j).Values[i];
                    break;

                case ArrowTypeId.Float:
                    newInputRow[j] = (Double)(((FloatArray)array_j).Values[i]);
                    break;

                case ArrowTypeId.Double:
                    newInputRow[j] = ((DoubleArray)array_j).Values[i];
                    break;

                case ArrowTypeId.String:
                    newInputRow[j] = ((StringArray)array_j).GetString(i);
                    break;

                case ArrowTypeId.Timestamp:
                    TimestampArray tArray         = (TimestampArray)array_j;
                    var            type           = (TimestampType)tArray.Data.DataType;
                    double         timeStampMilli = tArray.Values[i] / MillisecToTickRatio;
                    DateTime       dtDateTime     = epoch.AddMilliseconds(timeStampMilli);
                    newInputRow[j] = dtDateTime;
                    break;

                case ArrowTypeId.Binary:
                    newInputRow[j] = ((BinaryArray)array_j).GetBytes(i).ToArray();
                    break;

                case ArrowTypeId.Boolean:
                    newInputRow[j] = ((BooleanArray)array_j).GetBoolean(i);
                    break;

                default:
                    throw new Exception("Unsupported Arrow array type: " + array_j.Data.DataType.TypeId);
                }
            }

            this.input.OnNext(Record.Create(this.schema, newInputRow));
        }

        //Write outputs if there is any
        if (this.outputRecords.Count > 0)
        {
            List <IRecord> rows = new List <IRecord>();
            while (this.outputRecords.Count > 0)
            {
                rows.Add(this.outputRecords.Dequeue());
            }

            var recordBatch = createOutputRecordBatch(rows);
            WriteRecordBatch(recordBatch);

            return(recordBatch.Length);
        }

        return(0);
    }