Core/DBLayer: Fixed a few mismatched types and possible invalid memory access with aggregate fields in queries