Fix toString for SingleThreadSpecializable ConstantExprs (#16084)

This commit is contained in:
Zoltan Haindrich 2024-03-13 11:48:52 +01:00 committed by GitHub
parent aa2959b2bd
commit 818cc9eedf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 175 additions and 4 deletions

View File

@ -115,6 +115,11 @@ abstract class ConstantExpr<T> implements Expr, Expr.SingleThreadSpecializable
protected abstract ExprEval<T> realEval();
@Override
public String toString()
{
return String.valueOf(value);
}
@Override
public Expr toSingleThreaded()
@ -145,6 +150,18 @@ abstract class ConstantExpr<T> implements Expr, Expr.SingleThreadSpecializable
return eval;
}
@Override
public String stringify()
{
return eval.toExpr().stringify();
}
@Override
public String toString()
{
return eval.toExpr().toString();
}
@Override
public int hashCode()
{

View File

@ -19,20 +19,174 @@
package org.apache.druid.math.expr;
import org.apache.druid.math.expr.Expr.ObjectBinding;
import org.apache.druid.segment.column.TypeStrategies;
import org.apache.druid.segment.column.TypeStrategiesTest.NullableLongPair;
import org.apache.druid.segment.column.TypeStrategiesTest.NullableLongPairTypeStrategy;
import org.apache.druid.testing.InitializedNullHandlingTest;
import org.junit.Test;
import java.math.BigInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class ConstantExprTest extends InitializedNullHandlingTest
{
@Test
public void testLongSingleThreadedExpr()
public void testLongArrayExpr()
{
LongExpr expr = new LongExpr(11L);
assertNotSame(expr.eval(null), expr.eval(null));
ArrayExpr arrayExpr = new ArrayExpr(ExpressionType.LONG_ARRAY, new Long[] {1L, 3L});
checkExpr(
arrayExpr,
true,
"[1, 3]",
"ARRAY<LONG>[1, 3]",
arrayExpr
);
}
@Test
public void testStringArrayExpr()
{
ArrayExpr arrayExpr = new ArrayExpr(ExpressionType.STRING_ARRAY, new String[] {"foo", "bar"});
checkExpr(
arrayExpr,
true,
"[foo, bar]",
"ARRAY<STRING>['foo', 'bar']",
arrayExpr
);
}
@Test
public void testBigIntegerExpr()
{
checkExpr(
new BigIntegerExpr(BigInteger.valueOf(37L)),
true,
"37",
"37",
// after reparsing it will become a LongExpr
new LongExpr(37L)
);
}
@Test
public void testComplexExpr()
{
TypeStrategies.registerComplex("nullablePair", new NullableLongPairTypeStrategy());
ComplexExpr complexExpr = new ComplexExpr(
ExpressionTypeFactory.getInstance().ofComplex("nullablePair"),
new NullableLongPair(21L, 37L)
);
checkExpr(
complexExpr,
true,
"Pair{lhs=21, rhs=37}",
"complex_decode_base64('nullablePair', 'AAAAAAAAAAAVAAAAAAAAAAAl')",
complexExpr
);
}
@Test
public void testDoubleExpr()
{
checkExpr(
new DoubleExpr(11.73D),
true,
"11.73",
"11.73",
new DoubleExpr(11.73D)
);
}
@Test
public void testNullDoubleExpr()
{
TypeStrategies.registerComplex("nullablePair", new NullableLongPairTypeStrategy());
checkExpr(
new NullDoubleExpr(),
true,
"null",
"null",
// the expressions 'null' is always parsed as a StringExpr(null)
new StringExpr(null)
);
}
@Test
public void testNullLongExpr()
{
checkExpr(
new NullLongExpr(),
true,
"null",
"null",
// the expressions 'null' is always parsed as a StringExpr(null)
new StringExpr(null)
);
}
@Test
public void testLong()
{
checkExpr(
new LongExpr(11L),
true,
"11",
"11",
new LongExpr(11L)
);
}
@Test
public void testString()
{
checkExpr(
new StringExpr("some"),
true,
"some",
"'some'",
new StringExpr("some")
);
}
@Test
public void testStringNull()
{
checkExpr(
new StringExpr(null),
true,
null,
"null",
new StringExpr(null)
);
}
private void checkExpr(
Expr expr,
boolean supportsSingleThreaded,
String expectedToString,
String expectedStringify,
Expr expectedReparsedExpr)
{
ObjectBinding bindings = InputBindings.nilBindings();
if (expr.getLiteralValue() != null) {
assertNotSame(expr.eval(bindings), expr.eval(bindings));
}
Expr singleExpr = Expr.singleThreaded(expr);
assertSame(singleExpr.eval(null), singleExpr.eval(null));
if (supportsSingleThreaded) {
assertSame(singleExpr.eval(bindings), singleExpr.eval(bindings));
} else {
assertNotSame(singleExpr.eval(bindings), singleExpr.eval(bindings));
}
assertEquals(expectedToString, expr.toString());
assertEquals(expectedStringify, expr.stringify());
assertEquals(expectedToString, singleExpr.toString());
String stringify = singleExpr.stringify();
Expr reParsedExpr = Parser.parse(stringify, ExprMacroTable.nil());
assertEquals(expectedReparsedExpr, reParsedExpr);
}
}