Skip to content

HOTFIX: Fix rapi_bind length mismatch when saving citation networks#261

Closed
seanthimons wants to merge 3 commits intointegrationfrom
hotfix/citation-network-bind-length-v2
Closed

HOTFIX: Fix rapi_bind length mismatch when saving citation networks#261
seanthimons wants to merge 3 commits intointegrationfrom
hotfix/citation-network-bind-length-v2

Conversation

@seanthimons
Copy link
Copy Markdown
Owner

Hotfix

Issue: When saving a citation network: Error saving network: rapi_bind: Bind parameter values need to have the same length

Root cause: Two issues in save_network() (R/db.R):

  1. source_notebook_id = NULL passed directly to dbExecute as a zero-length bind parameter — DuckDB requires all bind params to have equal length
  2. nodes_df$community %||% NA_character_ and nodes_df$is_overlap %||% FALSE produce scalar (length-1) fallbacks when columns are absent, mixed with length-N columns in the data.frame passed to dbWriteTable

Fix:

  1. Coerce NULLNA_character_ for source_notebook_id before binding
  2. Replace scalar %||% fallbacks with rep() to ensure length-N vectors

Triage findings:

  • Code trace: Both dbExecute (metadata insert) and dbWriteTable (nodes bulk insert) had parameter length mismatches
  • Git history: The community column addition and missing NULL handling were introduced at different times
  • Test status: Existing tests always passed source_notebook_id = "nb-1" and included is_overlap/community columns, masking the bug

Test plan

  • Existing test suite passes (no new failures introduced)
  • New regression test: NULL source_notebook_id + missing is_overlap/community columns
  • Manual verification: save a network from Citation Network tab (no notebook context)

seanthimons and others added 3 commits March 20, 2026 13:27
feat: v14.0 citation audit filters + network node sizing (#135, #145)
…-evolution"

This reverts commit 152ffdb, reversing
changes made to b2d0816.
Two issues caused "Bind parameter values need to have the same length":

1. NULL source_notebook_id passed directly to dbExecute created a
   zero-length bind parameter. Now coerced to NA_character_.

2. Scalar fallbacks for missing is_overlap/community columns produced
   length-1 vectors mixed with length-N columns. Now uses rep() to
   ensure proper length-N vectors.

Adds regression test covering NULL source_notebook_id + missing columns.
Copilot AI review requested due to automatic review settings March 24, 2026 21:25
@seanthimons seanthimons changed the base branch from main to integration March 24, 2026 21:29
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Hotfix to prevent DuckDB bind-parameter length mismatches when saving citation networks, especially in “standalone” contexts where source_notebook_id is NULL and some node columns aren’t present.

Changes:

  • Coerces source_notebook_id from NULL to NA_character_ before dbExecute() to avoid zero-length bind parameters.
  • Ensures is_overlap / community fallbacks are length-N vectors (via rep(...)) to avoid dbWriteTable() column length mismatches.
  • Adds regression tests covering NULL source_notebook_id and missing is_overlap/community.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

File Description
R/db.R Fixes parameter binding and data.frame column length handling in save_network()
tests/testthat/test-save-network.R Adds regression coverage for the failing save scenario and a “full columns present” case

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1523 to +1524
is_overlap = as.logical(if (!is.null(nodes_df$is_overlap)) nodes_df$is_overlap else rep(FALSE, nrow(nodes_df))),
community = as.character(if (!is.null(nodes_df$community)) nodes_df$community else rep(NA_character_, nrow(nodes_df))),
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save_network() always includes a community column in the data.frame passed to dbWriteTable(…, "network_nodes", …, append = TRUE). In the repo migrations, network_nodes is created in migrations/006_create_citation_networks.sql without community, and migrations/010_add_multi_seed_support.sql adds is_overlap but not community. A fresh DB built from migrations will therefore likely fail when saving a network. Please add a migration to ALTER TABLE network_nodes ADD COLUMN IF NOT EXISTS community VARCHAR (and consider making save_network() resilient by omitting community when the column doesn’t exist).

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +29
DBI::dbExecute(con, "CREATE TABLE IF NOT EXISTS network_nodes (
network_id VARCHAR NOT NULL, paper_id VARCHAR NOT NULL,
is_seed BOOLEAN DEFAULT FALSE, title VARCHAR NOT NULL, authors VARCHAR,
year INTEGER, venue VARCHAR, doi VARCHAR, cited_by_count INTEGER DEFAULT 0,
x_position DOUBLE, y_position DOUBLE,
is_overlap BOOLEAN DEFAULT FALSE, community VARCHAR,
PRIMARY KEY (network_id, paper_id),
FOREIGN KEY (network_id) REFERENCES citation_networks(id)
)")
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test creates a custom network_nodes schema that includes community and is_overlap. This diverges from the schema managed by repo migrations (e.g., migrations/006_create_citation_networks.sql doesn’t define community and migrations/010_add_multi_seed_support.sql doesn’t add it), so the regression test can pass even when a fresh migrated DB would fail. Consider initializing the schema via the project’s migration/init helpers (or otherwise deriving the test table definitions from the migration SQL) so the test stays aligned with production schema.

Copilot uses AI. Check for mistakes.
@seanthimons
Copy link
Copy Markdown
Owner Author

Recreating branch from integration to resolve conflicts

@seanthimons seanthimons deleted the hotfix/citation-network-bind-length-v2 branch March 25, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants