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

[nereids](datetime) fix wrong result type of datetime add with interval as first arg #26957

Merged
merged 2 commits into from
Nov 14, 2023
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 @@ -1334,7 +1334,7 @@ public Expression visitArithmeticBinary(ArithmeticBinaryContext ctx) {
throw new ParseException("Only supported: " + Operator.ADD, ctx);
}
Interval interval = (Interval) left;
return new TimestampArithmetic(Operator.ADD, right, interval.value(), interval.timeUnit(), true);
return new TimestampArithmetic(Operator.ADD, right, interval.value(), interval.timeUnit());
}

if (right instanceof Interval) {
Expand All @@ -1347,7 +1347,7 @@ public Expression visitArithmeticBinary(ArithmeticBinaryContext ctx) {
throw new ParseException("Only supported: " + Operator.ADD + " and " + Operator.SUBTRACT, ctx);
}
Interval interval = (Interval) right;
return new TimestampArithmetic(op, left, interval.value(), interval.timeUnit(), false);
return new TimestampArithmetic(op, left, interval.value(), interval.timeUnit());
}

return ParserUtils.withOrigin(ctx, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,21 @@
public class TimestampArithmetic extends Expression implements BinaryExpression, PropagateNullableOnDateLikeV2Args {

private final String funcName;
private final boolean intervalFirst;
private final Operator op;
private final TimeUnit timeUnit;

public TimestampArithmetic(Operator op, Expression e1, Expression e2, TimeUnit timeUnit, boolean intervalFirst) {
this(null, op, e1, e2, timeUnit, intervalFirst);
public TimestampArithmetic(Operator op, Expression e1, Expression e2, TimeUnit timeUnit) {
this(null, op, e1, e2, timeUnit);
}

/**
* Full parameter constructor.
*/
public TimestampArithmetic(String funcName, Operator op, Expression e1, Expression e2, TimeUnit timeUnit,
boolean intervalFirst) {
public TimestampArithmetic(String funcName, Operator op, Expression e1, Expression e2, TimeUnit timeUnit) {
super(ImmutableList.of(e1, e2));
Preconditions.checkState(op == Operator.ADD || op == Operator.SUBTRACT);
this.funcName = funcName;
this.op = op;
this.intervalFirst = intervalFirst;
this.timeUnit = timeUnit;
}

Expand All @@ -76,21 +73,16 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
public TimestampArithmetic withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 2);
return new TimestampArithmetic(this.funcName, this.op, children.get(0), children.get(1),
this.timeUnit, this.intervalFirst);
this.timeUnit);
}

public Expression withFuncName(String funcName) {
return new TimestampArithmetic(funcName, this.op, children.get(0), children.get(1), this.timeUnit,
this.intervalFirst);
return new TimestampArithmetic(funcName, this.op, children.get(0), children.get(1), this.timeUnit);
}

@Override
public DataType getDataType() throws UnboundException {
int dateChildIndex = 0;
if (intervalFirst) {
dateChildIndex = 1;
}
DataType childType = child(dateChildIndex).getDataType();
DataType childType = child(0).getDataType();
if (childType instanceof DateTimeV2Type) {
return childType;
}
Expand Down Expand Up @@ -149,21 +141,12 @@ public String toSql() {
strBuilder.append(")");
return strBuilder.toString();
}
if (intervalFirst) {
// Non-function-call like version with interval as first operand.
strBuilder.append("INTERVAL ");
strBuilder.append(child(1).toSql()).append(" ");
strBuilder.append(timeUnit);
strBuilder.append(" ").append(op.toString()).append(" ");
strBuilder.append(child(0).toSql());
} else {
// Non-function-call like version with interval as second operand.
strBuilder.append(child(0).toSql());
strBuilder.append(" ").append(op.toString()).append(" ");
strBuilder.append("INTERVAL ");
strBuilder.append(child(1).toSql()).append(" ");
strBuilder.append(timeUnit);
}
// Non-function-call like version with interval as second operand.
strBuilder.append(child(0).toSql());
strBuilder.append(" ").append(op.toString()).append(" ");
strBuilder.append("INTERVAL ");
strBuilder.append(child(1).toSql()).append(" ");
strBuilder.append(timeUnit);
return strBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void testTimestampFold() {

// a + interval 1 day
Slot a = SlotReference.of("a", DateTimeV2Type.SYSTEM_DEFAULT);
TimestampArithmetic arithmetic = new TimestampArithmetic(Operator.ADD, a, Literal.of(1), TimeUnit.DAY, false);
TimestampArithmetic arithmetic = new TimestampArithmetic(Operator.ADD, a, Literal.of(1), TimeUnit.DAY);
Expression process = process(arithmetic);
assertRewrite(process, process);
}
Expand Down
Loading
Loading