confluentinc / ksql

The database purpose-built for stream processing applications.
https://ksqldb.io
Other
80 stars 1.04k forks source link

IF Function #3906

Open robinroos opened 4 years ago

robinroos commented 4 years ago

I have the following stream which joins "position" with "market" information:

ksql> select * from fx_position_by_pair p LEFT JOIN fx_spot_market_table m ON m.Pair = p.Pair;
+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
|P_ROWTIME    |P_ROWKEY     |P_LONGSHORT  |P_BOOK       |P_PAIR       |P_SETTLEDATE |P_BASEAMOUNT |P_QUOTEDAMOUN|P_POSITIONKEY|P_AVGPRICE   |M_ROWTIME    |M_ROWKEY     |M_PAIR       |M_BID        |M_OFFER      |
|             |             |             |             |             |             |             |T            |             |             |             |             |             |             |             |
+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
|1574088316834|EURUSD       |LONG         |UKFXSALES    |EURUSD       |18213        |4000000.0    |-4938000.0   |UKFXSALES:EUR|1.2345       |1574088313243|EURUSD       |EURUSD       |1.2555       |1.256        |
|             |             |             |             |             |             |             |             |USD:18213    |             |             |             |             |             |             |
^CQuery terminated
ksql> 

I wish to value the "position", which involves (inter alia) multiplying P_BaseAmount either by M_Bid (if P_BaseAmount > 0) or by M_Offer.

For now I will fork the stream, and use separate KSQL stream processing statements to encode the two variants (one to select P_BaseAmount * M_Bid and one to select P_BaseAmount * M_Offer) and others to re-join the streams.

Perhaps that is the "correct" streams-based approach, but I'd appreciate the option to code it instead as a single stream processor with an IF function.

mikebin commented 4 years ago

Could this possibly be done with a CASE statement instead: https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-reference/select-push-query/#case

robinroos commented 4 years ago

Thanks, I will take a look at CASE and revert.