Page MenuHomePhabricator

Return actual integers when selecting integer fields from the DB
Open, LowPublic

Description

@ArielGlenn pointed out that the database always returns strings, even when the field type is an integer. This has always been the case. However, I think it's trivially fixable, and would be relatively harmless. There will be a little bit of CPU overhead, and many tests will need to be updated.

The underlying C API mysql_fetch_row() is stringly typed, and both mysqli and the old mysql extensions just return the strings. But mysqli can provide type information for returned fields, so you can use that to conditionally cast them.

Event Timeline

Change 663718 had a related patch set uploaded (by Tim Starling; owner: Tim Starling):
[mediawiki/core@master] [WIP] MySQL: Return integers as integers

https://gerrit.wikimedia.org/r/663718

Sorry, I changed notification settings in phabriactor so I won't get email for subscriber changes. I'll take a look ASAP.

Is there a performance penalty for getting the type info? I'd imagine that at some point we'd send an extra query to get the schema info.

We could also get this info from the abstract schema definition, I suppose

I like the idea in general, I don't know about the underlying mysqli extension but if it can request them in integer (and return as string or integer), it'd reduce considerable network traffic (network saturation between databases was brought up as a concern before IIRC). On the code-level, I'm worried it would break lots of bad assumptions made in the code and cause all sorts of nasty bugs (but once fixed, it'll definitely help prevent future bugs). Is there any big benefit from it beside more predictable code?

We could also get this info from the abstract schema definition, I suppose

That goes against what we have had in the mediawiki for a long time (where the frame of reference is the schema of the database not the schema outlined in the code). It's not that bad to change that assumption but this can lead to problems when there's drifts. For example we changed data type of a field but we haven't deployed it yet to production or all sorts of drifts caused by all random reasons.

Change 663718 abandoned by Tim Starling:

[mediawiki/core@master] [WIP] MySQL: Return integers as integers

Reason:

old

https://gerrit.wikimedia.org/r/663718