SQL: fix name of Arithmetic functions (elastic/x-pack-elasticsearch#3884)

* SQL: fix name of Arithmetic functions

Remove id from name of Arithmetic function and improve name of Neg

Original commit: elastic/x-pack-elasticsearch@4f3e8d6a2d
This commit is contained in:
Costin Leau 2018-02-09 19:48:22 +02:00 committed by GitHub
parent 7504e33da7
commit 3c6f9d4d29
3 changed files with 56 additions and 7 deletions

View File

@ -82,11 +82,6 @@ public abstract class ArithmeticFunction extends BinaryScalarFunction {
@Override
public String name() {
return toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(left());
@ -103,11 +98,15 @@ public abstract class ArithmeticFunction extends BinaryScalarFunction {
sb.insert(pos, "(");
sb.append(")");
}
sb.append(")#");
sb.append(functionId());
sb.append(")");
return sb.toString();
}
@Override
public String toString() {
return name() + "#" + functionId();
}
protected boolean useParanthesis() {
return !(left() instanceof Literal) || !(right() instanceof Literal);
}

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.NamedExpression;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.UnaryArithmeticProcessor.UnaryArithmeticOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
@ -49,6 +50,11 @@ public class Neg extends UnaryScalarFunction {
return field().dataType();
}
@Override
public String name() {
return "-" + (field() instanceof NamedExpression && field().resolved() ? Expressions.name(field()) : field().toString());
}
@Override
protected String formatScript(String template) {
// Painless supports negating (and hopefully its corner cases)

View File

@ -0,0 +1,44 @@
/*
* 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;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Add;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Div;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Mod;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Mul;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Neg;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Sub;
import static org.elasticsearch.xpack.sql.tree.Location.EMPTY;
public class NamedExpressionTests extends ESTestCase {
public void testArithmeticFunctionName() {
Add add = new Add(EMPTY, l(5), l(2));
assertEquals("(5 + 2)", add.name());
Div div = new Div(EMPTY, l(5), l(2));
assertEquals("(5 / 2)", div.name());
Mod mod = new Mod(EMPTY, l(5), l(2));
assertEquals("(5 % 2)", mod.name());
Mul mul = new Mul(EMPTY, l(5), l(2));
assertEquals("(5 * 2)", mul.name());
Sub sub = new Sub(EMPTY, l(5), l(2));
assertEquals("(5 - 2)", sub.name());
Neg neg = new Neg(EMPTY, l(5));
assertEquals("-5", neg.name());
}
private static Literal l(Object value) {
return Literal.of(EMPTY, value);
}
}