Skip to content

Read dscale and round result to that precision. Fixes #1466 - #1915

Merged
roji merged 2 commits into
npgsql:devfrom
StillLearnin:dev
May 7, 2018
Merged

roji merged 2 commits into
npgsql:devfrom
StillLearnin:dev

Conversation

@StillLearnin

Copy link
Copy Markdown
Contributor

No description provided.

@YohDeadfall

Copy link
Copy Markdown
Contributor

Thanks for fixing the issue! Could you add a test or modify Numeric? We need to check that the binary representation of the returned value is the same as specified.

@StillLearnin

Copy link
Copy Markdown
Contributor Author

I'll check into that.

@roji

roji commented May 4, 2018

Copy link
Copy Markdown
Member

I think this is a fix for #1466.

However, I'm a bit concerned about the perf impact of adding a Math.Round() on each and every read - can we have a benchmark for that? I'm pretty sure it's possible to provide a more efficient implementation for dscale (again, see discussion in #1466).

@YohDeadfall

Copy link
Copy Markdown
Contributor

@roji Let's have that fix now and improve performance later. I know you are interested in performance, but there is a much more terrible thing. Just take a look on GetNumericHeader which uses decimal.ToString().

@StillLearnin

Copy link
Copy Markdown
Contributor Author

Less than 1.5 seconds to randomly generate 2.5 Million numbers, divide them by 1000, round them to two decimal places, to me doesn't seem too bad since the rounding will be less than that.

https://dotnetfiddle.net/93lYdb

@StillLearnin

StillLearnin commented May 4, 2018 •

Copy link
Copy Markdown
Contributor Author

However, there is another issue. While this fix is better than the current operation, it could be improved.

Columns of type numeric have a floating scale and this code will work correctly for them.
Column types that specify scale such as numeric(1000,2) should be rounded to the correct scale even if the data is less precise.

For instance:
Given a column type of numeric(1000,2) and a value of 1.2

  • current code returns 1.2000.
  • my fix returns 1.2.
  • the best option would be to return 1.20.

@StillLearnin

StillLearnin commented May 4, 2018 •

Copy link
Copy Markdown
Contributor Author

For what it is worth, dscale is the actual number of places after the decimal point, not the scale of the column. The FieldDescription shows a PostresType of {numeric} even though it is actually numeric(1000,2)

So I'm not sure how we could get the actual column scale currently .

@StillLearnin

Copy link
Copy Markdown
Contributor Author

Could you add a test or modify Numeric? We need to check that the binary representation of the returned value is the same as specified.

I'm having a hard time trying to think of a test case that isn't already covered by the existing tests.

I think this PR is ready to be merged "as is" since it improves the quality of the returned value and fixes a bug, though it might sometimes add about 90ns to the read time of a numeric column. (see updated fiddle)

@roji

roji commented May 5, 2018

Copy link
Copy Markdown
Member

If it's really just 90ns than I guess it should be ok, but I'd really prefer seeing a proper BenchmarkDotNet benchmark (including memory allocations) of before and after rather than a hand-rolled StopWatch-based benchmark.

@StillLearnin I'm not sure I agree that the scale defined on the numeric(precision, scale) datatype should have any impact on how many zeros are tacked on to the decimal value read. The way I understand it, the scale in the datatype definition defines the maximum scale saved in the database, much like the length in varchar(x) defines the maximum length. It doesn't mean that all strings have to be of length X. In other words, I think the value returned should be 1.2 and not 1.20, and that this PR is good (again I'd just like to see a clearer benchmark for perf).

However, you do raise an unrelated but important point, of the PostgresType not expressing the precision/scale, can you please open a separate issue on this?

@StillLearnin

Copy link
Copy Markdown
Contributor Author

Excellent explanation. I'll open a new ticket and look into producing a "proper" benchmark.

@roji

roji commented May 5, 2018

Copy link
Copy Markdown
Member

@StillLearnin ok great. I'm actually fine with this PR, it would just be good to run a specific BenchmarkDotNet benchmark that exercises reading decimals (see the test/Npgsql.Benchmarks project). You don't even have to include the benchmark in the PR, just post the code you used here along with the results (before vs. after), that should be enough to document things.

@YohDeadfall

Copy link
Copy Markdown
Contributor
[Config(typeof(Config))]
public class GetFieldValue
{
    readonly NpgsqlConnection _conn;
    readonly NpgsqlCommand _cmd;
    readonly NpgsqlDataReader _reader;

    public GetFieldValue()
    {
        _conn = BenchmarkEnvironment.OpenConnection();
        _cmd = new NpgsqlCommand("SELECT 1.2::numeric", _conn);
        _reader = _cmd.ExecuteReader();
        _reader.Read();
    }

    [Benchmark]
    public void ReadDecimal() => _reader.GetFieldValue<decimal>(0);

    class Config : ManualConfig
    {
        public Config() => Add(StatisticColumn.OperationsPerSecond);
    }
}
BenchmarkDotNet=v0.10.12, OS=Windows 10.0.17661
Intel Xeon CPU E5-2687W 0 3.10GHz, 2 CPU, 32 logical cores and 16 physical cores
.NET Core SDK=2.1.104
  [Host]     : .NET Core 2.0.6 (Framework 4.6.26212.01), 64bit RyuJIT
  DefaultJob : .NET Core 2.0.6 (Framework 4.6.26212.01), 64bit RyuJIT
Method Mean Error StdDev Median Op/s
ReadDecimal (before) 319.1 ns 23.96 ns 70.65 ns 341.4 ns 3,133,876.4
ReadDecimal (after) 380.8 ns 27.44 ns 80.92 ns 422.0 ns 2,626,301.1

@StillLearnin

Copy link
Copy Markdown
Contributor Author

@YohDeadfall thanks for getting that done for me. I'm crazy busy and hadn't gotten to try it yet. Looks like it's actually less than 90 ns increase so we should be good to go!

@roji
roji self-requested a review May 6, 2018 05:48
@roji

roji commented May 6, 2018

Copy link
Copy Markdown
Member

Looks good, thanks @YohDeadfall. By the way I systematically use MemoryDiagnoser to make sure how many allocations are added, if at all (in this case I'm pretty sure Math.Round() should be zero-allocation).

I guess the only thing missing is a test that demonstrates what this PR does and ensures no regressions occur in the future - that Npgsql returns 1.2 and not 1.2000 or anything else. @StillLearnin if you could add this that would be great, other @YohDeadfall I'm OK with merging.

@StillLearnin

Copy link
Copy Markdown
Contributor Author

Sounds good. I’ll add it today.

@roji roji added the 🪲 bug label May 7, 2018
@roji roji added this to the 4.0 milestone May 7, 2018
@roji
roji merged this pull request into npgsql:dev May 7, 2018
@roji

roji commented May 7, 2018

Copy link
Copy Markdown
Member

Thanks @StillLearnin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants