0

My strong conviction is that no solution for this exists, but I thought I'd ask anyways. My question is precisely this one:

Are there any way to execute a query inside the string value (like eval) in PostgreSQL?

But for Amazon Redshift, which does not support stored functions. Specifically, I believe my expressions can all be evaluated as Booleans with one or more expressions such as 'X <= 10' concatenated with ANDs and ORs, i.e.:

string = 'X <= 10 AND Y = 5 AND Z >= 0'

I can easily replace into the variables with the correct values but am trying not to resort to building my own parser. Anyone see a nice way to do this? Thanks!

Community
  • 1
  • 1
Eddie E.
  • 219
  • 4
  • 7

1 Answers1

0

You're in luck! Amazon Redshift recently introduced User Defined Functions that can call Python code.

Therefore, you can take advantage of the Python eval() command to evaluate a condition.

Here's some code that worked for me:

CREATE FUNCTION f_eval(condition TEXT)
RETURNS boolean
VOLATILE AS $$
return eval(condition)
$$ LANGUAGE plpythonu;

Then run it with:

SELECT f_eval('5 < 6 and 3 < 4');

This returns true in SQL.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470