Fix bug in the export query function