473,378 Members | 1,623 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,378 software developers and data experts.

Dynamic expression evaluation

Hello,

Imagine we have the following kind of table, with two values (a and b), and a varchar (f) representing an expression.

----------------------------------
CREATE TABLE public.test
(
id serial NOT NULL,
a int4,
b int4,
f varchar(50),
CONSTRAINT id PRIMARY KEY (id)
) WITHOUT OIDS;

INSERT INTO public.test(a,b,f) VALUES(2,3,'a+b');
INSERT INTO public.test(a,b,f) VALUES(12,3,'a*b');
INSERT INTO public.test(a,b,f) VALUES(5,6,'a+2*b');
----------------------------------

Is there a simple way of doing "kind of" a

SELECT *, EVAL(f) FROM public.test;

.... and having f evaluated as an expression, so that we get back:

------------------------------
id a b f eval
------------------------------
1 2 3 a+b 5
2 12 3 a*b 36
3 5 6 a+2*b 17
------------------------------
Has anyone done anything like that already?

Thanks!

Philippe

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
1 3858
"Philippe Lang" <ph***********@attiksystem.ch> writes:
Is there a simple way of doing "kind of" a
SELECT *, EVAL(f) FROM public.test; ... and having f evaluated as an expression, so that we get back: ------------------------------
id a b f eval
------------------------------
1 2 3 a+b 5
2 12 3 a*b 36
3 5 6 a+2*b 17
------------------------------


Not really. You can sort of approximate eval() with plpgsql's EXECUTE:

regression=# create or replace function eval(text) returns int as '
regression'# declare res record;
regression'# begin
regression'# for res in execute ''select '' || $1 || '' as result'' loop
regression'# return res.result;
regression'# end loop;
regression'# end' language plpgsql;
CREATE FUNCTION
regression=# select eval ('23+34');
eval
------
57
(1 row)

regression=#

but this has a problem with supporting more than one result type (hmm,
maybe you could fake that with 7.4's polymorphism?). And I don't see
any way at all for the function to have access to the other values in
the row, as your example presumes it would do.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon Wigzell | last post by:
A client wants have acess to an online databases records controlled by group strings and evaluation strings e.g., each use would have in his client record a group string like this: And each...
31
by: NickName | last post by:
/* goal: dynamic evaluation of table row platform: sql 2000 */ use northwind; declare @tbl sysname set @tbl = 'customers' EXEC('select count(*) from ' +@tbl)
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
8
by: manan.kathuria | last post by:
hi all , the expression in question is ++i&&++j||++k most sources say that since the result of the || operation is decided by the LHS itself , the right side is not computed my point of...
35
by: A Jafarpour | last post by:
Hi everyone, hope someone can tell if there is any way to, dynamically, build an statement and then call some functions to execute the statement. I know examples always help, so here what I am...
8
by: Brian Blais | last post by:
Hello, I have a string input from the user, and want to parse it to a number, and would like to know how to do it. I would like to be able to accept arithmetic operations, like: '5+5'...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
2
by: Tim Johnson | last post by:
I'm having trouble understanding this part of the expression evaluation rules ('6.5 Expressions', second item, C99 spec; the C++ wording is presumably identical): What precisely does the...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.