-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL: rewrite ROUND and TRUNCATE functions with a different optional p…
…arameter handling method (#40242) * Rewrite Round and Truncate functions to have a slightly different approach to handling the optional parameter in the constructor. Until now the optional parameter was considered 0 if the value was missing and the constructor was filling in this value. The current solution is to have the optional parameter as null right until the actual calculation is done. (cherry picked from commit 3e314f8)
- Loading branch information
Showing
17 changed files
with
548 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...a/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathPipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.sql.expression.function.scalar.math; | ||
|
||
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder; | ||
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryOptionalMathProcessor.BinaryOptionalMathOperation; | ||
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe; | ||
import org.elasticsearch.xpack.sql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.sql.tree.Location; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class BinaryOptionalMathPipe extends Pipe { | ||
|
||
private final Pipe left, right; | ||
private final BinaryOptionalMathOperation operation; | ||
|
||
public BinaryOptionalMathPipe(Location location, Expression expression, Pipe left, Pipe right, BinaryOptionalMathOperation operation) { | ||
super(location, expression, right == null ? Arrays.asList(left) : Arrays.asList(left, right)); | ||
this.left = left; | ||
this.right = right; | ||
this.operation = operation; | ||
} | ||
|
||
@Override | ||
public final Pipe replaceChildren(List<Pipe> newChildren) { | ||
int childrenSize = newChildren.size(); | ||
if (childrenSize > 2 || childrenSize < 1) { | ||
throw new IllegalArgumentException("expected [1 or 2] children but received [" + newChildren.size() + "]"); | ||
} | ||
return replaceChildren(newChildren.get(0), childrenSize == 1 ? null : newChildren.get(1)); | ||
} | ||
|
||
@Override | ||
public final Pipe resolveAttributes(AttributeResolver resolver) { | ||
Pipe newLeft = left.resolveAttributes(resolver); | ||
Pipe newRight = right == null ? right : right.resolveAttributes(resolver); | ||
if (newLeft == left && newRight == right) { | ||
return this; | ||
} | ||
return replaceChildren(newLeft, newRight); | ||
} | ||
|
||
@Override | ||
public boolean supportedByAggsOnlyQuery() { | ||
return right == null ? left.supportedByAggsOnlyQuery() : left.supportedByAggsOnlyQuery() || right.supportedByAggsOnlyQuery(); | ||
} | ||
|
||
@Override | ||
public boolean resolved() { | ||
return left.resolved() && (right == null || right.resolved()); | ||
} | ||
|
||
protected Pipe replaceChildren(Pipe newLeft, Pipe newRight) { | ||
return new BinaryOptionalMathPipe(location(), expression(), newLeft, newRight, operation); | ||
} | ||
|
||
@Override | ||
public final void collectFields(SqlSourceBuilder sourceBuilder) { | ||
left.collectFields(sourceBuilder); | ||
if (right != null) { | ||
right.collectFields(sourceBuilder); | ||
} | ||
} | ||
|
||
@Override | ||
protected NodeInfo<BinaryOptionalMathPipe> info() { | ||
return NodeInfo.create(this, BinaryOptionalMathPipe::new, expression(), left, right, operation); | ||
} | ||
|
||
@Override | ||
public BinaryOptionalMathProcessor asProcessor() { | ||
return new BinaryOptionalMathProcessor(left.asProcessor(), right == null ? null : right.asProcessor(), operation); | ||
} | ||
|
||
public Pipe right() { | ||
return right; | ||
} | ||
|
||
public Pipe left() { | ||
return left; | ||
} | ||
|
||
public BinaryOptionalMathOperation operation() { | ||
return operation; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(left, right, operation); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
BinaryOptionalMathPipe other = (BinaryOptionalMathPipe) obj; | ||
return Objects.equals(left, other.left) | ||
&& Objects.equals(right, other.right) | ||
&& Objects.equals(operation, other.operation); | ||
} | ||
} |
Oops, something went wrong.