@@ -326,4 +326,70 @@ def test_string_cte_with_underscores_and_numbers
326326 popular_posts = Post . where ( "views_count > 100" )
327327 assert_equal popular_posts . to_a , popular_posts_from_cte
328328 end
329+
330+ def test_string_cte_with_multiline_expressions
331+ # Test multiline CTE expressions with complex formatting
332+ multiline_cte = <<~SQL . strip
333+ filtered_tracker_issue_extras AS (
334+ SELECT tie.scheduled_in_external_sprint_ids,
335+ tie.tracker_project_issue_id
336+ FROM tracker_issue_extras tie
337+ JOIN repo_issues ri
338+ ON ri.tracker_project_issue_id = tie.tracker_project_issue_id
339+ WHERE ri.primary_committer_id = ANY(ARRAY[1]::bigint[])
340+ AND ri.repo_id = ANY(ARRAY[2, 3, 1]::bigint[])
341+ )
342+ SQL
343+
344+ # This should parse successfully without raising an error
345+ assert_nothing_raised do
346+ Post . with ( multiline_cte ) . to_sql
347+ end
348+
349+ # Test with newlines in different positions
350+ cte_with_newlines = "popular_posts AS (\n SELECT *\n FROM posts\n WHERE views_count > 100\n )"
351+ assert_nothing_raised do
352+ Post . with ( cte_with_newlines ) . to_sql
353+ end
354+
355+ # Test with complex nested subqueries and multiline formatting
356+ complex_multiline_cte = <<~SQL . strip
357+ complex_analysis AS (
358+ SELECT
359+ p.id,
360+ p.title,
361+ (SELECT COUNT(*)
362+ FROM comments c
363+ WHERE c.post_id = p.id
364+ AND c.created_at > '2023-01-01') as recent_comments,
365+ CASE
366+ WHEN p.views_count > 1000 THEN 'popular'
367+ WHEN p.views_count > 100 THEN 'moderate'
368+ ELSE 'low'
369+ END as popularity
370+ FROM posts p
371+ WHERE p.published_at IS NOT NULL
372+ )
373+ SQL
374+
375+ assert_nothing_raised do
376+ Post . with ( complex_multiline_cte ) . to_sql
377+ end
378+ end
379+
380+ def test_string_cte_with_user_provided_multiline_example
381+ # Test the specific multiline example provided by the user
382+ user_multiline_cte = "filtered_tracker_issue_extras AS (\n SELECT tie.scheduled_in_external_sprint_ids,\n tie.tracker_project_issue_id\n FROM tracker_issue_extras tie\n JOIN repo_issues ri\n ON ri.tracker_project_issue_id = tie.tracker_project_issue_id\n WHERE ri.primary_committer_id = ANY(ARRAY[[1]]::bigint[])\n AND ri.repo_id = ANY(ARRAY[[2, 3, 1]]::bigint[])\n )\n "
383+
384+ # This should parse successfully without raising an error
385+ result = Post . with ( user_multiline_cte ) . to_sql
386+
387+ # The table name gets quoted by PostgreSQL, so check for quoted version
388+ assert result . include? ( "WITH \" filtered_tracker_issue_extras\" AS" ) , "Should include WITH clause with user's table name (quoted)"
389+ assert result . include? ( "tie.scheduled_in_external_sprint_ids" ) , "Should include specific column from user's example"
390+ assert result . include? ( "tracker_issue_extras tie" ) , "Should include table alias from user's example"
391+ assert result . include? ( "JOIN repo_issues ri" ) , "Should include JOIN clause from user's example"
392+ assert result . include? ( "ARRAY[[1]]::bigint[]" ) , "Should preserve complex array syntax"
393+ assert result . include? ( "ARRAY[[2, 3, 1]]::bigint[]" ) , "Should preserve complex array with multiple values"
394+ end
329395end
0 commit comments