-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3a7e81b
Added support for string manipulating functions with more than one pa…
astefan d23edee
PR review changes: another test for the insert function, additional s…
astefan d212a0c
Incorporated PR review comments.
astefan 7b6e8c5
Merge remote-tracking branch 'remotes/origin/master' into 31604_2nd_part
astefan 8b3bca0
Clarified the role of the BiFunction inside the BinaryStringFunction …
astefan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Javadocs would be helpful here.