I had a look at XSB’s CSV handling predicates, notably load_csv/2 and load_dsv/3. The idea was to have a look which of this functionality would fit SWI-Prolog’s CSV reader nicely. The obvious candidate that also came up in our discussion on JSON is the column type specifier, which allows for example for
Asking the first column to be stored as atom, the second converted to integer and the last to a floating point number. That looks good. Trying to implement it though, I got less convinced, as we need to specify
What to do if the conversion fails?
What do to with empty fields? This is a refinement of the first.
XSB prints a warning and stores the failed field as an atom. Printed warnings are hard to handle in programming code though Sensible actions I can see are
As XSB
Raise an exception and stop processing
Skip the row as invalid data
…
Empty fields for typed fields are also problematic. One could use a null-specifier. Does this need to be column-specific, or is that an overkill?
Note that if we do not have column type specifiers, we do not have these problems as CSV fields are strings as far as the CSV standard is concerned.
Conflating error policy with the type spec looks like the wrong axis: whether a bad field should raise, skip the row or fall back to an atom is normally a property of the dataset, not of one column, so a single option on load_csv/2 (something like on_error(error|skip|string)) is easier to document and to keep consistent. Empty fields are the one case where per-column meaning is plausible, but a library default (empty means null, distinct from the empty string) plus a global option probably covers most uses. If you do want refinement, letting the type term carry options, e.g. integer{on_error:skip}, keeps the simple form simple.
An option for specifying CSV files types is a good/useful idea. Then again, and being in the middle of improving Logtalk own csv library, the more I dive into it, the more I wish that this format will go the way of the dinosaurs, preferably sooner rather than later. Nowadays, there are much better interchange formats that happen to have decent specs.
I fully agree. On the other hand, CSV is rather elegant in the sense of being simple, minimal and easily readable. If they just would create a new decent spec …
As is, I’m still doubting. Column specifiers are nice, but if there is is an error there are many sensible ways to deal with the error (stop processing, skip row, replace cell by null, use a fallback translation (e.g., an atom or string), etc. To cover all this in the API makes the API rather complicated.
Unfortunately that is not theory. CSV that is input for notably data analysis with large amounts of data tend to have cells that do not satisfy what you think should be in that column. How you should handle that is pretty much application specific.
Also there is a way to handle errors using dedicated predicate callbacks. Example:
main :- load_csv('myfile.csv', mypred(atom,integer,float)), do_stuf.
% When an error is found the following predicate will be called
csv_error(mypred, _Row, Col, Value, Replacement) :-
Col == 2 ->
term_to_atom(Value, Replacement)
; false. % Will skip the row
The main idea is to make a user to specify csv_error/5 predicate. So library will just have some sensible defaults and will call csv_error(mypred, ....). If it is false – row is skipped, if it is true the replacement is used, if it throws an exception then it aborts.
Give more power to the users, error handling is always application dependent. It is also possible to encode exact error cause as an additional argument. If it is needed for some reason.
And even more of a practical side note, a lot of CSV files that are generated by legacy tools have non-standard and highly variable header. It can look something like:
ACCOUNT REPORT
USER: John Doe
# date,amount
26.02,33.33
This kind of documents also should be handled somehow, the easiest would be either to have a format specifier that will skip first N lines, or to have an API the will accept stream instead of a file name, so I as a user can do custom parsing of a header and then pass the rest to the load_csv.