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

Add comparsion operator for SQL #635

Merged
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.
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 @@ -27,6 +27,11 @@
public abstract class AbstractExprNumberValue extends AbstractExprValue {
private final Number value;

@Override
public boolean isNumber() {
return true;
}

@Override
public Integer integerValue() {
return value.intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public abstract class AbstractExprValue implements ExprValue {
*/
@Override
public int compareTo(ExprValue other) {
if (this.isNull() || this.isMissing()) {
return this.compare(other);
} else if (other.isNull() || other.isMissing()) {
return -other.compareTo(this);
if (this.isNull() || this.isMissing() || other.isNull() || other.isMissing()) {
throw new IllegalStateException(
String.format("[BUG] Unreachable, Comparing with NULL or MISSING is undefined"));
}
if (!this.type().equals(other.type())) {
if ((this.isNumber() && other.isNumber()) || this.type() == other.type()) {
return compare(other);
} else {
throw new ExpressionEvaluationException(
String.format(
"compare expected value have same type, but with [%s, %s]",
this.type(), other.type()));
}
return compare(other);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

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

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.ExpressionEvaluationException;
import java.util.Objects;

/**
* Expression Missing Value.
* Missing value only equal to missing value, and is smaller than any other value.
*/
public class ExprMissingValue extends AbstractExprValue {
private static final ExprValue instance = new ExprMissingValue();
private static final ExprMissingValue instance = new ExprMissingValue();

private ExprMissingValue() {
}

public static ExprValue of() {
public static ExprMissingValue of() {
return instance;
}

Expand All @@ -40,26 +40,23 @@ public Object value() {

@Override
public ExprType type() {
throw new ExpressionEvaluationException("invalid to call type operation on missing value");
return ExprCoreType.UNKNOWN;
}

@Override
public boolean isMissing() {
return true;
}

/**
* When MISSING value compare to other expression value.
* 1) MISSING is equal to MISSING.
* 2) MISSING is less than all other expression values.
*/
@Override
public int compare(ExprValue other) {
return other.isMissing() ? 0 : -1;
throw new IllegalStateException(String.format("[BUG] Unreachable, Comparing with MISSING is "
+ "undefined"));
}

/**
* Missing value is equal to Missing value.
* Notes, this function should only used for Java Object Compare.
*/
@Override
public boolean equal(ExprValue other) {
Expand All @@ -70,4 +67,9 @@ public boolean equal(ExprValue other) {
public int hashCode() {
return Objects.hashCode("MISSING");
}

@Override
public String toString() {
return "MISSING";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@

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

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.ExpressionEvaluationException;
import java.util.Objects;

/**
* Expression Null Value.
* Null value
* <li>equal to null value.
* <li>large than missing value.
* <li>less than any other value.
*/
public class ExprNullValue extends AbstractExprValue {
private static final ExprValue instance = new ExprNullValue();
private static final ExprNullValue instance = new ExprNullValue();

private ExprNullValue() {
}
Expand All @@ -37,7 +33,12 @@ public int hashCode() {
return Objects.hashCode("NULL");
}

public static ExprValue of() {
@Override
public String toString() {
return "NULL";
}

public static ExprNullValue of() {
return instance;
}

Expand All @@ -48,31 +49,26 @@ public Object value() {

@Override
public ExprType type() {
throw new ExpressionEvaluationException("invalid to call type operation on null value");
return ExprCoreType.UNKNOWN;
}

@Override
public boolean isNull() {
return true;
}

/**
* When NULL value compare to other expression value.
* 1) NULL is equal to NULL.
* 2) NULL is large than MISSING.
* 3) NULL is less than all other expression values.
*/
@Override
public int compare(ExprValue other) {
return other.isNull() ? 0 : other.isMissing() ? 1 : -1;
throw new IllegalStateException(
String.format("[BUG] Unreachable, Comparing with NULL is undefined"));
}

/**
* NULL value is equal to NULL value.
* Notes, this function should only used for Java Object Compare.
*/
@Override
public boolean equal(ExprValue other) {
return other.isNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ default boolean isMissing() {
return false;
}

/**
* Is Number value.
*
* @return true: is number value, otherwise false
*/
default boolean isNumber() {
return false;
}

/**
* Get the {@link BindingTuple}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public static int distance(ExprType type1, ExprType type2) {
}

private static int distance(ExprType type1, ExprType type2, int distance) {
if (type1 == UNKNOWN) {
return IMPOSSIBLE_WIDENING;
} else if (type1 == type2) {
if (type1 == type2) {
return distance;
} else if (type1 == UNKNOWN) {
return IMPOSSIBLE_WIDENING;
} else {
return type1.getParent().stream()
.map(parentOfType1 -> distance(parentOfType1, type2, distance + 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ public FunctionExpression like(Expression... expressions) {
return function(BuiltinFunctionName.LIKE, expressions);
}

public FunctionExpression notLike(Expression... expressions) {
return function(BuiltinFunctionName.NOT_LIKE, expressions);
}

public Aggregator avg(Expression... expressions) {
return aggregate(BuiltinFunctionName.AVG, expressions);
}
Expand All @@ -289,4 +293,12 @@ private Aggregator aggregate(BuiltinFunctionName functionName, Expression... exp
return (Aggregator) repository.compile(
functionName.getName(), Arrays.asList(expressions));
}

public FunctionExpression isnull(Expression... expressions) {
return function(BuiltinFunctionName.IS_NULL, expressions);
}

public FunctionExpression isnotnull(Expression... expressions) {
return function(BuiltinFunctionName.IS_NOT_NULL, expressions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public enum BuiltinFunctionName {
GREATER(FunctionName.of(">")),
GTE(FunctionName.of(">=")),
LIKE(FunctionName.of("like")),
NOT_LIKE(FunctionName.of("not like")),

/**
* Date and Time Functions.
Expand All @@ -87,7 +88,13 @@ public enum BuiltinFunctionName {
*/
AVG(FunctionName.of("avg")),
SUM(FunctionName.of("sum")),
COUNT(FunctionName.of("count"));
COUNT(FunctionName.of("count")),

/**
* NULL Test.
*/
IS_NULL(FunctionName.of("is null")),
IS_NOT_NULL(FunctionName.of("is not null"));

private final FunctionName name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,20 @@ public SerializableFunction<ExprValue, ExprValue> nullMissingHandling(
}
};
}

/**
* Wrapper the binary ExprValue function with default NULL and MISSING handling.
*/
public SerializableBiFunction<ExprValue, ExprValue, ExprValue> nullMissingHandling(
SerializableBiFunction<ExprValue, ExprValue, ExprValue> function) {
return (v1, v2) -> {
if (v1.isMissing() || v2.isMissing()) {
return ExprValueUtils.missingValue();
} else if (v1.isNull() || v2.isNull()) {
return ExprValueUtils.nullValue();
} else {
return function.apply(v1, v2);
}
};
}
}
Loading