package org.apache.calcite.adapter.jdbc;

import io.pivotal.beach.calcite.programs.BasicForcedRule;
import org.apache.calcite.adapter.jdbc.tools.JdbcRelBuilder;
import org.apache.calcite.adapter.jdbc.tools.JdbcRelBuilderFactory;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.TableModify.Operation;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.logical.LogicalTableModify;

/**
 * Created by tzoloc on 11/25/16.
 */

public class JournalledInsertRule implements BasicForcedRule {
	@Override
	public RelNode apply(RelNode originalRel, JdbcRelBuilderFactory relBuilderFactory) {

		if (!(originalRel instanceof LogicalTableModify)) {
			return null;
		}

		LogicalTableModify tableModify = (LogicalTableModify) originalRel;

		if (!tableModify.getOperation().equals(Operation.INSERT)) {
			return null;
		}

		if (!(tableModify.getInput() instanceof LogicalProject)) {
			return null;
		}

		return LogicalTableModify.create(
				tableModify.getTable(),
				tableModify.getCatalogReader(),
				((LogicalProject) tableModify.getInput()).getInput(),
				Operation.INSERT,
				null, //List < String > updateColumnList,
				null, //List < RexNode > sourceExpressionList,
				tableModify.isFlattened()
		);
	}
}

@@ -679,17 +679,16 @@
 tory) %7B%0A
-%0A
 %09%09if (!(
@@ -995,127 +995,62 @@
 %0A%0A%09%09
-return LogicalTableModify.create(%0A%09%09%09%09tableModify.getTable(),%0A%09%09%09%09tableModify.getCatalogReader(),%0A%09%09%09%09((LogicalProject)
+// Bypass the projection%0A%09%09tableModify.replaceInput(0,
  tab
@@ -1067,17 +1067,17 @@
 etInput(
-)
+0
 ).getInp
@@ -1083,163 +1083,34 @@
 put(
-),%0A%09%09%09%09Operation.INSERT,%0A%09%09%09%09null, //List %3C String %3E updateColumnList,%0A%09%09%09%09null, //List %3C RexNode %3E sourceExpressionList,%0A%09%09%09%09tableModify.isFlattened()%0A%09%09)
+0));%0A%0A%09%09return tableModify
 ;%0A%09%7D

Clarify functionality of JournalledInsertNode by simplifying code