Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.metadata;
import com.google.common.collect.ImmutableList;
import io.prestosql.metadata.PolymorphicScalarFunction.PolymorphicScalarFunctionChoice;
import io.prestosql.spi.function.InvocationConvention.InvocationArgumentConvention;
import io.prestosql.spi.function.InvocationConvention.InvocationReturnConvention;
import io.prestosql.spi.function.OperatorType;
import io.prestosql.spi.type.Type;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.prestosql.metadata.FunctionKind.SCALAR;
import static io.prestosql.metadata.Signature.mangleOperatorName;
import static io.prestosql.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION;
import static io.prestosql.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL;
import static io.prestosql.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL;
import static java.util.Arrays.asList;
import static java.util.Collections.nCopies;
import static java.util.Objects.requireNonNull;
public final class PolymorphicScalarFunctionBuilder
{
private final Class> clazz;
private Signature signature;
private boolean nullableResult;
private List argumentDefinitions;
private String description = "";
private Optional hidden = Optional.empty();
private Boolean deterministic;
private final List choices = new ArrayList<>();
public PolymorphicScalarFunctionBuilder(Class> clazz)
{
this.clazz = requireNonNull(clazz, "clazz is null");
}
public PolymorphicScalarFunctionBuilder signature(Signature signature)
{
this.signature = requireNonNull(signature, "signature is null");
this.hidden = Optional.of(hidden.orElse(isOperator(signature)));
return this;
}
public PolymorphicScalarFunctionBuilder nullableResult(boolean nullableResult)
{
this.nullableResult = nullableResult;
return this;
}
public PolymorphicScalarFunctionBuilder argumentDefinitions(FunctionArgumentDefinition... argumentDefinitions)
{
requireNonNull(argumentDefinitions, "argumentDefinitions is null");
checkState(this.argumentDefinitions == null, "The argumentDefinitions method must be invoked only once, and must be invoked before the choice method");
this.argumentDefinitions = ImmutableList.copyOf(argumentDefinitions);
return this;
}
public PolymorphicScalarFunctionBuilder description(String description)
{
this.description = description;
return this;
}
public PolymorphicScalarFunctionBuilder hidden(boolean hidden)
{
this.hidden = Optional.of(hidden);
return this;
}
public PolymorphicScalarFunctionBuilder deterministic(boolean deterministic)
{
this.deterministic = deterministic;
return this;
}
public PolymorphicScalarFunctionBuilder choice(Function choiceSpecification)
{
// if the argumentProperties is not set yet. We assume it is set to the default value.
if (argumentDefinitions == null) {
argumentDefinitions = nCopies(signature.getArgumentTypes().size(), new FunctionArgumentDefinition(false));
}
ChoiceBuilder choiceBuilder = new ChoiceBuilder(clazz, signature);
choiceSpecification.apply(choiceBuilder);
choices.add(choiceBuilder.build());
return this;
}
public SqlScalarFunction build()
{
checkState(signature != null, "signature is null");
checkState(deterministic != null, "deterministic is null");
return new PolymorphicScalarFunction(
new FunctionMetadata(
signature,
nullableResult,
argumentDefinitions,
hidden.orElse(false),
deterministic,
description,
SCALAR),
choices);
}
@SafeVarargs
public static Function> concat(Function>... extraParametersFunctions)
{
return context -> {
ImmutableList.Builder