From 6f19c0c3cd988f92881874994d8e62670facde3b Mon Sep 17 00:00:00 2001 From: carneiro-cw <156914855+carneiro-cw@users.noreply.github.com> Date: Wed, 17 Apr 2024 18:38:55 -0300 Subject: [PATCH] fix: historical slots query (#618) --- ...639d1b29b7d7b3c4cc0792e1ce67456cd8b36.json | 30 +++++++++++++++++++ ...5ad34c8be567788d9288fe786f67b3be7c8f1.json | 30 ------------------- .../sql/select_historical_slots.sql | 15 ++++++++-- 3 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 .sqlx/query-7a46794bf17b8ae29e3a4774946639d1b29b7d7b3c4cc0792e1ce67456cd8b36.json delete mode 100644 .sqlx/query-8289e1461bf6053c3f2096b595d5ad34c8be567788d9288fe786f67b3be7c8f1.json diff --git a/.sqlx/query-7a46794bf17b8ae29e3a4774946639d1b29b7d7b3c4cc0792e1ce67456cd8b36.json b/.sqlx/query-7a46794bf17b8ae29e3a4774946639d1b29b7d7b3c4cc0792e1ce67456cd8b36.json new file mode 100644 index 000000000..bbe64aafb --- /dev/null +++ b/.sqlx/query-7a46794bf17b8ae29e3a4774946639d1b29b7d7b3c4cc0792e1ce67456cd8b36.json @@ -0,0 +1,30 @@ +{ + "db_name": "PostgreSQL", + "query": "WITH max_block_number AS (\n SELECT idx,\n MAX(block_number) as num\n FROM historical_slots\n WHERE block_number <= $3\n AND account_address = $2\n AND idx = ANY($1)\n GROUP BY idx\n)\nSELECT historical_slots.idx as \"index: _\",\nvalue as \"value: _\"\nFROM historical_slots\n JOIN max_block_number ON max_block_number.idx = historical_slots.idx\n AND max_block_number.num = historical_slots.block_number\n", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "index: _", + "type_info": "Bytea" + }, + { + "ordinal": 1, + "name": "value: _", + "type_info": "Bytea" + } + ], + "parameters": { + "Left": [ + "ByteaArray", + "Bytea", + "Numeric" + ] + }, + "nullable": [ + false, + false + ] + }, + "hash": "7a46794bf17b8ae29e3a4774946639d1b29b7d7b3c4cc0792e1ce67456cd8b36" +} diff --git a/.sqlx/query-8289e1461bf6053c3f2096b595d5ad34c8be567788d9288fe786f67b3be7c8f1.json b/.sqlx/query-8289e1461bf6053c3f2096b595d5ad34c8be567788d9288fe786f67b3be7c8f1.json deleted file mode 100644 index 110b23a49..000000000 --- a/.sqlx/query-8289e1461bf6053c3f2096b595d5ad34c8be567788d9288fe786f67b3be7c8f1.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT idx as \"index: _\", value as \"value: _\"\nFROM historical_slots\nWHERE idx = ANY($1) AND account_address = $2 AND block_number = $3\n", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "index: _", - "type_info": "Bytea" - }, - { - "ordinal": 1, - "name": "value: _", - "type_info": "Bytea" - } - ], - "parameters": { - "Left": [ - "ByteaArray", - "Bytea", - "Numeric" - ] - }, - "nullable": [ - false, - false - ] - }, - "hash": "8289e1461bf6053c3f2096b595d5ad34c8be567788d9288fe786f67b3be7c8f1" -} diff --git a/src/eth/storage/postgres_permanent/sql/select_historical_slots.sql b/src/eth/storage/postgres_permanent/sql/select_historical_slots.sql index 558239741..988b6acb2 100644 --- a/src/eth/storage/postgres_permanent/sql/select_historical_slots.sql +++ b/src/eth/storage/postgres_permanent/sql/select_historical_slots.sql @@ -1,3 +1,14 @@ -SELECT idx as "index: _", value as "value: _" +WITH max_block_number AS ( + SELECT idx, + MAX(block_number) as num + FROM historical_slots + WHERE block_number <= $3 + AND account_address = $2 + AND idx = ANY($1) + GROUP BY idx +) +SELECT historical_slots.idx as "index: _", +value as "value: _" FROM historical_slots -WHERE idx = ANY($1) AND account_address = $2 AND block_number = $3 + JOIN max_block_number ON max_block_number.idx = historical_slots.idx + AND max_block_number.num = historical_slots.block_number