Skip to content
Closed
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
5 changes: 5 additions & 0 deletions docs/changelog/117873.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 117873
summary: "Optional named arguments for function in map - option #2"
area: ES|QL
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static List<NamedWriteableRegistry.Entry> expressions() {
entries.add(new NamedWriteableRegistry.Entry(Expression.class, e.name, in -> (Expression) e.reader.read(in)));
}
entries.add(Literal.ENTRY);
entries.add(NamedLiterals.ENTRY);
return entries;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.core.expression;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.core.util.PlanStreamInput;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.xpack.esql.core.type.DataType.UNSUPPORTED;

/**
* Represent a collect of key-value pairs as function arguments.
*/
public class NamedLiterals extends Literal {
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
Expression.class,
"NamedLiteras",
NamedLiterals::readFrom
);

private final Map<String, String> args;

public NamedLiterals(Source source, Map<String, String> args) {
super(source, args, UNSUPPORTED);
this.args = args;
}

private static NamedLiterals readFrom(StreamInput in) throws IOException {
Source source = Source.readFrom((StreamInput & PlanStreamInput) in);
Map<String, String> args = in.readMap(StreamInput::readString, StreamInput::readString);
return new NamedLiterals(source, args);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
Source.EMPTY.writeTo(out);
out.writeMap(args, StreamOutput::writeString, StreamOutput::writeString);
}

@Override
public String getWriteableName() {
return ENTRY.name;
}

@Override
protected NodeInfo<? extends NamedLiterals> info() {
return NodeInfo.create(this, NamedLiterals::new, args);
}

public Map<String, String> args() {
return args;
}

@Override
public DataType dataType() {
return UNSUPPORTED;
}

@Override
public Object fold() {
return args;
}

@Override
public int hashCode() {
return Objects.hash(args);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

NamedLiterals other = (NamedLiterals) obj;
return Objects.equals(args, other.args);
}

@Override
public String toString() {
return String.valueOf(args);
}

@Override
public String nodeString() {
return toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Set<String> getSupportedAnnotationTypes() {
"org.elasticsearch.injection.guice.Inject",
"org.elasticsearch.xpack.esql.expression.function.FunctionInfo",
"org.elasticsearch.xpack.esql.expression.function.Param",
"org.elasticsearch.xpack.esql.expression.function.MapParam",
"org.elasticsearch.rest.ServerlessScope",
"org.elasticsearch.xcontent.ParserConstructor",
"org.elasticsearch.core.UpdateForV9",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Tests to validate maps as inputs to functions, these functions are under snapshot only

mapCount
required_capability: optional_named_argument_map_for_function
ROW x = 1
| EVAL c = map_count({"option1":"value1", "option2":2, "option3":3.0, "option4":true})
;

x:integer |c:long
1 |4
;

mapCountIndex
required_capability: optional_named_argument_map_for_function
FROM employees
| EVAL c = map_count({"option1":"value1", "option2":2, "option3":3.0, "option4":true})
| KEEP emp_no, c
| SORT emp_no
| LIMIT 1
;

emp_no:integer |c:long
10001 |4
;

mapKeys
required_capability: optional_named_argument_map_for_function
ROW x = 1
| EVAL k = map_keys({"option1":"value1", "option2":2, "option3":3.0, "option4":true})
;

x:integer |k:keyword
1 |"option1, option2, option3, option4"
;

mapKeysIndex
required_capability: optional_named_argument_map_for_function
FROM employees
| EVAL k = map_keys({"option1":"value1", "option2":2, "option3":3.0, "option4":true})
| KEEP emp_no, k
| SORT emp_no
| LIMIT 1
;

emp_no:integer |k:keyword
10001 |"option1, option2, option3, option4"
;

mapValues
required_capability: optional_named_argument_map_for_function
ROW x = 1
| EVAL v = map_values({"option1":"value1", "option2":2, "option3":3.0, "option4":true})
;

x:integer |v:keyword
1 |"value1, 2, 3.0, true"
;

mapKeysIndex
required_capability: optional_named_argument_map_for_function
FROM employees
| EVAL v = map_values({"option1":"value1", "option2":2, "option3":3.0, "option4":true})
| KEEP emp_no, v
| SORT emp_no
| LIMIT 1
;

emp_no:integer |v:keyword
10001 |"value1, 2, 3.0, true"
;
3 changes: 3 additions & 0 deletions x-pack/plugin/esql/src/main/antlr/EsqlBaseLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ ASTERISK : '*';
SLASH : '/';
PERCENT : '%';

LEFT_BRACES : {this.isDevVersion()}? '{';
RIGHT_BRACES : {this.isDevVersion()}? '}';

NESTED_WHERE : WHERE -> type(WHERE);

NAMED_OR_POSITIONAL_PARAM
Expand Down
136 changes: 69 additions & 67 deletions x-pack/plugin/esql/src/main/antlr/EsqlBaseLexer.tokens

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

15 changes: 14 additions & 1 deletion x-pack/plugin/esql/src/main/antlr/EsqlBaseParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,26 @@ primaryExpression
;

functionExpression
: functionName LP (ASTERISK | (booleanExpression (COMMA booleanExpression)*))? RP
: functionName LP (ASTERISK | (functionArgument (COMMA functionArgument)*))? RP
;

functionName
: identifierOrParameter
;

functionArgument
: booleanExpression #functionArgumentDefault
| {this.isDevVersion()}? LEFT_BRACES namedConstants RIGHT_BRACES #functionArgumentWithName
;

namedConstants
: namedConstant (COMMA namedConstant)*
;

namedConstant
: key=string COLON value=constant
;

dataType
: identifier #toDataType
;
Expand Down
Loading