Copilot commented on code in PR #37: URL: https://github.com/apache/iotdb-client-csharp/pull/37#discussion_r2174180331
########## src/Apache.IoTDB/DataStructure/Column.cs: ########## @@ -0,0 +1,324 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +using System; +using System.Linq; + +namespace Apache.IoTDB.DataStructure +{ + public enum ColumnEncoding : byte + { + ByteArray, + Int32Array, + Int64Array, + BinaryArray, + Rle + } + + public class Binary + { + public byte[] Data { get; } + + public Binary(byte[] data) + { + Data = data; + } + } + + public interface Column + { + TSDataType GetDataType(); + ColumnEncoding GetEncoding(); + bool GetBoolean(int position); + int GetInt(int position); + long GetLong(int position); + float GetFloat(int position); + double GetDouble(int position); + Binary GetBinary(int position); + object GetObject(int position); + + bool[] GetBooleans(); + int[] GetInts(); + long[] GetLongs(); + float[] GetFloats(); + double[] GetDoubles(); + Binary[] GetBinaries(); + object[] GetObjects(); + + bool MayHaveNull(); + bool IsNull(int position); + bool[] GetNulls(); + + int GetPositionCount(); + } + + public abstract class BaseColumn : Column + { + public virtual TSDataType GetDataType() => throw new NotSupportedException(); + public virtual ColumnEncoding GetEncoding() => throw new NotSupportedException(); + public virtual bool GetBoolean(int position) => throw new NotSupportedException("GetBoolean not supported"); + public virtual int GetInt(int position) => throw new NotSupportedException("GetInt not supported"); + public virtual long GetLong(int position) => throw new NotSupportedException("GetLong not supported"); + public virtual float GetFloat(int position) => throw new NotSupportedException("GetFloat not supported"); + public virtual double GetDouble(int position) => throw new NotSupportedException("GetDouble not supported"); + public virtual Binary GetBinary(int position) => throw new NotSupportedException("GetBinary not supported"); + public virtual object GetObject(int position) => throw new NotSupportedException("GetObject not supported"); + + public virtual bool[] GetBooleans() => throw new NotSupportedException("GetBooleans not supported"); + public virtual int[] GetInts() => throw new NotSupportedException("GetInts not supported"); + public virtual long[] GetLongs() => throw new NotSupportedException("GetLongs not supported"); + public virtual float[] GetFloats() => throw new NotSupportedException("GetFloats not supported"); + public virtual double[] GetDoubles() => throw new NotSupportedException("GetDoubles not supported"); + public virtual Binary[] GetBinaries() => throw new NotSupportedException("GetBinaries not supported"); + public virtual object[] GetObjects() => throw new NotSupportedException("GetObjects not supported"); + + public virtual bool MayHaveNull() => false; + public virtual bool IsNull(int position) => false; + public virtual bool[] GetNulls() => new bool[GetPositionCount()]; + public abstract int GetPositionCount(); + } + + public abstract class PrimitiveColumn<T> : BaseColumn + { + protected readonly T[] _values; + protected readonly int _arrayOffset; + protected readonly int _positionCount; + protected readonly bool[] _valueIsNull; + + private readonly TSDataType _dataType; + private readonly ColumnEncoding _encoding; + + protected PrimitiveColumn( + TSDataType dataType, + ColumnEncoding encoding, + int arrayOffset, + int positionCount, + bool[] valueIsNull, + T[] values) + { + if (arrayOffset < 0) + throw new ArgumentException("arrayOffset is negative"); + if (positionCount < 0) + throw new ArgumentException("positionCount is negative"); + if (values == null || values.Length - arrayOffset < positionCount) + throw new ArgumentException("values array is too short"); + if (valueIsNull != null && valueIsNull.Length - arrayOffset < positionCount) + throw new ArgumentException("isNull array is too short"); + + _dataType = dataType; + _encoding = encoding; + _arrayOffset = arrayOffset; + _positionCount = positionCount; + _valueIsNull = valueIsNull; + _values = values; + } + + public override TSDataType GetDataType() => _dataType; + public override ColumnEncoding GetEncoding() => _encoding; + + public override bool MayHaveNull() => _valueIsNull != null; + public override bool IsNull(int position) => _valueIsNull?[position + _arrayOffset] ?? false; + public override bool[] GetNulls() + { + if (_valueIsNull == null) + return new bool[_positionCount]; + + return _valueIsNull.Skip(_arrayOffset).Take(_positionCount).ToArray(); + } + + public override int GetPositionCount() => _positionCount; + } + + public class TimeColumn : PrimitiveColumn<long> + { + public TimeColumn(int arrayOffset, int positionCount, long[] values) + : base( + dataType: TSDataType.INT64, + encoding: ColumnEncoding.Int64Array, + arrayOffset: arrayOffset, + positionCount: positionCount, + valueIsNull: null, + values: values) + { } + + public override long GetLong(int position) => _values[position + _arrayOffset]; + public override long[] GetLongs() => _values.Skip(_arrayOffset).Take(_positionCount).ToArray(); + public override object GetObject(int position) => GetLong(position); + public override object[] GetObjects() => GetLongs().Cast<object>().ToArray(); + + public long GetStartTime() => _values[_arrayOffset]; + public long GetEndTime() => _values[_arrayOffset + _positionCount - 1]; + public long[] GetTimes() => _values.Skip(_arrayOffset).Take(_positionCount).ToArray(); + } + + public class BinaryColumn : PrimitiveColumn<Binary> + { + public BinaryColumn(int arrayOffset, int positionCount, bool[] valueIsNull, Binary[] values) + : base( + TSDataType.TEXT, + ColumnEncoding.BinaryArray, + arrayOffset: arrayOffset, + positionCount: positionCount, + valueIsNull: null, + values: values) + { } + + public override Binary GetBinary(int position) => _values[position + _arrayOffset]; + public override Binary[] GetBinaries() => _values.Skip(_arrayOffset).Take(_positionCount).ToArray(); + public override object GetObject(int position) => GetBinary(position); + public override object[] GetObjects() => GetBinaries().Cast<object>().ToArray(); + } + + public class IntColumn : PrimitiveColumn<int> + { + public IntColumn(int arrayOffset, int positionCount, bool[] valueIsNull, int[] values) + : base( Review Comment: Constructors ignore the passed 'valueIsNull' array by hardcoding it to null; this discards null-indicator information. Pass the actual 'valueIsNull' parameter to the base class. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
