Skip to content

Converting a MaybeTable Expr (Expr a) into a Expr (Maybe a) #359

@JackKelly-Bellroy

Description

@JackKelly-Bellroy

My initial objective was to convert the result of an optional subquery into a Rel8able record, but I think the issue title describes the root issue.

I am building a query that checks multiple sources for the same data, and I'm trying to return a record-of-Maybes so it's clear which source had the data I'm looking for. My first instinct was to make a fake "table" record:

    data LookupResult f = LookupResult
      { fromSource1 :: Column f (Maybe Int)
      , fromSource2 :: Column f (Maybe Int)
      } deriving stock (Generic)
        deriving anyclass (Rel8able)

Both tables I'm querying have the same primary key, but the tables are slightly different: source1 is a nullable column in a normal database table, declared as a Column f (Maybe Int). source2 is a non-nullable column in a different table, declared as Column f Int. We expect source1 to always exist, so it is fetched using each and filter in the standard rel8 idiom.

source2 is expected to only sometimes contain the row matching our primary key, but its column is NOT NULL, so I am currently fetching it by applying optional to the result of an each/filter query.

This query against source2 gives me a MaybeTable Expr (Expr Int), and I'm not sure how to get an Expr (Maybe Int) that I can store into my record. maybeTable brings me no joy: maybeTable Nothing Just returns Maybe (Expr Int).

I can work around this by assembling the record outside of the run1 call, using fmap. But if I want to do more inside a query, I think it would be neat to be able to derive an Expr (Maybe a) from the corresponding MaybeTable. But is it actually possible to do so?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions