Skip to content

Conversation

@cpsievert
Copy link
Contributor

@cpsievert cpsievert commented Jan 16, 2026

Summary

  • Replace LIMIT 0 with WHERE 1=0 to get column names (standard SQL)
  • Replace LIMIT 1 with dbSendQuery() + dbFetch(n=1) for type detection sampling

Fixes #112

Problem

SQL Server uses TOP N instead of LIMIT N, causing errors like:

Incorrect syntax near '1'.
<SQL> 'SELECT * FROM "planning"."OpsAppSched" LIMIT 1'

The R package's DBISource class was using LIMIT syntax in two places:

  1. initialize(): SELECT * FROM table LIMIT 0 to get column names
  2. get_schema_impl(): SELECT * FROM table LIMIT 1 to sample a row for type detection

Solution

Use database-agnostic approaches that work across all SQL databases:

Fix 1: WHERE 1=0 instead of LIMIT 0

WHERE 1=0 is ANSI SQL that returns zero rows while preserving column metadata. This is a standard pattern supported by all SQL databases including SQL Server, PostgreSQL, MySQL, SQLite, and DuckDB.

Fix 2: dbFetch(n=1) instead of LIMIT 1

DBI's dbSendQuery() + dbFetch(n=1) + dbClearResult() pattern delegates row limiting to the database driver, which handles dialect-specific syntax internally. This is the same approach already used by DBISource$test_query().

Why Python doesn't need changes

The Python package handles SQL Server through different mechanisms:

  1. DataFrameSource: Uses in-memory DuckDB which supports LIMIT
  2. SQLAlchemySource: Already has a fallback at lines 657-662:
    try:
        result = read_sql(text(limit_query), conn)
    except Exception:
        # If LIMIT syntax doesn't work, fall back to regular read and take first row
        result = read_sql(text(query), conn).head(1)
  3. PolarsLazySource: Uses Polars SQLContext which supports LIMIT
  4. IbisSource: Uses result.limit(1).execute() which is Ibis's API that handles SQL dialect translation automatically

Test plan

  • All R tests pass (538 tests)
  • All Python tests pass (251 tests)
  • Manual testing with SQL Server (not available in CI)

🤖 Generated with Claude Code

Replace LIMIT syntax with database-agnostic alternatives in DBISource:
- Use `WHERE 1=0` instead of `LIMIT 0` to get column names
- Use `dbSendQuery()` + `dbFetch(n=1)` instead of `LIMIT 1` for sampling

Fixes #112

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Contributor

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

This PR fixes SQL Server compatibility issues in the R package's DBISource class by replacing non-standard LIMIT clauses with database-agnostic SQL patterns. The changes ensure the code works across all SQL database systems including SQL Server, which uses TOP N instead of LIMIT N.

Changes:

  • Replace LIMIT 0 with WHERE 1=0 for column name retrieval
  • Replace LIMIT 1 with dbSendQuery() + dbFetch(n=1) pattern for type detection sampling

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

Copy link
Contributor

@gadenbuie gadenbuie left a comment

Choose a reason for hiding this comment

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

1️⃣ 🟰 0️⃣

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.

querychat not working with SqlServer

3 participants