SQL: Fix long lines in function package (elastic/x-pack-elasticsearch#3452)

Adds line breaks in long lines in SQL's function package. Rewrites one
line to use `string + string` style instead of `String.format` style
because that is "more normal" in Elasticsearch.

Original commit: elastic/x-pack-elasticsearch@2f4d0358af
This commit is contained in:
Nik Everett 2017-12-29 16:52:08 -05:00 committed by GitHub
parent e45ec84a03
commit f5ff5a94a0
10 changed files with 62 additions and 48 deletions

View File

@ -19,15 +19,6 @@
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]LiteralAttribute.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]TypedAttribute.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]UnresolvedAttribute.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]UnresolvedFunction.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]aggregate[/\\]AggregateFunctionAttribute.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]aggregate[/\\]InnerAggregate.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]scalar[/\\]Cast.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]scalar[/\\]ScalarFunctionAttribute.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]scalar[/\\]arithmetic[/\\]ArithmeticFunction.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]scalar[/\\]arithmetic[/\\]BinaryArithmeticProcessorDefinition.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]scalar[/\\]arithmetic[/\\]Neg.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]expression[/\\]function[/\\]scalar[/\\]datetime[/\\]DateTimeFunction.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]optimizer[/\\]Optimizer.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]parser[/\\]CommandBuilder.java" checks="LineLength" />
<suppress files="sql[/\\]server[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]xpack[/\\]sql[/\\]parser[/\\]ExpressionBuilder.java" checks="LineLength" />

View File

@ -20,8 +20,11 @@ public class UnresolvedFunction extends Function implements Unresolvable {
private final String name;
private final boolean distinct;
private final String unresolvedMsg;
// flag to indicate analysis has been applied and there's no point in doing it again
// this is an optimization to prevent searching for a better unresolved message over and over again
/**
* Flag to indicate analysis has been applied and there's no point in
* doing it again this is an optimization to prevent searching for a
* better unresolved message over and over again.
*/
private final boolean analyzed;
public UnresolvedFunction(Location location, String name, boolean distinct, List<Expression> children) {
@ -29,9 +32,11 @@ public class UnresolvedFunction extends Function implements Unresolvable {
}
/**
* Constructor used for specifying a more descriptive message (typically 'did you mean') instead of the default one.
* Constructor used for specifying a more descriptive message (typically
* 'did you mean') instead of the default one.
*/
public UnresolvedFunction(Location location, String name, boolean distinct, List<Expression> children, boolean analyzed, String unresolvedMessage) {
public UnresolvedFunction(Location location, String name, boolean distinct, List<Expression> children,
boolean analyzed, String unresolvedMessage) {
super(location, children);
this.name = name;
this.distinct = distinct;
@ -95,4 +100,4 @@ public class UnresolvedFunction extends Function implements Unresolvable {
}
return msg;
}
}
}

View File

@ -18,11 +18,13 @@ public class AggregateFunctionAttribute extends FunctionAttribute {
private final String propertyPath;
AggregateFunctionAttribute(Location location, String name, DataType dataType, ExpressionId id, String functionId, String propertyPath) {
AggregateFunctionAttribute(Location location, String name, DataType dataType, ExpressionId id,
String functionId, String propertyPath) {
this(location, name, dataType, null, false, id, false, functionId, propertyPath);
}
AggregateFunctionAttribute(Location location, String name, DataType dataType, String qualifier, boolean nullable, ExpressionId id, boolean synthetic, String functionId, String propertyPath) {
AggregateFunctionAttribute(Location location, String name, DataType dataType, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic, String functionId, String propertyPath) {
super(location, name, dataType, qualifier, nullable, id, synthetic, functionId);
this.propertyPath = propertyPath;
}
@ -37,14 +39,16 @@ public class AggregateFunctionAttribute extends FunctionAttribute {
}
@Override
protected Attribute clone(Location location, String name, DataType dataType, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
protected Attribute clone(Location location, String name, DataType dataType, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic) {
// this is highly correlated with QueryFolder$FoldAggregate#addFunction (regarding the function name within the querydsl)
// that is the functionId is actually derived from the expression id to easily track it across contexts
return new AggregateFunctionAttribute(location, name, dataType, qualifier, nullable, id, synthetic, functionId(), propertyPath);
}
public AggregateFunctionAttribute withFunctionId(String functionId, String propertyPath) {
return new AggregateFunctionAttribute(location(), name(), dataType(), qualifier(), nullable(), id(), synthetic(), functionId, propertyPath);
return new AggregateFunctionAttribute(location(), name(), dataType(), qualifier(), nullable(),
id(), synthetic(), functionId, propertyPath);
}
@Override
@ -61,4 +65,4 @@ public class AggregateFunctionAttribute extends FunctionAttribute {
protected String label() {
return "a->" + functionId();
}
}
}

View File

@ -50,7 +50,7 @@ public class InnerAggregate extends AggregateFunction {
public DataType dataType() {
return inner.dataType();
}
@Override
public String functionId() {
return outer.id().toString();
@ -59,7 +59,8 @@ public class InnerAggregate extends AggregateFunction {
@Override
public AggregateFunctionAttribute toAttribute() {
// this is highly correlated with QueryFolder$FoldAggregate#addFunction (regarding the function name within the querydsl)
return new AggregateFunctionAttribute(location(), name(), dataType(), outer.id(), functionId(), AggPath.metricValue(functionId(), innerId));
return new AggregateFunctionAttribute(location(), name(), dataType(), outer.id(), functionId(),
AggPath.metricValue(functionId(), innerId));
}
@Override
@ -75,4 +76,4 @@ public class InnerAggregate extends AggregateFunction {
public String name() {
return "(" + inner.functionName() + "#" + inner.id() + "/" + outer.toString() + ")";
}
}
}

View File

@ -74,7 +74,8 @@ public class Cast extends UnaryScalarFunction {
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(field()), new CastProcessor(DataTypeConversion.conversionFor(from(), to())));
return new UnaryProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(field()),
new CastProcessor(DataTypeConversion.conversionFor(from(), to())));
}
@Override
@ -91,4 +92,4 @@ public class Cast extends UnaryScalarFunction {
public String toString() {
return functionName() + "(" + field().toString() + " AS " + to().sqlName() + ")#" + id();
}
}
}

View File

@ -22,13 +22,14 @@ public class ScalarFunctionAttribute extends FunctionAttribute {
private final Expression orderBy;
private final ProcessorDefinition processorDef;
ScalarFunctionAttribute(Location location, String name, DataType dataType, ExpressionId id, String functionId, ScriptTemplate script,
Expression orderBy, ProcessorDefinition processorDef) {
ScalarFunctionAttribute(Location location, String name, DataType dataType, ExpressionId id,
String functionId, ScriptTemplate script, Expression orderBy, ProcessorDefinition processorDef) {
this(location, name, dataType, null, true, id, false, functionId, script, orderBy, processorDef);
}
ScalarFunctionAttribute(Location location, String name, DataType dataType, String qualifier, boolean nullable, ExpressionId id,
boolean synthetic, String functionId, ScriptTemplate script, Expression orderBy, ProcessorDefinition processorDef) {
ScalarFunctionAttribute(Location location, String name, DataType dataType, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic, String functionId, ScriptTemplate script,
Expression orderBy, ProcessorDefinition processorDef) {
super(location, name, dataType, qualifier, nullable, id, synthetic, functionId);
this.script = script;
this.orderBy = orderBy;
@ -49,14 +50,15 @@ public class ScalarFunctionAttribute extends FunctionAttribute {
@Override
protected Expression canonicalize() {
return new ScalarFunctionAttribute(location(), "<none>", dataType(), null, true, id(), false, functionId(), script, orderBy,
processorDef);
return new ScalarFunctionAttribute(location(), "<none>", dataType(), null, true, id(), false,
functionId(), script, orderBy, processorDef);
}
@Override
protected Attribute clone(Location location, String name, DataType dataType, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
return new ScalarFunctionAttribute(location, name, dataType, qualifier, nullable, id, synthetic, functionId(), script, orderBy,
processorDef);
protected Attribute clone(Location location, String name, DataType dataType, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic) {
return new ScalarFunctionAttribute(location, name, dataType, qualifier, nullable, id, synthetic,
functionId(), script, orderBy, processorDef);
}
@Override

View File

@ -73,7 +73,9 @@ public abstract class ArithmeticFunction extends BinaryScalarFunction {
}
protected final BinaryArithmeticProcessorDefinition makeProcessorDefinition() {
return new BinaryArithmeticProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(left()), ProcessorDefinitions.toProcessorDefinition(right()), operation);
return new BinaryArithmeticProcessorDefinition(this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()), operation);
}
@Override
@ -107,4 +109,4 @@ public abstract class ArithmeticFunction extends BinaryScalarFunction {
protected boolean useParanthesis() {
return !(left() instanceof Literal) || !(right() instanceof Literal);
}
}
}

View File

@ -16,7 +16,8 @@ public class BinaryArithmeticProcessorDefinition extends BinaryProcessorDefiniti
private final BinaryArithmeticOperation operation;
public BinaryArithmeticProcessorDefinition(Expression expression, ProcessorDefinition left, ProcessorDefinition right, BinaryArithmeticOperation operation) {
public BinaryArithmeticProcessorDefinition(Expression expression, ProcessorDefinition left,
ProcessorDefinition right, BinaryArithmeticOperation operation) {
super(expression, left, right);
this.operation = operation;
}

View File

@ -47,6 +47,7 @@ public class Neg extends UnaryScalarFunction {
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(field()), new UnaryArithmeticProcessor(UnaryArithmeticOperation.NEGATE));
return new UnaryProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(field()),
new UnaryArithmeticProcessor(UnaryArithmeticOperation.NEGATE));
}
}

View File

@ -53,9 +53,11 @@ public abstract class DateTimeFunction extends UnaryScalarFunction {
@Override
protected TypeResolution resolveType() {
return field().dataType().same(DataTypes.DATE) ?
TypeResolution.TYPE_RESOLVED :
new TypeResolution("Function '%s' cannot be applied on a non-date expression ('%s' of type '%s')", functionName(), Expressions.name(field()), field().dataType().esName());
if (field().dataType().same(DataTypes.DATE)) {
return TypeResolution.TYPE_RESOLVED;
}
return new TypeResolution("Function [" + functionName() + "] cannot be applied on a non-date expression (["
+ Expressions.name(field()) + "] of type [" + field().dataType().esName() + "])");
}
@Override
@ -69,14 +71,17 @@ public abstract class DateTimeFunction extends UnaryScalarFunction {
params.variable(field.name());
} else {
// TODO ewwww
/* This uses the Java 8 time API because Painless doesn't whitelist creation of new
* Joda classes. */
/*
* This uses the Java 8 time API because Painless doesn't whitelist creation of new
* Joda classes.
*
* The actual script is
* ZonedDateTime.ofInstant(Instant.ofEpochMilli(<insert doc field>.value.millis),
* ZoneId.of(<insert user tz>)).get(ChronoField.get(MONTH_OF_YEAR))
*/
// ideally JodaTime should be used since that's internally used and there are subtle differences between that and the JDK API
// all variables are externalized to reuse the script across invocations
// the actual script is ZonedDateTime.ofInstant(Instant.ofEpochMilli(<insert doc field>.value.millis), ZoneId.of(<insert user tz>)).get(ChronoField.get(MONTH_OF_YEAR))
template = formatTemplate("ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc[{}].value.millis), ZoneId.of({})).get(ChronoField.valueOf({}))");
template = formatTemplate("ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc[{}].value.millis), "
+ "ZoneId.of({})).get(ChronoField.valueOf({}))");
params.variable(field.name())
.variable(timeZone.getID())
.variable(chronoField().name());
@ -102,7 +107,8 @@ public abstract class DateTimeFunction extends UnaryScalarFunction {
@Override
protected final ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(field()), new DateTimeProcessor(extractor(), timeZone));
return new UnaryProcessorDefinition(this, ProcessorDefinitions.toProcessorDefinition(field()),
new DateTimeProcessor(extractor(), timeZone));
}
protected abstract DateTimeExtractor extractor();