Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm facing problem with json_decode() #717

Closed
lzerma opened this issue Nov 6, 2014 · 26 comments
Closed

I'm facing problem with json_decode() #717

lzerma opened this issue Nov 6, 2014 · 26 comments

Comments

@lzerma
Copy link

lzerma commented Nov 6, 2014

Hi,

I'm facing problem with Notice: json_decode(): integer overflow detected.

The weird is that in my dev enviroment this work fine but when I put this in the stage enviroment with another hardware and strucuture (AWS) I'm get this error.

The full log is:

"query": {
                  "aggs": {
                     "max_price": {
                        "nested": {
                           "path": "avail.rooms"
                        },
                        "aggs": {
                           "max_price": {
                              "max": {
                                 "field": "avail.rooms.rate.priceavg"
                              }
                           }
                        }
                     },
                     "min_price": {
                        "nested": {
                           "path": "avail.rooms"
                        },
                        "aggs": {
                           "min_price": {
                              "min": {
                                 "field": "avail.rooms.rate.priceavg"
                              }
                           }
                        }
                     },
                     "suppliers_map": {
                        "terms": {
                           "field": "supplier_id",
                           "size": 1000
                        }
                     }
                  },
                  "query": {
                     "filtered": {
                        "filter": {
                           "bool": {
                              "must": [
                                 {
                                    "and": {
                                       "filters": [
                                          {
                                             "term": {
                                                "city_id": 4202
                                             }
                                          },
                                          {
                                             "term": {
                                                "avail.checkin": "2014-12-15"
                                             }
                                          },
                                          {
                                             "term": {
                                                "avail.los": 2
                                             }
                                          },
                                          {
                                             "term": {
                                                "active": true
                                             }
                                          }
                                       ]
                                    }
                                 },
                                 {
                                    "nested": {
                                       "path": "avail.rooms",
                                       "query": {
                                          "filtered": {
                                             "filter": {
                                                "and": {
                                                   "filters": [
                                                      {
                                                         "range": {
                                                            "avail.rooms.capacity": {
                                                               "gte": 1
                                                            }
                                                         }
                                                      },
                                                      {
                                                         "range": {
                                                            "avail.rooms.availmin": {
                                                               "gte": 1
                                                            }
                                                         }
                                                      },
                                                      {
                                                         "range": {
                                                            "avail.rooms.rate.priceavg": {
                                                               "from": "105",
                                                               "to": "2991"
                                                            }
                                                         }
                                                      }
                                                   ]
                                                }
                                             }
                                          }
                                       }
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  },
                  "sort": {
                     "category_order": {
                        "order": "desc"
                     }
                  }
               },
               "error_message": "Notice: json_decode(): integer overflow detected in /home/ota/html/vendor/ruflin/elastica/lib/Elastica/JSON.php line 33",
               "error_code": "Notice: json_decode(): integer overflow detected in /home/ota/html/vendor/ruflin/elastica/lib/Elastica/JSON.php line 33"
            }
```json

You know what I solve this? 

Thank you!
@CrAzE124
Copy link
Contributor

CrAzE124 commented Nov 6, 2014

I'm assuming you're doing something along the lines of this?

$query = new Query();
$query->setSort(array(
    'category_order' => array(
        'order' => 'asc',
    ),
);

The solution for me was to simply do this:

$query = new Query();
$query->setSort(array(
    'category_order' => array(              
        'order' => 'asc',
        'missing' => PHP_INT_MAX -1,
    ),
);

Not sure why this worked or where I came up with the solution, perhaps @ruflin can shed some light on this and whether it's an issue or not? If it is, I'd also be happy to have a look at it.

@ruflin
Copy link
Owner

ruflin commented Nov 6, 2014

@CrAzE124 So far I didn't know about this problem. But I assume you found also the solution here: remicollet/pecl-json-c#13 ?

@CrAzE124
Copy link
Contributor

CrAzE124 commented Nov 7, 2014

@ruflin yeah that's where I found it. It's weird though, I don't really see how or why that solves the problem in terms of the JSON overflow.

@damienalexandre
Copy link
Contributor

It just tell Elasticsearch to use PHP_INT_MAX -1 as missing value for the sort, and not a very very big integer as it does actually. All the sorting values are given back in the query response so this is a problem when PHP try to decode the JSON with the very very big sorting value.

@lzerma
Copy link
Author

lzerma commented Nov 8, 2014

Thanks guys...

I'll try to do this and I'm write here if that solve my problem or not.

Regards!

@ruflin
Copy link
Owner

ruflin commented Nov 11, 2014

I'm not sure if we should handle this "problem" in Elastica itself or should leave it to the engineer to solve?

@CrAzE124
Copy link
Contributor

Is it not possible to loop through the $sortArgs array in the Elastica\Query::setSort() method, and if the missing field is not set for each sort value, just add it in as 'missing' => PHP_INT_MAX - 1? That way if the developer wants to set the missing param to something else, he/she has the flexibility to do so. Just an idea. Not sure if this is the optimal solution.

@ruflin
Copy link
Owner

ruflin commented Nov 11, 2014

@CrAzE124 I think that would work and would prevent the problem. Pull request? ;-)

@damienalexandre
Copy link
Contributor

That means Elastica is going to edit all requests containing a sort to handle this case; that may not be the best thing to do - Elastica should not do stuffs on the queries if I do not ask him.
I like the way they handle this issue in the official PHP client: elastic/elasticsearch-php#146

@json_decode but with proper Exception support.

@CrAzE124
Copy link
Contributor

@damienalexandre I agree with you in that it's a bit of a dirty way to do it, but I do believe it lessens the burden on the developer to add 'missing' => PHP_INT_MAX -1 to every sort if not specifically set. I might be a little bias towards this as I myself struggled to figure out what was causing the error.

We could probably catch that specific overflow exception and alert the user of a possible solution in a "cleaner" way (as in re-throwing a custom error which says something like "Try using 'missing' => PHP_INT_MAX - 1" or whatever). I'll toy around with it a bit later on and do a pull request.

@ruflin
Copy link
Owner

ruflin commented Nov 16, 2014

The solution could also have performance implication. The cleanest one would be the exception option mentioned by @damienalexandre. In the other issue there is also a mention of using JSON_BIGINT_AS_STRING. Does this work?

@demon
Copy link

demon commented Jan 12, 2015

That would work but only in 5.4+ basically.

See for example this implementation where you munge the json after recieving it and if the int is longer than PHP_INT_MAX rewrite it as a string before passing it to json_decode() if you don't have JSON_BIGINT_AS_STRING support.

@ruflin
Copy link
Owner

ruflin commented Jan 13, 2015

As Elastica "officially" only supports 5.4+ this wouldn't be an issue, right?

@ruflin
Copy link
Owner

ruflin commented Jan 13, 2015

We could even introduce this as a "Util" method to keep it compatible with PHP 5.3 and remove it later.

@demon
Copy link

demon commented Jan 20, 2015

It also seems to work around it when jsonc is present unless I'm misreading it. In which case we'd still want it even in 5.4+

@ruflin
Copy link
Owner

ruflin commented Jan 22, 2015

Anyone time for a pull request? ;-)

@yellow1912
Copy link

I also need this as well

Temporarily added this in the parse function to fix it:

// add code to set use JSON_BIGINT_AS_STRING by default
        if (!isset($args[2])) {
            $args[2] = 512;
        }

        if (isset($args[3])) {
            $args[3] = $args[3] | JSON_BIGINT_AS_STRING;
        } else {
            $args[3] = JSON_BIGINT_AS_STRING;
        }

@ruflin
Copy link
Owner

ruflin commented May 11, 2015

@yellow1912 Where exactly did you put these lines in? Which PHP Version are you using?

@yellow1912
Copy link

@ruflin I'm using PHP 5.5.12. The file is JSON.php

Here is the gist: https://gist.github.com/yellow1912/47418d628415599a5123

@ruflin
Copy link
Owner

ruflin commented May 13, 2015

Interesting solution. I actually think we could use exactly that code in Elastica. What do you think?

@mfn
Copy link
Contributor

mfn commented May 22, 2015

I was hit by this issue to but I wouldn't want Elastica to some magic here; I as a developer need to know such corner cases and incompatibilities. The solution is to manually add the missing. I'd suggest to rather improve the documentation (i.e. setSize() method and adding this clause, referencing this issue for example).

@ruflin
Copy link
Owner

ruflin commented Jun 1, 2015

@mfn I understand the concern that Elastica should not "overwrite" potential php settings. At the same time we should allow the developer to also set a default without having to set it for every "setSort". So I would suggest to take the code suggested by @yellow1912 and make it available through a variable in the client which can be set to true. Something like bigintConversion. And of course, the documentation must be improved.

@oldskool
Copy link
Contributor

Experienced the same issue today when trying to sort on a nested field that can also have a null value. Adding the missing key with the PHP_INT_MAX - 1 solved this problem, although @yellow1912's solution seems cleaner.

@ruflin Any idea if and when that solution will be merged/tagged as a release?

@ruflin
Copy link
Owner

ruflin commented Sep 22, 2015

@oldskool Unfortunately so far there is no pull request open for this change. In case we get a pull request it will directly go into the next release. Feel free to open one with the code provided by @yellow1912.

@oldskool
Copy link
Contributor

@ruflin Done, #941

@ruflin
Copy link
Owner

ruflin commented Sep 22, 2015

@oldskool Cool, thanks. See my comments.

ruflin added a commit that referenced this issue Nov 7, 2015
Add JSON_BIGINT_AS_STRING solution for #717
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

No branches or pull requests

8 participants