implement join rewrite