Skip to content

Commit

Permalink
Refactor format, ignore3vl, pi, substr to new function registry
Browse files Browse the repository at this point in the history
  • Loading branch information
seut authored and mergify[bot] committed Mar 31, 2020
1 parent 8be09e6 commit f9e4c32
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 62 deletions.
48 changes: 19 additions & 29 deletions sql/src/main/java/io/crate/expression/scalar/FormatFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,40 @@

package io.crate.expression.scalar;

import com.google.common.base.Preconditions;
import io.crate.data.Input;
import io.crate.metadata.BaseFunctionResolver;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionImplementation;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.Scalar;
import io.crate.metadata.functions.params.FuncParams;
import io.crate.types.DataType;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import java.util.List;
import java.util.Locale;

import static io.crate.metadata.functions.params.Param.ANY;
import static io.crate.metadata.functions.params.Param.STRING;
import static io.crate.metadata.functions.TypeVariableConstraint.typeVariableOfAnyType;
import static io.crate.types.TypeSignature.parseTypeSignature;

public class FormatFunction extends Scalar<String, Object> {

public static final String NAME = "format";
private FunctionInfo info;

public static void register(ScalarFunctionModule module) {
module.register(NAME, new Resolver());
module.register(
Signature.scalar(
NAME,
DataTypes.STRING.getTypeSignature(),
parseTypeSignature("E"),
DataTypes.STRING.getTypeSignature()
)
.withTypeVariableConstraints(typeVariableOfAnyType("E"))
.withVariableArity(),
argumentTypes ->
new FormatFunction(
new FunctionInfo(new FunctionIdent(NAME, argumentTypes), DataTypes.STRING)
)

);
}

private FormatFunction(FunctionInfo info) {
Expand All @@ -72,23 +81,4 @@ public final String evaluate(TransactionContext txnCtx, Input<Object>... args) {
public FunctionInfo info() {
return info;
}

private static class Resolver extends BaseFunctionResolver {

protected Resolver() {
super(FuncParams.builder(STRING)
.withIndependentVarArgs(ANY)
.build());
}

private static FunctionInfo createInfo(List<DataType> types) {
return new FunctionInfo(new FunctionIdent(NAME, types), DataTypes.STRING);
}

@Override
public FunctionImplementation getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
Preconditions.checkArgument(dataTypes.size() > 1 && dataTypes.get(0) == DataTypes.STRING);
return new FormatFunction(createInfo(dataTypes));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import io.crate.data.Input;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.Scalar;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import java.util.Collections;
Expand All @@ -50,7 +51,14 @@ public class Ignore3vlFunction extends Scalar<Boolean, Boolean> {
new FunctionIdent(NAME, Collections.singletonList(DataTypes.BOOLEAN)), DataTypes.BOOLEAN);

public static void register(ScalarFunctionModule module) {
module.register(new Ignore3vlFunction());
module.register(
Signature.scalar(
NAME,
DataTypes.BOOLEAN.getTypeSignature(),
DataTypes.BOOLEAN.getTypeSignature()
),
argumentTypes -> new Ignore3vlFunction()
);
}

@Override
Expand Down
9 changes: 8 additions & 1 deletion sql/src/main/java/io/crate/expression/scalar/PiFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.Scalar;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import java.util.List;
Expand All @@ -37,7 +38,13 @@ public final class PiFunction extends Scalar<Double, Object> {
private final FunctionInfo info;

public static void register(ScalarFunctionModule module) {
module.register(new PiFunction());
module.register(
Signature.scalar(
NAME,
DataTypes.DOUBLE.getTypeSignature()
),
args -> new PiFunction()
);
}

public PiFunction() {
Expand Down
57 changes: 27 additions & 30 deletions sql/src/main/java/io/crate/expression/scalar/SubstrFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@

import com.google.common.annotations.VisibleForTesting;
import io.crate.data.Input;
import io.crate.metadata.BaseFunctionResolver;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionImplementation;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.Scalar;
import io.crate.metadata.functions.params.FuncParams;
import io.crate.metadata.functions.params.Param;
import io.crate.types.DataType;
import io.crate.metadata.TransactionContext;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import javax.annotation.Nonnull;
import java.util.List;

public class SubstrFunction extends Scalar<String, Object> {

Expand All @@ -48,7 +43,31 @@ private SubstrFunction(FunctionInfo info) {
}

public static void register(ScalarFunctionModule module) {
module.register(NAME, new Resolver());
module.register(
Signature.scalar(
NAME,
DataTypes.STRING.getTypeSignature(),
DataTypes.INTEGER.getTypeSignature(),
DataTypes.STRING.getTypeSignature()
),
argumentTypes ->
new SubstrFunction(
new FunctionInfo(new FunctionIdent(NAME, argumentTypes), DataTypes.STRING)
)
);
module.register(
Signature.scalar(
NAME,
DataTypes.STRING.getTypeSignature(),
DataTypes.INTEGER.getTypeSignature(),
DataTypes.INTEGER.getTypeSignature(),
DataTypes.STRING.getTypeSignature()
),
argumentTypes ->
new SubstrFunction(
new FunctionInfo(new FunctionIdent(NAME, argumentTypes), DataTypes.STRING)
)
);
}

@Override
Expand Down Expand Up @@ -104,26 +123,4 @@ static String evaluate(@Nonnull String inputStr, int beginIdx, int len) {
static String substring(String value, int begin, int end) {
return value.substring(begin, end);
}

private static class Resolver extends BaseFunctionResolver {

private static final Param INTEGER_COMPATIBLE_TYPES = Param.of(DataTypes.BYTE, DataTypes.SHORT, DataTypes.INTEGER);

protected Resolver() {
super(FuncParams.builder(
Param.STRING,
INTEGER_COMPATIBLE_TYPES)
.withVarArgs(INTEGER_COMPATIBLE_TYPES).limitVarArgOccurrences(1)
.build());
}

private static FunctionInfo createInfo(List<DataType> types) {
return new FunctionInfo(new FunctionIdent(NAME, types), DataTypes.STRING);
}

@Override
public FunctionImplementation getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
return new SubstrFunction(createInfo(dataTypes));
}
}
}
6 changes: 6 additions & 0 deletions sql/src/main/java/io/crate/metadata/functions/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public Signature withTypeVariableConstraints(TypeVariableConstraint... typeVaria
.build();
}

public Signature withVariableArity() {
return Signature.builder(this)
.setVariableArity(true)
.build();
}

/*
* Forbid coercion of argument types.
* This prevents e.g. matching a numeric_only function with convertible argument (text).
Expand Down

0 comments on commit f9e4c32

Please sign in to comment.