Example #1
0
    /// <summary>
    /// Increments the column at the given index, returning false if the
    /// value is already the maximum.
    /// </summary>
    /// <param name="index">The column index to increment.</param>
    internal bool IncrementColumn(int index)
    {
        if (!IsSet(index))
        {
            throw new ArgumentException($"Column index {index} has not been set.");
        }

        ColumnSchema column = Schema.GetColumn(index);

        if (column.IsFixedSize)
        {
            KuduType    type = column.Type;
            Span <byte> data = GetRowAllocColumn(index, column.Size);

            switch (type)
            {
            case KuduType.Bool:
            {
                bool isFalse = data[0] == 0;
                data[0] = 1;
                return(isFalse);
            }

            case KuduType.Int8:
            {
                sbyte existing = KuduEncoder.DecodeInt8(data);
                if (existing == sbyte.MaxValue)
                {
                    return(false);
                }

                KuduEncoder.EncodeInt8(data, (sbyte)(existing + 1));
                return(true);
            }

            case KuduType.Int16:
            {
                short existing = KuduEncoder.DecodeInt16(data);
                if (existing == short.MaxValue)
                {
                    return(false);
                }

                KuduEncoder.EncodeInt16(data, (short)(existing + 1));
                return(true);
            }

            case KuduType.Int32:
            {
                int existing = KuduEncoder.DecodeInt32(data);
                if (existing == int.MaxValue)
                {
                    return(false);
                }

                KuduEncoder.EncodeInt32(data, existing + 1);
                return(true);
            }

            case KuduType.Date:
            {
                int existing = KuduEncoder.DecodeInt32(data);
                if (existing == EpochTime.MaxDateValue)
                {
                    return(false);
                }

                KuduEncoder.EncodeInt32(data, existing + 1);
                return(true);
            }

            case KuduType.Int64:
            case KuduType.UnixtimeMicros:
            {
                long existing = KuduEncoder.DecodeInt64(data);
                if (existing == long.MaxValue)
                {
                    return(false);
                }

                KuduEncoder.EncodeInt64(data, existing + 1);
                return(true);
            }

            case KuduType.Float:
            {
                float existing    = KuduEncoder.DecodeFloat(data);
                float incremented = existing.NextUp();
                if (existing == incremented)
                {
                    return(false);
                }

                KuduEncoder.EncodeFloat(data, incremented);
                return(true);
            }

            case KuduType.Double:
            {
                double existing    = KuduEncoder.DecodeDouble(data);
                double incremented = existing.NextUp();
                if (existing == incremented)
                {
                    return(false);
                }

                KuduEncoder.EncodeDouble(data, incremented);
                return(true);
            }

            case KuduType.Decimal32:
            {
                int existing  = KuduEncoder.DecodeInt32(data);
                int precision = column.TypeAttributes !.Precision.GetValueOrDefault();
                if (existing == DecimalUtil.MaxDecimal32(precision))
                {
                    return(false);
                }

                KuduEncoder.EncodeInt32(data, existing + 1);
                return(true);
            }

            case KuduType.Decimal64:
            {
                long existing  = KuduEncoder.DecodeInt64(data);
                int  precision = column.TypeAttributes !.Precision.GetValueOrDefault();
                if (existing == DecimalUtil.MaxDecimal64(precision))
                {
                    return(false);
                }

                KuduEncoder.EncodeInt64(data, existing + 1);
                return(true);
            }

            case KuduType.Decimal128:
            {
                KuduInt128 existing  = KuduEncoder.DecodeInt128(data);
                int        precision = column.TypeAttributes !.Precision.GetValueOrDefault();
                if (existing == DecimalUtil.MaxDecimal128(precision))
                {
                    return(false);
                }

                KuduEncoder.EncodeInt128(data, existing + 1);
                return(true);
            }

            default:
                throw new Exception($"Unsupported data type {type}");
            }
        }
        else
        {
            // Column is either string, binary, or varchar.
            ReadOnlySpan <byte> data = GetVarLengthColumn(index);
            var incremented          = new byte[data.Length + 1];
            data.CopyTo(incremented);
            WriteBinary(index, incremented);
            return(true);
        }
    }
Example #2
0
    private void WriteFloat(int columnIndex, float value)
    {
        Span <byte> span = GetSpanInRowAllocAndSetBitSet(columnIndex, 4);

        KuduEncoder.EncodeFloat(span, value);
    }