You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From the documentation and especially the "Inspect Data Values" section, it's not clear for me how to get proper value typings based on column types.
From what I understand columnType and columnValue are coming from different function return (probably from getColumnTypes() and value() for instance).
I don't think there's a trivial way to get the data type inferred like that, so in your example do you suggest to cast the corresponding column value based on its column type ?
Example :
if(columnTypes[0].typeId===DuckDBTypeId.VARCHAR){constvalue=result.value(0,1)asDuckDBArrayValue;//fetching same column, row: 1value.items// ok}
Is there a better way using your api ?
The text was updated successfully, but these errors were encountered:
Great question. The API doesn't currently provide a lot of help here. It's a tricky problem, and the best approach probably depends on context.
Because the column types of a result exist only at runtime, it's not possible to infer compile-time types for them. However, perhaps a library such as Zod could be used to declare an expected list of column names & types, validate the result, and return a typed result. Making that work robustly and in general will take some thought & work.
In the meantime, if you know the expected column type, you can use a type assertion (as above) to get values of the expected TypeScript type. The type of a generic value, DuckDBValue is a type union of all valid value types, so the compiler should let you know if you try to make a type assertion that can't ever be true.
Another option is to use one of the provided convenience functions to convert the result data into JSON-serializable objects (i.e. get...Json). This has some cost, but reduces the number of types you have to deal with.
That's a good point about the documentation. The type inference should work for the columnType examples, but not the columnValue examples the way I've written them. I'll think of how to improve that.
It is possible to get type inference for values by using instanceof on the vector. For example:
constcolumnVector=chunk.getColumnVector(columnIndex);if(columnVectorinstanceofDuckDBIntegerVector){constvalue=columnVector.getItem(rowIndex);// The type of value is inferred correctly here.}
Hi,
From the documentation and especially the "Inspect Data Values" section, it's not clear for me how to get proper value typings based on column types.
From what I understand
columnType
andcolumnValue
are coming from different function return (probably fromgetColumnTypes()
andvalue()
for instance).I don't think there's a trivial way to get the data type inferred like that, so in your example do you suggest to cast the corresponding column value based on its column type ?
Example :
Is there a better way using your api ?
The text was updated successfully, but these errors were encountered: