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

SQL: Provide null-safe scripts for Not and Neg #34877

Merged
merged 2 commits into from
Oct 26, 2018
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 @@ -6,9 +6,7 @@
package org.elasticsearch.xpack.sql.expression.function.scalar;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
Expand Down Expand Up @@ -71,9 +69,8 @@ protected TypeResolution resolveType() {
}

@Override
protected Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()),
new CastProcessor(DataTypeConversion.conversionFor(from(), to())));
protected Processor makeProcessor() {
return new CastProcessor(DataTypeConversion.conversionFor(from(), to()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
package org.elasticsearch.xpack.sql.expression.function.scalar;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;

Expand Down Expand Up @@ -34,12 +38,20 @@ public final UnaryScalarFunction replaceChildren(List<Expression> newChildren) {
}
return replaceChild(newChildren.get(0));
}

protected abstract UnaryScalarFunction replaceChild(Expression newChild);

public Expression field() {
return field;
}

@Override
public final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), makeProcessor());
}

protected abstract Processor makeProcessor();

@Override
public boolean foldable() {
return field.foldable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeProcessor.DateTimeExtractor;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
Expand Down Expand Up @@ -63,13 +61,13 @@ public ScriptTemplate scriptWithField(FieldAttribute field) {
*/
protected abstract ChronoField chronoField();

protected abstract DateTimeExtractor extractor();

@Override
protected Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new DateTimeProcessor(extractor(), timeZone()));
protected Processor makeProcessor() {
return new DateTimeProcessor(extractor(), timeZone());
}

protected abstract DateTimeExtractor extractor();

@Override
public DataType dataType() {
return DataType.INTEGER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.NamedDateTimeProcessor.NameExtractor;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
Expand Down Expand Up @@ -51,8 +49,8 @@ public ScriptTemplate scriptWithField(FieldAttribute field) {
}

@Override
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new NamedDateTimeProcessor(nameExtractor, timeZone()));
protected Processor makeProcessor() {
return new NamedDateTimeProcessor(nameExtractor, timeZone());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
Expand Down Expand Up @@ -53,8 +51,8 @@ protected Quarter replaceChild(Expression newChild) {
}

@Override
protected Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new QuarterProcessor(timeZone()));
protected Processor makeProcessor() {
return new QuarterProcessor(timeZone());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.math;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor.MathOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;

Expand Down Expand Up @@ -64,8 +62,8 @@ protected TypeResolution resolveType() {
}

@Override
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new MathProcessor(operation()));
protected Processor makeProcessor() {
return new MathProcessor(operation());
}

protected abstract MathOperation operation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.util.StringUtils;
Expand Down Expand Up @@ -49,8 +47,8 @@ protected TypeResolution resolveType() {
}

@Override
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new StringProcessor(operation()));
protected Processor makeProcessor() {
return new StringProcessor(operation());
}

protected abstract StringOperation operation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;

Expand Down Expand Up @@ -51,8 +49,8 @@ protected TypeResolution resolveType() {
}

@Override
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new StringProcessor(operation()));
protected Processor makeProcessor() {
return new StringProcessor(operation());
}

protected abstract StringOperation operation();
Expand All @@ -72,6 +70,11 @@ public String processScript(String template) {
template));
}

@Override
public int hashCode() {
return Objects.hash(field());
}

@Override
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != getClass()) {
Expand All @@ -80,9 +83,4 @@ public boolean equals(Object obj) {
UnaryStringIntFunction other = (UnaryStringIntFunction) obj;
return Objects.equals(other.field(), field());
}

@Override
public int hashCode() {
return Objects.hash(field());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import org.elasticsearch.xpack.sql.expression.function.scalar.string.ReplaceFunctionProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.SubstringFunctionProcessor;
import org.elasticsearch.xpack.sql.expression.predicate.IsNotNullProcessor;
import org.elasticsearch.xpack.sql.expression.predicate.logical.BinaryLogicProcessor.BinaryLogicOperation;
import org.elasticsearch.xpack.sql.expression.predicate.logical.NotProcessor;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.BinaryArithmeticProcessor.BinaryArithmeticOperation;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.UnaryArithmeticProcessor.UnaryArithmeticOperation;
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.BinaryComparisonProcessor.BinaryComparisonOperation;
import org.elasticsearch.xpack.sql.expression.predicate.regex.RegexProcessor.RegexOperation;
import org.elasticsearch.xpack.sql.util.StringUtils;
Expand Down Expand Up @@ -102,6 +105,14 @@ public static Boolean or(Boolean left, Boolean right) {
return BinaryLogicOperation.OR.apply(left, right);
}

public static Boolean not(Boolean expression) {
return NotProcessor.apply(expression);
}

public static Boolean notNull(Object expression) {
return IsNotNullProcessor.apply(expression);
}

//
// Regex
//
Expand All @@ -116,20 +127,24 @@ public static Number add(Number left, Number right) {
return BinaryArithmeticOperation.ADD.apply(left, right);
}

public static Number sub(Number left, Number right) {
return BinaryArithmeticOperation.SUB.apply(left, right);
public static Number div(Number left, Number right) {
return BinaryArithmeticOperation.DIV.apply(left, right);
}

public static Number mod(Number left, Number right) {
return BinaryArithmeticOperation.MOD.apply(left, right);
}

public static Number mul(Number left, Number right) {
return BinaryArithmeticOperation.MUL.apply(left, right);
}

public static Number div(Number left, Number right) {
return BinaryArithmeticOperation.DIV.apply(left, right);
public static Number neg(Number value) {
return UnaryArithmeticOperation.NEGATE.apply(value);
}

public static Number mod(Number left, Number right) {
return BinaryArithmeticOperation.MOD.apply(left, right);
public static Number sub(Number left, Number right) {
return BinaryArithmeticOperation.SUB.apply(left, right);
}

public static Number round(Number v, Number s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@

public final class Scripts {

public static final String DOC_VALUE = "doc[{}].value";
public static final String SQL_SCRIPTS = "{sql}";
public static final String PARAM = "{}";

private Scripts() {}

private static final Map<Pattern, String> FORMATTING_PATTERNS = Collections.unmodifiableMap(Stream.of(
new SimpleEntry<>("doc[{}].value", "{sql}.docValue(doc,{})"),
new SimpleEntry<>("{sql}", InternalSqlScriptUtils.class.getSimpleName()),
new SimpleEntry<>("{}", "params.%s"))
new SimpleEntry<>(DOC_VALUE, SQL_SCRIPTS + ".docValue(doc,{})"),
new SimpleEntry<>(SQL_SCRIPTS, InternalSqlScriptUtils.class.getSimpleName()),
new SimpleEntry<>(PARAM, "params.%s"))
.collect(toMap(e -> Pattern.compile(e.getKey(), Pattern.LITERAL), Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new)));

/**
Expand Down Expand Up @@ -83,4 +87,4 @@ public static ScriptTemplate binaryMethod(String methodName, ScriptTemplate left
.build(),
dataType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@
*/
package org.elasticsearch.xpack.sql.expression.predicate;

import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.UnaryExpression;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.gen.script.Scripts;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.DataTypes;

public class IsNotNull extends UnaryExpression {
public class IsNotNull extends UnaryScalarFunction {

public IsNotNull(Location location, Expression child) {
super(location, child);
public IsNotNull(Location location, Expression field) {
super(location, field);
}

@Override
protected NodeInfo<IsNotNull> info() {
return NodeInfo.create(this, IsNotNull::new, child());
return NodeInfo.create(this, IsNotNull::new, field());
}

@Override
Expand All @@ -33,17 +32,17 @@ protected IsNotNull replaceChild(Expression newChild) {

@Override
public Object fold() {
return child().fold() != null && !DataTypes.isNull(child().dataType());
return field().fold() != null && !DataTypes.isNull(field().dataType());
}

@Override
protected Pipe makePipe() {
throw new SqlIllegalArgumentException("Not supported yet");
protected Processor makeProcessor() {
return IsNotNullProcessor.INSTANCE;
}

@Override
public ScriptTemplate asScript() {
throw new SqlIllegalArgumentException("Not supported yet");
public String processScript(String script) {
return Scripts.formatTemplate(Scripts.SQL_SCRIPTS + ".notNull(" + script + ")");
}

@Override
Expand All @@ -55,9 +54,4 @@ public boolean nullable() {
public DataType dataType() {
return DataType.BOOLEAN;
}

@Override
public String toString() {
return child().toString() + " IS NOT NULL";
}
}
}
Loading