The BEGIN ATOMIC ... END block is a problem. You hinder the optimizer to optimize the overall statement because of this block. (Of course, the optimizer is getting smarter about this by the day, but still you break with the relational model and that makes things unnecessarily hard.)
Try this:
Code:
CREATE FUNCTION test(...)
RETURNS DECIMAL(24, 4)
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
RETURN <col> FROM <table name>
@
(@ is the statement terminator.)