|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.sql.expression.predicate.conditional; |
| 8 | + |
| 9 | +import org.elasticsearch.xpack.sql.expression.Expression; |
| 10 | +import org.elasticsearch.xpack.sql.expression.Expressions; |
| 11 | +import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe; |
| 12 | +import org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder; |
| 13 | +import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate; |
| 14 | +import org.elasticsearch.xpack.sql.tree.Location; |
| 15 | +import org.elasticsearch.xpack.sql.tree.NodeInfo; |
| 16 | +import org.elasticsearch.xpack.sql.type.DataType; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder; |
| 22 | + |
| 23 | +/** |
| 24 | + * Accepts 2 arguments of any data type and returns null if they are equal, |
| 25 | + * and the 1st argument otherwise. |
| 26 | + */ |
| 27 | +public class NullIf extends ConditionalFunction { |
| 28 | + |
| 29 | + private DataType dataType; |
| 30 | + |
| 31 | + public NullIf(Location location, Expression left, Expression right) { |
| 32 | + super(location, Arrays.asList(left, right)); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected NodeInfo<? extends NullIf> info() { |
| 37 | + return NodeInfo.create(this, NullIf::new, children().get(0), children().get(1)); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Expression replaceChildren(List<Expression> newChildren) { |
| 42 | + return new NullIf(location(), newChildren.get(0), newChildren.get(1)); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected TypeResolution resolveType() { |
| 47 | + dataType = children().get(0).dataType(); |
| 48 | + return TypeResolution.TYPE_RESOLVED; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public DataType dataType() { |
| 53 | + return dataType; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean foldable() { |
| 58 | + return Expressions.foldable(children()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean nullable() { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object fold() { |
| 68 | + return NullIfProcessor.apply(children().get(0).fold(), children().get(1).fold()); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public ScriptTemplate asScript() { |
| 73 | + ScriptTemplate left = asScript(children().get(0)); |
| 74 | + ScriptTemplate right = asScript(children().get(1)); |
| 75 | + String template = "{sql}.nullif(" + left.template() + "," + right.template() + ")"; |
| 76 | + ParamsBuilder params = paramsBuilder(); |
| 77 | + params.script(left.params()); |
| 78 | + params.script(right.params()); |
| 79 | + |
| 80 | + return new ScriptTemplate(template, params.build(), dataType); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected Pipe makePipe() { |
| 85 | + return new NullIfPipe(location(), this, |
| 86 | + Expressions.pipe(children().get(0)), Expressions.pipe(children().get(1))); |
| 87 | + } |
| 88 | +} |
0 commit comments