io.prestosql.sql.tree.BindExpression 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.prestosql.sql.tree;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
/**
* "$INTERNAL$BIND"(value, targetFunction)
*
* When invoked, the returned function inserts the given value as
* the leading argument to the targetFunction. The other arguments are
* passed unchanged.
*
* Bind is fundamentally first class, just like function applications.
* It requires special treatment from the type system. There is no way
* to write out the type of `bind`. The return type of `bind` is function.
* Its type looks somewhat like:
*
* - X, (X) -> Y => () -> Y
*
- X1, (X1, X2) -> Y => (X2) -> Y
*
- X1, (X1, X2, X3) -> Y => (X2, X3) -> Y
*
- X1, (X1, X2, X3, X4) -> Y => (X2, X3, X4) -> Y
*
- ...
*
*
* Lambda capturing is implemented through desugaring in Presto. This
* expression facilitates desugaring.
*/
public class BindExpression
extends Expression
{
private final List values;
// Function expression must be of function type.
// It is not necessarily a lambda. For example, it can be another bind expression.
private final Expression function;
public BindExpression(List values, Expression function)
{
this(Optional.empty(), values, function);
}
public BindExpression(NodeLocation location, List values, Expression function)
{
this(Optional.of(location), values, function);
}
private BindExpression(Optional location, List values, Expression function)
{
super(location);
this.values = requireNonNull(values, "value is null");
this.function = requireNonNull(function, "function is null");
}
public List getValues()
{
return values;
}
public Expression getFunction()
{
return function;
}
@Override
public R accept(AstVisitor visitor, C context)
{
return visitor.visitBindExpression(this, context);
}
@Override
public List getChildren()
{
ImmutableList.Builder nodes = ImmutableList.builder();
return nodes.addAll(values)
.add(function)
.build();
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BindExpression that = (BindExpression) o;
return Objects.equals(values, that.values) &&
Objects.equals(function, that.function);
}
@Override
public int hashCode()
{
return Objects.hash(values, function);
}
}