Skip to content

Commit

Permalink
Add a shortcut for building aggregate function signatures.
Browse files Browse the repository at this point in the history
  • Loading branch information
kovrus authored and mergify[bot] committed Apr 2, 2020
1 parent 6a30696 commit 547eeda
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ public final class ArrayAgg extends AggregationFunction<List<Object>, List<Objec

public static void register(AggregationImplModule module) {
module.register(
Signature.builder()
.name(NAME)
.kind(FunctionInfo.Type.AGGREGATE)
.argumentTypes(parseTypeSignature("E"))
.typeVariableConstraints(typeVariable("E"))
.returnType(parseTypeSignature("array(E)"))
.build(),
Signature.aggregate(
NAME,
parseTypeSignature("E"),
parseTypeSignature("array(E)")
).withTypeVariableConstraints(typeVariable("E")),
args -> new ArrayAgg(args.get(0))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ public class CountAggregation extends AggregationFunction<CountAggregation.LongS

public static void register(AggregationImplModule mod) {
mod.register(
Signature.builder()
.name(NAME)
.kind(FunctionInfo.Type.AGGREGATE)
.typeVariableConstraints(typeVariable("V"))
.argumentTypes(parseTypeSignature("V"))
.returnType(DataTypes.LONG.getTypeSignature())
.build(),
Signature.aggregate(
NAME,
parseTypeSignature("V"),
DataTypes.LONG.getTypeSignature()
).withTypeVariableConstraints(typeVariable("V")),
args -> new CountAggregation(
new FunctionInfo(
new FunctionIdent(NAME, args),
Expand All @@ -77,11 +75,7 @@ public static void register(AggregationImplModule mod) {
true)
);
mod.register(
Signature.builder()
.name(NAME)
.kind(FunctionInfo.Type.AGGREGATE)
.returnType(DataTypes.LONG.getTypeSignature())
.build(),
Signature.aggregate(NAME, DataTypes.LONG.getTypeSignature()),
args -> new CountAggregation(COUNT_STAR_FUNCTION, false)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ public final class StringAgg extends AggregationFunction<StringAgg.StringAggStat

public static void register(AggregationImplModule mod) {
mod.register(
Signature.builder()
.name(NAME)
.kind(FunctionInfo.Type.AGGREGATE)
.argumentTypes(
DataTypes.STRING.getTypeSignature(),
DataTypes.STRING.getTypeSignature())
.returnType(DataTypes.STRING.getTypeSignature())
.build(),
Signature.aggregate(
NAME,
DataTypes.STRING.getTypeSignature(),
DataTypes.STRING.getTypeSignature(),
DataTypes.STRING.getTypeSignature()),
args -> new StringAgg()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ public abstract class AbstractFunctionModule<T extends FunctionImplementation> e
private HashMap<FunctionName, List<FuncResolver>> functionImplementations = new HashMap<>();
private MapBinder<FunctionName, List<FuncResolver>> implementationsBinder;

/**
* @deprecated Use {@link #register(Signature, Function)} instead.
*/
public void register(T impl) {
functions.put(impl.info().ident(), impl);
}

/**
* @deprecated Use {@link #register(Signature, Function)} instead.
*/
public void register(String name, FunctionResolver functionResolver) {
register(new FunctionName(name), functionResolver);
}

/**
* @deprecated Use {@link #register(Signature, Function)} instead.
*/
public void register(FunctionName qualifiedName, FunctionResolver functionResolver) {
resolver.put(qualifiedName, functionResolver);
}
Expand Down
25 changes: 22 additions & 3 deletions sql/src/main/java/io/crate/metadata/functions/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@

public final class Signature {

/**
* See {@link #aggregate(FunctionName, TypeSignature...)}
*/
public static Signature aggregate(String name, TypeSignature... types) {
return scalar(new FunctionName(null, name), types);
}

/**
* Shortcut for creating a signature of type {@link FunctionInfo.Type#AGGREGATE}.
* The last element of the given types is handled as the return type.
*
* @param name The fqn function name.
* @param types The argument and return (last element) types
* @return The created signature
*/
public static Signature aggregate(FunctionName name, TypeSignature... types) {
return signatureBuilder(name, FunctionInfo.Type.AGGREGATE, types).build();
}

/**
* See {@link #scalar(FunctionName, TypeSignature...)}
*/
Expand All @@ -49,14 +68,14 @@ public static Signature scalar(String name, TypeSignature... types) {
* @return The created signature
*/
public static Signature scalar(FunctionName name, TypeSignature... types) {
return scalarBuilder(name, types).build();
return signatureBuilder(name, FunctionInfo.Type.SCALAR, types).build();
}

private static Signature.Builder scalarBuilder(FunctionName name, TypeSignature... types) {
private static Signature.Builder signatureBuilder(FunctionName name, FunctionInfo.Type type, TypeSignature... types) {
assert types.length > 0 : "Types must contain at least the return type (last element), 0 types given";
Builder builder = Signature.builder()
.name(name)
.kind(FunctionInfo.Type.SCALAR)
.kind(type)
.returnType(types[types.length - 1]);
if (types.length > 1) {
builder.argumentTypes(Arrays.copyOf(types, types.length - 1));
Expand Down

0 comments on commit 547eeda

Please sign in to comment.