Fix handling of duplicate fields
Add the corresponding test query