Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

add serialization support for expression #712

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public boolean isNumber() {
return true;
}

@Override
public Short shortValue() {
return value.shortValue();
}

@Override
public Integer integerValue() {
return value.intValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.data.model;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;

/**
* Expression Short Value.
*/
public class ExprShortValue extends AbstractExprNumberValue {

public ExprShortValue(Number value) {
super(value);
}

@Override
public Object value() {
return shortValue();
}

@Override
public ExprType type() {
return ExprCoreType.SHORT;
}

@Override
public String toString() {
return shortValue().toString();
}

@Override
public int compare(ExprValue other) {
return Short.compare(shortValue(), other.shortValue());
}

@Override
public boolean equal(ExprValue other) {
return shortValue().equals(other.shortValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ default BindingTuple bindingTuples() {
return BindingTuple.EMPTY;
}

/**
* Get short value.
*/
default Short shortValue() {
throw new ExpressionEvaluationException(
"invalid to get shortValue from value of type " + type());
}

/**
* Get integer value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.amazon.opendistroforelasticsearch.sql.data.type;

import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -91,10 +92,14 @@ public String typeName() {
}

/**
* Retrun all the valid ExprCoreType.
* Return all the valid ExprCoreType.
*/
public static List<ExprType> coreTypes() {
return Arrays.stream(ExprCoreType.values()).filter(type -> type != UNKNOWN)
.collect(Collectors.toList());
}

public static List<ExprType> numberTypes() {
return ImmutableList.of(INTEGER, LONG, FLOAT, DOUBLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.expression;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprShortValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
Expand All @@ -28,6 +29,10 @@
public class DSL {
private final BuiltinFunctionRepository repository;

public static LiteralExpression literal(Short value) {
return new LiteralExpression(new ExprShortValue(value));
}

public static LiteralExpression literal(Integer value) {
return new LiteralExpression(ExprValueUtils.integerValue(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class FunctionDSL {
* @return FunctionResolver.
*/
public static FunctionResolver define(FunctionName functionName,
Function<FunctionName, Pair<FunctionSignature,
SerializableFunction<FunctionName, Pair<FunctionSignature,
FunctionBuilder>>... functions) {
return define(functionName, Arrays.asList(functions));
}
Expand All @@ -57,7 +57,7 @@ public static FunctionResolver define(FunctionName functionName,
* @return FunctionResolver.
*/
public static FunctionResolver define(FunctionName functionName,
List<Function<FunctionName, Pair<FunctionSignature,
List<SerializableFunction<FunctionName, Pair<FunctionSignature,
FunctionBuilder>>> functions) {

FunctionResolver.FunctionResolverBuilder builder = FunctionResolver.builder();
Expand All @@ -69,6 +69,41 @@ public static FunctionResolver define(FunctionName functionName,
return builder.build();
}

/**
* No Arg Function Implementation.
*
* @param function {@link ExprValue} based unary function.
* @param returnType return type.
* @return Unary Function Implementation.
*/
public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> impl(
SerializableNoArgFunction<ExprValue> function,
ExprType returnType) {

return functionName -> {
FunctionSignature functionSignature =
new FunctionSignature(functionName, Collections.emptyList());
FunctionBuilder functionBuilder =
arguments -> new FunctionExpression(functionName, Collections.emptyList()) {
@Override
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) {
return function.get();
}

@Override
public ExprType type() {
return returnType;
}

@Override
public String toString() {
return String.format("%s()", functionName);
}
};
return Pair.of(functionSignature, functionBuilder);
};
}

/**
* Unary Function Implementation.
*
Expand Down Expand Up @@ -152,6 +187,50 @@ public String toString() {
};
}

/**
* Triple Function Implementation.
*
* @param function {@link ExprValue} based unary function.
* @param returnType return type.
* @param args1Type argument type.
* @param args2Type argument type.
* @return Binary Function Implementation.
*/
public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> impl(
SerializableTriFunction<ExprValue, ExprValue, ExprValue, ExprValue> function,
ExprType returnType,
ExprType args1Type,
ExprType args2Type,
ExprType args3Type) {

return functionName -> {
FunctionSignature functionSignature =
new FunctionSignature(functionName, Arrays.asList(args1Type, args2Type, args3Type));
FunctionBuilder functionBuilder =
arguments -> new FunctionExpression(functionName, arguments) {
@Override
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) {
ExprValue arg1 = arguments.get(0).valueOf(valueEnv);
ExprValue arg2 = arguments.get(1).valueOf(valueEnv);
ExprValue arg3 = arguments.get(2).valueOf(valueEnv);
return function.apply(arg1, arg2, arg3);
}

@Override
public ExprType type() {
return returnType;
}

@Override
public String toString() {
return String.format("%s(%s, %s, %s)", functionName, arguments.get(0).toString(),
arguments.get(1).toString(), arguments.get(2).toString());
}
};
return Pair.of(functionSignature, functionBuilder);
};
}

/**
* Wrapper the unary ExprValue function with default NULL and MISSING handling.
*/
Expand Down Expand Up @@ -183,4 +262,20 @@ public static SerializableBiFunction<ExprValue, ExprValue, ExprValue> nullMissin
}
};
}

/**
* Wrapper the triple ExprValue function with default NULL and MISSING handling.
*/
public SerializableTriFunction<ExprValue, ExprValue, ExprValue, ExprValue> nullMissingHandling(
SerializableTriFunction<ExprValue, ExprValue, ExprValue, ExprValue> function) {
return (v1, v2, v3) -> {
if (v1.isMissing() || v2.isMissing() || v3.isMissing()) {
return ExprValueUtils.missingValue();
} else if (v1.isNull() || v2.isNull() || v3.isNull()) {
return ExprValueUtils.nullValue();
} else {
return function.apply(v1, v2, v3);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.expression.function;

import java.io.Serializable;
import java.util.function.Supplier;

/**
* Serializable no argument function.
*/
public interface SerializableNoArgFunction<T> extends Supplier<T>, Serializable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.sql.expression.function;

import java.io.Serializable;

/**
* Serializable Triple Function.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <V> the type of the third argument to the function
* @param <R> the type of the result of the function
*/
public interface SerializableTriFunction<T, U, V, R> extends Serializable {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @param v the third function argument
* @return the function result
*/
R apply(T t, U u, V v);
}
Loading