Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subqueries using count(*) and + inferred as boolean instead of string #357

Closed
karlhorky opened this issue Jan 3, 2025 · 3 comments · Fixed by #359
Closed

Subqueries using count(*) and + inferred as boolean instead of string #357

karlhorky opened this issue Jan 3, 2025 · 3 comments · Fixed by #359

Comments

@karlhorky
Copy link
Collaborator

karlhorky commented Jan 3, 2025

Describe the bug

Usage of summed count(*) subqueries (using + operator) has yielded string in versions before @ts-safeql/[email protected], with 3.6.1 this instead yields boolean:

// 💥 Query has incorrect type annotation.
//	Expected: { employee_count: string; }
//	Actual: { employee_count: boolean; }[]
await sql<{ employee_count: string }[]>`
  SELECT
    (
      SELECT count(*) FROM caregivers
    ) + (
      SELECT count(*) FROM caregiver_assistants
    ) AS employee_count
`;

Workaround

Cast the expression to text in the query:

// ✅
await sql<{ employee_count: string }[]>`
  SELECT
    (
      (
        SELECT count(*) FROM caregivers
      ) + (
        SELECT count(*) FROM caregiver_assistants
      )
    )::text AS employee_count
`;

To Reproduce

See above

Expected behavior

Inference of string, because pg_typeof() reports bigint, which is a string with Postgres.js

Screenshots

--

Desktop (please complete the following information):

  • OS: macOS Sequoia 15.2 (24C101)
  • PostgreSQL version 14.13
  • Version 3.6.1

Additional context

Originally introduced in this PR:

@Newbie012
Copy link
Collaborator

Oh this is bad... pushing a fix

@karlhorky
Copy link
Collaborator Author

@Newbie012 Thanks for #359!

I can confirm that @ts-safeql/[email protected] correctly infers string now:

// ✅
await sql<{ employee_count: string }[]>`
  SELECT
    (
      SELECT count(*) FROM caregivers
    ) + (
      SELECT count(*) FROM caregiver_assistants
    ) AS employee_count
`;

@karlhorky
Copy link
Collaborator Author

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 a pull request may close this issue.

2 participants