Skip to content

Commit 18db52d

Browse files
committed
store: ensure consistent id type in FindRangeQuery SQL output
1 parent 03d2d4d commit 18db52d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,19 @@ impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {
18841884
} else {
18851885
self.mut_range.compare_column(&mut out)
18861886
}
1887-
out.push_sql("as block_number, id, vid\n");
1887+
// Cast id to bytea to ensure consistent types across UNION
1888+
// The actual id type can be text, bytea, or numeric depending on the entity
1889+
out.push_sql("as block_number, ");
1890+
let pk_column = table.primary_key();
1891+
1892+
// We only support entity id types of string, bytes, and int8.
1893+
match pk_column.column_type {
1894+
ColumnType::String => out.push_sql("id::bytea"),
1895+
ColumnType::Bytes => out.push_sql("id"),
1896+
ColumnType::Int8 => out.push_sql("id::text::bytea"),
1897+
_ => out.push_sql("id::bytea"),
1898+
}
1899+
out.push_sql(" as id, vid\n");
18881900
out.push_sql(" from ");
18891901
out.push_sql(table.qualified_name.as_str());
18901902
out.push_sql(" e\n where");
@@ -1906,7 +1918,7 @@ impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {
19061918
// In case we have only immutable entities, the upper range will not create any
19071919
// select statement. So here we have to generate an SQL statement thet returns
19081920
// empty result.
1909-
out.push_sql("select 'dummy_entity' as entity, to_jsonb(1) as data, 1 as block_number, 1 as id, 1 as vid where false");
1921+
out.push_sql("select 'dummy_entity' as entity, to_jsonb(1) as data, 1 as block_number, '\\x'::bytea as id, 1 as vid where false");
19101922
} else {
19111923
out.push_sql("\norder by block_number, entity, id");
19121924
}

0 commit comments

Comments
 (0)