Skip to content

Commit 2b1435a

Browse files
committed
store: Defer wrapping of SQL queries until execution
The wrapping with to_jsonb is closely tied to how we run the query
1 parent bd5817f commit 2b1435a

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

store/postgres/src/deployment_store.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ impl DeploymentStore {
292292
conn: &mut PgConnection,
293293
query: &str,
294294
) -> Result<Vec<SqlQueryObject>, QueryExecutionError> {
295+
let query = format!("select to_jsonb(sub.*) as data from ({}) as sub", query);
296+
295297
let query = diesel::sql_query(query);
296298

297299
// Execute the provided SQL query

store/postgres/src/sql/parser.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ impl Parser {
2121
validator.validate_statements(&mut statements)?;
2222

2323
let statement = statements
24-
.get_mut(0)
24+
.get(0)
2525
.ok_or_else(|| anyhow!("No SQL statements found"))?;
2626

27-
let sql = format!(
28-
"select to_jsonb(sub.*) as data from ( {} ) as sub",
29-
statement
30-
);
31-
Ok(sql)
27+
Ok(statement.to_string())
3228
}
3329
}
3430

store/postgres/src/sql/parser_tests.yaml

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,32 @@
77

88
- sql: select symbol, address from token where decimals > 10
99
ok: >
10-
select to_jsonb(sub.*) as data from (
1110
SELECT symbol, address FROM (
1211
SELECT id, address, symbol, name, decimals FROM "sgd0815"."token" WHERE block_range @> 2147483647) AS token
13-
WHERE decimals > 10 ) as sub
12+
WHERE decimals > 10
1413
- sql: >
15-
with tokens as (
16-
select * from (values
17-
('0x0000000000000000000000000000000000000000','ETH','Ethereum',18),
18-
('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48','USDC','USD Coin',6)
19-
) as t(address,symbol,name,decimals)
20-
)
21-
22-
select date, t.symbol, SUM(amount)/pow(10,t.decimals) as amount
23-
from (select
24-
date(to_timestamp(block_timestamp) at time zone 'utc') as date,
25-
token, amount
26-
from swap as sm,
27-
unnest(sm.amounts_in,sm.tokens_in) as smi(amount,token)
28-
union all
29-
select
30-
date(to_timestamp(block_timestamp) at time zone 'utc') as date,
31-
token, amount
32-
from swap as sm,
33-
unnest(sm.amounts_out,sm.tokens_out) as smo(amount,token)) as tp
34-
inner join
35-
tokens as t on t.address = tp.token
36-
group by tp.date, t.symbol, t.decimals
37-
order by tp.date desc, amount desc
14+
with tokens as (
15+
select * from (values
16+
('0x0000000000000000000000000000000000000000','ETH','Ethereum',18),
17+
('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48','USDC','USD Coin',6)
18+
) as t(address,symbol,name,decimals))
19+
select date, t.symbol, SUM(amount)/pow(10,t.decimals) as amount
20+
from (select
21+
date(to_timestamp(block_timestamp) at time zone 'utc') as date,
22+
token, amount
23+
from swap as sm,
24+
unnest(sm.amounts_in,sm.tokens_in) as smi(amount,token)
25+
union all
26+
select
27+
date(to_timestamp(block_timestamp) at time zone 'utc') as date,
28+
token, amount
29+
from swap as sm,
30+
unnest(sm.amounts_out,sm.tokens_out) as smo(amount,token)) as tp
31+
inner join
32+
tokens as t on t.address = tp.token
33+
group by tp.date, t.symbol, t.decimals
34+
order by tp.date desc, amount desc
3835
ok: >
39-
select to_jsonb(sub.*) as data from (
4036
WITH tokens AS (
4137
SELECT * FROM (
4238
VALUES ('0x0000000000000000000000000000000000000000', 'ETH', 'Ethereum', 18),
@@ -54,4 +50,4 @@
5450
UNNEST(sm.amounts_out, sm.tokens_out) AS smo (amount, token)) AS tp
5551
JOIN tokens AS t ON t.address = tp.token
5652
GROUP BY tp.date, t.symbol, t.decimals
57-
ORDER BY tp.date DESC, amount DESC ) as sub
53+
ORDER BY tp.date DESC, amount DESC

0 commit comments

Comments
 (0)