Beispiel #1
0
    public static byte[] EncodeDefaultValue(ColumnSchema columnSchema, object value)
    {
        var type = columnSchema.Type;

        return(type switch
        {
            KuduType.Int8 => EncodeInt8((sbyte)value),
            KuduType.Int16 => EncodeInt16((short)value),
            KuduType.Int32 => EncodeInt32((int)value),
            KuduType.Int64 => EncodeInt64((long)value),
            KuduType.String => EncodeString((string)value),
            KuduType.Varchar => EncodeString((string)value),
            KuduType.Bool => EncodeBool((bool)value),
            KuduType.Float => EncodeFloat((float)value),
            KuduType.Double => EncodeDouble((double)value),
            KuduType.Binary => (byte[])value,
            KuduType.UnixtimeMicros => EncodeDateTime((DateTime)value),
            KuduType.Date => EncodeDate((DateTime)value),
            KuduType.Decimal32 => EncodeDecimal32(
                (decimal)value,
                columnSchema.TypeAttributes !.Precision.GetValueOrDefault(),
                columnSchema.TypeAttributes.Scale.GetValueOrDefault()),
            KuduType.Decimal64 => EncodeDecimal64(
                (decimal)value,
                columnSchema.TypeAttributes !.Precision.GetValueOrDefault(),
                columnSchema.TypeAttributes.Scale.GetValueOrDefault()),
            KuduType.Decimal128 => EncodeDecimal128(
                (decimal)value,
                columnSchema.TypeAttributes !.Precision.GetValueOrDefault(),
                columnSchema.TypeAttributes.Scale.GetValueOrDefault()),
            _ => throw new Exception($"Unknown data type {type}"),
        });
Beispiel #2
0
        protected override int?GetNumericScale()
        {
            switch (PrecisionSource.GetValueOrDefault())
            {
            case 0:
                return(Precision.GetValueOrDefault());

            // When the precision is set to one (1), the Organization.PricingDecimalPrecision value is used.
            case 1:
                // todo need to support grabbing this info from the organization. For now just always using metadata.
                return(Precision.GetValueOrDefault());

            // When the precision is set to two (2), the TransactionCurrency.CurrencyPrecision value is used.
            case 2:
                // todo: need to grab this information from the transaction currency itself..!
                return(Precision.GetValueOrDefault());

            default:
                return(Precision.GetValueOrDefault());
            }
        }
        protected void OnKindChanged()
        {
            switch (Kind)
            {
            case IndexKind.Hash:
                MinPrecision = 1;
                MaxPrecision = 8;

                if (Precision.GetValueOrDefault(3) > MaxPrecision)
                {
                    Precision = 3;
                }
                break;

            case IndexKind.Range:
                MinPrecision = 1;
                MaxPrecision = 100;
                break;

            case IndexKind.Spatial:
                Precision = null;
                break;
            }
        }
Beispiel #4
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);
        }
    }
Beispiel #5
0
    /// <summary>
    /// Sets the column to the minimum possible value for the column's type.
    /// </summary>
    /// <param name="index">The index of the column to set to the minimum.</param>
    internal void SetMin(int index)
    {
        ColumnSchema column = Schema.GetColumn(index);
        KuduType     type   = column.Type;

        switch (type)
        {
        case KuduType.Bool:
            WriteBool(index, false);
            break;

        case KuduType.Int8:
            WriteSByte(index, sbyte.MinValue);
            break;

        case KuduType.Int16:
            WriteInt16(index, short.MinValue);
            break;

        case KuduType.Int32:
            WriteInt32(index, int.MinValue);
            break;

        case KuduType.Date:
            WriteInt32(index, EpochTime.MinDateValue);
            break;

        case KuduType.Int64:
        case KuduType.UnixtimeMicros:
            WriteInt64(index, long.MinValue);
            break;

        case KuduType.Float:
            WriteFloat(index, float.MinValue);
            break;

        case KuduType.Double:
            WriteDouble(index, double.MinValue);
            break;

        case KuduType.Decimal32:
            WriteInt32(index, DecimalUtil.MinDecimal32(
                           column.TypeAttributes !.Precision.GetValueOrDefault()));
            break;

        case KuduType.Decimal64:
            WriteInt64(index, DecimalUtil.MinDecimal64(
                           column.TypeAttributes !.Precision.GetValueOrDefault()));
            break;

        case KuduType.Decimal128:
        {
            KuduInt128 min = DecimalUtil.MinDecimal128(
                column.TypeAttributes !.Precision.GetValueOrDefault());
            Span <byte> span = GetSpanInRowAllocAndSetBitSet(index, 16);
            KuduEncoder.EncodeInt128(span, min);
            break;
        }

        case KuduType.String:
        case KuduType.Varchar:
            WriteString(index, string.Empty);
            break;

        case KuduType.Binary:
            WriteBinary(index, Array.Empty <byte>());
            break;

        default:
            throw new Exception($"Unsupported data type {type}");
        }
    }
Beispiel #6
0
        private AxisPrintCoordinates GetPrintCoordinates(Size printArea, int offset)
        {
            Size actualyPrintArea = new Size(printArea.Width - offset, printArea.Height);

            AxisPrintCoordinates c = new AxisPrintCoordinates();

            c.offset = offset;

            // minimum X coordinate (starts at zero)
            c.minimumX = Margins.Left;

            // maximum X coordinate
            c.maximumX = actualyPrintArea.Width - Margins.Right;

            // x range
            c.rangeX = c.maximumX - c.minimumX;

            // X offset from index
            c.indexOffsetX = AxisIndexOffsetAmount;

            // minimum Y coordinate
            c.minimumY = (base.RangeStart * actualyPrintArea.Height) + Margins.Top;

            // maximum Y coordinate
            c.maximumY = (base.RangeEnd * actualyPrintArea.Height) - Margins.Bottom;

            // y range
            c.rangeY = c.maximumY - c.minimumY;

            // X coordinate of the vertical axis line (all other y positions should be based off of this value)
            c.axisVerticalLineX = (Position == YAxisPosition.Left) ?
                                  c.maximumX - c.indexOffsetX :
                                  c.indexOffsetX;

            if (c.axisVerticalLineX < MinimumVerticalLineXOffset)
            {
                c.axisVerticalLineX = MinimumVerticalLineXOffset;
            }

            // effective tick widths
            c.smallTickWidth   = SmallTickWidth.HasValue ? SmallTickWidth.Value : DefaultSmallTickWidth;
            c.largeTickWidth   = LargeTickWidth.HasValue ? LargeTickWidth.Value : DefaultLargeTickWidth;
            c.maximumTickWidth = (c.largeTickWidth > c.smallTickWidth) ? c.largeTickWidth : c.smallTickWidth;

            // outermost x coordinate (away from vertical axis line) for all ticks
            c.outermostTickX = (Position == YAxisPosition.Left) ?
                               c.axisVerticalLineX - c.maximumTickWidth :
                               c.axisVerticalLineX + c.maximumTickWidth;

            // actual tick X coordinates
            c.smallTickX = (Position == YAxisPosition.Left) ?
                           c.axisVerticalLineX - c.smallTickWidth :
                           c.axisVerticalLineX;

            c.largeTickX = (Position == YAxisPosition.Left) ?
                           c.axisVerticalLineX - c.largeTickWidth :
                           c.axisVerticalLineX;

            // horizontal margin between tick line and label
            c.tickLineToLabelMargin = -2F;

            c.smallLabelSize = TextRenderer.MeasureText((EffectiveMaximum).ToString(Format), SmallLabelFont);
            c.largeLabelSize = TextRenderer.MeasureText((EffectiveMaximum).ToString(Format), LargeLabelFont);

            c.maximumLablelWidth = (c.smallLabelSize.Width > c.largeLabelSize.Width) ?
                                   c.smallLabelSize.Width :
                                   c.largeLabelSize.Width;

            // all labels are aligned vertically on the same Y coordinate.
            c.labelX = (Position == YAxisPosition.Left) ?
                       c.outermostTickX - c.tickLineToLabelMargin - c.maximumLablelWidth :
                       c.outermostTickX + c.tickLineToLabelMargin;

            var actualTitle = string.IsNullOrEmpty(Unit) ? Name : $"{Name} ({Unit})";

            if (DrawTitleVertically)
            {
                Size horizontalSize = TextRenderer.MeasureText(actualTitle, AxisFont);
                // flip horizontal size to vertical
                c.titleSize = new Size(
                    horizontalSize.Height,
                    horizontalSize.Width);
            }
            else
            {
                c.titleSize = TextRenderer.MeasureText(actualTitle, AxisFont);
            }

            c.titleX = (Position == YAxisPosition.Left) ?
                       c.labelX - c.titleToLabelMargin - c.titleSize.Width :
                       c.labelX + c.titleToLabelMargin;

            c.titleY = c.minimumY + ((c.maximumY - c.minimumY) / 2);

            c.TitleLabel = new TitleLabel()
            {
                Text = actualTitle,
                X    = c.titleX,
                Y    = c.titleY
            };

            float valueRange = Math.Abs(EffectiveMaximum - EffectiveMinimum);
            float largeStep  = LargeStep.HasValue ? LargeStep.Value : (valueRange / 3);
            float smallStep  = SmallStep.HasValue ? SmallStep.Value : (largeStep / 3);

            float startYPosition       = InvertRange ? -EffectiveMaximum : EffectiveMinimum;
            float maxYPosition         = InvertRange ? -EffectiveMinimum : EffectiveMaximum;
            float scalingRangeMinimum  = InvertRange ? -c.maximumY : c.minimumY;
            float scalingRangeMaximum  = InvertRange ? -c.minimumY : c.maximumY;
            float scalingValuesMinimum = InvertRange ? -EffectiveMinimum : EffectiveMinimum;
            float scalingValuesMaximum = InvertRange ? -EffectiveMaximum : EffectiveMaximum;

            for (float i = startYPosition; i < maxYPosition; i += largeStep)
            {
                var largeStepValue = i;
                var largeScaledY   = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                           scalingValuesMinimum, scalingValuesMaximum, largeStepValue);
                c.LargeTicks.Add(new Tick()
                {
                    X     = c.largeTickX,
                    Y     = largeScaledY,
                    Width = LargeTickWidth.GetValueOrDefault(8)
                });
                c.LargeLabels.Add(new TickLabel()
                {
                    X    = c.labelX,
                    Y    = largeScaledY - (LargeLabelFont.SizeInPoints),
                    Text = FormatLabel(Math.Abs(Math.Round(largeStepValue, Precision.GetValueOrDefault(2))))
                });

                for (float x = i + smallStep; x < i + largeStep; x += smallStep)
                {
                    var smallStepValue = x;
                    var smallScaledY   = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                               scalingValuesMinimum, scalingValuesMaximum, smallStepValue);

                    c.SmallTicks.Add(new Tick()
                    {
                        X     = c.smallTickX,
                        Y     = smallScaledY,
                        Width = SmallTickWidth.GetValueOrDefault(4)
                    });

                    c.SmallLabels.Add(new TickLabel()
                    {
                        X    = c.labelX,
                        Y    = smallScaledY - (SmallLabelFont.SizeInPoints),
                        Text = FormatLabel(Math.Abs(Math.Round(smallStepValue, Precision.GetValueOrDefault(2))))
                    });
                }
            }

            var lastScaledY = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                    scalingValuesMinimum, scalingValuesMaximum, scalingValuesMaximum);

            c.LargeTicks.Add(new Tick()
            {
                X     = c.largeTickX,
                Y     = Math.Abs(InvertRange ? scalingRangeMinimum : scalingRangeMaximum),
                Width = LargeTickWidth.GetValueOrDefault(8)
            });
            c.LargeLabels.Add(new TickLabel()
            {
                X    = c.labelX,
                Y    = Math.Abs(InvertRange ? scalingRangeMinimum : scalingRangeMaximum) - (LargeLabelFont.SizeInPoints),
                Text = FormatLabel(Math.Abs(InvertRange ? scalingValuesMinimum : scalingValuesMaximum))
            });

            return(c);
        }
Beispiel #7
0
        protected override int?GetNumericPrecision()
        {
            switch (PrecisionSource.GetValueOrDefault())
            {
            // When the precision is set to zero (0), the MoneyAttributeMetadata.Precision value is used.
            case 0:
                var numericPrecision = Math.Max(MinValue.ToString().Length, MaxValue.ToString().Length) + Precision.GetValueOrDefault();
                return(numericPrecision);

            // When the precision is set to one (1), the Organization.PricingDecimalPrecision value is used.
            case 1:
                // todo need to support grabbing this info from the organization. For now just always using metadata.
                var orgPrecision = Math.Max(MinValue.ToString().Length, MaxValue.ToString().Length) + Precision.GetValueOrDefault();
                return(orgPrecision);

            // When the precision is set to two (2), the TransactionCurrency.CurrencyPrecision value is used.
            case 2:
                // todo: need to grab this information from the transaction currency itself..!
                var currencyPrecision = Math.Max(MinValue.ToString().Length, MaxValue.ToString().Length) + Precision.GetValueOrDefault();
                return(currencyPrecision);

            default:
                var defaultPrecision = Math.Max(MinValue.ToString().Length, MaxValue.ToString().Length) + Precision.GetValueOrDefault();
                return(defaultPrecision);
            }
        }