Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add valueOf() to Expression #1055

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -184,7 +184,7 @@ public Expression visitConstantFunction(ConstantFunction node, AnalysisContext c
}

var value = visitFunction(node, context);
value = DSL.literal(value.valueOf(null));
value = DSL.literal(value.valueOf());
context.getConstantFunctionValues().put(valueName, value);
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static NamedExpression named(Expression expression) {
return (NamedExpression) expression;
}
if (expression instanceof ParseExpression) {
return named(((ParseExpression) expression).getIdentifier().valueOf(null).stringValue(),
return named(((ParseExpression) expression).getIdentifier().valueOf().stringValue(),
expression);
}
return named(expression.toString(), expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
public interface Expression extends Serializable {

/**
* Evaluate the value of expression that does not depend on value environment.
*/
default ExprValue valueOf() {
return valueOf(null);
}

/**
* Evaluate the value of expression in the value environment.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public TakeAggregator(List<Expression> arguments, ExprCoreType returnType) {

@Override
public TakeState create() {
return new TakeState(getArguments().get(1).valueOf(null).integerValue());
return new TakeState(getArguments().get(1).valueOf().integerValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class GrokExpression extends ParseExpression {
*/
public GrokExpression(Expression sourceField, Expression pattern, Expression identifier) {
super("grok", sourceField, pattern, identifier);
this.grok = grokCompiler.compile(pattern.valueOf(null).stringValue());
this.grok = grokCompiler.compile(pattern.valueOf().stringValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ParseExpression(String functionName, Expression sourceField, Expression p
this.sourceField = sourceField;
this.pattern = pattern;
this.identifier = identifier;
this.identifierStr = identifier.valueOf(null).stringValue();
this.identifierStr = identifier.valueOf().stringValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class PatternsExpression extends ParseExpression {
*/
public PatternsExpression(Expression sourceField, Expression pattern, Expression identifier) {
super("patterns", sourceField, pattern, identifier);
String patternStr = pattern.valueOf(null).stringValue();
String patternStr = pattern.valueOf().stringValue();
useCustomPattern = !patternStr.isEmpty();
if (useCustomPattern) {
this.pattern = Pattern.compile(patternStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class RegexExpression extends ParseExpression {
*/
public RegexExpression(Expression sourceField, Expression pattern, Expression identifier) {
super("regex", sourceField, pattern, identifier);
this.regexPattern = Pattern.compile(pattern.valueOf(null).stringValue());
this.regexPattern = Pattern.compile(pattern.valueOf().stringValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean hasNext() {
@Override
public ExprValue next() {
List<ExprValue> values = valuesIterator.next().stream()
.map(expr -> expr.valueOf(null))
.map(expr -> expr.valueOf())
.collect(Collectors.toList());
return new ExprCollectionValue(values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class Rounding<T> {
* Create Rounding instance.
*/
public static Rounding<?> createRounding(SpanExpression span) {
ExprValue interval = span.getValue().valueOf(null);
ExprValue interval = span.getValue().valueOf();
ExprType type = span.type();

if (LONG.isCompatible(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public void constant_function_returns_constant_cached_value() {
var values = List.of(analyze(AstDSL.constantFunction("now")),
analyze(AstDSL.constantFunction("now")), analyze(AstDSL.constantFunction("now")));
assertTrue(values.stream().allMatch(v ->
v.valueOf(null) == analyze(AstDSL.constantFunction("now")).valueOf(null)));
v.valueOf() == analyze(AstDSL.constantFunction("now")).valueOf()));
}

@Test
Expand All @@ -593,17 +593,17 @@ public void function_returns_non_constant_value() {
// different values
var values = List.of(analyze(function("sysdate")), analyze(function("sysdate")),
analyze(function("sysdate")), analyze(function("sysdate")));
var referenceValue = analyze(function("sysdate")).valueOf(null);
assertTrue(values.stream().noneMatch(v -> v.valueOf(null) == referenceValue));
var referenceValue = analyze(function("sysdate")).valueOf();
assertTrue(values.stream().noneMatch(v -> v.valueOf() == referenceValue));
}

@Test
public void now_as_a_function_not_cached() {
// // We can call `now()` as a function, in that case nothing should be cached
var values = List.of(analyze(function("now")), analyze(function("now")),
analyze(function("now")), analyze(function("now")));
var referenceValue = analyze(function("now")).valueOf(null);
assertTrue(values.stream().noneMatch(v -> v.valueOf(null) == referenceValue));
var referenceValue = analyze(function("now")).valueOf();
assertTrue(values.stream().noneMatch(v -> v.valueOf() == referenceValue));
}

protected Expression analyze(UnresolvedExpression unresolvedExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void name_a_parse_expression() {
DSL.literal("group"));
NamedExpression named = DSL.named(parse);
assertEquals(parse, named.getDelegated());
assertEquals(parse.getIdentifier().valueOf(null).stringValue(), named.getName());
assertEquals(parse.getIdentifier().valueOf().stringValue(), named.getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ public void time() {

expr = dsl.time(dsl.date(DSL.literal("2020-01-02")));
assertEquals(TIME, expr.type());
assertEquals(new ExprTimeValue("00:00:00"), expr.valueOf(null));
assertEquals(new ExprTimeValue("00:00:00"), expr.valueOf());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ protected FunctionExpression fromUnixTime(Expression value, Expression format) {
}

protected LocalDateTime fromUnixTime(Long value) {
return fromUnixTime(DSL.literal(value)).valueOf(null).datetimeValue();
return fromUnixTime(DSL.literal(value)).valueOf().datetimeValue();
}

protected LocalDateTime fromUnixTime(Double value) {
return fromUnixTime(DSL.literal(value)).valueOf(null).datetimeValue();
return fromUnixTime(DSL.literal(value)).valueOf().datetimeValue();
}

protected String fromUnixTime(Long value, String format) {
return fromUnixTime(DSL.literal(value), DSL.literal(format)).valueOf(null).stringValue();
return fromUnixTime(DSL.literal(value), DSL.literal(format)).valueOf().stringValue();
}

protected String fromUnixTime(Double value, String format) {
return fromUnixTime(DSL.literal(value), DSL.literal(format)).valueOf(null).stringValue();
return fromUnixTime(DSL.literal(value), DSL.literal(format)).valueOf().stringValue();
}

protected FunctionExpression makedate(Expression year, Expression dayOfYear) {
Expand All @@ -89,7 +89,7 @@ protected FunctionExpression makedate(Expression year, Expression dayOfYear) {
}

protected LocalDate makedate(Double year, Double dayOfYear) {
return makedate(DSL.literal(year), DSL.literal(dayOfYear)).valueOf(null).dateValue();
return makedate(DSL.literal(year), DSL.literal(dayOfYear)).valueOf().dateValue();
}

protected FunctionExpression maketime(Expression hour, Expression minute, Expression second) {
Expand All @@ -101,7 +101,7 @@ protected FunctionExpression maketime(Expression hour, Expression minute, Expres

protected LocalTime maketime(Double hour, Double minute, Double second) {
return maketime(DSL.literal(hour), DSL.literal(minute), DSL.literal(second))
.valueOf(null).timeValue();
.valueOf().timeValue();
}

protected FunctionExpression period_add(Expression period, Expression months) {
Expand All @@ -112,7 +112,7 @@ protected FunctionExpression period_add(Expression period, Expression months) {
}

protected Integer period_add(Integer period, Integer months) {
return period_add(DSL.literal(period), DSL.literal(months)).valueOf(null).integerValue();
return period_add(DSL.literal(period), DSL.literal(months)).valueOf().integerValue();
}

protected FunctionExpression period_diff(Expression first, Expression second) {
Expand All @@ -123,7 +123,7 @@ protected FunctionExpression period_diff(Expression first, Expression second) {
}

protected Integer period_diff(Integer first, Integer second) {
return period_diff(DSL.literal(first), DSL.literal(second)).valueOf(null).integerValue();
return period_diff(DSL.literal(first), DSL.literal(second)).valueOf().integerValue();
}

protected FunctionExpression unixTimeStampExpr() {
Expand All @@ -133,7 +133,7 @@ protected FunctionExpression unixTimeStampExpr() {
}

protected Long unixTimeStamp() {
return unixTimeStampExpr().valueOf(null).longValue();
return unixTimeStampExpr().valueOf().longValue();
}

protected FunctionExpression unixTimeStampOf(Expression value) {
Expand All @@ -144,18 +144,18 @@ protected FunctionExpression unixTimeStampOf(Expression value) {
}

protected Double unixTimeStampOf(Double value) {
return unixTimeStampOf(DSL.literal(value)).valueOf(null).doubleValue();
return unixTimeStampOf(DSL.literal(value)).valueOf().doubleValue();
}

protected Double unixTimeStampOf(LocalDate value) {
return unixTimeStampOf(DSL.literal(new ExprDateValue(value))).valueOf(null).doubleValue();
return unixTimeStampOf(DSL.literal(new ExprDateValue(value))).valueOf().doubleValue();
}

protected Double unixTimeStampOf(LocalDateTime value) {
return unixTimeStampOf(DSL.literal(new ExprDatetimeValue(value))).valueOf(null).doubleValue();
return unixTimeStampOf(DSL.literal(new ExprDatetimeValue(value))).valueOf().doubleValue();
}

protected Double unixTimeStampOf(Instant value) {
return unixTimeStampOf(DSL.literal(new ExprTimestampValue(value))).valueOf(null).doubleValue();
return unixTimeStampOf(DSL.literal(new ExprTimestampValue(value))).valueOf().doubleValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,54 +132,54 @@ public void checkOfDoubleWithFormat(Double value, String format, String expected
@Test
public void checkInvalidFormat() {
assertEquals(new ExprStringValue("q"),
fromUnixTime(DSL.literal(0L), DSL.literal("%q")).valueOf(null));
fromUnixTime(DSL.literal(0L), DSL.literal("%q")).valueOf());
assertEquals(new ExprStringValue(""),
fromUnixTime(DSL.literal(0L), DSL.literal("")).valueOf(null));
fromUnixTime(DSL.literal(0L), DSL.literal("")).valueOf());
}

@Test
public void checkValueOutsideOfTheRangeWithoutFormat() {
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(-1L)).valueOf(null));
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(-1.5d)).valueOf(null));
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771200L)).valueOf(null));
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771200d)).valueOf(null));
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(-1L)).valueOf());
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(-1.5d)).valueOf());
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771200L)).valueOf());
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771200d)).valueOf());
}

@Test
public void checkInsideTheRangeWithoutFormat() {
assertNotEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771199L)).valueOf(null));
assertNotEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771199d)).valueOf(null));
assertNotEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771199L)).valueOf());
assertNotEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(32536771199d)).valueOf());
}

@Test
public void checkValueOutsideOfTheRangeWithFormat() {
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(32536771200L), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(32536771200L), DSL.literal("%d")).valueOf());
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(32536771200d), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(32536771200d), DSL.literal("%d")).valueOf());
}

@Test
public void checkInsideTheRangeWithFormat() {
assertNotEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(32536771199L), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(32536771199L), DSL.literal("%d")).valueOf());
assertNotEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(32536771199d), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(32536771199d), DSL.literal("%d")).valueOf());
}

@Test
public void checkNullOrNegativeValues() {
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(ExprNullValue.of())).valueOf(null));
assertEquals(ExprNullValue.of(), fromUnixTime(DSL.literal(ExprNullValue.of())).valueOf());
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(-1L), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(-1L), DSL.literal("%d")).valueOf());
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(-1.5d), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(-1.5d), DSL.literal("%d")).valueOf());
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(42L), DSL.literal(ExprNullValue.of())).valueOf(null));
fromUnixTime(DSL.literal(42L), DSL.literal(ExprNullValue.of())).valueOf());
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(ExprNullValue.of()), DSL.literal("%d")).valueOf(null));
fromUnixTime(DSL.literal(ExprNullValue.of()), DSL.literal("%d")).valueOf());
assertEquals(ExprNullValue.of(),
fromUnixTime(DSL.literal(ExprNullValue.of()), DSL.literal(ExprNullValue.of()))
.valueOf(null));
.valueOf());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ private static Stream<Arguments> functionNames() {

private Temporal extractValue(FunctionExpression func) {
switch ((ExprCoreType)func.type()) {
case DATE: return func.valueOf(null).dateValue();
case DATETIME: return func.valueOf(null).datetimeValue();
case TIME: return func.valueOf(null).timeValue();
case DATE: return func.valueOf().dateValue();
case DATETIME: return func.valueOf().datetimeValue();
case TIME: return func.valueOf().timeValue();
// unreachable code
default: throw new IllegalArgumentException(String.format("%s", func.type()));
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public void test_now_like_functions(Function<Expression[], FunctionExpression> f

for (var wrongFspValue: List.of(-1, 10)) {
var exception = assertThrows(IllegalArgumentException.class,
() -> function.apply(new Expression[]{DSL.literal(wrongFspValue)}).valueOf(null));
() -> function.apply(new Expression[]{DSL.literal(wrongFspValue)}).valueOf());
assertEquals(String.format("Invalid `fsp` value: %d, allowed 0 to 6", wrongFspValue),
exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public static Stream<Arguments> getInvalidTestData() {
@ParameterizedTest
@MethodSource("getInvalidTestData")
public void period_add_returns_null_on_invalid_input(int period) {
assertNull(period_add(DSL.literal(period), DSL.literal(1)).valueOf(null).value());
assertNull(period_diff(DSL.literal(period), DSL.literal(1)).valueOf(null).value());
assertNull(period_diff(DSL.literal(1), DSL.literal(period)).valueOf(null).value());
assertNull(period_add(DSL.literal(period), DSL.literal(1)).valueOf().value());
assertNull(period_diff(DSL.literal(period), DSL.literal(1)).valueOf().value());
assertNull(period_diff(DSL.literal(1), DSL.literal(period)).valueOf().value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private static Stream<Arguments> getInvalidDoubleSamples() {
@MethodSource("getInvalidDoubleSamples")
public void checkInvalidDoubleCausesNull(Double value) {
assertEquals(ExprNullValue.of(),
unixTimeStampOf(DSL.literal(new ExprDoubleValue(value))).valueOf(null),
unixTimeStampOf(DSL.literal(new ExprDoubleValue(value))).valueOf(),
new DecimalFormat("0.#").format(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void add(ExprValue op1, ExprValue op2) {
FunctionExpression expression = dsl.add(literal(op1), literal(op2));
ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type());
assertEquals(expectedType, expression.type());
assertValueEqual(BuiltinFunctionName.ADD, expectedType, op1, op2, expression.valueOf(null));
assertValueEqual(BuiltinFunctionName.ADD, expectedType, op1, op2, expression.valueOf());
assertEquals(String.format("+(%s, %s)", op1.toString(), op2.toString()), expression.toString());
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public void subtract(ExprValue op1, ExprValue op2) {
ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type());
assertEquals(expectedType, expression.type());
assertValueEqual(BuiltinFunctionName.SUBTRACT, expectedType, op1, op2,
expression.valueOf(null));
expression.valueOf());
assertEquals(String.format("-(%s, %s)", op1.toString(), op2.toString()),
expression.toString());
}
Expand All @@ -159,7 +159,7 @@ public void multiply(ExprValue op1, ExprValue op2) {
ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type());
assertEquals(expectedType, expression.type());
assertValueEqual(BuiltinFunctionName.MULTIPLY, expectedType, op1, op2,
expression.valueOf(null));
expression.valueOf());
assertEquals(String.format("*(%s, %s)", op1.toString(), op2.toString()),
expression.toString());
}
Expand All @@ -170,7 +170,7 @@ public void divide(ExprValue op1, ExprValue op2) {
FunctionExpression expression = dsl.divide(literal(op1), literal(op2));
ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type());
assertEquals(expectedType, expression.type());
assertValueEqual(BuiltinFunctionName.DIVIDE, expectedType, op1, op2, expression.valueOf(null));
assertValueEqual(BuiltinFunctionName.DIVIDE, expectedType, op1, op2, expression.valueOf());
assertEquals(String.format("/(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

Expand All @@ -187,7 +187,7 @@ public void module(ExprValue op1, ExprValue op2) {
FunctionExpression expression = dsl.module(literal(op1), literal(op2));
ExprType expectedType = WideningTypeRule.max(op1.type(), op2.type());
assertEquals(expectedType, expression.type());
assertValueEqual(BuiltinFunctionName.MODULES, expectedType, op1, op2, expression.valueOf(null));
assertValueEqual(BuiltinFunctionName.MODULES, expectedType, op1, op2, expression.valueOf());
assertEquals(String.format("%%(%s, %s)", op1.toString(), op2.toString()),
expression.toString());

Expand Down
Loading