Doobie Type Mapping Issue with PostgreSQL NULL Arrays
Nobody has claimed this yet.
- Dominant language
- Scala
- Stars
- 2.2k
- Forks
- 379
- Avg merge
- 14m
- Merged PRs (30d)
- 8
Description
during unit tests Doobie's type checker reports errors on PostgreSQL nullable array columns (text[]) even when the Scala model correctly uses Option[List[A]].
✕ C14 my_array_column ARRAY (_text) NULL → Array[A] Reading a NULL value into Array[A] will result in a runtime failure. Fix this by making the schema type NOT NULL or by changing the Scala type to Option[Array[A]]
Context
- PostgreSQL columns defined as nullable
text[]arrays - Model class fields correctly defined as
Option[List[CustomType]] - Custom value classes extending
AnyValaround String
final case class CustomType(string: String) extends AnyVal - Appropriate Meta instances defined and in scope
Reproduction
- Define PostgreSQL columns as nullable
text[] - Define case class with
Option[List[ValueClass]]fields - Run Doobie's type checker
- Observe type mismatch errors about
Array[A]vsOption[Array[A]]
The issue appears to be in Doobie's internal array type handling rather than in our code, as the model is already correctly using Option[List[A]] types.
Also I need to write custom implicits to be able to map to value classes - it would be nice if it will work out of the box
Option[CustomType1] - works out of the box
Option[List[String]] - works out of the box
Option[List[CustomType1]] - doesn't works
final case class CustomType1(string: String) extends AnyVal
...
final case class CustomType5(string: String) extends AnyVal
implicit val optionListStringMeta: Meta[Option[List[String]]] =
implicitly[Meta[Array[Option[String]]]]
.imap(
_.toList.sequence
)(_.sequence.toArray)
implicit val customType1Meta: Meta[Option[List[CustomType1]]] =
implicitly[Meta[Option[List[String]]]]
.imap(_.map(_.map(CustomType1(_))))(_.map(_.map(_.string)))
...
implicit val customType5Meta: Meta[Option[List[CustomType5]]] =
implicitly[Meta[Option[List[String]]]]
.imap(_.map(_.map(CustomType5(_))))(_.map(_.map(_.string)))
Scala 2.13.16, Doobie 1.0.0-RC8
following imports are used
import cats.implicits.*
import doobie.*
import doobie.implicits.*
import doobie.postgres.implicits.*
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the nullable PostgreSQL text[] case with Scala 2.13.16, Doobie 1.0.0-RC8, and the imports shown. Inspect Doobie's internal array type handling and type-checker behavior for Option[List[CustomType]] versus Array[A]. Done means nullable arrays are checked against the Option-wrapped model type and value-class lists work without the custom Meta implicits.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, scala
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100