Skip to content

Templates are rendered improperly when aggregations are used #326

@gorbak25

Description

@gorbak25

On corrosion commit 85a0aed with the following sql schema

CREATE TABLE foo (
    id INTEGER NOT NULL PRIMARY KEY
);

Templates using aggregation functions like sum or json_group_array render improperly
For ex. with the following rhai template

<%= sql("SELECT sum(foo.id) FROM foo").to_json(#{pretty: true}) %>

The result in an empty database looks like this

[
  {
    "sum(foo.id)": null
  }
]

Then after inserting some values INSERT INTO foo(id) VALUES (1),(2),(3),(4);
the result is

[
  {
    "sum(foo.id)": null
  },
  {
    "sum(foo.id)": 10
  }
]

Then after INSERT INTO foo(id) VALUES (5);
the result is

[
  {
    "sum(foo.id)": null
  },
  {
    "sum(foo.id)": 10
  },
  {
    "sum(foo.id)": 5
  }
]

While I would expect the template to render as

[
  {
    "sum(foo.id)": 15
  }
]

The same issue is present when using json_group_array with the following template <%= sql("SELECT json_group_array(json_object('id', foo.id)) FROM foo").to_json(#{pretty: true}) %>

I would suggest to either disallow aggregations in the query parser or rerun the entire query when new data arrives.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions