Skip to content

Commit 79d7c92

Browse files
authored
teach #[pg_cast] to support 3-argument CAST functions (pgcentralfoundation#2119)
CAST functions can have 1 argument or 3 arguments where the latter is the source type plus the type modifier ("typmod") and explicit context for the destination type. This changes `#[pg_cast]` to blindly support any number of arguments on the function to which it's attached and then Postgres will sort it out during DDL playback.
1 parent 129f09f commit 79d7c92

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

pgrx-sql-entity-graph/src/pg_extern/entity/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,6 @@ impl ToSql for PgExternEntity {
482482
}
483483
Err(err) => return Err(err.into()),
484484
};
485-
if self.metadata.arguments.len() != 1 {
486-
return Err(eyre!(
487-
"PG cast function ({}) must have exactly one argument, got {}",
488-
self.name,
489-
self.metadata.arguments.len()
490-
));
491-
}
492-
if self.fn_args.len() != 1 {
493-
return Err(eyre!(
494-
"PG cast function ({}) must have exactly one argument, got {}",
495-
self.name,
496-
self.fn_args.len()
497-
));
498-
}
499485
let source_arg = self
500486
.metadata
501487
.arguments

pgrx-tests/src/tests/pg_cast_tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ mod pg_catalog {
3838
// look, it's just a test, okay?
3939
true
4040
}
41+
42+
// there is a 3-arg version of CAST functions that pass through the user-provided "type modifier"
43+
// and if it's an explicit cast or not. This function itself is enough to test that we can generate
44+
// the proper code for the function
45+
#[pg_cast]
46+
fn testcasttype_to_testcasttype(
47+
i: TestCastType,
48+
_typmod: i32,
49+
_is_explicit: bool,
50+
) -> TestCastType {
51+
i
52+
}
4153
}
4254

4355
#[cfg(any(test, feature = "pg_test"))]

0 commit comments

Comments
 (0)