Skip to content

Commit

Permalink
[CALCITE-4804] Support Snapshot operator serialization and deserizali…
Browse files Browse the repository at this point in the history
…zation

This closes apache#2955
  • Loading branch information
JiajunBernoulli authored and tanclary committed Nov 28, 2022
1 parent 630059b commit 9c8fa96
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/main/java/org/apache/calcite/rel/core/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelInput;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelWriter;
import org.apache.calcite.rel.SingleRel;
Expand All @@ -36,6 +37,8 @@
import java.util.List;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

/**
* Relational expression that returns the contents of a relation expression as
* it was at a given time in the past.
Expand All @@ -56,6 +59,17 @@ public abstract class Snapshot extends SingleRel implements Hintable {

//~ Constructors -----------------------------------------------------------

/**
* Creates a Snapshot by parsing serialized output.
*/
public Snapshot(RelInput input) {
this(input.getCluster(),
input.getTraitSet(),
ImmutableList.of(),
input.getInput(),
requireNonNull(input.getExpression("period"), "period"));
}

/**
* Creates a Snapshot.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelCollationTraitDef;
import org.apache.calcite.rel.RelDistributionTraitDef;
import org.apache.calcite.rel.RelInput;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Snapshot;
import org.apache.calcite.rel.hint.RelHint;
Expand All @@ -40,6 +41,14 @@
public class LogicalSnapshot extends Snapshot {

//~ Constructors -----------------------------------------------------------

/**
* Creates a LogicalSnapshot by parsing serialized output.
*/
public LogicalSnapshot(RelInput input) {
super(input);
}

/**
* Creates a LogicalSnapshot.
*
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.JsonBuilder;
import org.apache.calcite.util.TestUtil;
import org.apache.calcite.util.TimestampString;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -867,6 +868,30 @@ void testAggregateWithAlias(SqlExplainFormat format) {
assertThat(s, isLinux(expected));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4804">[CALCITE-4804]
* Support Snapshot operator serialization and deserizalization</a>. */
@Test void testSnapshot() {
// Equivalent SQL:
// SELECT *
// FROM products_temporal FOR SYSTEM_TIME AS OF TIMESTAMP '2011-07-20 12:34:56'
final RelBuilder builder = RelBuilder.create(RelBuilderTest.config().build());
RelNode root =
builder.scan("products_temporal")
.snapshot(
builder.getRexBuilder().makeTimestampLiteral(
new TimestampString("2011-07-20 12:34:56"), 0))
.build();

RelJsonWriter jsonWriter = new RelJsonWriter();
root.explain(jsonWriter);
String relJson = jsonWriter.asString();
String s = deserializeAndDumpToTextFormat(getSchema(root), relJson);
String expected = "LogicalSnapshot(period=[2011-07-20 12:34:56])\n"
+ " LogicalTableScan(table=[[scott, products_temporal]])\n";
assertThat(s, isLinux(expected));
}

@Test void testDeserializeInvalidOperatorName() {
final FrameworkConfig config = RelBuilderTest.config().build();
final RelBuilder builder = RelBuilder.create(config);
Expand Down

0 comments on commit 9c8fa96

Please sign in to comment.