Query rewriting with filtering constraints