io.trino.sql.planner.plan.UnnestNode Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.sql.planner.plan;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.errorprone.annotations.Immutable;
import io.trino.sql.planner.Symbol;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;
@Immutable
public class UnnestNode
extends PlanNode
{
private final PlanNode source;
private final List replicateSymbols;
private final List mappings;
private final Optional ordinalitySymbol;
private final JoinType joinType;
@JsonCreator
public UnnestNode(
@JsonProperty("id") PlanNodeId id,
@JsonProperty("source") PlanNode source,
@JsonProperty("replicateSymbols") List replicateSymbols,
@JsonProperty("mappings") List mappings,
@JsonProperty("ordinalitySymbol") Optional ordinalitySymbol,
@JsonProperty("joinType") JoinType joinType)
{
super(id);
this.source = requireNonNull(source, "source is null");
this.replicateSymbols = ImmutableList.copyOf(requireNonNull(replicateSymbols, "replicateSymbols is null"));
checkArgument(source.getOutputSymbols().containsAll(replicateSymbols), "Source does not contain all replicateSymbols");
requireNonNull(mappings, "mappings is null");
checkArgument(!mappings.isEmpty(), "mappings is empty");
this.mappings = ImmutableList.copyOf(mappings);
this.ordinalitySymbol = requireNonNull(ordinalitySymbol, "ordinalitySymbol is null");
this.joinType = requireNonNull(joinType, "joinType is null");
}
@Override
public List getOutputSymbols()
{
ImmutableList.Builder outputSymbolsBuilder = ImmutableList.builder()
.addAll(replicateSymbols)
.addAll(mappings.stream()
.map(Mapping::getOutputs)
.flatMap(Collection::stream)
.collect(toImmutableList()));
ordinalitySymbol.ifPresent(outputSymbolsBuilder::add);
return outputSymbolsBuilder.build();
}
@JsonProperty
public PlanNode getSource()
{
return source;
}
@JsonProperty
public List getReplicateSymbols()
{
return replicateSymbols;
}
@JsonProperty
public List getMappings()
{
return mappings;
}
@JsonProperty
public Optional getOrdinalitySymbol()
{
return ordinalitySymbol;
}
@JsonProperty
public JoinType getJoinType()
{
return joinType;
}
@Override
public List getSources()
{
return ImmutableList.of(source);
}
@Override
public R accept(PlanVisitor visitor, C context)
{
return visitor.visitUnnest(this, context);
}
@Override
public PlanNode replaceChildren(List newChildren)
{
return new UnnestNode(getId(), Iterables.getOnlyElement(newChildren), replicateSymbols, mappings, ordinalitySymbol, joinType);
}
public static class Mapping
{
private final Symbol input;
private final List outputs;
@JsonCreator
public Mapping(
@JsonProperty("input") Symbol input,
@JsonProperty("outputs") List outputs)
{
this.input = requireNonNull(input, "input is null");
requireNonNull(outputs, "outputs is null");
this.outputs = ImmutableList.copyOf(outputs);
}
@JsonProperty
public Symbol getInput()
{
return input;
}
@JsonProperty
public List getOutputs()
{
return outputs;
}
}
}