Skip to content

Commit 6bd5aa0

Browse files
committed
Example: 4 cross-type operators from one function
The SQL generator can infer it all except what name to give the synthetic negator function. The human has to choose that.
1 parent 2ec0b3e commit 6bd5aa0

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

‎pljava-examples/src/main/java/org/postgresql/pljava/example/annotation/ComplexScalar.java‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2015 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2004-2021 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -242,6 +242,41 @@ public static boolean componentsEQ(ComplexScalar a, ComplexScalar b)
242242
return a.m_x == b.m_x && a.m_y == b.m_y;
243243
}
244244

245+
/**
246+
* True if the complex argument is real-valued and equal to the real
247+
* argument.
248+
*<p>
249+
* From one equality method on (complex,double) can be synthesized all four
250+
* cross-type operators, {@code =} and {@code <>} for that pair of types and
251+
* their {@code TWIN} commutators. One of the {@code <>} twins does need to
252+
* specify what its synthetic function should be named.
253+
*/
254+
@Operator(
255+
name = "javatest.=",
256+
commutator = TWIN, negator = "javatest.<>",
257+
provides = "complex:double relationals"
258+
)
259+
@Operator(
260+
name = "javatest.=",
261+
synthetic = TWIN, negator = "javatest.<>",
262+
provides = "complex:double relationals"
263+
)
264+
@Operator(
265+
name = "javatest.<>", synthetic = "javatest.neToReal",
266+
commutator = TWIN, provides = "complex:double relationals"
267+
)
268+
@Operator(
269+
name = "javatest.<>", synthetic = TWIN,
270+
provides = "complex:double relationals"
271+
)
272+
@Function(
273+
schema = "javatest", effects = IMMUTABLE, onNullInput = RETURNS_NULL
274+
)
275+
public static boolean eqToReal(ComplexScalar a, double b)
276+
{
277+
return a.m_x == b && 0. == a.m_y;
278+
}
279+
245280
/**
246281
* As an ordinary function, returns the lesser in magnitude of two
247282
* arguments; as a simple aggregate, returns the least in magnitude over its

0 commit comments

Comments
 (0)