Expression Evaluation

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

gsherry1
Charter Member
Charter Member
Posts: 173
Joined: Fri Jun 17, 2005 8:31 am
Location: Canada

Expression Evaluation

Post by gsherry1 »

Hello All,

I have a column which contains a mathematical predicate expression. The possible operaters are +, -, *, /, =, >, < .
For example '3 + 2 = 7 - 5' (a false expression, not a typo).
There is no specified limit on the number of operands and operators in the expression.

I was wondering what is easiest way to evaluate the expression as true or false in DataStage. Is there something like an eval command?

Thanks
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

The easiest - and correct - way in server jobs is to do nothing. :D

Because there are no data types, context governs the use of the result of an expression. Your expression '3 + 2 = 7 - 5' (whether quoted or not) returns 0 which, in a Boolean context, is interpreted as "false" - for example if it were the test expression in an IF statement, that statement would take its ELSE branch.

In BASIC, an expression that returns 0, " " or "" are interpreted as false in a Boolean context, while any other non-null value is interpreted as true.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
patonp
Premium Member
Premium Member
Posts: 110
Joined: Thu Mar 11, 2004 7:59 am
Location: Toronto, ON

Post by patonp »

I've tried this out, but it doesn't seem to be working for me.

In my example, I have an inbound column named Lnk_From_Src.ExpEval. The column is defined as a varchar and contains numeric expressions that need to be evaluated at run time(i.e. '2+3=4+3', '1+1=2-0'). Within DataStage, I'd like to evaluate whether the contents of the column are true or false for each row. Is there any way to do this?
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

:? What about it "isn't working"? How are you using your expression field and what kind of output are you seeing?

Curious what happens when you try something simple like:

Code: Select all

If Lnk_FromSrc.ExpEval Then 'True' Else 'False'
in a derivation for various values?
-craig

"You can never have too many knives" -- Logan Nine Fingers
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Dynamically parsing this string at runtime is difficult. The PERFORM engine has a keyword EVAL which does this, but it is not present in BASIC, and to use the ITYPE() function would also be a bit complex and quite slow.

It might be worth your time to write a dict entry I-Type that uses EVAL and then reading that in a routine using ITYPE. You would need to write the string in question to a hashed file, but I think it would work.

Apart from that I can only think of writing your own parser for this - not a huge effort, but certainly not trivial either.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

ITYPE brilliant answer. Does EVAL work in BASIC? Got to try it.
Mamu Kim
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Kim,

no, I checked and couldn't find an EVAL or similar in BASIC - but somehow I think that I've done something like this before, I just can't remember how. The ITYPE() call will work on a dummy file, but it is rather slow. I wonder if there is a FMT() alternative to get this done...
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Assertion: DataStage BASIC does not have an expression evaluating function.

Now that I understand the requirement, I'd suggest going out to the UNIX expr command to evaluate these expressions. Beware, however, that the expression elements have to be separated by spaces, and any operator meaningful to the shell needs to be escaped, for example \>.

To use Arnd's suggestion you are going to have to write an arbritrary expression evaluator using DataStage BASIC. An I-type can only evaluate its own expression, not an arbitrary one.

Yours does seem to be an odd requirement in an ETL task, however.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Ray

I am not sure that was the requirement but still a very cool idea by Arnd.
Mamu Kim
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

I did a quick and dirty test that did work - in a routine do an

Code: Select all

MathExpression= '1+2+3+4=9'
EXECUTE "LIST VOC FIRST 1 ID.SUP COL.SUP HDR.SUP  EVAL '":MathExpression:"'" CAPTURING ScreenIO RETURNING ERRORCODE
And it will work when you parse the resultant ScreenIO string. Not pretty or fast or efficient or recommended, but it will work.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

May I suggest SQL instaad? RetrieVe has to write a temporary entry to the file dictionary (DICT VOC in your case). UniVerse SQL does not require write permission to the dictionary because it doesn't write anything there.

Code: Select all

MathExpression= '1+2+3+4=9' 
Command = "SELECT EVAL " : DQuote(MathExpression) : " FROM VOC FIRST 1;"
EXECUTE Command CAPTURING ScreenIO RETURNING ERRORCODE
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Need to create table dual.
Mamu Kim
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Rubbish :P

You can use any extant "UniVerse" file that has a file dictionary defined.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

I noticed we have one on Sql Server. Think he was an Oracle fanatic?
Mamu Kim
gsherry1
Charter Member
Charter Member
Posts: 173
Joined: Fri Jun 17, 2005 8:31 am
Location: Canada

Calling expr command on Windows.

Post by gsherry1 »

ray.wurlod wrote:Assertion: DataStage BASIC does not have an expression evaluating function.

Now that I understand the requirement, I'd suggest going out to the UNIX expr command to evaluate these expressions. Beware, however, that the expression elements have to be separated by spaces, and any operator meaningful to the shell needs to be escaped, for example \>.

To use Arnd's suggestion you are going to have to write an arbritrary expression evaluator using DataStage BASIC. An I-type can only evaluate its own expression, not an arbitrary one.

Yours does seem to be an odd requirement in an ETL task, however.
I was going to try the expr command, but I get an error that it is not a valid OS command. How do I check to determine if MKS package is installed? I could not find any references to this anywhere in the pdfs that shipped with DataStage 7.5.
Post Reply