Code Monkey home page Code Monkey logo

Comments (9)

bjori avatar bjori commented on August 11, 2024

I thought PHP7 on Windows had 64bit integers?

was does the following result in for you on this configuration:

<?php
$array = ['$set' => ["TestInt" => 4294967295]];
var_dump($array);
?>

from mongo-php-driver.

jacqueswaller avatar jacqueswaller commented on August 11, 2024

PHP7 on Windows does have 64bit integers. The snippet above results in

array(1) { ["$set"]=> array(1) { ["TestInt"]=> int(4294967295) } }

The loss of precision happens when interacting with the Mongo driver, in that it treats every number as Int32.

$inc suffers from the same problem as $set. That is, if I try to [$inc => ["TestInt" => 4294967295]] I will actually subtract 1 from TestInt. Further testing has revealed that queries suffer from the same problem: If I were to make a query with a filter like "TestInt" => ['$lt' => 4294967295], I'll actually be looking for documents where TestInt is less than -1.

Edit: I'm actually using MongoDB driver 1.1.2, now, but it exhibits the same behaviour.

from mongo-php-driver.

jacqueswaller avatar jacqueswaller commented on August 11, 2024

It seems that I lied when I proclaimed that reading an Int64 from the db is fine. It turns out that it comes back as a string and is liable to have its property name mangled.

With the following test document

{ 
    "_id" : ObjectId("569d015eb26caf586174252a"), 
    "TestObj" : {
        "Slots" : NumberLong(-429496729552)
    }
}

if I fetch it in php and then var_dump the result, I get:

object(stdClass)#13 (2) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#11 (1) {
    ["oid"]=>
    string(24) "569d015eb26caf586174252a"
  }
  ["TestObj"]=>
  object(stdClass)#12 (1) {
    ["Slots0"]=>
    string(13) "-429496729552"
  }
}

Notice that "Slots" became "Slots0". The appended characters seem to depend on the combination of property name and integer value. With the right values you can end up with invalid UTF8 :(

from mongo-php-driver.

jmikola avatar jmikola commented on August 11, 2024

I'm tracking both issues in this thread with PHPC-544 and will aim to have a fix for this in the next 1.1.x release.

from mongo-php-driver.

jmikola avatar jmikola commented on August 11, 2024

@jacqueswaller: The second issue you're seeing here (property name corruption) is actually independent of the first. I've diagnosed it in PHPC-592 but will be able to resolve it concurrently with the original serialization issue.

Offhand, were you at all aware that the int64 was being cast to a string by the driver? We actually log a warning, but I fear most users may be oblivious to that unless they've enabled logging. Only error/critical log messages get turned into exceptions. We certainly don't want to throw here, as that would interrupt control flow for users, but perhaps we can compromise by emitting a PHP warning in this case.

from mongo-php-driver.

jacqueswaller avatar jacqueswaller commented on August 11, 2024

Indeed, I did notice that it was coming back as a string, despite running 64-bit php/driver. When I write
print(PHP_INT_SIZE . ", " . PHP_INT_MAX);
I get
8, 9223372036854775807

I haven't done anything special for logging beyond starting mongodb with mongod.exe -logpath C:\MongoDB\logs\mongo.log. I don't see any integer overflow warnings there, though. Unless you're talking about a php log of some kind.

from mongo-php-driver.

jmikola avatar jmikola commented on August 11, 2024

I haven't done anything special for logging beyond starting mongodb with mongod.exe -logpath C:\MongoDB\logs\mongo.log. I don't see any integer overflow warnings there, though. Unless you're talking about a php log of some kind.

@jacqueswaller: I was referring to the mongodb.debug INI option. The C driver (i.e. libmongoc) has its own error reporting, and that INI option allows you to hook into it and capture output. This is far from ideal, as most users aren't going to run their applications with debug logging enabled (assuming they're even aware of the option). Starting in 1.2.0, we hope to raise an exception or PHP warning (see: PHP-313).

Anywho, 1.1.3 has just been released and should include the necessary fix for your issue. I'll leave this open until you can confirm that it resolves your problem. Thanks for your patience on this.

from mongo-php-driver.

jacqueswaller avatar jacqueswaller commented on August 11, 2024

Confirmed: the wrap around issue when using large numbers seems to be fixed. Doing a $set with 4294967295 will set an Int64 in the db, $inc will increment instead of decrementing by 1 and querying $lte with 4294967295 gives the results I expect (instead of returning documents where TestInt <= -1). Similarly, if I then $set over my TestInt with a small number, it converts it to an Int32 in the db. Basically everything behaves the way I'd expect it to 👍

The "returns large ints as a string" and "mangled property name" issues also both seem to be fixed (at least with the test object I provided before). var_dump now gives me

object(stdClass)#15 (2) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#13 (1) {
    ["oid"]=>
    string(24) "569d015eb26caf586174252a"
  }
  ["MiscBin"]=>
  object(stdClass)#14 (1) {
    ["Slots"]=>
    int(-429496729552)
  }
}

as expected.

from mongo-php-driver.

jmikola avatar jmikola commented on August 11, 2024

@jacqueswaller: Great to hear. Thanks for following up 👍

from mongo-php-driver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.