473,396 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function returning uple?

2
Hello all,

Suppose that there is a table "mytable" with a column "mycolumn", and
two functions f1 and f2. The following select:
Expand|Select|Wrap|Line Numbers
  1. SELECT *, f1(myfield), f2(myfield) from mytable;
  2.  
outputs the columns:
Expand|Select|Wrap|Line Numbers
  1. myfield, f1, f2
  2.  
Is it possible to write a function f that can be invoked like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT *, f(myfield) from mytable;
  2.  
and still produce the columns below (add two columns along the columns
from mytable):
Expand|Select|Wrap|Line Numbers
  1. myfield, f1, f2
  2.  
The reason for this question is that f1 and f2 are expensive to compute separately, but it would be more efficient to be both computed in a single function.

Thanks in advance,
Ovidiu
Dec 18 '07 #1
3 2474
rski
700 Expert 512MB
Hello all,

Suppose that there is a table "mytable" with a column "mycolumn", and
two functions f1 and f2. The following select:
Expand|Select|Wrap|Line Numbers
  1. SELECT *, f1(myfield), f2(myfield) from mytable;
  2.  
outputs the columns:
Expand|Select|Wrap|Line Numbers
  1. myfield, f1, f2
  2.  
Is it possible to write a function f that can be invoked like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT *, f(myfield) from mytable;
  2.  
and still produce the columns below (add two columns along the columns
from mytable):
Expand|Select|Wrap|Line Numbers
  1. myfield, f1, f2
  2.  
The reason for this question is that f1 and f2 are expensive to compute separately, but it would be more efficient to be both computed in a single function.

Thanks in advance,
Ovidiu
I do not understand what do you want. You say you have table with column mycolumn and use select
select *,f1(myfield),f2(myfield) from mytable; ??
What is myfield? What do the functions f1, f2 do?
Dec 18 '07 #2
oghogh
2
Hello,

Thanks for pointing this out, there was an error in my post, "myfield" is actually the field of the table previously referred as "mycolumn".

Suppose that "myfield" is a polyline (in postGIS) and the two functions are:
Expand|Select|Wrap|Line Numbers
  1. f1=projection_point(myfield, point_p): returns a point
  2. f2=projection_distance(myfield, point_p): returns a float
  3.  
where point_p has a value I specify.

Computing f1 and f2 separately is expensive (one requires the other to be known), but they could be both computed more efficiently in a single call to:
Expand|Select|Wrap|Line Numbers
  1. f3=projection_info(myfield, point_p): returns the uple (point, distance)
  2.  
So I would like to replace a call like:
Expand|Select|Wrap|Line Numbers
  1. select *, projection_point(geometry, point_p), projection_distance(geometry,point_p) from mytable;
  2.  
with something like:
Expand|Select|Wrap|Line Numbers
  1. select *, projection_info(geometry, point_p) from mytable;
  2.  
where "geometry" is a column of "mytable" and "point_p" a given point.

The rows that result from executing the last SQL should contain all the fields from "mytable", to which two fields are added: one containing the projection point, and the other the projection distance.

I would like to know if this is possible at all in PostgreSQL - for testing purposes we could work with:
Expand|Select|Wrap|Line Numbers
  1. f1 = increase_value(myfield, delta) : returns value(myfield) + delta
  2. f2 = decrease_value(myfield, delta) : returns value(myfield) - delta
  3.  
and the aggregated function would be
Expand|Select|Wrap|Line Numbers
  1. f3=inc_dec(myfield,delta): returns [value(myfield)+delta,value(myfield)-delta]
  2.  
Please let me know if you need any additional information.

Best regards,
Ovidiu
Dec 19 '07 #3
rski
700 Expert 512MB
Hello,

Thanks for pointing this out, there was an error in my post, "myfield" is actually the field of the table previously referred as "mycolumn".

Suppose that "myfield" is a polyline (in postGIS) and the two functions are:
Expand|Select|Wrap|Line Numbers
  1. f1=projection_point(myfield, point_p): returns a point
  2. f2=projection_distance(myfield, point_p): returns a float
  3.  
where point_p has a value I specify.

Computing f1 and f2 separately is expensive (one requires the other to be known), but they could be both computed more efficiently in a single call to:
Expand|Select|Wrap|Line Numbers
  1. f3=projection_info(myfield, point_p): returns the uple (point, distance)
  2.  
So I would like to replace a call like:
Expand|Select|Wrap|Line Numbers
  1. select *, projection_point(geometry, point_p), projection_distance(geometry,point_p) from mytable;
  2.  
with something like:
Expand|Select|Wrap|Line Numbers
  1. select *, projection_info(geometry, point_p) from mytable;
  2.  
where "geometry" is a column of "mytable" and "point_p" a given point.

The rows that result from executing the last SQL should contain all the fields from "mytable", to which two fields are added: one containing the projection point, and the other the projection distance.

I would like to know if this is possible at all in PostgreSQL - for testing purposes we could work with:
Expand|Select|Wrap|Line Numbers
  1. f1 = increase_value(myfield, delta) : returns value(myfield) + delta
  2. f2 = decrease_value(myfield, delta) : returns value(myfield) - delta
  3.  
and the aggregated function would be
Expand|Select|Wrap|Line Numbers
  1. f3=inc_dec(myfield,delta): returns [value(myfield)+delta,value(myfield)-delta]
  2.  
Please let me know if you need any additional information.

Best regards,
Ovidiu
Well my english is really poor but if you're asking if postgres function can return a record (or multiple values) the answer is yes. Just see in documentatioon.
Dec 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: noahlt | last post by:
I'm trying to write a website updating script, but when I run the script, my function to search the DOM tree returns None instead of what it should. I have this program: -------- import sys...
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
5
by: Cancerbero | last post by:
Hi (first, excuse me for my bad english) As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is that right? Based...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
2
by: None | last post by:
Hello, 1. The prototype of function signal in signal.h: void (*signal(int sig, void (*func)(int)))(int); is some complex to me. Would you please explain it to me in detail with the C...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
13
by: Sri Harsha Dandibhotla | last post by:
Hello all. I recently came across a function declaration as : char(*(*x()))(); This was not in some code but it was in a C questions thread on some group. I tried to decipher what it returns but...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.