ExpandViews extends LogicalOperatorTransform { @Override public OperatorNode<SequenceOperator> visitSequenceOperator(OperatorNode<SequenceOperator> node) { if (node.getOperator() != SequenceOperator.SCAN) { return super.visitSequenceOperator(node); } List<String> name = (List<String>) node.getArgument(0); List<OperatorNode<ExpressionOperator>> arguments = (List<OperatorNode<ExpressionOperator>>) node.getArgument(1); if (arguments.size() > 0) { return super.visitSequenceOperator(node); } OperatorNode<SequenceOperator> view = scope.getView(name); if (view == null) { return super.visitSequenceOperator(node); } view = view.copy(); String alias = (String) node.getAnnotation("alias"); if (alias != null) { view.putAnnotation("alias", alias); } return view; } ExpandViews(ViewRegistry scope); @Override OperatorNode<SequenceOperator> visitSequenceOperator(OperatorNode<SequenceOperator> node);  }
@Test public void testExpandView() throws IOException { OperatorNode<SequenceOperator> query = OperatorNode.create(SequenceOperator.FILTER, OperatorNode.create(SequenceOperator.JOIN, OperatorNode.create(SequenceOperator.SCAN, Lists.newArrayList("left"), ImmutableList.of()).putAnnotation("alias", "left"), OperatorNode.create(SequenceOperator.SCAN, Lists.newArrayList("right"), ImmutableList.of()).putAnnotation("alias", "right"), OperatorNode.create(ExpressionOperator.EQ, OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "left"), "id"), OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "right"), "id"))), OperatorNode.create(ExpressionOperator.EQ, OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "left"), "id"), OperatorNode.create(ExpressionOperator.LITERAL, "1"))); OperatorNode<SequenceOperator> transformed = new JoinFilterPushDown().visitSequenceOperator(query); Assert.assertEquals(transformed.getOperator(), SequenceOperator.JOIN); Assert.assertEquals(((OperatorNode) transformed.getArgument(0)).getOperator(), SequenceOperator.FILTER); Assert.assertEquals(((OperatorNode) transformed.getArgument(1)).getOperator(), SequenceOperator.SCAN); Registry scope = new Registry(); scope.defineView("left", OperatorNode.create(SequenceOperator.FILTER, OperatorNode.create(SequenceOperator.SCAN, Lists.newArrayList("fancy"), ImmutableList.of()).putAnnotation("alias", "fancy"), OperatorNode.create(ExpressionOperator.EQ, OperatorNode.create(ExpressionOperator.PROPREF, OperatorNode.create(ExpressionOperator.READ_RECORD, "fancy"), "id"), OperatorNode.create(ExpressionOperator.LITERAL, "1")))); transformed = new ExpandViews(scope).visitSequenceOperator(transformed); Assert.assertEquals(transformed.getOperator(), SequenceOperator.JOIN); Assert.assertEquals(((OperatorNode) transformed.getArgument(0)).getOperator(), SequenceOperator.FILTER); Assert.assertEquals(((OperatorNode) transformed.getArgument(1)).getOperator(), SequenceOperator.SCAN); OperatorNode<SequenceOperator> left = (OperatorNode<SequenceOperator>) transformed.getArgument(0); Assert.assertEquals(((OperatorNode) left.getArgument(0)).getOperator(), SequenceOperator.FILTER); OperatorNode<SequenceOperator> scan = (OperatorNode) ((OperatorNode) left.getArgument(0)).getArgument(0); Assert.assertEquals(scan.getOperator(), SequenceOperator.SCAN); Assert.assertEquals(scan.getArgument(0), ImmutableList.of("fancy")); }