-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break QueryTranslator into QL and SQL (#52397)
Refactor the code to allow contextual parameterization of dateFormat and name. Separate aggs/query implementation though there's room for improvement in the future
- Loading branch information
Showing
8 changed files
with
588 additions
and
257 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/ExpressionTranslator.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,35 @@ | ||
/* | ||
* 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.ql.planner; | ||
|
||
import org.elasticsearch.xpack.ql.expression.Expression; | ||
import org.elasticsearch.xpack.ql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.ql.querydsl.query.NestedQuery; | ||
import org.elasticsearch.xpack.ql.querydsl.query.Query; | ||
import org.elasticsearch.xpack.ql.util.ReflectionUtils; | ||
|
||
public abstract class ExpressionTranslator<E extends Expression> { | ||
|
||
private final Class<E> typeToken = ReflectionUtils.detectSuperTypeForRuleLike(getClass()); | ||
|
||
@SuppressWarnings("unchecked") | ||
public Query translate(Expression exp, TranslatorHandler handler) { | ||
return (typeToken.isInstance(exp) ? asQuery((E) exp, handler) : null); | ||
} | ||
|
||
protected abstract Query asQuery(E e, TranslatorHandler handler); | ||
|
||
public static Query wrapIfNested(Query query, Expression exp) { | ||
if (query != null && exp instanceof FieldAttribute) { | ||
FieldAttribute fa = (FieldAttribute) exp; | ||
if (fa.isNested()) { | ||
return new NestedQuery(fa.source(), fa.nestedParent().name(), query); | ||
} | ||
} | ||
return query; | ||
} | ||
} |
350 changes: 350 additions & 0 deletions
350
x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/ExpressionTranslators.java
Large diffs are not rendered by default.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/QlTranslatorHandler.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,44 @@ | ||
/* | ||
* 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.ql.planner; | ||
|
||
import org.elasticsearch.xpack.ql.expression.Expression; | ||
import org.elasticsearch.xpack.ql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.ql.expression.NamedExpression; | ||
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction; | ||
import org.elasticsearch.xpack.ql.querydsl.query.Query; | ||
import org.elasticsearch.xpack.ql.querydsl.query.ScriptQuery; | ||
|
||
public class QlTranslatorHandler implements TranslatorHandler { | ||
|
||
@Override | ||
public Query asQuery(Expression e) { | ||
return ExpressionTranslators.toQuery(e, this); | ||
} | ||
|
||
@Override | ||
public Query wrapFunctionQuery(ScalarFunction sf, Expression field, Query q) { | ||
if (field instanceof FieldAttribute) { | ||
return ExpressionTranslator.wrapIfNested(q, field); | ||
} | ||
return new ScriptQuery(sf.source(), sf.asScript()); | ||
} | ||
|
||
@Override | ||
public String nameOf(Expression e) { | ||
if (e instanceof NamedExpression) { | ||
return ((NamedExpression) e).name(); | ||
} else { | ||
return e.sourceText(); | ||
} | ||
} | ||
|
||
@Override | ||
public String dateFormat(Expression e) { | ||
return null; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/TranslatorHandler.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,27 @@ | ||
/* | ||
* 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.ql.planner; | ||
|
||
import org.elasticsearch.xpack.ql.expression.Expression; | ||
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction; | ||
import org.elasticsearch.xpack.ql.querydsl.query.Query; | ||
|
||
/** | ||
* Parameterized handler used during query translation. | ||
* | ||
* Provides contextual utilities for an individual query to be performed. | ||
*/ | ||
public interface TranslatorHandler { | ||
|
||
Query asQuery(Expression e); | ||
|
||
Query wrapFunctionQuery(ScalarFunction sf, Expression field, Query q); | ||
|
||
String nameOf(Expression e); | ||
|
||
String dateFormat(Expression e); | ||
} |
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
Oops, something went wrong.