Skip to content

Commit

Permalink
Refactor all string scalars 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 30, 2020
1 parent 2cc9652 commit 4902037
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,24 @@

package io.crate.expression.scalar.string;

import java.util.List;

import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.UnaryScalar;
import io.crate.metadata.BaseFunctionResolver;
import io.crate.metadata.FunctionImplementation;
import io.crate.metadata.FunctionName;
import io.crate.metadata.functions.params.FuncParams;
import io.crate.types.DataType;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import static io.crate.types.TypeSignature.parseTypeSignature;

public final class AsciiFunction {

public static void register(ScalarFunctionModule module) {
module.register(
new FunctionName(null, "ascii"),
new BaseFunctionResolver(FuncParams.builder(List.of(DataTypes.STRING)).build()) {

@Override
public FunctionImplementation getForTypes(List<DataType> types) throws IllegalArgumentException {
return new UnaryScalar<>(
"ascii",
types.get(0),
DataTypes.INTEGER,
AsciiFunction::ascii
);
}
}
Signature.scalar(
"ascii",
parseTypeSignature("text"),
parseTypeSignature("integer")
),
args ->
new UnaryScalar<>("ascii", args.get(0), DataTypes.INTEGER, AsciiFunction::ascii)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.arithmetic.BinaryScalar;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import java.nio.charset.StandardCharsets;
Expand All @@ -35,11 +36,31 @@
import java.util.function.BinaryOperator;
import java.util.function.Function;

import static io.crate.types.TypeSignature.parseTypeSignature;

public class EncodeDecodeFunction {

public static void register(ScalarFunctionModule module) {
module.register(new BinaryScalar<>(new Encode(), "encode", DataTypes.STRING, FunctionInfo.DETERMINISTIC_ONLY));
module.register(new BinaryScalar<>(new Decode(), "decode", DataTypes.STRING, FunctionInfo.DETERMINISTIC_ONLY));
module.register(
Signature.scalar(
"encode",
parseTypeSignature("text"),
parseTypeSignature("text"),
parseTypeSignature("text")
),
args ->
new BinaryScalar<>(new Encode(), "encode", DataTypes.STRING, FunctionInfo.DETERMINISTIC_ONLY)
);
module.register(
Signature.scalar(
"decode",
parseTypeSignature("text"),
parseTypeSignature("text"),
parseTypeSignature("text")
),
args ->
new BinaryScalar<>(new Decode(), "decode", DataTypes.STRING, FunctionInfo.DETERMINISTIC_ONLY)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import io.crate.common.Hex;
import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.UnaryScalar;
import io.crate.types.DataType;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;
import org.elasticsearch.common.hash.MessageDigests;

Expand All @@ -35,9 +35,26 @@
import java.util.function.Function;
import java.util.function.Supplier;

import static io.crate.types.TypeSignature.parseTypeSignature;

public final class HashFunctions {

private static final DataType SUPPORTED_INPUT_TYPE = DataTypes.STRING;
public static void register(ScalarFunctionModule module) {
register(module, "md5", HashMethod.MD5::digest);
register(module, "sha1", HashMethod.SHA1::digest);
}

private static void register(ScalarFunctionModule module, String name, Function<String, String> func) {
module.register(
Signature.scalar(
name,
parseTypeSignature("text"),
parseTypeSignature("text")
),
argumentTypes ->
new UnaryScalar<>(name, DataTypes.STRING, DataTypes.STRING, func)
);
}

private enum HashMethod {
MD5(MessageDigests::md5),
Expand Down Expand Up @@ -68,12 +85,4 @@ public String digest(String input) {
}


private static void register(ScalarFunctionModule module, String name, Function<String, String> func) {
module.register(new UnaryScalar<>(name, SUPPORTED_INPUT_TYPE, DataTypes.STRING, func));
}

public static void register(ScalarFunctionModule module) {
register(module, "md5", HashMethod.MD5::digest);
register(module, "sha1", HashMethod.SHA1::digest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@

import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.UnaryScalar;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import static io.crate.types.TypeSignature.parseTypeSignature;

public final class InitCapFunction {

public static void register(ScalarFunctionModule module) {
module.register(new UnaryScalar<>("initcap", DataTypes.STRING, DataTypes.STRING, InitCapFunction::toCapital));
module.register(
Signature.scalar(
"initcap",
parseTypeSignature("text"),
parseTypeSignature("text")
),
args ->
new UnaryScalar<>("initcap", DataTypes.STRING, DataTypes.STRING, InitCapFunction::toCapital)
);
}

private static String toCapital(String val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,32 @@

import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.UnaryScalar;
import io.crate.types.DataType;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Function;

public final class LengthFunction {

private static final List<DataType<?>> SUPPORTED_TYPES = List.of(DataTypes.STRING, DataTypes.UNDEFINED);
import static io.crate.types.TypeSignature.parseTypeSignature;

private static void register(ScalarFunctionModule module, String name, Function<String, Integer> func) {
for (DataType<?> inputType : SUPPORTED_TYPES) {
module.register(new UnaryScalar<>(name, inputType, DataTypes.INTEGER, func));
}
}
public final class LengthFunction {

public static void register(ScalarFunctionModule module) {
register(module, "octet_length", x -> x.getBytes(StandardCharsets.UTF_8).length);
register(module, "bit_length", x -> x.getBytes(StandardCharsets.UTF_8).length * Byte.SIZE);
register(module, "char_length", String::length);
register(module, "length", String::length);
}

private static void register(ScalarFunctionModule module, String name, Function<String, Integer> func) {
module.register(
Signature.scalar(
name,
parseTypeSignature("text"),
parseTypeSignature("integer")
),
args -> new UnaryScalar<>(name, DataTypes.STRING, DataTypes.INTEGER, func)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@

import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.UnaryScalar;
import io.crate.metadata.functions.Signature;
import io.crate.sql.Identifiers;
import io.crate.types.DataTypes;

import static io.crate.types.TypeSignature.parseTypeSignature;


public final class QuoteIdentFunction {

public static void register(ScalarFunctionModule module) {
module.register(new UnaryScalar<>(
"quote_ident",
DataTypes.STRING,
DataTypes.STRING,
Identifiers::quoteIfNeeded)
module.register(
Signature.scalar(
"quote_ident",
parseTypeSignature("text"),
parseTypeSignature("text")
),
args ->
new UnaryScalar<>("quote_ident", DataTypes.STRING, DataTypes.STRING, Identifiers::quoteIfNeeded)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,38 @@

package io.crate.expression.scalar.string;

import com.google.common.collect.ImmutableList;
import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.TripleScalar;
import io.crate.metadata.FunctionIdent;
import io.crate.metadata.FunctionInfo;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;
import org.elasticsearch.common.Strings;

import java.util.List;

import static io.crate.types.TypeSignature.parseTypeSignature;


public final class ReplaceFunction {

public static void register(ScalarFunctionModule module) {
module.register(new TripleScalar<>(functionInfo("replace"), Strings::replace));
}
private static final String NAME = "replace";

private static FunctionInfo functionInfo(String name) {
return new FunctionInfo(new FunctionIdent(name, ImmutableList.of(DataTypes.STRING, DataTypes.STRING, DataTypes.STRING)), DataTypes.STRING);
public static void register(ScalarFunctionModule module) {
module.register(
Signature.scalar(
NAME,
parseTypeSignature("text"),
parseTypeSignature("text"),
parseTypeSignature("text"),
parseTypeSignature("text")
),
args -> new TripleScalar<>(
new FunctionInfo(
new FunctionIdent(NAME, List.of(DataTypes.STRING, DataTypes.STRING, DataTypes.STRING)),
DataTypes.STRING
),
Strings::replace)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,44 @@

import io.crate.expression.scalar.ScalarFunctionModule;
import io.crate.expression.scalar.UnaryScalar;
import io.crate.metadata.functions.Signature;
import io.crate.types.DataTypes;

import java.util.Locale;

import static io.crate.types.TypeSignature.parseTypeSignature;

public final class StringCaseFunction {

public static void register(ScalarFunctionModule module) {
module.register(new UnaryScalar<>("upper", DataTypes.STRING, DataTypes.STRING, (String val) -> val.toUpperCase(Locale.ENGLISH)));
module.register(new UnaryScalar<>("lower", DataTypes.STRING, DataTypes.STRING, (String val) -> val.toLowerCase(Locale.ENGLISH)));
module.register(
Signature.scalar(
"upper",
parseTypeSignature("text"),
parseTypeSignature("text")
),
args ->
new UnaryScalar<String, String>(
"upper",
DataTypes.STRING,
DataTypes.STRING,
val -> val.toUpperCase(Locale.ENGLISH)
)
);
module.register(
Signature.scalar(
"lower",
parseTypeSignature("text"),
parseTypeSignature("text")
),
args ->
new UnaryScalar<String, String>(
"lower",
DataTypes.STRING,
DataTypes.STRING,
val -> val.toLowerCase(Locale.ENGLISH)
)
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,38 @@
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;
import java.util.Locale;
import java.util.function.BiFunction;

import static io.crate.types.TypeSignature.parseTypeSignature;

public class StringLeftRightFunction extends Scalar<String, Object> {

public static void register(ScalarFunctionModule module) {
module.register(new StringLeftRightFunction("left", StringLeftRightFunction::left));
module.register(new StringLeftRightFunction("right", StringLeftRightFunction::right));
module.register(
Signature.scalar(
"left",
parseTypeSignature("text"),
parseTypeSignature("integer"),
parseTypeSignature("text")
),
argumentTypes ->
new StringLeftRightFunction("left", StringLeftRightFunction::left)
);
module.register(
Signature.scalar(
"right",
parseTypeSignature("text"),
parseTypeSignature("integer"),
parseTypeSignature("text")
),
argumentTypes ->
new StringLeftRightFunction("right", StringLeftRightFunction::right)
);
}

private final FunctionInfo info;
Expand Down
Loading

0 comments on commit 4902037

Please sign in to comment.