Some improvement to sparse input support

Hi @fareshedayati,

You'll find here a first batch of improvements for your pr #2984.

Best,
Arnaud