Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-4804] Support Snapshot operator serialization and deserizali… #2955

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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