Skip to content

Commit

Permalink
Merge branch 'dev-simple_query_string-#192-sql-simple' of https://git…
Browse files Browse the repository at this point in the history
…hub.com/Bit-Quill/opensearch-project-sql into dev-simple_query_string-#192-sql-simple
  • Loading branch information
MaxKsyunz committed May 13, 2022
2 parents 70a7457 + 6c1d9bb commit 3a2ff8a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
27 changes: 15 additions & 12 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,20 @@ specificFunction
;

relevanceFunction
: relevanceFunctionName LR_BRACKET
: relevanceFunctionType1 | relevanceFunctionType2
;

// Field is a single column
relevanceFunctionType1
: relevanceFunctionType1Name LR_BRACKET
field=relevanceField COMMA query=relevanceQuery
(COMMA relevanceArg)* RR_BRACKET
| relevanceFunctionNameEx LR_BRACKET
fields=relevanceFields COMMA query=relevanceQuery
(COMMA relevanceArg)* RR_BRACKET
(COMMA relevanceArg)* RR_BRACKET;

// Field is a list of columns
relevanceFunctionType2
: relevanceFunctionType2Name LR_BRACKET
LT_SQR_PRTHS field=relevanceField (COMMA field=relevanceField)* RT_SQR_PRTHS
COMMA query=relevanceQuery (COMMA relevanceArg)* RR_BRACKET
;

convertedDataType
Expand Down Expand Up @@ -385,11 +393,11 @@ flowControlFunctionName
: IF | IFNULL | NULLIF | ISNULL
;

relevanceFunctionName
relevanceFunctionType1Name
: MATCH
;

relevanceFunctionNameEx
relevanceFunctionType2Name
: SIMPLE_QUERY_STRING
;

Expand Down Expand Up @@ -418,11 +426,6 @@ relevanceArgName
relevanceField
: relevanceArgValue;

relevanceFields
// simple_query_string function accepts list of field as well in format '["field1", "field2", ... ]'
: (LT_SQR_PRTHS relevanceArgValue (COMMA relevanceArgValue)* RT_SQR_PRTHS)
;

relevanceQuery
: relevanceArgValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.opensearch.sql.ast.expression.AggregateFunction;
import org.opensearch.sql.ast.expression.AllFields;
import org.opensearch.sql.ast.expression.And;
import org.opensearch.sql.ast.expression.AttributeList;
import org.opensearch.sql.ast.expression.Case;
import org.opensearch.sql.ast.expression.Cast;
import org.opensearch.sql.ast.expression.DataType;
Expand Down Expand Up @@ -364,13 +365,26 @@ public UnresolvedExpression visitConvertedDataType(

@Override
public UnresolvedExpression visitRelevanceFunction(RelevanceFunctionContext ctx) {
if (ctx.relevanceFunctionType1() != null) {
return new Function(
ctx.relevanceFunctionType1().relevanceFunctionType1Name().getText().toLowerCase(),
relevanceArgumentsType1(ctx.relevanceFunctionType1()));
}
else {
return new Function(
ctx.relevanceFunctionType2().relevanceFunctionType2Name().getText().toLowerCase(),
relevanceArgumentsType2(ctx.relevanceFunctionType2()));
}
/*
ctx.relevanceFunctionType1().relevanceFunctionType1Name()
ParserRuleContext func = ctx.relevanceFunctionNameEx();
if (func == null) {
func = ctx.relevanceFunctionName();
}
return new Function(
func.getText().toLowerCase(),
relevanceArguments(ctx));
*/
}

private Function visitFunction(String functionName, FunctionArgsContext args) {
Expand All @@ -395,16 +409,12 @@ private QualifiedName visitIdentifiers(List<IdentContext> identifiers) {
);
}

private List<UnresolvedExpression> relevanceArguments(RelevanceFunctionContext ctx) {
private List<UnresolvedExpression> relevanceArgumentsType1(OpenSearchSQLParser.RelevanceFunctionType1Context ctx) {
// all the arguments are defaulted to string values
// to skip environment resolving and function signature resolving
ImmutableList.Builder<UnresolvedExpression> builder = ImmutableList.builder();
ParserRuleContext field = ctx.field;
if (field == null) {
field = ctx.fields;
}
builder.add(new UnresolvedArgument("field",
new Literal(StringUtils.unquoteText(field.getText()), DataType.STRING)));
new Literal(StringUtils.unquoteText(ctx.field.getText()), DataType.STRING)));
builder.add(new UnresolvedArgument("query",
new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING)));
ctx.relevanceArg().forEach(v -> builder.add(new UnresolvedArgument(
Expand All @@ -413,4 +423,24 @@ private List<UnresolvedExpression> relevanceArguments(RelevanceFunctionContext c
return builder.build();
}

private List<UnresolvedExpression> relevanceArgumentsType2(OpenSearchSQLParser.RelevanceFunctionType2Context ctx) {
// all the arguments are defaulted to string values
// to skip environment resolving and function signature resolving
ImmutableList.Builder<UnresolvedExpression> builder = ImmutableList.builder();
var fields =
ctx.getRuleContexts(OpenSearchSQLParser.RelevanceFieldContext.class);
ImmutableList.Builder<UnresolvedExpression> b2 = ImmutableList.builder();
for (var i = 0; i < fields.size(); i++) {
b2.add(new UnresolvedArgument(String.format("field_%d", i),
new Literal(StringUtils.unquoteText(fields.get(i).getText()), DataType.STRING)));
}
var lst2 = new AttributeList(b2.build());
builder.add(new UnresolvedArgument("fields", lst2));
builder.add(new UnresolvedArgument("query",
new Literal(StringUtils.unquoteText(ctx.query.getText()), DataType.STRING)));
ctx.relevanceArg().forEach(v -> builder.add(new UnresolvedArgument(
v.relevanceArgName().getText().toLowerCase(), new Literal(StringUtils.unquoteText(
v.relevanceArgValue().getText()), DataType.STRING))));
return builder.build();
}
}

0 comments on commit 3a2ff8a

Please sign in to comment.