Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 31, 2025

This PR addresses the issue where dm_paste() generates extremely long pipe chains that can cause C stack overflow errors and are difficult to read and debug.

Problem

When working with large data models containing many tables and relationships, dm_paste() would generate a single pipe chain with hundreds of operations:

dm(...) %>%
  dm_add_pk(table1, id) %>%
  dm_add_pk(table2, id) %>%
  ... # potentially 500+ operations
  dm_add_fk(table500, main_id, main)

This could trigger Error: C stack usage 7970324 is too close to the limit with older R/magrittr versions and creates unwieldy code that's hard to debug.

Solution

The fix implements automatic pipe chunking when the number of operations exceeds a configurable threshold (default: 100). Large pipe chains are split into manageable chunks with intermediate variable assignments:

dm_step_1 <- dm(...) %>%
  dm_add_pk(table1, id) %>%
  ... # first 100 operations

dm_step_2 <- dm_step_1 %>%
  dm_add_fk(table51, main_id, main) %>%
  ... # next 100 operations

dm_step_2 %>%
  dm_add_fk(table151, main_id, main) %>%
  ... # remaining operations

Key Changes

  • Modified dm_paste_impl() to collect and count all operations before formatting
  • Added dm_paste_chunk_operations() function to handle the chunking logic
  • Maintains full backward compatibility - small dm objects work exactly as before
  • Works with both magrittr (%>%) and base R (|>) pipe operators
  • Added comprehensive test coverage for edge cases

Testing

The implementation has been thoroughly tested with:

  • Edge cases (0, 100, 101, 250 operations)
  • Both pipe operators (%>% and |>)
  • Large dm objects with 500+ tables
  • Integration with existing test suite

Fixes #400.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] dm_paste() generates pipes that are too long Fix dm_paste() pipe length issue by implementing operation chunking Jul 31, 2025
Copilot AI requested a review from krlmlr July 31, 2025 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

dm_paste() generates pipes that are too long

2 participants