martin-georgiev / postgresql-for-doctrine

PostgreSQL enhancements for Doctrine. Provides support for advanced data types (json, jssnb, arrays), text search, array operators and jsonb specific functions.

Home Page:https://packagist.org/packages/martin-georgiev/postgresql-for-doctrine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Arithmetic expression inside greatest() leads to infinite parsing loop

sjuvonen opened this issue · comments

I noticed that using an arithmetic operation inside greatest() causes parser to enter an infinite loop.

My doctrine/dbal is at version 2.12.1.

Consider this working example:

$result = $this->doctrine
    ->getManagerForClass(Foo::class)
    ->createQueryBuilder()

      // Here e.g. "sqrt(30) * 100" triggers infinite loop but plain "sqrt(30)" will work just fine.
      ->select('
          greatest(
              sqrt(30) * 100,
              sqrt(11) * 150
          )
          AS score
      ')
      ->from(Foo::class, 'a')
      ->getQuery()
      ->getResult()
      ;

var_dump($result);

My understanding of the parser is limited but I managed to make this behavior stop by overriding $commonNodeMapping in Greatest.php. I have no idea what this will break in exchange...

class Greatest extends BaseVariadicFunction
{
    // Default seems to be 'StringPrimary'.
    protected $commonNodeMapping = 'SimpleArithmeticExpression';

    protected function customiseFunction(): void
    {
        $this->setFunctionPrototype('greatest(%s)');
    }
}

Thanks for reporting this. Do you mind opening a PR with your proposed solution?