io.trino.sql.planner.plan.ApplyNode 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.errorprone.annotations.Immutable;
import io.trino.sql.planner.Symbol;
import io.trino.sql.tree.Node;
import java.util.List;
import java.util.Map;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
@Immutable
public class ApplyNode
extends PlanNode
{
private final PlanNode input;
private final PlanNode subquery;
/**
* Correlation symbols, returned from input (outer plan) used in subquery (inner plan)
*/
private final List correlation;
/**
* Expressions that use subquery symbols.
*
* Subquery expressions are different than other expressions
* in a sense that they might use an entire subquery result
* as an input (e.g: "x IN (subquery)", "x < ALL (subquery)").
* Such expressions are invalid in linear operator context
* (e.g: ProjectNode) in logical plan, but are correct in
* ApplyNode context.
*
* Example 1:
* - expression: input_symbol_X IN (subquery_symbol_Y)
* - meaning: if set consisting of all values for subquery_symbol_Y contains value represented by input_symbol_X
*
* Example 2:
* - expression: input_symbol_X < ALL (subquery_symbol_Y)
* - meaning: if input_symbol_X is smaller than all subquery values represented by subquery_symbol_Y
*
*/
private final Map subqueryAssignments;
/**
* HACK!
* Used for error reporting in case this ApplyNode is not supported
*/
private final Node originSubquery;
@JsonCreator
public ApplyNode(
@JsonProperty("id") PlanNodeId id,
@JsonProperty("input") PlanNode input,
@JsonProperty("subquery") PlanNode subquery,
@JsonProperty("subqueryAssignments") Map subqueryAssignments,
@JsonProperty("correlation") List correlation,
@JsonProperty("originSubquery") Node originSubquery)
{
super(id);
requireNonNull(input, "input is null");
requireNonNull(subquery, "subquery is null");
requireNonNull(subqueryAssignments, "subqueryAssignments is null");
requireNonNull(correlation, "correlation is null");
requireNonNull(originSubquery, "originSubquery is null");
checkArgument(input.getOutputSymbols().containsAll(correlation), "Input does not contain symbols from correlation");
this.input = input;
this.subquery = subquery;
this.subqueryAssignments = subqueryAssignments;
this.correlation = ImmutableList.copyOf(correlation);
this.originSubquery = originSubquery;
}
@JsonProperty("input")
public PlanNode getInput()
{
return input;
}
@JsonProperty("subquery")
public PlanNode getSubquery()
{
return subquery;
}
@JsonProperty("subqueryAssignments")
public Map getSubqueryAssignments()
{
return subqueryAssignments;
}
@JsonProperty("correlation")
public List getCorrelation()
{
return correlation;
}
@JsonProperty("originSubquery")
public Node getOriginSubquery()
{
return originSubquery;
}
@Override
public List getSources()
{
return ImmutableList.of(input, subquery);
}
@Override
@JsonProperty("outputSymbols")
public List getOutputSymbols()
{
return ImmutableList.builder()
.addAll(input.getOutputSymbols())
.addAll(subqueryAssignments.keySet())
.build();
}
@Override
public R accept(PlanVisitor visitor, C context)
{
return visitor.visitApply(this, context);
}
@Override
public PlanNode replaceChildren(List newChildren)
{
checkArgument(newChildren.size() == 2, "expected newChildren to contain 2 nodes");
return new ApplyNode(getId(), newChildren.get(0), newChildren.get(1), subqueryAssignments, correlation, originSubquery);
}
public sealed interface SetExpression
permits In, Exists, QuantifiedComparison
{
List inputs();
}
public record In(Symbol value, Symbol reference)
implements SetExpression
{
@Override
public List inputs()
{
return ImmutableList.of(value, reference);
}
}
public record Exists()
implements SetExpression
{
@Override
public List inputs()
{
return ImmutableList.of();
}
}
public record QuantifiedComparison(Operator operator, Quantifier quantifier, Symbol value, Symbol reference)
implements SetExpression
{
@Override
public List inputs()
{
return ImmutableList.of(value, reference);
}
}
public enum Operator
{
EQUAL,
NOT_EQUAL,
LESS_THAN,
LESS_THAN_OR_EQUAL,
GREATER_THAN,
GREATER_THAN_OR_EQUAL,
}
public enum Quantifier
{
ALL,
ANY,
SOME,
}
}