Skip to content

Commit 176a0e6

Browse files
committed
amp: Change naming convention for CTEs
The old convention made queries very hard to read. We now simply demand that users stay away from tables etc. whose name starts with `amp_`
1 parent e8a1210 commit 176a0e6

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

docs/amp-powered-subgraphs.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The minimum spec version for Amp-powered subgraphs is `1.5.0`.
4141
- name: Transfer
4242
file: <IPFS CID of the SQL query file>
4343
```
44+
4445
</details>
4546

4647
### Data source structure
@@ -70,6 +71,7 @@ This is used to assign the subgraph to the appropriate indexing process.
7071
- name: Transfer
7172
file: <IPFS CID of the SQL query file>
7273
```
74+
7375
</details>
7476

7577
### `name`
@@ -97,6 +99,7 @@ This name is used for observability purposes and to identify progress and potent
9799
- name: Transfer
98100
file: <IPFS CID of the SQL query file>
99101
```
102+
100103
</details>
101104

102105
### `network`
@@ -127,6 +130,7 @@ This is used to validate that the SQL queries for this data source produce resul
127130
- name: Transfer
128131
file: <IPFS CID of the SQL query file>
129132
```
133+
130134
</details>
131135

132136
### `source`
@@ -158,6 +162,7 @@ This is used to validate that the SQL queries for this data source only query th
158162
- name: Transfer
159163
file: <IPFS CID of the SQL query file>
160164
```
165+
161166
</details>
162167

163168
### `source.tables`
@@ -185,6 +190,7 @@ This is used to validate that the SQL queries for this data source only query th
185190
- name: Transfer
186191
file: <IPFS CID of the SQL query file>
187192
```
193+
188194
</details>
189195

190196
### Optional `source.address`
@@ -193,7 +199,7 @@ Contains the contract address with which SQL queries in the data source interact
193199

194200
Enables SQL query reuse through `sg_source_address()` calls instead of hard-coding the contract address.
195201
SQL queries resolve `sg_source_address()` calls to this contract address.
196-
202+
197203
<details>
198204
<summary>Example YAML:</summary>
199205

@@ -215,6 +221,7 @@ SQL queries resolve `sg_source_address()` calls to this contract address.
215221
- name: Transfer
216222
file: <IPFS CID of the SQL query file>
217223
```
224+
218225
</details>
219226

220227
### Optional `source.startBlock`
@@ -245,6 +252,7 @@ _When not provided, defaults to block number `0`._
245252
- name: Transfer
246253
file: <IPFS CID of the SQL query file>
247254
```
255+
248256
</details>
249257

250258
### Optional `source.endBlock`
@@ -275,6 +283,7 @@ _When not provided, defaults to the maximum possible block number._
275283
- name: Transfer
276284
file: <IPFS CID of the SQL query file>
277285
```
286+
278287
</details>
279288

280289
### `transformer`
@@ -309,6 +318,7 @@ Represents the version of this transformer. Each version may contain a different
309318
- name: Transfers
310319
file: <IPFS CID of the SQL query file>
311320
```
321+
312322
</details>
313323

314324
### Optional `transformer.abis`
@@ -344,6 +354,7 @@ _When not provided, defaults to an empty list._
344354
- name: Transfer
345355
file: <IPFS CID of the SQL query file>
346356
```
357+
347358
</details>
348359

349360
### `transformer.tables`
@@ -368,6 +379,7 @@ type Block @entity(immutable: true) {
368379
```
369380

370381
**YAML manifest:**
382+
371383
```diff
372384
specVersion: 1.5.0
373385
+ dataSources:
@@ -386,6 +398,7 @@ type Block @entity(immutable: true) {
386398
+ - name: Block
387399
file: <IPFS CID of the SQL query file>
388400
```
401+
389402
</details>
390403

391404
### `transformer.tables[i].query`
@@ -419,6 +432,7 @@ _When not provided, the `file` field is used instead._
419432
- name: Block
420433
+ query: SELECT * FROM "edgeandnode/ethereum_mainnet".blocks;
421434
```
435+
422436
</details>
423437

424438
### `transformer.tables[i].file`
@@ -451,6 +465,7 @@ _When not provided, the `query` field is used instead._
451465
- name: Block
452466
+ file: <IPFS CID of the SQL query file>
453467
```
468+
454469
</details>
455470

456471
### Amp-powered subgraph examples
@@ -460,6 +475,11 @@ https://github.com/edgeandnode/amp-subgraph-examples
460475

461476
## SQL query requirements
462477

478+
### Names
479+
480+
The names of tables, columns, and aliases must not start with `amp_` as this
481+
prefix is reserved for internal use.
482+
463483
### Block numbers
464484

465485
Every SQL query in Amp-powered subgraphs must return the block number for every row.

graph/src/amp/sql/query_builder/block_range_query.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::{
22
collections::BTreeMap,
3-
hash::{BuildHasher, Hash, Hasher},
43
ops::{ControlFlow, RangeInclusive},
54
};
65

7-
use ahash::RandomState;
86
use alloy::primitives::BlockNumber;
97
use sqlparser_latest::ast::{self, VisitMut, VisitorMut};
108

@@ -23,11 +21,8 @@ pub(super) fn new_block_range_query(
2321
block_range: &RangeInclusive<BlockNumber>,
2422
) -> ast::Query {
2523
// CTE names are unique within a SQL query.
26-
// The hasher ensures that CTEs created for block range do not collide with user-defined CTEs.
27-
// Constant seeds ensure consistent block range queries for the same input parameters.
28-
let mut hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher();
2924

30-
let tables_to_ctes_mapping = new_tables_to_ctes_mapping(query, &mut hasher);
25+
let tables_to_ctes_mapping = new_tables_to_ctes_mapping(query);
3126
assert!(!tables_to_ctes_mapping.is_empty());
3227

3328
let mut cte_tables = Vec::with_capacity(tables_to_ctes_mapping.len());
@@ -46,24 +41,18 @@ pub(super) fn new_block_range_query(
4641
let block_range_query = format!(
4742
"WITH {cte_tables}, {source} AS ({query}) SELECT {source}.* FROM {source} ORDER BY {source}.{block_number_column}",
4843
cte_tables = cte_tables.join(", "),
49-
source = format!("source_{}", hasher.finish())
44+
source = format!("amp_src")
5045
);
5146

5247
parse_query(block_range_query).unwrap()
5348
}
5449

5550
/// Creates unique CTE names for every table referenced by the SQL query.
56-
fn new_tables_to_ctes_mapping(
57-
query: &ast::Query,
58-
hasher: &mut impl Hasher,
59-
) -> BTreeMap<TableReference, String> {
51+
fn new_tables_to_ctes_mapping(query: &ast::Query) -> BTreeMap<TableReference, String> {
6052
extract_tables(query)
6153
.into_iter()
62-
.map(|table| {
63-
table.hash(hasher);
64-
65-
(table, format!("block_range_{}", hasher.finish()))
66-
})
54+
.enumerate()
55+
.map(|(idx, table)| (table, format!("amp_br{}", idx + 1)))
6756
.collect()
6857
}
6958

@@ -137,18 +126,18 @@ mod tests {
137126
block_range_query.to_string(),
138127
parse_query(
139128
r#"
140-
WITH block_range_14621009630487609643 AS (
129+
WITH amp_br1 AS (
141130
SELECT * FROM d WHERE _block_num BETWEEN 0 AND 1000000
142131
),
143-
source_14621009630487609643 AS (
144-
SELECT a, b, c FROM block_range_14621009630487609643 AS d
132+
amp_src AS (
133+
SELECT a, b, c FROM amp_br1 AS d
145134
)
146135
SELECT
147-
source_14621009630487609643.*
136+
amp_src.*
148137
FROM
149-
source_14621009630487609643
138+
amp_src
150139
ORDER BY
151-
source_14621009630487609643.b
140+
amp_src.b
152141
"#
153142
)
154143
.unwrap()
@@ -167,21 +156,21 @@ mod tests {
167156
block_range_query.to_string(),
168157
parse_query(
169158
r#"
170-
WITH block_range_14621009630487609643 AS (
159+
WITH amp_br1 AS (
171160
SELECT * FROM d WHERE _block_num BETWEEN 0 AND 1000000
172161
),
173-
block_range_12377422807768256314 AS (
162+
amp_br2 AS (
174163
SELECT * FROM e WHERE _block_num BETWEEN 0 AND 1000000
175164
),
176-
source_12377422807768256314 AS (
177-
SELECT a, b, c FROM block_range_14621009630487609643 AS d JOIN block_range_12377422807768256314 AS e ON e.e = d.d
165+
amp_src AS (
166+
SELECT a, b, c FROM amp_br1 AS d JOIN amp_br2 AS e ON e.e = d.d
178167
)
179168
SELECT
180-
source_12377422807768256314.*
169+
amp_src.*
181170
FROM
182-
source_12377422807768256314
171+
amp_src
183172
ORDER BY
184-
source_12377422807768256314.b
173+
amp_src.b
185174
"#
186175
)
187176
.unwrap()

graph/src/amp/sql/query_builder/context_query.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use ahash::RandomState;
21
use itertools::Itertools;
32
use sqlparser_latest::ast;
43

@@ -19,16 +18,11 @@ pub(super) fn new_context_query<'a>(
1918
context_columns: impl IntoIterator<Item = &'a str>,
2019
) -> ast::Query {
2120
// CTE names are unique within a SQL query.
22-
// The hasher ensures that CTEs created for context do not collide with user-defined CTEs.
23-
// Constant seeds ensure consistent context queries for the same input parameters.
24-
let hasher = RandomState::with_seeds(0, 0, 0, 0);
25-
let query_hash = hasher.hash_one(query);
26-
2721
let context_columns = context_columns.into_iter().collect_vec();
2822
assert!(!context_columns.is_empty());
2923

30-
let context_cte = format!("context_{query_hash}");
31-
let source_cte = format!("source_{query_hash}");
24+
let context_cte = "amp_ctx";
25+
let source_cte = "amp_src";
3226

3327
let context_query = format!(
3428
"

0 commit comments

Comments
 (0)