Skip to content
Draft
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
2 changes: 1 addition & 1 deletion server/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
exports org.elasticsearch.inference.configuration;
exports org.elasticsearch.inference.validation;
exports org.elasticsearch.monitor.metrics;
exports org.elasticsearch.plugins.internal.rewriter to org.elasticsearch.inference;
exports org.elasticsearch.plugins.internal.rewriter;
exports org.elasticsearch.lucene.util.automaton;
exports org.elasticsearch.index.codec.perfield;
exports org.elasticsearch.index.codec.vectors to org.elasticsearch.test.knn, org.elasticsearch.gpu;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
###############################################
# Tests for Chunk function
#

chunkExample
required_capability: chunk_function

// tag::chunk-with-field[]
FROM books
| EVAL chunks = CHUNK(description, 1, 20)
// end::chunk-with-field[]
| KEEP book_no, title, chunks
| SORT book_no
| LIMIT 5
;
warningRegex:java.lang.IllegalArgumentException: single-value function encountered multi-value

// tag::chunk-with-field-result[]
book_no:keyword | title:text | chunks:keyword
1211 | The brothers Karamazov | null
1463 | Realms of Tolkien: Images of Middle-earth | null
1502 | Selected Passages from Correspondence with Friends | null
1937 | The Best Short Stories of Dostoevsky (Modern Library) | null
1985 | Brothers Karamazov | null
// end::chunk-with-field-result[]
;


chunkTextWithMatch
required_capability: chunk_function

FROM books
| WHERE MATCH(title, "Return")
| EVAL chunks = CHUNK(description, 1, 20)
| KEEP book_no, title, chunks;
ignoreOrder:true
warningRegex:java.lang.IllegalArgumentException: single-value function encountered multi-value

book_no:keyword | title:text | chunks:keyword
2714 | Return of the King Being the Third Part of The Lord of the Rings | null
7350 | Return of the Shadow | null
;



Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,12 @@ public enum Cap {
/**
* Support for the literal {@code m} suffix as an alias for {@code minute} in temporal amounts.
*/
TEMPORAL_AMOUNT_M;
TEMPORAL_AMOUNT_M,

/**
* Chunk function.
*/
CHUNK_FUNCTION(Build.current().isSnapshot());

private final boolean enabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.StYMax;
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.StYMin;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.ByteLength;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Chunk;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.LTrim;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Length;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.RTrim;
Expand Down Expand Up @@ -239,6 +240,7 @@ public static List<NamedWriteableRegistry.Entry> unaryScalars() {
entries.add(UrlEncode.ENTRY);
entries.add(UrlEncodeComponent.ENTRY);
entries.add(UrlDecode.ENTRY);
entries.add(Chunk.ENTRY);
// mv functions
entries.addAll(MvFunctionWritables.getNamedWriteables());
return entries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.StYMin;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.BitLength;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.ByteLength;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Chunk;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Concat;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Contains;
import org.elasticsearch.xpack.esql.expression.function.scalar.string.EndsWith;
Expand Down Expand Up @@ -553,7 +554,8 @@ private static FunctionDefinition[][] snapshotFunctions() {
def(L1Norm.class, L1Norm::new, "v_l1_norm"),
def(L2Norm.class, L2Norm::new, "v_l2_norm"),
def(Magnitude.class, Magnitude::new, "v_magnitude"),
def(Hamming.class, Hamming::new, "v_hamming") } };
def(Hamming.class, Hamming::new, "v_hamming"),
def(Chunk.class, tri(Chunk::new), "chunk") } };
}

public EsqlFunctionRegistry snapshotRegistry() {
Expand Down Expand Up @@ -1036,13 +1038,22 @@ public interface BinaryBuilder<T> {
@SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do
protected static <T extends Function> FunctionDefinition def(Class<T> function, TernaryBuilder<T> ctorRef, String... names) {
FunctionBuilder builder = (source, children, cfg) -> {
boolean hasMinimumOne = TwoOptionalArguments.class.isAssignableFrom(function);
boolean hasMinimumTwo = OptionalArgument.class.isAssignableFrom(function);
if (hasMinimumTwo && (children.size() > 3 || children.size() < 2)) {
if (hasMinimumOne && (children.size() > 3 || children.isEmpty())) {
throw new QlIllegalArgumentException("expects minimum one, maximum three arguments");
} else if (hasMinimumTwo && (children.size() > 3 || children.size() < 2)) {
throw new QlIllegalArgumentException("expects two or three arguments");
} else if (hasMinimumTwo == false && children.size() != 3) {
} else if (hasMinimumOne == false && hasMinimumTwo == false && children.size() != 3) {
throw new QlIllegalArgumentException("expects exactly three arguments");
}
return ctorRef.build(source, children.get(0), children.get(1), children.size() == 3 ? children.get(2) : null);

return ctorRef.build(
source,
children.get(0),
children.size() > 1 ? children.get(1) : null,
children.size() == 3 ? children.get(2) : null
);
};
return def(function, builder, names);
}
Expand Down
Loading