medly / medly/norm

Code generation fails for query with a conditional parameter

Open
#144 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
19
Forks
21
PR merge metrics
No merged PRs in 30d

Description

Consider the query

SELECT
  COUNT(*)
FROM some_table
  WHERE
    CASE
        WHEN :username IS NULL THEN 1 = 1 ELSE username = :username
      END

The following exception is thrown during code generation:

org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312)
	at org.postgresql.jdbc.PgPreparedStatement.getParameterMetaData(PgPreparedStatement.java:1585)
	at norm.analyzer.SqlAnalyzer.sqlModel(SqlAnalyzer.kt:23)
	at norm.api.NormApi.generate(NormApi.kt:22)
	at norm.cli.NormCli$run$1$2$1.invoke(NormCli.kt:80)
	at norm.cli.NormCli$run$1$2$1.invoke(NormCli.kt:28)
	at norm.fs.IO.process(IO.kt:22)
	at norm.cli.NormCli$run$1.invoke(NormCli.kt:80)
	at norm.cli.NormCli$run$1.invoke(NormCli.kt:28)
	at norm.util.PGKt.withPGConnection(PG.kt:14)
	at norm.cli.NormCli.run(NormCli.kt:68)
	at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:168)
	at com.github.ajalt.clikt.parsers.Parser.parse(Parser.kt:16)
	at com.github.ajalt.clikt.core.CliktCommand.parse(CliktCommand.kt:258)
	at com.github.ajalt.clikt.core.CliktCommand.parse$default(CliktCommand.kt:255)
	at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:273)
	at com.github.ajalt.clikt.core.CliktCommand.main(CliktCommand.kt:298)
	at norm.cli.NormCliKt.main(NormCli.kt:20)

Possible workaround (for postgres) - cast the parameter in the query

SELECT
  COUNT(*)
FROM some_table
  WHERE
    CASE
        WHEN CAST(:username AS VARCHAR) IS NULL THEN 1 = 1 ELSE username = CAST(:username AS VARCHAR)
      END

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at norm.analyzer.SqlAnalyzer.kt:23, where PostgreSQL parameter metadata is queried during code generation, and trace the call from NormApi.generate. Reproduce the failure with the conditional-parameter query in the issue, then verify that generation completes without the PostgreSQL type-inference exception and preserves the expected query handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.