Pradyot Patil
01/23/2024, 1:16 AMCREATE TABLE transactionTable (
-- Columns based on AVRO schema for kafka value
iban STRING,
amount STRING,
creditDebitIndicator STRING,
transactionSource STRING,
businessProductType STRING,
counterpartyAccountId STRING,
-- Watermark
ingestionTime TIMESTAMP(3) METADATA FROM 'timestamp',
WATERMARK FOR ingestionTime AS ingestionTime
) WITH (
-- connector options
'connector' = 'kafka',
...
)
Then I have 3 lookup tables based on compacted topic as follows (lookout for primary key):
CREATE TABLE consentTable (
-- Columns based on AVRO schema for kafka value
accountIBAN STRING NOT NULL,
registrationIndicator BOOLEAN NOT NULL,
relationType STRING NOT NULL,
-- Watermark
ingestionTime TIMESTAMP(3) METADATA FROM 'timestamp',
WATERMARK FOR ingestionTime AS ingestionTime - INTERVAL '0.500' SECOND,
-- lookup-table-compacted-topic
PRIMARY KEY (accountIBAN) NOT ENFORCED
) WITH (
-- connector options
'connector' = 'upsert-kafka',
-- format options for key options
'key.format' = 'raw',
...
)
CREATE TABLE recategoriseTable (
-- Columns based on AVRO schema for kafka value
accountIBAN STRING NOT NULL,
fingerPrint STRING NOT NULL,
`action` STRING NOT NULL,
-- Watermark
ingestionTime TIMESTAMP(3) METADATA FROM 'timestamp',
WATERMARK FOR ingestionTime AS ingestionTime - INTERVAL '0.500' SECOND,
-- lookup-table-compacted-topic
PRIMARY KEY (accountIBAN, fingerPrint) NOT ENFORCED
) WITH (
-- connector options
'connector' = 'upsert-kafka',
'key.format' = 'json',
...
)
CREATE TABLE lookupTable (
-- Columns based on AVRO schema for kafka value
relationType STRING NOT NULL,
debitCreditIndicator STRING NOT NULL,
modelRule STRING NOT NULL,
modelRuleValue STRING NOT NULL,
category STRING,
-- Watermark
ingestionTime TIMESTAMP(3) METADATA FROM 'timestamp',
WATERMARK FOR ingestionTime AS ingestionTime - INTERVAL '0.500' SECOND,
-- lookup-table-compacted-topic
PRIMARY KEY (relationType, debitCreditIndicator, modelRule, modelRuleValue) NOT ENFORCED
) WITH (
-- connector options
'connector' = 'upsert-kafka',
'key.format' = 'json',
)
First, I create a view to filter events in lookupTable :
CREATE VIEW business_product_type_view AS
SELECT
CAST(ROW(relationType, debitCreditIndicator, modelRule, modelRuleValue) AS ROW`relationType` STRING, `creditDebitIndicator` STRING, `modelRule` STRING, `modelRuleValue` STRING) AS cpKey,
debitCreditIndicator,
relationType,
modelRuleValue,
modelRule,
category,
ingestionTime
FROM
lookupTable
WHERE
modelRule IN ('businessProductType', 'counterAccountProductType', 'transactionSource');
Then, I have another view which basically filters out non-consented transactions and add couple of columns with a left join on second lookup table:
CREATE VIEW consentedtransactions_view AS
WITH step1 AS (
SELECT
pt.*,
CAST(
ROW(
CAST(nlt.relationType AS STRING),
CAST(creditDebitIndicator AS STRING),
CAST('businessProductType' AS STRING),
CAST(businessProductType AS STRING)
) AS ROW`relationType` STRING, `creditDebitIndicator` STRING, `modelRule` STRING, `modelRuleValue` STRING
) AS key_bpt
FROM
transactionTable AS pt
INNER JOIN
consentTable FOR SYSTEM_TIME AS OF pt.ingestionTime AS nlt
ON
pt.iban = nlt.accountIBAN
WHERE
nlt.registrationIndicator
)
SELECT
st1.*,
CASE brt.`action`
WHEN 'CREATE' THEN fingerPrint
END AS customerCategoryCode,
CASE brt.`action`
WHEN 'CREATE' THEN fingerPrint
END AS customerSubcategoryCode
FROM
step1 AS st1
LEFT JOIN
recategoriseTable FOR SYSTEM_TIME AS OF st1.ingestionTime AS brt
ON
ROW(st1.iban, st1.amount) = ROW(brt.accountIBAN, brt.fingerPrint);
Until this step I have data in both of the views. Then when I attempt to do a lookup join between the two views in the flink SQL client. But I get the following error :
Flink SQL> SELECT cv.key_bpt,
> bpt.category AS categoryPolicy1
> FROM consentedtransactions_view cv
> LEFT JOIN business_product_type_view FOR SYSTEM_TIME AS OF cv.ingestionTime AS bpt ON
> cv.key_bpt.`relationType` = bpt.relationType AND
> cv.key_bpt.`creditDebitIndicator` = bpt.debitCreditIndicator AND
> cv.key_bpt.`modelRule` = bpt.modelRule AND
> cv.key_bpt.`modelRuleValue` = bpt.modelRuleValue;
[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.ValidationException: Currently the join key in Temporal Table Join can not be empty.
---------------------------------------------------------------
OR
---------------------------------------------------------------
Flink SQL> SELECT cv.key_bpt,
> bpt.category AS categoryPolicy1
> FROM consentedtransactions_view cv
> LEFT JOIN business_product_type_view FOR SYSTEM_TIME AS OF cv.ingestionTime AS bpt ON
> cv.key_bpt = bpt.cpKey;
[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.ValidationException: Temporal Table Join requires primary key in versioned table, but no primary key can be found. The physical plan is: ...
I'm joining all the keys for the underlying table of business_product_type_view
. I tried the above two ways but both give me an error. Seems like I'm missing something when joining via views?