graphprotocol / graph-node

Graph Node indexes data from blockchains such as Ethereum and serves it over GraphQL
https://thegraph.com
Apache License 2.0
2.91k stars 974 forks source link

[Bug] Single "count" aggregation fails #5634

Closed YaroShkvorets closed 2 months ago

YaroShkvorets commented 2 months ago

Bug report

If I have a single "count" aggregation:

type Block @entity(timeseries: true) {
  id: Int8!
  timestamp: Timestamp!
  hash: Bytes!
  number: Int!
}

type Stats @aggregation(intervals: ["hour", "day"], source: "Block") {
  id: Int8!
  timestamp: Timestamp!

  count: Int! @aggregate(fn: "count")
}

graph-node fails with invalid SQL expression error:

postgres-defi    | 2024-09-07 20:45:32.788 UTC [91] ERROR:  syntax error at or near "from" at character 205
postgres-defi    | 2024-09-07 20:45:32.788 UTC [91] STATEMENT:  insert into "sgd1"."stats_hour"(id, timestamp, block$, "count") select max(id) as id, timestamp, $3, count(*) as "count" from (select id, date_bin('3600s', timestamp, 'epoch'::timestamptz) as timestamp,  from "sgd1"."block" where "sgd1"."block".timestamp >= $1 and "sgd1"."block".timestamp < $2 order by "sgd1"."block".timestamp) data group by timestamp

Note "," before "from" that shouldn't be there.

Subgraph example: https://github.com/YaroShkvorets/common-subgraphs/tree/main/aggregation-broken

Relevant log output

No response

IPFS hash

No response

Subgraph name or link to explorer

No response

Some information to help us out

OS information

None

YaroShkvorets commented 2 months ago

PR: https://github.com/graphprotocol/graph-node/pull/5639