question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLHydra's maxBy causes SqlHydra code to fail if querying an empty table

See original GitHub issue

Noticed this when trying to use a maxBy on an empty table. We were able to work around with a sortByDescending into take 1, (we also could grab a count and branch on whether that returned anything) but wanted to suggest it might be a bit more intuitive to have maxBy handle the null without breaking; perhaps by returning an option?

This problem was encountered using SqlHydra.SqlServer.

I believe this should be reproducible by creating a database with an empty table, creating the schema for said table, and querying for a maxBy on one of the fields. Our particular field was a dateTime2(6) if that is relevant.

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JordanMarrcommented, Aug 19, 2023
let thisWorks = async {
  let! result = selectAsync HydraReader.Read (createContext connString) {
    for record in table<dbo.weather_history > do
    where (record.weather_instrument = 0uy)
    select (maxBy (Some record.entry_timestamp)) 
    tryHead
  }
  return Option.flatten result
}

it’ll work properly because by selecting the max of Some record.entry_timestamp, it makes SqlHydra treat the type as an Option, and it successfully parses DBNull instead of crashing. I wonder if this is worth including in the docs by aggregates? It seems like a fairly common use case where you select an aggregate that might return NULL…

Very creative fix! I think that would definitely be helpful to have in the readme! Also, table names are now generated. See notes for this release.

1reaction
ntwilsoncommented, Aug 18, 2023

Hey @mjdupontMEA (and anyone else stumbling across this), I discovered a workaround without needing modifications. From your earlier example:

let thisFails = 
  selectAsync HydraReader.Read (createContext connString) {
    for record in table<dbo.weather_history > do
    where (record.weather_instrument = 0uy)
    select (maxBy record.entry_timestamp) }

if you change it to

let thisWorks = async {
  let! result = selectAsync HydraReader.Read (createContext connString) {
    for record in table<dbo.weather_history > do
    where (record.weather_instrument = 0uy)
    select (maxBy (Some record.entry_timestamp)) 
    tryHead
  }
  return Option.flatten result
}

it’ll work properly because by selecting the max of Some record.entry_timestamp, it makes SqlHydra treat the type as an Option, and it successfully parses DBNull instead of crashing. I wonder if this is worth including in the docs by aggregates? It seems like a fairly common use case where you select an aggregate that might return NULL…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · JordanMarr/SqlHydra
SqlHydra is a suite of NuGet packages for working with databases in F# ... SQLHydra's maxBy causes SqlHydra code to fail if querying...
Read more >
sql server - Application querying empty tables
Yes many empty tables and queries to empty tables are indication of sloppy coding. But if you are having major performance issues this...
Read more >
sql server 2005 - How to return a failure if a table is empty
I need to add a step to check if a table is empty, and somehow fail the step if it is. It's easy...
Read more >
Copy Activity fails due to empty data returned from Source ...
Copy Activity fails due to empty data returned from Source data store - Microsoft Q&A.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found