Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No target tables in UPDATE statement using CTE #453

Closed
mikerrr opened this issue Sep 15, 2023 · 1 comment · Fixed by #495
Closed

No target tables in UPDATE statement using CTE #453

mikerrr opened this issue Sep 15, 2023 · 1 comment · Fixed by #495
Labels
bug Something isn't working

Comments

@mikerrr
Copy link

mikerrr commented Sep 15, 2023

SQL statement (pure UPDATE) (see below). After parsing in parser_result target_tables are empty!

WITH data_table AS ( 
	SELECT 
		id, 
		user_id,
		recommended_vacancies,
		push_dtm,
		push_check_datetime
	FROM dap_pushs_multyrec_sent
	WHERE 
		status = 'CHECKED'
		AND push_dtm > NOW() - INTERVAL '2 months'
		AND push_success = 0
		AND (push_success_notimeframe_total IS NULL OR push_success_notimeframe_total = 0)
	),
-----
unnest_vacs AS (
	SELECT user_id, UNNEST (recommended_vacancies) AS vacancy_id FROM data_table
	),
----
activities_count_table AS ( 
	SELECT * FROM unnest_vacs	
	LEFT JOIN LATERAL(
		SELECT 
			count (*) AS num_activities_vacancy,
			min(ewa_created_on) AS reaction_dtm
		FROM dap_ewa 
		WHERE ewa_executor_id = unnest_vacs.user_id
		AND ewa_vacancy_id = unnest_vacs.vacancy_id
	) activities ON TRUE),
-----
data_with_actcount_unnested AS (
	SELECT 
		id, data_table.user_id, recommended_vacancies, vacancy_id, num_activities_vacancy, reaction_dtm,
		data_table.push_dtm, data_table.push_check_datetime
	FROM data_table
	LEFT JOIN activities_count_table ON activities_count_table.vacancy_id = ANY (data_table.recommended_vacancies) AND data_table.user_id = activities_count_table.user_id
	),
----
result_table AS (
	SELECT 
		id, user_id, 	
		recommended_vacancies,
		array_agg((num_activities_vacancy>0)::integer) AS push_success_notimeframe,
		sum((num_activities_vacancy>0)::integer) push_success_notimeframe_total,
		array_agg (reaction_dtm AT time ZONE 'UTC-3' - push_dtm) AS reaction_time_notimeframe		
	FROM data_with_actcount_unnested
	GROUP BY id, user_id, recommended_vacancies)
----
UPDATE dap_pushs_multyrec_sent
SET
	push_success_notimeframe = result_table.push_success_notimeframe,
	push_success_notimeframe_total = result_table.push_success_notimeframe_total,
	reaction_time_notimeframe = result_table.reaction_time_notimeframe
FROM result_table
WHERE result_table.id = dap_pushs_multyrec_sent.id;
parser_result = LineageRunner(sql_query, dialect = 'postgres')
@reata
Copy link
Owner

reata commented Sep 20, 2023

Yes, we don't support CTE used together with UPDATE yet.

The fix should be easy, we should delegate to UpdateExtractor as we do for SelectExtractor and CreateInsertExtractor used together with CTE.

if segment.type in ["select_statement", "set_expression"]:

@reata reata added the bug Something isn't working label Sep 20, 2023
@reata reata changed the title [BUG] No target tables in UPDATE statement after parsing No target tables in UPDATE statement using CTE Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants