Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Add CAST and CONVERT to SHOW FUNCTIONS #34940

Merged
merged 4 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class FunctionDefinition {
public interface Builder {
Function build(UnresolvedFunction uf, boolean distinct, TimeZone tz);
}

private final String name;
private final List<String> aliases;
private final Class<? extends Function> clazz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.xpack.sql.expression.function.aggregate.Sum;
import org.elasticsearch.xpack.sql.expression.function.aggregate.SumOfSquares;
import org.elasticsearch.xpack.sql.expression.function.aggregate.VarPop;
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayName;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayOfMonth;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayOfWeek;
Expand Down Expand Up @@ -84,6 +85,7 @@
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Mod;
import org.elasticsearch.xpack.sql.parser.ParsingException;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.StringUtils;

import java.util.Arrays;
Expand Down Expand Up @@ -116,14 +118,14 @@ public class FunctionRegistry {
public FunctionRegistry() {
defineDefaultFunctions();
}

/**
* Constructor specifying alternate functions for testing.
*/
FunctionRegistry(FunctionDefinition... functions) {
addToMap(functions);
}

private void defineDefaultFunctions() {
// Aggregate functions
addToMap(def(Avg.class, Avg::new),
Expand Down Expand Up @@ -206,11 +208,13 @@ private void defineDefaultFunctions() {
def(Space.class, Space::new),
def(Substring.class, Substring::new),
def(UCase.class, UCase::new));
// DataType conversion
addToMap(def(Cast.class, Cast::new, "CONVERT"));
// Special
addToMap(def(Score.class, Score::new));
}
protected void addToMap(FunctionDefinition...functions) {

void addToMap(FunctionDefinition...functions) {
// temporary map to hold [function_name/alias_name : function instance]
Map<String, FunctionDefinition> batchMap = new HashMap<>();
for (FunctionDefinition f : functions) {
Expand All @@ -227,7 +231,7 @@ protected void addToMap(FunctionDefinition...functions) {
// sort the temporary map by key name and add it to the global map of functions
defs.putAll(batchMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.<Entry<String, FunctionDefinition>, String,
.collect(Collectors.<Entry<String, FunctionDefinition>, String,
FunctionDefinition, LinkedHashMap<String, FunctionDefinition>> toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new)));
}
Expand Down Expand Up @@ -390,7 +394,7 @@ private static FunctionDefinition def(Class<? extends Function> function, Functi
private interface FunctionBuilder {
Function build(Location location, List<Expression> children, boolean distinct, TimeZone tz);
}

@SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do
static <T extends Function> FunctionDefinition def(Class<T> function,
ThreeParametersFunctionBuilder<T> ctorRef, String... aliases) {
Expand All @@ -408,11 +412,11 @@ static <T extends Function> FunctionDefinition def(Class<T> function,
};
return def(function, builder, false, aliases);
}

interface ThreeParametersFunctionBuilder<T> {
T build(Location location, Expression source, Expression exp1, Expression exp2);
}

@SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do
static <T extends Function> FunctionDefinition def(Class<T> function,
FourParametersFunctionBuilder<T> ctorRef, String... aliases) {
Expand All @@ -427,11 +431,29 @@ static <T extends Function> FunctionDefinition def(Class<T> function,
};
return def(function, builder, false, aliases);
}

interface FourParametersFunctionBuilder<T> {
T build(Location location, Expression source, Expression exp1, Expression exp2, Expression exp3);
}

/**
* Special method to create function definition for {@link Cast} as its
* signature is not compatible with {@link UnresolvedFunction}
*
* @return Cast function definition
*/
@SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do
private static <T extends Function> FunctionDefinition def(Class<T> function,
CastFunctionBuilder<T> ctorRef,
String... aliases) {
FunctionBuilder builder = (location, children, distinct, tz) ->
ctorRef.build(location, children.get(0), children.get(0).dataType());
return def(function, builder, false, aliases);
}
private interface CastFunctionBuilder<T> {
T build(Location location, Expression expression, DataType dataType);
}

private static String normalize(String name) {
// translate CamelCase to camel_case
return StringUtils.camelCaseToUnderscore(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.Score;


public enum FunctionType {

AGGREGATE(AggregateFunction.class),
SCALAR(ScalarFunction.class),
SCORE(Score.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Objects;

public class Cast extends UnaryScalarFunction {

private final DataType dataType;

public Cast(Location location, Expression field, DataType dataType) {
Expand Down Expand Up @@ -102,4 +103,4 @@ public String name() {
sb.insert(sb.length() - 1, " AS " + to().sqlName());
return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void testShowFunctions() throws IOException {
while (scalarFunction.matcher(line).matches()) {
line = readLine();
}

assertThat(line, RegexMatcher.matches("\\s*SCORE\\s*\\|\\s*SCORE\\s*"));
assertEquals("", readLine());
}
Expand Down
4 changes: 3 additions & 1 deletion x-pack/qa/sql/src/main/resources/command.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ RTRIM |SCALAR
SPACE |SCALAR
SUBSTRING |SCALAR
UCASE |SCALAR
SCORE |SCORE
CAST |SCALAR
CONVERT |SCALAR
SCORE |SCORE
;

showFunctionsWithExactMatch
Expand Down
4 changes: 3 additions & 1 deletion x-pack/qa/sql/src/main/resources/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ RTRIM |SCALAR
SPACE |SCALAR
SUBSTRING |SCALAR
UCASE |SCALAR
SCORE |SCORE
CAST |SCALAR
CONVERT |SCALAR
SCORE |SCORE
// end::showFunctions
;

Expand Down