io.trino.sql.relational.Expressions 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.relational;
import com.google.common.collect.ImmutableList;
import io.trino.metadata.ResolvedFunction;
import io.trino.spi.type.Type;
import java.util.Arrays;
import java.util.List;
public final class Expressions
{
private Expressions() {}
public static ConstantExpression constant(Object value, Type type)
{
return new ConstantExpression(value, type);
}
public static ConstantExpression constantNull(Type type)
{
return new ConstantExpression(null, type);
}
public static CallExpression call(ResolvedFunction resolvedFunction, RowExpression... arguments)
{
return new CallExpression(resolvedFunction, Arrays.asList(arguments));
}
public static CallExpression call(ResolvedFunction resolvedFunction, List arguments)
{
return new CallExpression(resolvedFunction, arguments);
}
public static InputReferenceExpression field(int field, Type type)
{
return new InputReferenceExpression(field, type);
}
public static List subExpressions(Iterable expressions)
{
ImmutableList.Builder builder = ImmutableList.builder();
for (RowExpression expression : expressions) {
expression.accept(new RowExpressionVisitor()
{
@Override
public Void visitCall(CallExpression call, Void context)
{
builder.add(call);
for (RowExpression argument : call.getArguments()) {
argument.accept(this, context);
}
return null;
}
@Override
public Void visitSpecialForm(SpecialForm specialForm, Void context)
{
builder.add(specialForm);
for (RowExpression argument : specialForm.getArguments()) {
argument.accept(this, context);
}
return null;
}
@Override
public Void visitInputReference(InputReferenceExpression reference, Void context)
{
builder.add(reference);
return null;
}
@Override
public Void visitConstant(ConstantExpression literal, Void context)
{
builder.add(literal);
return null;
}
@Override
public Void visitLambda(LambdaDefinitionExpression lambda, Void context)
{
builder.add(lambda);
lambda.getBody().accept(this, context);
return null;
}
@Override
public Void visitVariableReference(VariableReferenceExpression reference, Void context)
{
builder.add(reference);
return null;
}
}, null);
}
return builder.build();
}
}