-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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: Added support for string manipulating functions with more than one parameter #32356
Changes from 2 commits
3a7e81b
d23edee
d212a0c
7b6e8c5
8b3bca0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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.string; | ||
|
||
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate; | ||
import org.elasticsearch.xpack.sql.tree.Location; | ||
import org.elasticsearch.xpack.sql.type.DataType; | ||
import org.elasticsearch.xpack.sql.util.StringUtils; | ||
|
||
import java.util.Locale; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
|
||
import static java.lang.String.format; | ||
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder; | ||
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate; | ||
|
||
/** | ||
* Base class for binary functions that have the first parameter a string, the second parameter a number | ||
* or a string and the result can be a string or a number. | ||
*/ | ||
public abstract class BinaryStringFunction<T,R> extends BinaryScalarFunction { | ||
|
||
protected BinaryStringFunction(Location location, Expression left, Expression right) { | ||
super(location, left, right); | ||
} | ||
|
||
protected abstract BiFunction<String, T, R> operation(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see why this is a function ref - so that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some javadoc here explaining this. |
||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (!childrenResolved()) { | ||
return new TypeResolution("Unresolved children"); | ||
} | ||
|
||
if (!left().dataType().isString()) { | ||
return new TypeResolution("'%s' requires first parameter to be a string type, received %s", functionName(), left().dataType()); | ||
} | ||
|
||
return resolveSecondParameterInputType(right().dataType()); | ||
} | ||
|
||
protected abstract TypeResolution resolveSecondParameterInputType(DataType inputType); | ||
|
||
@Override | ||
public Object fold() { | ||
@SuppressWarnings("unchecked") | ||
T fold = (T) right().fold(); | ||
return operation().apply((String) left().fold(), fold); | ||
} | ||
|
||
@Override | ||
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) { | ||
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2) | ||
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"), | ||
StringUtils.underscoreToLowerCamelCase(operation().toString()), | ||
leftScript.template(), | ||
rightScript.template()), | ||
paramsBuilder() | ||
.script(leftScript.params()).script(rightScript.params()) | ||
.build(), dataType()); | ||
} | ||
|
||
@Override | ||
protected ScriptTemplate asScriptFrom(FieldAttribute field) { | ||
return new ScriptTemplate(formatScript("doc[{}].value"), | ||
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(), | ||
dataType()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(left(), right()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null || obj.getClass() != getClass()) { | ||
return false; | ||
} | ||
BinaryStringFunction<?,?> other = (BinaryStringFunction<?,?>) obj; | ||
return Objects.equals(other.left(), left()) | ||
&& Objects.equals(other.right(), right()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.string; | ||
|
||
import org.elasticsearch.xpack.sql.expression.Expression; | ||
import org.elasticsearch.xpack.sql.tree.Location; | ||
import org.elasticsearch.xpack.sql.type.DataType; | ||
|
||
/** | ||
* A binary string function with a numeric second parameter and a string result | ||
*/ | ||
public abstract class BinaryStringNumericFunction extends BinaryStringFunction<Number, String> { | ||
|
||
public BinaryStringNumericFunction(Location location, Expression left, Expression right) { | ||
super(location, left, right); | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveSecondParameterInputType(DataType inputType) { | ||
return inputType.isNumeric() ? | ||
TypeResolution.TYPE_RESOLVED : | ||
new TypeResolution("'%s' requires second parameter to be a numeric type, received %s", functionName(), inputType); | ||
} | ||
|
||
@Override | ||
public DataType dataType() { | ||
return DataType.KEYWORD; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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.string; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation; | ||
|
||
import java.io.IOException; | ||
import java.util.function.BiFunction; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Javadocs would be helpful here. |
||
public class BinaryStringNumericProcessor extends BinaryStringProcessor<BinaryStringNumericOperation, Number, String> { | ||
|
||
public static final String NAME = "sn"; | ||
|
||
public BinaryStringNumericProcessor(StreamInput in) throws IOException { | ||
super(in, i -> i.readEnum(BinaryStringNumericOperation.class)); | ||
} | ||
|
||
public BinaryStringNumericProcessor(Processor left, Processor right, BinaryStringNumericOperation operation) { | ||
super(left, right, operation); | ||
} | ||
|
||
public enum BinaryStringNumericOperation implements BiFunction<String, Number, String> { | ||
LEFT((s,c) -> { | ||
int i = c.intValue(); | ||
if (i < 0) return ""; | ||
return i > s.length() ? s : s.substring(0, i); | ||
}), | ||
RIGHT((s,c) -> { | ||
int i = c.intValue(); | ||
if (i < 0) return ""; | ||
return i > s.length() ? s : s.substring(s.length() - i); | ||
}), | ||
REPEAT((s,c) -> { | ||
int i = c.intValue(); | ||
if (i <= 0) return null; | ||
|
||
StringBuilder sb = new StringBuilder(s.length() * i); | ||
for (int j = 0; j < i; j++) { | ||
sb.append(s); | ||
} | ||
return sb.toString(); | ||
}); | ||
|
||
BinaryStringNumericOperation(BiFunction<String, Number, String> op) { | ||
this.op = op; | ||
} | ||
|
||
private final BiFunction<String, Number, String> op; | ||
|
||
@Override | ||
public String apply(String stringExp, Number count) { | ||
return op.apply(stringExp, count); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doWrite(StreamOutput out) throws IOException { | ||
out.writeEnum(operation()); | ||
} | ||
|
||
@Override | ||
protected Object doProcess(Object left, Object right) { | ||
if (left == null || right == null) { | ||
return null; | ||
} | ||
if (!(left instanceof String || left instanceof Character)) { | ||
throw new SqlIllegalArgumentException("A string/char is required; received [{}]", left); | ||
} | ||
if (!(right instanceof Number)) { | ||
throw new SqlIllegalArgumentException("A number is required; received [{}]", right); | ||
} | ||
|
||
return operation().apply(left instanceof Character ? left.toString() : (String) left, (Number) right); | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The javadoc says the result can be a string or a number but the
dataType
function below is always aKEYWORD
which makes me think it is always a string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
dataType
should be implemented in the concrete classes where the specific type should be specified.