Skip to content

Commit a0d8a0f

Browse files
map as function argument
1 parent 56368e6 commit a0d8a0f

File tree

30 files changed

+3066
-2065
lines changed

30 files changed

+3066
-2065
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Expression.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
3535
entries.add(new NamedWriteableRegistry.Entry(Expression.class, e.name, in -> (NamedExpression) e.reader.read(in)));
3636
}
3737
entries.add(Literal.ENTRY);
38+
entries.add(NamedLiterals.ENTRY);
3839
return entries;
3940
}
4041

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
package org.elasticsearch.xpack.esql.core.expression;
8+
9+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10+
import org.elasticsearch.common.io.stream.StreamInput;
11+
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
13+
import org.elasticsearch.xpack.esql.core.tree.Source;
14+
import org.elasticsearch.xpack.esql.core.type.DataType;
15+
import org.elasticsearch.xpack.esql.core.util.PlanStreamInput;
16+
17+
import java.io.IOException;
18+
import java.util.Map;
19+
import java.util.Objects;
20+
21+
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSUPPORTED;
22+
23+
/**
24+
* Represent a collect of key-value pairs as function arguments.
25+
*/
26+
public class NamedLiterals extends Literal {
27+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
28+
Expression.class,
29+
"NamedLiteras",
30+
NamedLiterals::readFrom
31+
);
32+
33+
private final Map<String, String> args;
34+
35+
public NamedLiterals(Source source, Map<String, String> args) {
36+
super(source, args, UNSUPPORTED);
37+
this.args = args;
38+
}
39+
40+
private static NamedLiterals readFrom(StreamInput in) throws IOException {
41+
Source source = Source.readFrom((StreamInput & PlanStreamInput) in);
42+
Map<String, String> args = in.readMap(StreamInput::readString, StreamInput::readString);
43+
return new NamedLiterals(source, args);
44+
}
45+
46+
@Override
47+
public void writeTo(StreamOutput out) throws IOException {
48+
Source.EMPTY.writeTo(out);
49+
out.writeMap(args, StreamOutput::writeString, StreamOutput::writeString);
50+
}
51+
52+
@Override
53+
public String getWriteableName() {
54+
return ENTRY.name;
55+
}
56+
57+
@Override
58+
protected NodeInfo<? extends NamedLiterals> info() {
59+
return NodeInfo.create(this, NamedLiterals::new, args);
60+
}
61+
62+
public Map<String, String> args() {
63+
return args;
64+
}
65+
66+
@Override
67+
public DataType dataType() {
68+
return UNSUPPORTED;
69+
}
70+
71+
@Override
72+
public Object fold() {
73+
return args;
74+
}
75+
76+
@Override
77+
public int hashCode() {
78+
return Objects.hash(args);
79+
}
80+
81+
@Override
82+
public boolean equals(Object obj) {
83+
if (this == obj) {
84+
return true;
85+
}
86+
if (obj == null || getClass() != obj.getClass()) {
87+
return false;
88+
}
89+
90+
NamedLiterals other = (NamedLiterals) obj;
91+
return Objects.equals(args, other.args);
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return String.valueOf(args);
97+
}
98+
99+
@Override
100+
public String nodeString() {
101+
return toString();
102+
}
103+
104+
}

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/querydsl/query/MatchQuery.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ public MatchQuery(Source source, String name, Object text, Map<String, String> o
6060
this.name = name;
6161
this.text = text;
6262
this.options = options;
63-
this.boost = null;
64-
this.fuzziness = null;
63+
String boost = options.get("boost");
64+
String fuzziness = options.get("fuzziness");
65+
this.boost = boost == null ? null : Double.valueOf(options.get("boost"));
66+
this.fuzziness = fuzziness == null ? null : Fuzziness.fromString(options.get("fuzziness"));
6567
}
6668

6769
public MatchQuery(Source source, String name, Object text, Double boost, Fuzziness fuzziness) {

x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/ConsumeProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Set<String> getSupportedAnnotationTypes() {
3939
"org.elasticsearch.injection.guice.Inject",
4040
"org.elasticsearch.xpack.esql.expression.function.FunctionInfo",
4141
"org.elasticsearch.xpack.esql.expression.function.Param",
42+
"org.elasticsearch.xpack.esql.expression.function.MapParam",
4243
"org.elasticsearch.rest.ServerlessScope",
4344
"org.elasticsearch.xcontent.ParserConstructor",
4445
"org.elasticsearch.core.UpdateForV9",

x-pack/plugin/esql/qa/testFixtures/src/main/resources/match-function.csv-spec

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,31 @@ emp_no:integer | first_name:keyword | last_name:keyword
197197
10041 | Uri | Lenart
198198
10043 | Yishay | Tzvieli
199199
;
200+
201+
mapCount
202+
required_capability: optional_named_argument_map_for_function
203+
row x = 1
204+
| eval y = map_count({"option1":"value2", "option2":2, "option3":3.0, "option4":true})
205+
;
206+
207+
x:integer |y:long
208+
1 |4
209+
;
210+
211+
matchWithOptions
212+
required_capability: match_function
213+
required_capability: optional_named_argument_map_for_function
214+
215+
FROM books
216+
| WHERE MATCH(author, "Faulkner", {"fuzziness":"auto", "boost":2, "analyzer":"whitespace"})
217+
| KEEP book_no, author
218+
| SORT book_no
219+
| LIMIT 5;
220+
221+
book_no:keyword | author:text
222+
2378 | [Carol Faulkner, Holly Byers Ochoa, Lucretia Mott]
223+
2713 | William Faulkner
224+
2847 | Colleen Faulkner
225+
2883 | William Faulkner
226+
3293 | Danny Faulkner
227+
;

x-pack/plugin/esql/src/main/antlr/EsqlBaseLexer.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ MINUS : '-';
209209
ASTERISK : '*';
210210
SLASH : '/';
211211
PERCENT : '%';
212+
213+
LEFT_BRACES : {this.isDevVersion()}? '{';
214+
RIGHT_BRACES : {this.isDevVersion()}? '}';
215+
212216
EXPRESSION_COLON : {this.isDevVersion()}? COLON -> type(COLON);
213217

214218
NESTED_WHERE : WHERE -> type(WHERE);

x-pack/plugin/esql/src/main/antlr/EsqlBaseLexer.tokens

Lines changed: 64 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/antlr/EsqlBaseParser.g4

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,26 @@ primaryExpression
101101
;
102102

103103
functionExpression
104-
: functionName LP (ASTERISK | (booleanExpression (COMMA booleanExpression)*))? RP
104+
: functionName LP (ASTERISK | (functionArgument (COMMA functionArgument)*))? RP
105105
;
106106

107107
functionName
108108
: identifierOrParameter
109109
;
110110

111+
functionArgument
112+
: booleanExpression #functionArgumentDefault
113+
| {this.isDevVersion()}? LEFT_BRACES namedConstants RIGHT_BRACES #functionArgumentWithName
114+
;
115+
116+
namedConstants
117+
: namedConstant (COMMA namedConstant)*
118+
;
119+
120+
namedConstant
121+
: key=string COLON value=constant
122+
;
123+
111124
dataType
112125
: identifier #toDataType
113126
;

0 commit comments

Comments
 (0)