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

Commit

Permalink
Add support of basic data types byte, short, binary and add data type…
Browse files Browse the repository at this point in the history
… testing (#780)

* update

* added binary in jdbc

* added data types IT

* addressed comments
  • Loading branch information
chloe-zh authored Oct 16, 2020
1 parent 6522f58 commit 081882c
Show file tree
Hide file tree
Showing 35 changed files with 677 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public boolean isNumber() {
return true;
}

@Override
public Byte byteValue() {
return value.byteValue();
}

@Override
public Short shortValue() {
return value.shortValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 Byte Value.
*/
public class ExprByteValue extends AbstractExprNumberValue {

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

@Override
public int compare(ExprValue other) {
return Byte.compare(byteValue(), other.byteValue());
}

@Override
public boolean equal(ExprValue other) {
return byteValue().equals(other.byteValue());
}

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

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

@Override
public String toString() {
return value().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ default BindingTuple bindingTuples() {
return BindingTuple.EMPTY;
}

/**
* Get byte value.
*/
default Byte byteValue() {
throw new ExpressionEvaluationException(
"invalid to get byteValue from value of type " + type());
}

/**
* Get short value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public static ExprValue booleanValue(Boolean value) {
return value ? LITERAL_TRUE : LITERAL_FALSE;
}

public static ExprValue byteValue(Byte value) {
return new ExprByteValue(value);
}

public static ExprValue shortValue(Short value) {
return new ExprShortValue(value);
}

public static ExprValue integerValue(Integer value) {
return new ExprIntegerValue(value);
}
Expand Down Expand Up @@ -105,6 +113,10 @@ public static ExprValue fromObjectValue(Object o) {
return tupleValue((Map) o);
} else if (o instanceof List) {
return collectionValue(((List) o));
} else if (o instanceof Byte) {
return byteValue((Byte) o);
} else if (o instanceof Short) {
return shortValue((Short) o);
} else if (o instanceof Integer) {
return integerValue((Integer) o);
} else if (o instanceof Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public enum ExprCoreType implements ExprType {
/**
* Numbers.
*/
SHORT,
BYTE,
SHORT(BYTE),
INTEGER(SHORT),
LONG(INTEGER),
FLOAT(LONG),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
public class DSL {
private final BuiltinFunctionRepository repository;

public static LiteralExpression literal(Byte value) {
return new LiteralExpression(ExprValueUtils.byteValue(value));
}

public static LiteralExpression literal(Short value) {
return new LiteralExpression(new ExprShortValue(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

package com.amazon.opendistroforelasticsearch.sql.expression.operator.arthmetic;

import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.BYTE;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.DOUBLE;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.FLOAT;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.INTEGER;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.LONG;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.SHORT;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprByteValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprDoubleValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprFloatValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprIntegerValue;
Expand Down Expand Up @@ -58,6 +60,10 @@ public static void register(BuiltinFunctionRepository repository) {

private static FunctionResolver add() {
return FunctionDSL.define(BuiltinFunctionName.ADD.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() + v2.byteValue())),
BYTE, BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprShortValue(v1.shortValue() + v2.shortValue())),
Expand All @@ -84,6 +90,10 @@ private static FunctionResolver add() {

private static FunctionResolver subtract() {
return FunctionDSL.define(BuiltinFunctionName.SUBTRACT.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() - v2.byteValue())),
BYTE, BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprShortValue(v1.shortValue() - v2.shortValue())),
Expand All @@ -110,6 +120,10 @@ private static FunctionResolver subtract() {

private static FunctionResolver multiply() {
return FunctionDSL.define(BuiltinFunctionName.MULTIPLY.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() * v2.byteValue())),
BYTE, BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprShortValue(v1.shortValue() * v2.shortValue())),
Expand All @@ -136,6 +150,10 @@ private static FunctionResolver multiply() {

private static FunctionResolver divide() {
return FunctionDSL.define(BuiltinFunctionName.DIVIDE.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() / v2.byteValue())),
BYTE, BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() :
Expand Down Expand Up @@ -167,6 +185,10 @@ private static FunctionResolver divide() {

private static FunctionResolver modules() {
return FunctionDSL.define(BuiltinFunctionName.MODULES.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> new ExprByteValue(v1.byteValue() % v2.byteValue())),
BYTE, BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

package com.amazon.opendistroforelasticsearch.sql.expression.operator.arthmetic;

import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.BYTE;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.DOUBLE;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.FLOAT;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.INTEGER;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.LONG;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.SHORT;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.STRING;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprByteValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprDoubleValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprFloatValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprIntegerValue;
Expand Down Expand Up @@ -97,6 +99,9 @@ public static void register(BuiltinFunctionRepository repository) {
*/
private static FunctionResolver abs() {
return FunctionDSL.define(BuiltinFunctionName.ABS.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(v -> new ExprByteValue(Math.abs(v.byteValue()))),
BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(v -> new ExprShortValue(Math.abs(v.shortValue()))),
SHORT, SHORT),
Expand Down Expand Up @@ -286,6 +291,11 @@ private static FunctionResolver log2() {
*/
private static FunctionResolver mod() {
return FunctionDSL.define(BuiltinFunctionName.MOD.getName(),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> v2.byteValue() == 0 ? ExprNullValue.of() :
new ExprByteValue(v1.byteValue() % v2.byteValue())),
BYTE, BYTE, BYTE),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(v1, v2) -> v2.shortValue() == 0 ? ExprNullValue.of() :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class ExprValueUtilsTest {
testTuple.put("1", new ExprIntegerValue(1));
}

private static List<ExprValue> numberValues = Stream.of(1, 1L, 1f, 1D)
private static List<ExprValue> numberValues = Stream.of((byte) 1, (short) 1, 1, 1L, 1f, 1D)
.map(ExprValueUtils::fromObjectValue).collect(Collectors.toList());

private static List<ExprValue> nonNumberValues = Arrays.asList(
Expand All @@ -86,6 +86,8 @@ public class ExprValueUtilsTest {
Lists.newArrayList(Iterables.concat(numberValues, nonNumberValues));

private static List<Function<ExprValue, Object>> numberValueExtractor = Arrays.asList(
ExprValue::byteValue,
ExprValue::shortValue,
ExprValueUtils::getIntegerValue,
ExprValueUtils::getLongValue,
ExprValueUtils::getFloatValue,
Expand All @@ -106,8 +108,8 @@ public class ExprValueUtilsTest {
Iterables.concat(numberValueExtractor, nonNumberValueExtractor, dateAndTimeValueExtractor));

private static List<ExprCoreType> numberTypes =
Arrays.asList(ExprCoreType.INTEGER, ExprCoreType.LONG, ExprCoreType.FLOAT,
ExprCoreType.DOUBLE);
Arrays.asList(ExprCoreType.BYTE, ExprCoreType.SHORT, ExprCoreType.INTEGER, ExprCoreType.LONG,
ExprCoreType.FLOAT, ExprCoreType.DOUBLE);
private static List<ExprCoreType> nonNumberTypes =
Arrays.asList(STRING, BOOLEAN, ARRAY, STRUCT);
private static List<ExprCoreType> dateAndTimeTypes =
Expand All @@ -116,7 +118,7 @@ public class ExprValueUtilsTest {
Lists.newArrayList(Iterables.concat(numberTypes, nonNumberTypes, dateAndTimeTypes));

private static Stream<Arguments> getValueTestArgumentStream() {
List<Object> expectedValues = Arrays.asList(1, 1L, 1f, 1D, "1", true,
List<Object> expectedValues = Arrays.asList((byte) 1, (short) 1, 1, 1L, 1f, 1D, "1", true,
Arrays.asList(integerValue(1)),
ImmutableMap.of("1", integerValue(1)),
LocalDate.parse("2012-08-07"),
Expand Down Expand Up @@ -248,6 +250,8 @@ public void constructDateAndTimeValue() {

@Test
public void hashCodeTest() {
assertEquals(new ExprByteValue(1).hashCode(), new ExprByteValue(1).hashCode());
assertEquals(new ExprShortValue(1).hashCode(), new ExprShortValue(1).hashCode());
assertEquals(new ExprIntegerValue(1).hashCode(), new ExprIntegerValue(1).hashCode());
assertEquals(new ExprStringValue("1").hashCode(), new ExprStringValue("1").hashCode());
assertEquals(new ExprCollectionValue(ImmutableList.of(new ExprIntegerValue(1))).hashCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.LITERAL_MISSING;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.LITERAL_NULL;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.LITERAL_TRUE;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.byteValue;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.collectionValue;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.doubleValue;
import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprValueUtils.floatValue;
Expand Down Expand Up @@ -143,6 +144,14 @@ public void natural_order_long_value() {
assertEquals(-1, ordering.compare(longValue(4L), longValue(5L)));
}

@Test
public void natural_order_byte_value() {
ExprValueOrdering ordering = ExprValueOrdering.natural();
assertEquals(1, ordering.compare(byteValue((byte) 5), byteValue((byte) 4)));
assertEquals(0, ordering.compare(byteValue((byte) 5), byteValue((byte) 5)));
assertEquals(-1, ordering.compare(byteValue((byte) 4), byteValue((byte) 5)));
}

@Test
public void natural_order_boolean_value() {
ExprValueOrdering ordering = ExprValueOrdering.natural();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistroforelasticsearch.sql.expression.function;

import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.BYTE;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.DOUBLE;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.FLOAT;
import static com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType.INTEGER;
Expand Down Expand Up @@ -44,6 +45,11 @@ class WideningTypeRuleTest {
private static Table<ExprCoreType, ExprCoreType, Integer> numberWidenRule =
new ImmutableTable.Builder<ExprCoreType, ExprCoreType,
Integer>()
.put(BYTE, SHORT, 1)
.put(BYTE, INTEGER, 2)
.put(BYTE, LONG, 3)
.put(BYTE, FLOAT, 4)
.put(BYTE, DOUBLE, 5)
.put(SHORT, INTEGER, 1)
.put(SHORT, LONG, 2)
.put(SHORT, FLOAT, 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprByteValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprDoubleValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprFloatValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprIntegerValue;
Expand Down Expand Up @@ -56,10 +57,12 @@
class ArithmeticFunctionTest extends ExpressionTestBase {

private static Stream<Arguments> arithmeticFunctionArguments() {
List<ExprValue> numberOp1 = Arrays.asList(new ExprShortValue(3), new ExprIntegerValue(3),
new ExprLongValue(3L), new ExprFloatValue(3f), new ExprDoubleValue(3D));
List<ExprValue> numberOp1 = Arrays.asList(new ExprByteValue(3), new ExprShortValue(3),
new ExprIntegerValue(3), new ExprLongValue(3L), new ExprFloatValue(3f),
new ExprDoubleValue(3D));
List<ExprValue> numberOp2 =
Arrays.asList(new ExprShortValue(2), new ExprIntegerValue(2), new ExprLongValue(3L),
Arrays.asList(new ExprByteValue(2), new ExprShortValue(2), new ExprIntegerValue(2),
new ExprLongValue(3L),
new ExprFloatValue(2f), new ExprDoubleValue(2D));
return Lists.cartesianProduct(numberOp1, numberOp2).stream()
.map(list -> Arguments.of(list.get(0), list.get(1)));
Expand Down Expand Up @@ -209,6 +212,29 @@ protected void assertValueEqual(BuiltinFunctionName builtinFunctionName, ExprTyp
ExprValue op2,
ExprValue actual) {
switch ((ExprCoreType) type) {
case BYTE:
Byte vb1 = op1.byteValue();
Byte vb2 = op2.byteValue();
Integer vbActual = actual.integerValue();
switch (builtinFunctionName) {
case ADD:
assertEquals(vb1 + vb2, vbActual);
return;
case SUBTRACT:
assertEquals(vb1 - vb2, vbActual);
return;
case DIVIDE:
assertEquals(vb1 / vb2, vbActual);
return;
case MULTIPLY:
assertEquals(vb1 * vb2, vbActual);
return;
case MODULES:
assertEquals(vb1 % vb2, vbActual);
return;
default:
throw new IllegalStateException("illegal function name: " + builtinFunctionName);
}
case SHORT:
Short vs1 = op1.shortValue();
Short vs2 = op2.shortValue();
Expand Down
Loading

0 comments on commit 081882c

Please sign in to comment.