Skip to content

Commit

Permalink
moved responsibility of noResult checks to planners
Browse files Browse the repository at this point in the history
  • Loading branch information
dobe committed Jan 26, 2015
1 parent ae211de commit 1fa7930
Show file tree
Hide file tree
Showing 25 changed files with 212 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public void table(TableIdent tableIdent) {
this.tableIdent = tableIdent;
}

@Override
public boolean hasNoResult() {
return false;
}

@Override
public <C, R> R accept(AnalyzedStatementVisitor<C, R> analyzedStatementVisitor, C context) {
return analyzedStatementVisitor.visitDDLAnalyzedStatement(this, context);
Expand Down
2 changes: 0 additions & 2 deletions sql/src/main/java/io/crate/analyze/AnalyzedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public abstract class AnalyzedStatement {
protected AnalyzedStatement(ParameterContext parameterContext) {
this.parameterContext = parameterContext;
}
public abstract boolean hasNoResult();

public abstract void normalize();

public ParameterContext parameterContext() {
Expand Down
5 changes: 0 additions & 5 deletions sql/src/main/java/io/crate/analyze/CopyAnalyzedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ public TableInfo table() {
return table;
}

@Override
public boolean hasNoResult() {
return false;
}

@Override
public void normalize() {
}
Expand Down
15 changes: 0 additions & 15 deletions sql/src/main/java/io/crate/analyze/DeleteAnalyzedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@

package io.crate.analyze;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import io.crate.analyze.relations.AnalyzedRelation;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

public class DeleteAnalyzedStatement extends AnalyzedStatement {

private static final Predicate<WhereClause> HAS_NO_RESULT_PREDICATE = new Predicate<WhereClause>() {
@Override
public boolean apply(@Nullable WhereClause input) {
return input != null && input.noMatch();
}
};

final List<WhereClause> whereClauses = new ArrayList<>();
final AnalyzedRelation analyzedRelation;

Expand All @@ -54,11 +44,6 @@ public List<WhereClause> whereClauses() {
return whereClauses;
}

@Override
public boolean hasNoResult() {
return Iterables.all(whereClauses, HAS_NO_RESULT_PREDICATE);
}

@Override
public void normalize() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public AnalyzedRelation subQueryRelation() {
return this.subQueryRelation;
}

@Override
public boolean hasNoResult() {
return false;
}

@Override
public void normalize() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ public List<String> routingValues() {
return routingValues;
}

@Override
public boolean hasNoResult() {
return false;
}

@Override
public void normalize() {

Expand Down
17 changes: 0 additions & 17 deletions sql/src/main/java/io/crate/analyze/QuerySpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@

package io.crate.analyze;

import io.crate.planner.symbol.Literal;
import io.crate.planner.symbol.Symbol;
import io.crate.planner.symbol.SymbolType;

import javax.annotation.Nullable;
import java.util.List;

import static com.google.common.base.MoreObjects.firstNonNull;

public class QuerySpec {

private List<Symbol> groupBy;
Expand Down Expand Up @@ -141,17 +137,4 @@ public void normalize(EvaluatingNormalizer normalizer) {
}
}

public boolean hasNoResult() {
if (having != null && having.symbolType() == SymbolType.LITERAL) {
Literal havingLiteral = (Literal) having;
if (havingLiteral.value() == false) {
return true;
}
}
if (hasAggregates() && groupBy() == null) {
return firstNonNull(limit(), 1) < 1 || offset() > 0;
}
return where.noMatch() || limit != null && limit == 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ public void normalize(EvaluatingNormalizer normalizer) {
querySpec.normalize(normalizer);
}

@Override
public boolean hasNoResult() {
return querySpec.hasNoResult();
}

public void normalize() {
}

Expand Down
11 changes: 0 additions & 11 deletions sql/src/main/java/io/crate/analyze/SetAnalyzedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@ public void persistent(boolean persistent) {
this.persistent = persistent;
}

@Override
public boolean hasNoResult() {
if (settings != null) {
return settings.getAsMap().isEmpty();
}
if (settingsToRemove != null) {
return settingsToRemove.size() == 0;
}
return true;
}

@Override
public void normalize() {

Expand Down
15 changes: 0 additions & 15 deletions sql/src/main/java/io/crate/analyze/UpdateAnalyzedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

package io.crate.analyze;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import io.crate.analyze.relations.AnalyzedRelation;
import io.crate.analyze.relations.AnalyzedRelationVisitor;
import io.crate.exceptions.ColumnUnknownException;
Expand All @@ -40,14 +38,6 @@

public class UpdateAnalyzedStatement extends AnalyzedStatement implements AnalyzedRelation {

private static final Predicate<NestedAnalyzedStatement> HAS_NO_RESULT_PREDICATE = new Predicate<NestedAnalyzedStatement>() {
@Override
public boolean apply(@Nullable NestedAnalyzedStatement input) {
return input != null && input.whereClause.noMatch();
}
};


private final List<NestedAnalyzedStatement> nestedStatements;
private final AnalyzedRelation sourceRelation;

Expand All @@ -66,11 +56,6 @@ public List<NestedAnalyzedStatement> nestedStatements() {
return nestedStatements;
}

@Override
public boolean hasNoResult() {
return Iterables.all(nestedStatements, HAS_NO_RESULT_PREDICATE);
}

@Override
public void normalize() {}

Expand Down
35 changes: 19 additions & 16 deletions sql/src/main/java/io/crate/planner/Planner.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ public Planner(ClusterService clusterService, AnalysisMetaData analysisMetaData)
*/
public Plan plan(Analysis analysis) {
AnalyzedStatement analyzedStatement = analysis.analyzedStatement();
if (analyzedStatement.hasNoResult()){
return NoopPlan.INSTANCE;
}
Plan plan = process(analyzedStatement, EMPTY_CONTEXT);
return plan;
return process(analyzedStatement, EMPTY_CONTEXT);
}

@Override
Expand Down Expand Up @@ -146,14 +142,17 @@ protected Plan visitDeleteStatement(DeleteAnalyzedStatement analyzedStatement, C
TableRelation tableRelation = (TableRelation) analyzedStatement.analyzedRelation();
WhereClauseAnalyzer whereClauseAnalyzer = new WhereClauseAnalyzer(analysisMetaData, tableRelation);
for (WhereClause whereClause : analyzedStatement.whereClauses()) {
if (whereClause.noMatch()){
continue;
}
WhereClauseContext whereClauseContext = whereClauseAnalyzer.analyze(whereClause);
if (whereClauseContext.ids().size() == 1 && whereClauseContext.routingValues().size() == 1) {
createESDeleteNode(tableRelation.tableInfo(), whereClauseContext, plan);
} else {
createESDeleteByQueryNode(tableRelation.tableInfo(), whereClauseContext, plan);
}
}
if (plan.isEmpty()){
if (plan.isEmpty()) {
return NoopPlan.INSTANCE;
}
return plan;
Expand Down Expand Up @@ -369,18 +368,22 @@ protected Plan visitCreateAnalyzerStatement(CreateAnalyzerAnalyzedStatement anal

@Override
public Plan visitSetStatement(SetAnalyzedStatement analysis, Context context) {
ESClusterUpdateSettingsNode node;
ESClusterUpdateSettingsNode node = null;
if (analysis.isReset()) {
// always reset persistent AND transient settings
node = new ESClusterUpdateSettingsNode(analysis.settingsToRemove(), analysis.settingsToRemove());
if (analysis.settingsToRemove() != null) {
node = new ESClusterUpdateSettingsNode(analysis.settingsToRemove(), analysis.settingsToRemove());
}
} else {
if (analysis.isPersistent()) {
node = new ESClusterUpdateSettingsNode(analysis.settings());
} else {
node = new ESClusterUpdateSettingsNode(ImmutableSettings.EMPTY, analysis.settings());
if (analysis.settings() != null) {
if (analysis.isPersistent()) {
node = new ESClusterUpdateSettingsNode(analysis.settings());
} else {
node = new ESClusterUpdateSettingsNode(ImmutableSettings.EMPTY, analysis.settings());
}
}
}
return new IterablePlan(node);
return node != null ? new IterablePlan(node) : NoopPlan.INSTANCE;
}

private void createESDeleteNode(TableInfo tableInfo, WhereClauseContext whereClauseContext, IterablePlan plan) {
Expand Down Expand Up @@ -417,7 +420,7 @@ private ESIndexNode createESIndexNode(InsertFromValuesAnalyzedStatement analysis
List<String> partitions = analysis.generatePartitions();
indices = partitions.toArray(new String[partitions.size()]);
} else {
indices = new String[]{ analysis.tableInfo().ident().esName() };
indices = new String[]{analysis.tableInfo().ident().esName()};
}

ESIndexNode indexNode = new ESIndexNode(
Expand All @@ -432,7 +435,7 @@ private ESIndexNode createESIndexNode(InsertFromValuesAnalyzedStatement analysis
}

static List<DataType> extractDataTypes(List<Projection> projections, @Nullable List<DataType> inputTypes) {
if (projections.size() == 0){
if (projections.size() == 0) {
return inputTypes;
}
int projectionIdx = projections.size() - 1;
Expand Down Expand Up @@ -476,7 +479,7 @@ public static String[] indices(TableInfo tableInfo, WhereClause whereClause) {
indices = org.elasticsearch.common.Strings.EMPTY_ARRAY;
} else if (!tableInfo.isPartitioned()) {
// table name for non-partitioned tables
indices = new String[]{ tableInfo.ident().name() };
indices = new String[]{tableInfo.ident().name()};
} else if (whereClause.partitions().isEmpty()) {
if (whereClause.noMatch()) {
return new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.crate.metadata.table.TableInfo;
import io.crate.planner.PlanNodeBuilder;
import io.crate.planner.PlannerContextBuilder;
import io.crate.planner.node.NoopPlannedAnalyzedRelation;
import io.crate.planner.node.dql.CollectNode;
import io.crate.planner.node.dql.DistributedGroupBy;
import io.crate.planner.node.dql.GroupByConsumer;
Expand Down Expand Up @@ -127,6 +128,9 @@ public AnalyzedRelation visitSelectAnalyzedStatement(SelectAnalyzedStatement sta
Symbol havingClause = null;
if(statement.querySpec().having() != null){
havingClause = tableRelation.resolveHaving(statement.querySpec().having());
if (!WhereClause.canMatch(havingClause)) {
return new NoopPlannedAnalyzedRelation(statement);
};
}
if (havingClause != null && havingClause instanceof Function) {
// replace aggregation symbols with input columns from previous projection
Expand Down
10 changes: 10 additions & 0 deletions sql/src/main/java/io/crate/planner/consumer/ESCountConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
import io.crate.metadata.table.TableInfo;
import io.crate.operation.aggregation.impl.CountAggregation;
import io.crate.planner.Planner;
import io.crate.planner.node.NoopPlannedAnalyzedRelation;
import io.crate.planner.node.dql.ESCountNode;
import io.crate.planner.symbol.Function;
import io.crate.planner.symbol.Symbol;

import java.util.List;

import static com.google.common.base.MoreObjects.firstNonNull;

public class ESCountConsumer implements Consumer {

private final Visitor visitor;
Expand Down Expand Up @@ -87,6 +90,13 @@ public PlannedAnalyzedRelation visitSelectAnalyzedStatement(SelectAnalyzedStatem
context.validationException(new VersionInvalidException());
return null;
}

if (firstNonNull(selectAnalyzedStatement.querySpec().limit(), 1) < 1 ||
selectAnalyzedStatement.querySpec().offset() > 0){
return new NoopPlannedAnalyzedRelation(selectAnalyzedStatement);
}


return new ESCountNode(Planner.indices(tableInfo, whereClauseContext.whereClause()),
whereClauseContext.whereClause());
}
Expand Down
13 changes: 11 additions & 2 deletions sql/src/main/java/io/crate/planner/consumer/ESGetConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.crate.exceptions.VersionInvalidException;
import io.crate.metadata.table.TableInfo;
import io.crate.planner.RowGranularity;
import io.crate.planner.node.NoopPlannedAnalyzedRelation;
import io.crate.planner.node.dql.ESGetNode;

public class ESGetConsumer implements Consumer {
Expand Down Expand Up @@ -104,6 +105,14 @@ public PlannedAnalyzedRelation visitSelectAnalyzedStatement(SelectAnalyzedStatem
} else {
indexName = tableInfo.ident().name();
}

Integer limit = statement.querySpec().limit();
if (limit != null){
if (limit == 0){
return new NoopPlannedAnalyzedRelation(statement);
}
}

OrderBy orderBy = statement.querySpec().orderBy();
if (orderBy == null){
return new ESGetNode(
Expand All @@ -112,7 +121,7 @@ public PlannedAnalyzedRelation visitSelectAnalyzedStatement(SelectAnalyzedStatem
whereClauseContext.ids(),
whereClauseContext.routingValues(),
null, null, null,
statement.querySpec().limit(),
limit,
statement.querySpec().offset(),
tableInfo.partitionedByColumns()
);
Expand All @@ -125,7 +134,7 @@ public PlannedAnalyzedRelation visitSelectAnalyzedStatement(SelectAnalyzedStatem
tableRelation.resolveAndValidateOrderBy(statement.querySpec().orderBy()),
orderBy.reverseFlags(),
orderBy.nullsFirst(),
statement.querySpec().limit(),
limit,
statement.querySpec().offset(),
tableInfo.partitionedByColumns()
);
Expand Down
Loading

0 comments on commit 1fa7930

Please sign in to comment.