Repository: mathesar-foundation/mathesar

Issue:
Support for `max` aggregation function while summarizing columns
## Problem
Currently, two aggregation functions (**list & count**) are supported as transformation steps while summarizing columns. Besides, there are many useful aggregation functions and `max` is one of them.

## Proposed solution
It would be great to support `max` as a transformation step while summarizing columns.

## Additional context
This feature request is a part of GSoC '23 project, [Add More Summarization Functions](https://summerofcode.withgoogle.com/programs/2023/projects/D9XvkQCN).



Discussion:

Resolving patch:
diff --git a/db/functions/base.py b/db/functions/base.py
--- a/db/functions/base.py
+++ b/db/functions/base.py
@@ -18,7 +18,7 @@
 from sqlalchemy import column, not_, and_, or_, func, literal, cast, distinct, INTEGER
 from sqlalchemy.dialects.postgresql import array_agg, TEXT, array
 from sqlalchemy.sql import quoted_name
-from sqlalchemy.sql.functions import GenericFunction, concat, percentile_disc, mode
+from sqlalchemy.sql.functions import GenericFunction, concat, percentile_disc, mode, max
 
 from db.engine import get_dummy_engine
 from db.functions import hints
@@ -383,6 +383,18 @@ def to_sa_expression(column_expr):
         return sa_call_sql_function('count', column_expr, return_type=PostgresType.INTEGER)
 
 
+class Max(DBFunction):
+    id = 'max'
+    name = 'max'
+    hints = tuple([
+        hints.aggregation,
+    ])
+
+    @staticmethod
+    def to_sa_expression(column_expr):
+        return max(column_expr)
+
+
 class Mode(DBFunction):
     id = 'mode'
     name = 'mode'
diff --git a/mathesar/models/query.py b/mathesar/models/query.py
--- a/mathesar/models/query.py
+++ b/mathesar/models/query.py
@@ -6,7 +6,7 @@
 from db.transforms.operations.deserialize import deserialize_transformation
 from db.transforms.operations.serialize import serialize_transformation
 from db.transforms.base import Summarize
-from db.functions.base import Count, ArrayAgg, Sum, Median, Mode, Percentage_True
+from db.functions.base import Count, ArrayAgg, Sum, Median, Mode, Percentage_True, Max
 from db.functions.packed import DistinctArrayAgg
 
 from mathesar.api.exceptions.query_exceptions.exceptions import DeletedColumnAccess
@@ -420,6 +420,7 @@ def _get_default_display_name_for_agg_output_alias(
             ArrayAgg.id: " list",
             Count.id: " count",
             Sum.id: " sum",
+            Max.id: " max",
             Median.id: " median",
             Mode.id: " mode",
             Percentage_True.id: " percentage true"


Test patch:
diff --git a/mathesar/tests/api/query/test_aggregation_functions.py b/mathesar/tests/api/query/test_aggregation_functions.py
--- a/mathesar/tests/api/query/test_aggregation_functions.py
+++ b/mathesar/tests/api/query/test_aggregation_functions.py
@@ -357,6 +357,68 @@ def test_mode_aggregation(library_ma_tables, get_uid, client):
     assert sorted(actual_records, key=lambda x: x['Checkout Month']) == expect_records
 
 
+def test_max_aggregation(library_ma_tables, get_uid, client):
+    _ = library_ma_tables
+    checkouts = {
+        t["name"]: t for t in client.get("/api/db/v0/tables/").json()["results"]
+    }["Checkouts"]
+    columns = {
+        c["name"]: c for c in checkouts["columns"]
+    }
+    request_data = {
+        "name": get_uid(),
+        "base_table": checkouts["id"],
+        "initial_columns": [
+            {"id": columns["Checkout Time"]["id"], "alias": "Checkout Time"},
+            {"id": columns["Patron"]["id"], "alias": "Patron"},
+        ],
+        "display_names": {
+            "Checkout Month": "Month",
+            "Max": "Max of patron",
+        },
+        "display_options": {
+            "Checkout Time": {
+                display_option_origin: "Checkout Time",
+            },
+            "Patron": {
+                display_option_origin: "Patron",
+            },
+        },
+        "transformations": [
+            {
+                "spec": {
+                    "grouping_expressions": [
+                        {
+                            "input_alias": "Checkout Time",
+                            "output_alias": "Checkout Month",
+                            "preproc": "truncate_to_month",
+                        }
+                    ],
+                    "aggregation_expressions": [
+                        {
+                            "input_alias": "Patron",
+                            "output_alias": "Max",
+                            "function": "max",
+                        }
+                    ]
+                },
+                "type": "summarize",
+            }
+        ]
+    }
+    response = client.post('/api/db/v0/queries/', data=request_data)
+    assert response.status_code == 201
+    query_id = response.json()['id']
+    expect_records = [
+        {'Checkout Month': '2022-05', 'Max': 29},
+        {'Checkout Month': '2022-06', 'Max': 27},
+        {'Checkout Month': '2022-07', 'Max': 29},
+        {'Checkout Month': '2022-08', 'Max': 29},
+    ]
+    actual_records = client.get(f'/api/db/v0/queries/{query_id}/records/').json()['results']
+    assert sorted(actual_records, key=lambda x: x['Checkout Month']) == expect_records
+
+
 def test_percentage_true_aggregation(payments_ma_table, get_uid, client):
     _ = payments_ma_table
     payments = {