filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.2k stars 2.84k forks source link

Summarize Query Bug on PostgreSQL #9177

Closed chrisreedio closed 11 months ago

chrisreedio commented 11 months ago

Package

filament/filament

Package Version

v3.0.80

Laravel Version

v10.28.0

Livewire Version

v3.0.10

PHP Version

v8.2.11

Problem description

When using a custom Summarizer on PostgreSQL, I get the following QueryException:

SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited identifier at or near """"

The generated query is:

SELECT
  sum(price * quantity) AS aggregate
FROM
  (
    SELECT
      *
    FROM
      "invoice_items"
  ) AS ""

You can see that the last line of the generated query is ) AS "".

Possible Solution Identified?

If this pans out, I can submit a PR for this fix.

I looked at https://github.com/filamentphp/filament/blob/62cf9339776b6ba517a510774bfe89feb3bd14fe/packages/tables/src/Columns/Summarizers/Summarizer.php#L108

and

https://github.com/filamentphp/filament/blob/62cf9339776b6ba517a510774bfe89feb3bd14fe/packages/tables/src/Concerns/CanSummarizeRecords.php#L70

When I took the line from CanSummarizeRecords and replaced L108 in Summarizer with it, the problem was resolved.

It seems on PostgreSQL we can't have the second argument of that function be null (the default).

Broken - Summarizer.php - L108

$query = DB::table($query->toBase());

Works - CanSummarizeRecords.php - L70

$query = DB::table($query->toBase(), $query->getModel()->getTable());

Expected behavior

It would display the proper summary such as this example from another project. chrome_f2ReTePhkA

Steps to reproduce

I have a Resource/Model with a quantity and price field/column.

I want to multiply them together for each line in the table to build out the 'total price'.

Tables\Columns\TextColumn::make('price')
    ->money()
    ->summarize(
        Summarizer::make()
            ->label('Total')
            ->money()
            ->using(fn (\Illuminate\Database\Query\Builder $query) => $query->sum(DB::raw('price * quantity')))
    ),

Reproduction repository

https://github.com/chrisreedio/filament-summarize-delim-bug/blob/main/app/Filament/Resources/InvoiceItemResource.php#L74

Relevant log output

No response

danharrin commented 11 months ago

Fixed by #9178.