David Bryson
01/11/2024, 1:22 AMdef create_top_n_table(table_env: TableEnvironment, input_table: str, partition_key: str):
top_n_table_query = f"""
CREATE VIEW top_n_table AS
SELECT
{partition_key},
MAX(signal) AS max_signal
FROM {input_table}
GROUP BY {partition_key}
"""
table_env.execute_sql(top_n_table_query)
def join_with_top_n(table_env: TableEnvironment, input_table: str, partition_key: str):
enriched_table_query = f"""
SELECT i.*,
t.max_signal,
CASE
WHEN i.signal > s.max_signal THEN i.signal - s.max_signal
ELSE 0
END AS signal_increase
FROM {input_table} AS i
JOIN top_n_table AS t
ON i.{partition_key} = t.{partition_key}
"""
return table_env.sql_query(enriched_table_query)
When I do this my Top-N becomes and update stream and my joins continue to get update values as new topNs come in. This is definitely not what I want, I would like my joined values to remain in the stream for writing to my table.
Do I need to be doing an interval join here? That also doesn't appear to do what I want. Very confused and not sure how to proceed.
Here is a sample input and output:
t = self.t_env.from_elements([(dt(2023, 1, 1, 0, 10, tzinfo=timezone.utc), 'a', 38),
(dt(2023, 1, 1, 0, 15, tzinfo=timezone.utc), 'a', 36),
(dt(2023, 1, 1, 0, 20, tzinfo=timezone.utc), 'a', 40)],
I get this output:
['+I[2022-12-31T161500Z, a, 36, 38, 0]',
'+U[2022-12-31T162000Z, a, 40, 40, 0]',
'+I[2022-12-31T162000Z, a, 40, 38, 2]',
'+I[2022-12-31T161000Z, a, 38, 38, 0]']
And as you can see when the Update happens I 'lose' my record of the difference between the latest signal value and the max. Very confused and any guidance would be helpful.David Bryson
01/11/2024, 8:36 PM