org.apache.calcite.rel.logical.LogicalTableModify Java Examples

The following examples show how to use org.apache.calcite.rel.logical.LogicalTableModify. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Prepare.java    From calcite with Apache License 2.0 6 votes vote down vote up
public PreparedResultImpl(
    RelDataType rowType,
    RelDataType parameterRowType,
    List<List<String>> fieldOrigins,
    List<RelCollation> collations,
    RelNode rootRel,
    LogicalTableModify.Operation tableModOp,
    boolean isDml) {
  this.rowType = Objects.requireNonNull(rowType);
  this.parameterRowType = Objects.requireNonNull(parameterRowType);
  this.fieldOrigins = Objects.requireNonNull(fieldOrigins);
  this.collations = ImmutableList.copyOf(collations);
  this.rootRel = Objects.requireNonNull(rootRel);
  this.tableModOp = tableModOp;
  this.isDml = isDml;
}
 
Example #2
Source File: EnumerableTableModifyExtensionRule.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode convert(RelNode rel) {
    final LogicalTableModify modify =
        (LogicalTableModify) rel;
    final ModifiableTable modifiableTable =
        modify.getTable().unwrap(ModifiableTable.class);
    if (modifiableTable == null) {
        return null;
    }
    final RelTraitSet traitSet =
        modify.getTraitSet().replace(EnumerableConvention.INSTANCE);
    return new EnumerableTableModifyExtension(
        modify.getCluster(), traitSet,
        modify.getTable(),
        modify.getCatalogReader(),
        convert(modify.getInput(), traitSet),
        modify.getOperation(),
        modify.getUpdateColumnList(),
        modify.getSourceExpressionList(),
        modify.isFlattened());
}
 
Example #3
Source File: EnumerableTableModifyRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelNode convert(RelNode rel) {
  final LogicalTableModify modify =
      (LogicalTableModify) rel;
  final ModifiableTable modifiableTable =
      modify.getTable().unwrap(ModifiableTable.class);
  if (modifiableTable == null) {
    return null;
  }
  final RelTraitSet traitSet =
      modify.getTraitSet().replace(EnumerableConvention.INSTANCE);
  return new EnumerableTableModify(
      modify.getCluster(), traitSet,
      modify.getTable(),
      modify.getCatalogReader(),
      convert(modify.getInput(), traitSet),
      modify.getOperation(),
      modify.getUpdateColumnList(),
      modify.getSourceExpressionList(),
      modify.isFlattened());
}
 
Example #4
Source File: Prepare.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected LogicalTableModify.Operation mapTableModOp(
    boolean isDml, SqlKind sqlKind) {
  if (!isDml) {
    return null;
  }
  switch (sqlKind) {
  case INSERT:
    return LogicalTableModify.Operation.INSERT;
  case DELETE:
    return LogicalTableModify.Operation.DELETE;
  case MERGE:
    return LogicalTableModify.Operation.MERGE;
  case UPDATE:
    return LogicalTableModify.Operation.UPDATE;
  default:
    return null;
  }
}
 
Example #5
Source File: JdbcRelBuilder.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
public JdbcRelBuilder insertCopying(
		LogicalTableModify original,
		Table table
) {
	List<String> name = JdbcTableUtils.getQualifiedName(original.getTable(), table);

	push(new LogicalTableModify(
			cluster,
			original.getTraitSet(),
			relOptSchema.getTableForMember(name),
			original.getCatalogReader(),
			peek(),
			TableModify.Operation.INSERT,
			null,
			null,
			original.isFlattened()
	));
	return this;
}
 
Example #6
Source File: RelWriterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTableModifyInsert() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  RelNode project = builder
      .scan("EMP")
      .project(builder.fields(), ImmutableList.of(), true)
      .build();
  LogicalTableModify modify = LogicalTableModify.create(
      project.getInput(0).getTable(),
      (Prepare.CatalogReader) project.getInput(0).getTable().getRelOptSchema(),
      project,
      TableModify.Operation.INSERT,
      null,
      null,
      false);
  String relJson = RelOptUtil.dumpPlan("", modify,
      SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s = deserializeAndDumpToTextFormat(getSchema(modify), relJson);
  final String expected = ""
      + "LogicalTableModify(table=[[scott, EMP]], operation=[INSERT], flattened=[false])\n"
      + "  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], "
      + "COMM=[$6], DEPTNO=[$7])\n"
      + "    LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example #7
Source File: AbstractForcedRule.java    From calcite-sql-rewriter with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode apply(RelNode originalRel, JdbcRelBuilderFactory relBuilderFactory) {

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

	LogicalTableModify tableModify = (LogicalTableModify) originalRel;

	if (tableModify.getOperation() != operation) {
		return null;
	}

	Table baseTable = JdbcTableUtils.getJdbcTable(tableModify);
	if (!(baseTable instanceof JournalledJdbcTable)) {
		// Not a journal table; nothing to do
		return null;
	}
	JournalledJdbcTable journalTable = (JournalledJdbcTable) baseTable;

	return doApply(tableModify, journalTable, relBuilderFactory);
}
 
Example #8
Source File: SqlToRelConverterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testRelShuttleForLogicalTableModify() {
  final String sql = "insert into emp select * from emp";
  final LogicalTableModify rel = (LogicalTableModify) tester.convertSqlToRel(sql).rel;
  final List<RelNode> rels = new ArrayList<>();
  final RelShuttleImpl visitor = new RelShuttleImpl() {
    @Override public RelNode visit(LogicalTableModify modify) {
      RelNode visitedRel = super.visit(modify);
      rels.add(visitedRel);
      return visitedRel;
    }
  };
  visitor.visit(rel);
  assertThat(rels.size(), is(1));
  assertThat(rels.get(0), isA(LogicalTableModify.class));
}
 
Example #9
Source File: JdbcTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public TableModify toModificationRel(RelOptCluster cluster,
    RelOptTable table, CatalogReader catalogReader, RelNode input,
    Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  jdbcSchema.convention.register(cluster.getPlanner());

  return new LogicalTableModify(cluster, cluster.traitSetOf(Convention.NONE),
      table, catalogReader, input, operation, updateColumnList,
      sourceExpressionList, flattened);
}
 
Example #10
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalTableModify rel) {
  LogicalTableModify newRel =
      LogicalTableModify.create(
          rel.getTable(),
          rel.getCatalogReader(),
          getNewForOldRel(rel.getInput()),
          rel.getOperation(),
          rel.getUpdateColumnList(),
          rel.getSourceExpressionList(),
          true);
  setNewForOldRel(rel, newRel);
}
 
Example #11
Source File: ListTransientTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public TableModify toModificationRel(
    RelOptCluster cluster,
    RelOptTable table,
    Prepare.CatalogReader catalogReader,
    RelNode child,
    TableModify.Operation operation,
    List<String> updateColumnList,
    List<RexNode> sourceExpressionList,
    boolean flattened) {
  return LogicalTableModify.create(table, catalogReader, child, operation,
      updateColumnList, sourceExpressionList, flattened);
}
 
Example #12
Source File: FrameworksTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public TableModify toModificationRel(RelOptCluster cluster,
    RelOptTable table, Prepare.CatalogReader catalogReader, RelNode child,
    TableModify.Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  return LogicalTableModify.create(table, catalogReader, child, operation,
      updateColumnList, sourceExpressionList, flattened);
}
 
Example #13
Source File: AbstractModifiableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public TableModify toModificationRel(
    RelOptCluster cluster,
    RelOptTable table,
    Prepare.CatalogReader catalogReader,
    RelNode child,
    TableModify.Operation operation,
    List<String> updateColumnList,
    List<RexNode> sourceExpressionList,
    boolean flattened) {
  return LogicalTableModify.create(table, catalogReader, child, operation,
      updateColumnList, sourceExpressionList, flattened);
}
 
Example #14
Source File: SqlToRelConverterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-3826">[CALCITE-3826]
 * UPDATE assigns wrong type to bind variables</a>.
 */
@Test void testDynamicParamTypesInUpdate() {
  RelNode rel = tester.convertSqlToRel("update emp set sal = ?, ename = ? where empno = ?").rel;
  LogicalTableModify modify = (LogicalTableModify) rel;
  List<RexNode> parameters = modify.getSourceExpressionList();
  assertThat(parameters.size(), is(2));
  assertThat(parameters.get(0).getType().getSqlTypeName(), is(SqlTypeName.INTEGER));
  assertThat(parameters.get(1).getType().getSqlTypeName(), is(SqlTypeName.VARCHAR));
}
 
Example #15
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTableModifyUpdate() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  RelNode filter = builder
      .scan("EMP")
      .filter(
          builder.call(
              SqlStdOperatorTable.EQUALS,
              builder.field("JOB"),
              builder.literal("c")))
      .build();
  LogicalTableModify modify = LogicalTableModify.create(
      filter.getInput(0).getTable(),
      (Prepare.CatalogReader) filter.getInput(0).getTable().getRelOptSchema(),
      filter,
      TableModify.Operation.UPDATE,
      ImmutableList.of("ENAME"),
      ImmutableList.of(builder.literal("a")),
      false);
  String relJson = RelOptUtil.dumpPlan("", modify,
      SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s = deserializeAndDumpToTextFormat(getSchema(modify), relJson);
  final String expected = ""
      + "LogicalTableModify(table=[[scott, EMP]], operation=[UPDATE], updateColumnList=[[ENAME]],"
      + " sourceExpressionList=[['a']], flattened=[false])\n"
      + "  LogicalFilter(condition=[=($2, 'c')])\n"
      + "    LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example #16
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTableModifyDelete() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  RelNode filter = builder
      .scan("EMP")
      .filter(
          builder.call(
              SqlStdOperatorTable.EQUALS,
              builder.field("JOB"),
              builder.literal("c")))
      .build();
  LogicalTableModify modify = LogicalTableModify.create(
      filter.getInput(0).getTable(),
      (Prepare.CatalogReader) filter.getInput(0).getTable().getRelOptSchema(),
      filter,
      TableModify.Operation.DELETE,
      null,
      null,
      false);
  String relJson = RelOptUtil.dumpPlan("", modify,
      SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s = deserializeAndDumpToTextFormat(getSchema(modify), relJson);
  final String expected = ""
      + "LogicalTableModify(table=[[scott, EMP]], operation=[DELETE], flattened=[false])\n"
      + "  LogicalFilter(condition=[=($2, 'c')])\n"
      + "    LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example #17
Source File: Table.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Override
public TableModify toModificationRel(
    RelOptCluster cluster,
    RelOptTable table,
    Prepare.CatalogReader catalogReader,
    RelNode child,
    TableModify.Operation operation,
    List<String> updateColumnList,
    List<RexNode> sourceExpressionList,
    boolean flattened) {
    return LogicalTableModify.create(table, catalogReader, child, operation,
        updateColumnList, sourceExpressionList, flattened);
}
 
Example #18
Source File: RelFieldTrimmer.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
 * {@link org.apache.calcite.rel.logical.LogicalTableModify}.
 */
public TrimResult trimFields(LogicalTableModify modifier, ImmutableBitSet fieldsUsed,
        Set<RelDataTypeField> extraFields) {
    // Ignore what consumer wants. We always project all columns.
    Util.discard(fieldsUsed);

    final RelDataType rowType = modifier.getRowType();
    final int fieldCount = rowType.getFieldCount();
    RelNode input = modifier.getInput();

    // We want all fields from the child.
    final int inputFieldCount = input.getRowType().getFieldCount();
    final ImmutableBitSet inputFieldsUsed = ImmutableBitSet.range(inputFieldCount);

    // Create input with trimmed columns.
    final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
    TrimResult trimResult = trimChild(modifier, input, inputFieldsUsed, inputExtraFields);
    RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;
    if (!inputMapping.isIdentity()) {
        // We asked for all fields. Can't believe that the child decided
        // to permute them!
        throw new AssertionError("Expected identity mapping, got " + inputMapping);
    }

    LogicalTableModify newModifier = modifier;
    if (newInput != input) {
        newModifier = modifier.copy(modifier.getTraitSet(), Collections.singletonList(newInput));
    }
    assert newModifier.getClass() == modifier.getClass();

    // Always project all fields.
    Mapping mapping = Mappings.createIdentity(fieldCount);
    return result(newModifier, mapping);
}
 
Example #19
Source File: RelWriterTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testTableModifyMerge() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  RelNode deptScan = builder.scan("DEPT").build();
  RelNode empScan = builder.scan("EMP").build();
  builder.push(deptScan);
  builder.push(empScan);
  RelNode project = builder
      .join(JoinRelType.LEFT,
          builder.call(
              SqlStdOperatorTable.EQUALS,
              builder.field(2, 0, "DEPTNO"),
              builder.field(2, 1, "DEPTNO")))
      .project(
          builder.literal(0),
          builder.literal("x"),
          builder.literal("x"),
          builder.literal(0),
          builder.literal("20200501 10:00:00"),
          builder.literal(0),
          builder.literal(0),
          builder.literal(0),
          builder.literal("false"),
          builder.field(1, 0, 2),
          builder.field(1, 0, 3),
          builder.field(1, 0, 4),
          builder.field(1, 0, 5),
          builder.field(1, 0, 6),
          builder.field(1, 0, 7),
          builder.field(1, 0, 8),
          builder.field(1, 0, 9),
          builder.field(1, 0, 10),
          builder.literal("a"))
      .build();
  // for sql:
  // merge into emp using dept on emp.deptno = dept.deptno
  // when matched then update set job = 'a'
  // when not matched then insert values(0, 'x', 'x', 0, '20200501 10:00:00', 0, 0, 0, 0)
  LogicalTableModify modify = LogicalTableModify.create(
      empScan.getTable(),
      (Prepare.CatalogReader) empScan.getTable().getRelOptSchema(),
      project,
      TableModify.Operation.MERGE,
      ImmutableList.of("ENAME"),
      null,
      false);
  String relJson = RelOptUtil.dumpPlan("", modify,
      SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s = deserializeAndDumpToTextFormat(getSchema(modify), relJson);
  final String expected = ""
      + "LogicalTableModify(table=[[scott, EMP]], operation=[MERGE], "
      + "updateColumnList=[[ENAME]], flattened=[false])\n"
      + "  LogicalProject($f0=[0], $f1=['x'], $f2=['x'], $f3=[0], $f4=['20200501 10:00:00'], "
      + "$f5=[0], $f6=[0], $f7=[0], $f8=['false'], LOC=[$2], EMPNO=[$3], ENAME=[$4], JOB=[$5], "
      + "MGR=[$6], HIREDATE=[$7], SAL=[$8], COMM=[$9], DEPTNO=[$10], $f18=['a'])\n"
      + "    LogicalJoin(condition=[=($0, $10)], joinType=[left])\n"
      + "      LogicalTableScan(table=[[scott, DEPT]])\n"
      + "      LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example #20
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
 * {@link org.apache.calcite.rel.logical.LogicalTableModify}.
 */
public TrimResult trimFields(
    LogicalTableModify modifier,
    ImmutableBitSet fieldsUsed,
    Set<RelDataTypeField> extraFields) {
  // Ignore what consumer wants. We always project all columns.
  Util.discard(fieldsUsed);

  final RelDataType rowType = modifier.getRowType();
  final int fieldCount = rowType.getFieldCount();
  RelNode input = modifier.getInput();

  // We want all fields from the child.
  final int inputFieldCount = input.getRowType().getFieldCount();
  final ImmutableBitSet inputFieldsUsed =
      ImmutableBitSet.range(inputFieldCount);

  // Create input with trimmed columns.
  final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
  TrimResult trimResult =
      trimChild(modifier, input, inputFieldsUsed, inputExtraFields);
  RelNode newInput = trimResult.left;
  final Mapping inputMapping = trimResult.right;
  if (!inputMapping.isIdentity()) {
    // We asked for all fields. Can't believe that the child decided
    // to permute them!
    throw new AssertionError(
        "Expected identity mapping, got " + inputMapping);
  }

  LogicalTableModify newModifier = modifier;
  if (newInput != input) {
    newModifier =
        modifier.copy(
            modifier.getTraitSet(),
            Collections.singletonList(newInput));
  }
  assert newModifier.getClass() == modifier.getClass();

  // Always project all fields.
  Mapping mapping = Mappings.createIdentity(fieldCount);
  return result(newModifier, mapping);
}
 
Example #21
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void rewriteRel(LogicalTableModify rel) {
    LogicalTableModify newRel = LogicalTableModify.create(rel.getTable(), rel.getCatalogReader(),
            getNewForOldRel(rel.getInput()), rel.getOperation(), rel.getUpdateColumnList(),
            rel.getSourceExpressionList(), true);
    setNewForOldRel(rel, newRel);
}
 
Example #22
Source File: EnumerableTableModifyRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an EnumerableTableModifyRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public EnumerableTableModifyRule(RelBuilderFactory relBuilderFactory) {
  super(LogicalTableModify.class, (Predicate<RelNode>) r -> true,
      Convention.NONE, EnumerableConvention.INSTANCE, relBuilderFactory,
      "EnumerableTableModificationRule");
}
 
Example #23
Source File: Prepare.java    From calcite with Apache License 2.0 4 votes vote down vote up
public LogicalTableModify.Operation getTableModOp() {
  return tableModOp;
}
 
Example #24
Source File: Prepare.java    From calcite with Apache License 2.0 4 votes vote down vote up
public LogicalTableModify.Operation getTableModOp() {
  return null;
}
 
Example #25
Source File: CalciteMaterializer.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalTableModify modify) {
  return modify;
}
 
Example #26
Source File: RelShuttleImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalTableModify modify) {
  return visitChildren(modify);
}
 
Example #27
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 4 votes vote down vote up
@Override
public TableModify toModificationRel(RelOptCluster cluster, RelOptTable table, Prepare.CatalogReader catalogReader, RelNode child, TableModify.Operation operation, List<String> updateColumnList, List<RexNode> sourceExpressionList, boolean flattened) {
    return LogicalTableModify.create(table, catalogReader, child, operation,
            updateColumnList, sourceExpressionList, flattened);
}
 
Example #28
Source File: AbstractForcedRule.java    From calcite-sql-rewriter with Apache License 2.0 4 votes vote down vote up
abstract public RelNode doApply(
LogicalTableModify tableModify, JournalledJdbcTable journalTable,
JdbcRelBuilderFactory relBuilderFactory);
 
Example #29
Source File: JournalledUpdateRule.java    From calcite-sql-rewriter with Apache License 2.0 4 votes vote down vote up
@Override
public RelNode doApply(LogicalTableModify tableModify, JournalledJdbcTable journalTable,
		JdbcRelBuilderFactory relBuilderFactory) {

	if (!(tableModify.getInput() instanceof LogicalProject)) {
		throw new IllegalStateException("Unknown Calcite UPDATE structure");
	}

	String versionField = journalTable.getVersionField();

	// Merge the Update's update column expression into the target INSERT
	LogicalProject project = (LogicalProject) tableModify.getInput();
	List<RexNode> desiredFields = new ArrayList<>();
	List<String> desiredNames = new ArrayList<>();

	for (Pair<RexNode, String> field : project.getNamedProjects()) {
		if (field.getKey() instanceof RexInputRef) {
			int index = tableModify.getUpdateColumnList().indexOf(field.getValue());
			if (index != -1) {
				desiredFields.add(tableModify.getSourceExpressionList().get(index));
			}
			else {
				desiredFields.add(field.getKey());
			}
			desiredNames.add(field.getValue());
		}
	}

	JdbcRelBuilder relBuilder = relBuilderFactory.create(
			tableModify.getCluster(),
			tableModify.getTable().getRelOptSchema()
	);

	relBuilder.push(project.getInput());

	JournalVersionType versionType = journalTable.getVersionType();
	if (!versionType.isValidSqlType(relBuilder.field(versionField).getType().getSqlTypeName())) {
		throw new IllegalStateException("Incorrect journalVersionType! Column 'version_number' is of type: "
				+ relBuilder.field(versionField).getType().getSqlTypeName()
				+ " but the journalVersionType is " + versionType);
	}
	if (versionType.updateRequiresExplicitVersion()) {
		RexNode newVersion = versionType.incrementVersion(relBuilder, relBuilder.field(versionField));
		desiredFields.add(newVersion);
		desiredNames.add(versionField);
	}

	relBuilder.project(desiredFields, desiredNames);

	// Convert the UPDATE into INSERT TableModify operations
	relBuilder.insertCopying(
			tableModify,
			journalTable.getJournalTable()
	);

	return relBuilder.build();
}
 
Example #30
Source File: JournalledInsertRule.java    From calcite-sql-rewriter with Apache License 2.0 4 votes vote down vote up
@Override
public RelNode doApply(LogicalTableModify tableModify, JournalledJdbcTable journalTable,
		JdbcRelBuilderFactory relBuilderFactory) {

	JdbcRelBuilder relBuilder = relBuilderFactory.create(
			tableModify.getCluster(),
			tableModify.getTable().getRelOptSchema()
	);

	RelNode input = tableModify.getInput();
	if (input instanceof LogicalValues) {

		// TODO: do we need to do anything here?
		relBuilder.push(input);

	}
	else if (input instanceof LogicalProject) {

		LogicalProject project = (LogicalProject) input;
		List<RexNode> desiredFields = new ArrayList<>();
		List<String> desiredNames = new ArrayList<>();
		for (Pair<RexNode, String> field : project.getNamedProjects()) {
			if (field.getKey() instanceof RexInputRef) {
				desiredFields.add(field.getKey());
				desiredNames.add(field.getValue());
			}
		}

		relBuilder.push(project.getInput());
		relBuilder.project(desiredFields, desiredNames);

	}
	else {
		throw new IllegalStateException("Unknown Calcite INSERT structure");
	}

	relBuilder.insertCopying(
			tableModify,
			journalTable.getJournalTable()
	);

	return relBuilder.build();

}