Skip to content

Fix find_source_in_db crash on empty Sources table - #266

Open
kelle wants to merge 4 commits into
mainfrom
claude/issue-265-wxf2d9
Open

Fix find_source_in_db crash on empty Sources table#266
kelle wants to merge 4 commits into
mainfrom
claude/issue-265-wxf2d9

Conversation

@kelle

@kelle kelle commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • find_source_in_db's spatial fallback search crashed with a pandas KeyError when db.query_region(...) was called against a completely empty Sources table (it can return a columnless DataFrame in that case), breaking the very first ingest_source(..., search_db=True) into a fresh database.
  • An empty Sources table can have no matches by definition, so find_source_in_db now checks for that up front (db.query(db.Sources).first() is None) and returns [] immediately, before any name search, fuzzy search, SIMBAD resolution, or query_region call. This is a single early-return guard — both query_region call sites, and their existing error behavior for genuinely bad ra_col_name/dec_col_name arguments, are unchanged.

Fixes #265.

Test plan

  • Added test_find_source_in_db_empty_sources_table, which builds a fresh empty database from the template schema and asserts find_source_in_db returns [] instead of raising; the SQLAlchemy engine is disposed in a finally block.
  • Verified the new test fails with the pre-fix code (reproducing the reported KeyError) and passes with the fix.
  • Verified test_find_source_in_db_errors's existing expectations (bad ra_col_name/dec_col_name against a non-empty table still raises KeyError/AstroDBError as before) are unaffected.
  • Ran pytest tests/test_sources.py and the full suite — no new failures versus the pre-fix baseline (remaining failures are pre-existing, caused by no SIMBAD network access in this environment).
  • python -m py_compile astrodb_utils/sources.py

🤖 Generated with Claude Code

https://claude.ai/code/session_016UKKrx5abQaERu4vRaZJrP

query_region can return a columnless DataFrame when the Sources table
has zero rows, which previously raised an unguarded pandas KeyError
when the ra/dec columns were indexed. Now that case is detected and
treated as "no spatial matches" so the first ingest into a fresh
database with search_db=True no longer crashes; a KeyError from
genuinely wrong ra_col_name/dec_col_name still raises AstroDBError.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016UKKrx5abQaERu4vRaZJrP
The first query_region call site (direct ra/dec) previously let a
KeyError from bad ra_col_name/dec_col_name propagate unwrapped, and
test_find_source_in_db_errors relies on that. The shared empty-table
guard from the previous commit incorrectly wrapped it into
AstroDBError in all cases. Only wrap into AstroDBError when the
Sources table is actually empty; otherwise re-raise the original
KeyError (first call site) or the original AstroDBError message
(second, SIMBAD-resolved call site), matching pre-existing behavior.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016UKKrx5abQaERu4vRaZJrP
@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.35%. Comparing base (dd07e99) to head (c5e664f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #266      +/-   ##
==========================================
+ Coverage   72.10%   72.35%   +0.25%     
==========================================
  Files          19       19              
  Lines        1649     1664      +15     
  Branches      211      212       +1     
==========================================
+ Hits         1189     1204      +15     
  Misses        379      379              
  Partials       81       81              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Replace the two try/except KeyError wrappers around query_region with
a single early return when the Sources table has zero rows. An empty
table can have no matches by definition, so this is simpler, avoids
wasted SIMBAD/fuzzy search work, and keeps both query_region call
sites' original error behavior completely unchanged for the
non-empty-table case.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016UKKrx5abQaERu4vRaZJrP

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

There are a few concrete follow-ups (notably resource cleanup in the new test and aligning PR description with the implemented fix) that should be addressed before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR prevents astrodb_utils.sources.find_source_in_db from crashing when called against a brand-new database whose Sources table has zero rows, and adds a regression test to cover the empty-table scenario (Fixes #265).

Changes:

  • Added an early-return guard in find_source_in_db when the Sources table is empty.
  • Added a regression test that creates an empty schema-only database and asserts find_source_in_db returns [] instead of raising.
File summaries
File Description
astrodb_utils/sources.py Adds an empty-Sources short-circuit to avoid query_region crashing on empty tables.
tests/test_sources.py Adds a regression test that exercises find_source_in_db against a freshly created empty database.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread astrodb_utils/sources.py
Comment thread tests/test_sources.py Outdated
Comment thread astrodb_utils/sources.py
- Use query(...).first() is None instead of count() == 0 to check for
  an empty Sources table, avoiding a full COUNT(*) scan.
- Dispose the SQLAlchemy engine in test_find_source_in_db_empty_sources_table
  so the temporary sqlite file's handle is released.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016UKKrx5abQaERu4vRaZJrP
@kelle
kelle requested a review from dr-rodriguez September 8, 2026 21:00
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.

find_source_in_db's spatial fallback search crashes on a completely empty Sources table

3 participants