Skip to content

Commit

Permalink
Break QueryTranslator into QL and SQL (#52397)
Browse files Browse the repository at this point in the history
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
costin authored Feb 17, 2020
1 parent cb9be14 commit e086f81
Show file tree
Hide file tree
Showing 8 changed files with 588 additions and 257 deletions.
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;
}
}

Large diffs are not rendered by default.

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;
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.xpack.ql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.ql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.ql.planner.ExpressionTranslators;
import org.elasticsearch.xpack.ql.querydsl.query.Query;
import org.elasticsearch.xpack.ql.rule.Rule;
import org.elasticsearch.xpack.ql.rule.RuleExecutor;
Expand Down Expand Up @@ -90,7 +91,6 @@
import java.util.concurrent.atomic.AtomicReference;

import static org.elasticsearch.xpack.ql.util.CollectionUtils.combine;
import static org.elasticsearch.xpack.sql.planner.QueryTranslator.and;
import static org.elasticsearch.xpack.sql.planner.QueryTranslator.toAgg;
import static org.elasticsearch.xpack.sql.planner.QueryTranslator.toQuery;
import static org.elasticsearch.xpack.sql.type.SqlDataTypes.DATE;
Expand Down Expand Up @@ -181,7 +181,7 @@ protected PhysicalPlan rule(FilterExec plan) {

Query query = null;
if (qContainer.query() != null || qt.query != null) {
query = and(plan.source(), qContainer.query(), qt.query);
query = ExpressionTranslators.and(plan.source(), qContainer.query(), qt.query);
}
Aggs aggs = addPipelineAggs(qContainer, qt, plan);

Expand Down
Loading

0 comments on commit e086f81

Please sign in to comment.