-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL: Added support for string manipulating functions with more than o…
…ne parameter (#32356) Added support for string manipulating functions with more than one parameter: CONCAT, LEFT, RIGHT, REPEAT, POSITION, LOCATE, REPLACE, SUBSTRING, INSERT
- Loading branch information
Showing
55 changed files
with
4,649 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...a/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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); | ||
} | ||
|
||
/* | ||
* the operation the binary function handles can receive one String argument, a number or String as second argument | ||
* and it can return a number or a String. The BiFunction below is the base operation for the subsequent implementations. | ||
* T is the second argument, R is the result of applying the operation. | ||
*/ | ||
protected abstract BiFunction<String, T, R> operation(); | ||
|
||
@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()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...lasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...asticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Processor class covering string manipulating functions that have the first parameter as string, | ||
* second parameter as numeric and a string result. | ||
*/ | ||
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; | ||
} | ||
|
||
} |
Oops, something went wrong.