Query types with Option's support#84
Conversation
|
Looks very good. |
|
@Araq ready for review. I had to refactor the design to lower performance overhead. It's now 1.15x the speed. It also doesn't support row hooks now, only field hooks. Sort of odd since it's just adding a function call. Though I guess a function for each field adds up. |
What? I thought it's all done at compile-time. |
It is compile time, but |
|
@Araq ready to review again. Most of the overhead was due to extra copies into the "option" wrapper and another copy due not binding results directly into the I changed the hook signatures from I cleaned up the macros that handled the typed bindings. Ugh, on stuff where the LLM's don't have much source material they struggle much more. Salvageable, but definitely the result of getting it sorta close and hammering it until it fit. Glad I added a benchmark though, gotta watch these LLMs. ;) Here's what it looks like now: proc fromQueryHook*(val: var seq[string], x: string) =
val = x.split(",")
type
SplitMessageRow = object
parts: seq[string] ## uses fromQueryHook
test "applies fromQueryHook per destination field type":
let rows = query(SplitMessageRow):
select tb_string(typstring as parts)
orderby typstring
check rows[0].parts == @["alice", "bob"]
check rows[1].parts == @["carol", "dave"]
It also supports mapping nullable columns into Option fields:
type
NullableNoteOptionRow = object
id: int
note: Option[string]
test "maps nullable column to Option":
let rows = query(NullableNoteOptionRow):
select tb_nullable(id, note)
orderby id
check rows[0].id == 1
check rows[0].note.isNone
check rows[1].id == 2
check rows[1].note.isSome
check rows[1].note.get == "hello"
|
|
fine for now but row-based null values can be much faster and more elegant. |
What do you mean? As in the entire row is null? |
|
seq[(T, bool)] vs (seq[T], IntSet -- bit i is set if column i is null). This allows pros like me to use the |
Plus an IntSet on a row basis wouldn't change much performance wise. The expensive bit seems to be the |
@Araq what do you think of this? It adds an optional return type to the
queryDSL.This allows moving the DSL forward to support Option/nulls better while maintaining backwards compatability. It make many queries a bit nicer which often just map the tuple to a Nim type anyways. It also supports
fromQueryHookhooks similar jsonutilsto/from. Though perhaps the hooks could have better names.