Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nadment committed Jan 15, 2025
1 parent d4336e6 commit f4422f7
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public IExpression compile(IExpressionContext context, Call call) throws Express
private static final class StringEndsWithFunction extends EndsWithFunction {
public static final StringEndsWithFunction INSTANCE = new StringEndsWithFunction();

private StringEndsWithFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
String value = operands[0].getValue(String.class);
Expand All @@ -87,10 +83,6 @@ public Object eval(final IExpression[] operands) {
private static final class BinaryEndsWithFunction extends EndsWithFunction {
public static final BinaryEndsWithFunction INSTANCE = new BinaryEndsWithFunction();

private BinaryEndsWithFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
byte[] value = operands[0].getValue(byte[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public IExpression compile(IExpressionContext context, Call call) throws Express
private static final class StringReverseFunction extends ReverseFunction {
public static final ReverseFunction INSTANCE = new StringReverseFunction();

private StringReverseFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
String value = operands[0].getValue(String.class);
Expand All @@ -76,10 +72,6 @@ public Object eval(final IExpression[] operands) {
private static final class BinaryReverseFunction extends ReverseFunction {
public static final ReverseFunction INSTANCE = new BinaryReverseFunction();

private BinaryReverseFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
final byte[] value = operands[0].getValue(byte[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public IExpression compile(IExpressionContext context, Call call) throws Express
private static final class StringRightFunction extends RightFunction {
public static final RightFunction INSTANCE = new StringRightFunction();

private StringRightFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
String str = operands[0].getValue(String.class);
Expand All @@ -86,10 +82,6 @@ public Object eval(final IExpression[] operands) {
private static final class BinaryRightFunction extends RightFunction {
public static final RightFunction INSTANCE = new BinaryRightFunction();

private BinaryRightFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
byte[] bytes = operands[0].getValue(byte[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public IExpression compile(final IExpressionContext context, final Call call)
private static final class StringToBooleanFunction extends ToBooleanFunction {
private static final StringToBooleanFunction INSTANCE = new StringToBooleanFunction();

private StringToBooleanFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
String value = operands[0].getValue(String.class);
Expand All @@ -80,10 +76,6 @@ public Object eval(final IExpression[] operands) {
private static final class IntegerToBooleanFunction extends ToBooleanFunction {
private static final IntegerToBooleanFunction INSTANCE = new IntegerToBooleanFunction();

private IntegerToBooleanFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Long value = operands[0].getValue(Long.class);
Expand All @@ -94,10 +86,6 @@ public Object eval(final IExpression[] operands) {
private static final class NumberToBooleanFunction extends ToBooleanFunction {
private static final NumberToBooleanFunction INSTANCE = new NumberToBooleanFunction();

private NumberToBooleanFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
BigDecimal value = operands[0].getValue(BigDecimal.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ public Object eval(final IExpression[] operands) {
private static final class BooleanToCharFunction extends ToCharFunction {
private static final ToCharFunction INSTANCE = new BooleanToCharFunction();

public BooleanToCharFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Boolean value = operands[0].getValue(Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ public Object eval(final IExpression[] operands) {
private static final class IntegerToDateFunction extends ToDateFunction {
private static final IntegerToDateFunction INSTANCE = new IntegerToDateFunction();

private IntegerToDateFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Long value = operands[0].getValue(Long.class);
Expand All @@ -106,10 +102,6 @@ public Object eval(final IExpression[] operands) {
private static final class NumberToDateFunction extends ToDateFunction {
private static final ToDateFunction INSTANCE = new NumberToDateFunction();

private NumberToDateFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
BigDecimal value = operands[0].getValue(BigDecimal.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public class TryToBinaryFunction extends Function {
private static final class StringHexTryToBinaryFunction extends TryToBinaryFunction {
private static final TryToBinaryFunction INSTANCE = new StringHexTryToBinaryFunction();

private StringHexTryToBinaryFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
final String value = operands[0].getValue(String.class);
Expand All @@ -60,10 +56,6 @@ private static final class StringUtf8TryToBinaryFunction extends TryToBinaryFunc

private static final TryToBinaryFunction INSTANCE = new StringUtf8TryToBinaryFunction();

private StringUtf8TryToBinaryFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
final String value = operands[0].getValue(String.class);
Expand All @@ -81,10 +73,6 @@ public Object eval(final IExpression[] operands) {
private static final class StringBase64TryToBinaryFunction extends TryToBinaryFunction {
private static final TryToBinaryFunction INSTANCE = new StringBase64TryToBinaryFunction();

private StringBase64TryToBinaryFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
final String value = operands[0].getValue(String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public IExpression compile(final IExpressionContext context, final Call call)
private static final class StringTryToBooleanFunction extends TryToBooleanFunction {
private static final StringTryToBooleanFunction INSTANCE = new StringTryToBooleanFunction();

private StringTryToBooleanFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
String value = operands[0].getValue(String.class);
Expand All @@ -81,10 +77,6 @@ public Object eval(final IExpression[] operands) {
private static final class IntegerTryToBooleanFunction extends TryToBooleanFunction {
private static final IntegerTryToBooleanFunction INSTANCE = new IntegerTryToBooleanFunction();

private IntegerTryToBooleanFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Long value = operands[0].getValue(Long.class);
Expand All @@ -95,10 +87,6 @@ public Object eval(final IExpression[] operands) {
private static final class NumberTryToBooleanFunction extends TryToBooleanFunction {
private static final NumberTryToBooleanFunction INSTANCE = new NumberTryToBooleanFunction();

private NumberTryToBooleanFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
BigDecimal value = operands[0].getValue(BigDecimal.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ public Object eval(final IExpression[] operands) {
private static final class IntegerTryToDateFunction extends TryToDateFunction {
private static final TryToDateFunction INSTANCE = new IntegerTryToDateFunction();

private IntegerTryToDateFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
Long value = operands[0].getValue(Long.class);
Expand All @@ -109,10 +105,6 @@ public Object eval(final IExpression[] operands) {
private static final class NumberTryToDateFunction extends TryToDateFunction {
private static final TryToDateFunction INSTANCE = new NumberTryToDateFunction();

private NumberTryToDateFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
BigDecimal value = operands[0].getValue(BigDecimal.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ public Object eval(final IExpression[] operands) {
private static final class DateTryToNumberFunction extends TryToNumberFunction {
private static final TryToNumberFunction INSTANCE = new DateTryToNumberFunction();

private DateTryToNumberFunction() {
super();
}

@Override
public Object eval(final IExpression[] operands) {
ZonedDateTime value = operands[0].getValue(ZonedDateTime.class);
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/org/apache/hop/expression/type/BinaryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public byte[] cast(final Object value, String pattern) throws ConversionExceptio
if (value == null) {
return null;
}

if (value instanceof byte[] bytes) {
return bytes;
}
Expand All @@ -109,7 +108,14 @@ public byte[] cast(final Object value, String pattern) throws ConversionExceptio
@Override
public boolean compareEqual(Object left, Object right) {
if (left instanceof byte[] l && right instanceof byte[] r) {
return equalsTo(l, r);
if (l.length != r.length) return false;
for (int i = 0; i < l.length; i++) {
int compare = l[i] - r[i];
if (compare != 0) {
return false;
}
}
return true;
}
return super.compareEqual(left, right);
}
Expand All @@ -121,17 +127,4 @@ public int compare(Object left, Object right) {
}
return super.compare(left, right);
}

protected static boolean equalsTo(final byte[] left, final byte[] right) {

if (left.length != right.length) return false;

for (int i = 0; i < left.length; i++) {
int compare = left[i] - right[i];
if (compare != 0) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public TypeComparability getComparability() {
@Override
public <T> T convert(final Object value, final Class<T> clazz) throws ConversionException {

if (value == null) return null;
if (value == null) {
return null;
}
if (clazz.isInstance(value)) {
return clazz.cast(value);
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/apache/hop/expression/OperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void ElementAt() throws Exception {

evalFails("[1,3,5][0]", ErrorCode.INVALID_ARRAY_INDEX);
evalFails("[1,3,5][9]", ErrorCode.INVALID_ARRAY_INDEX);

optimize("[FIELD_INTEGER,3,5][1]");
optimize("[FIELD_STRING,'A','B'][1]");
}

@Test
Expand Down Expand Up @@ -1487,12 +1490,14 @@ void AtTimeZone() throws Exception {
"(TIMESTAMP '2023-05-25 10:48:00' AT TIME ZONE 'UTC') AT TIME ZONE 'Asia/Singapore'",
ZonedDateTime.of(2023, 5, 25, 10, 48, 00, 0, ZoneId.of("Asia/Singapore")));

evalNull("NULL_DATE AT TIME ZONE 'CET'");

evalFails("TIMESTAMP '2023-05-25 20:48:00' AT TIME ZONE 'XYZ'", ErrorCode.INVALID_TIMEZONE);
evalFails("TIMESTAMP '2023-05-25 20:48:00' AT TIME 'Europe/Paris'", ErrorCode.SYNTAX_ERROR);
evalFails("TIMESTAMP '2023-05-25 20:48:00' AT TIME ZONE", ErrorCode.SYNTAX_ERROR);
evalFails("TIMESTAMP '2023-05-25 20:48:00' AT ZONE 'Europe/Paris'", ErrorCode.SYNTAX_ERROR);

optimize("TIMESTAMP '2023-05-25 20:48:00' AT TIME ZONE 'Europe/Paris'");
optimize("FIELD_DATE AT TIME ZONE 'Europe/Paris'");
}

@Test
Expand Down
Loading

0 comments on commit f4422f7

Please sign in to comment.