Skip to content

Commit

Permalink
remove rowGranularity from Analysis/AnalyzedStatement
Browse files Browse the repository at this point in the history
It is not allowed to have a column with a finer granularity
then the table has in the query. So it is unnecessary to
keep track of the max rowGranularity because
tableInfo.rowGranularity() will always have the max
rowGranularity.
  • Loading branch information
mfussenegger committed Nov 27, 2014
1 parent b109166 commit 641c803
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public boolean apply(@Nullable ReferenceInfo input) {
protected List<Symbol> outputSymbols = ImmutableList.of();

protected WhereClause whereClause = WhereClause.MATCH_ALL;
protected RowGranularity rowGranularity;
protected boolean hasAggregates = false;
protected boolean hasSysExpressions = false;
protected boolean sysExpressionsAllowed = false;
Expand Down Expand Up @@ -101,7 +100,6 @@ public void table(TableIdent tableIdent) {
onlyScalarsAllowed = schemaInfo.systemSchema();
sysExpressionsAllowed = schemaInfo.systemSchema();
table = tableInfo;
updateRowGranularity(table.rowGranularity());
}

public void editableTable(TableIdent tableIdent) throws TableUnknownException,
Expand All @@ -121,7 +119,6 @@ public void editableTable(TableIdent tableIdent) throws TableUnknownException,
String.format("aliases are read only cannot modify \"%s\"", tableIdent.name()));
}
table = tableInfo;
updateRowGranularity(table.rowGranularity());
}

@Override
Expand Down Expand Up @@ -152,10 +149,9 @@ private Reference allocateReference(ReferenceIdent ident, boolean unique, boolea
}
}
if (info.granularity().finerThan(table.rowGranularity())) {
throw new UnsupportedOperationException(
String.format(Locale.ENGLISH,
"Cannot resolve reference '%s.%s', reason: finer granularity than table '%s'",
info.ident().tableIdent().fqn(), info.ident().columnIdent().fqn(), table.ident().fqn()));
throw new UnsupportedOperationException(String.format(Locale.ENGLISH,
"Cannot resolve reference '%s.%s', reason: finer granularity than table '%s'",
info.ident().tableIdent().fqn(), info.ident().columnIdent().fqn(), table.ident().fqn()));
}
if (reference == null) {
reference = new Reference(info);
Expand All @@ -164,7 +160,6 @@ private Reference allocateReference(ReferenceIdent ident, boolean unique, boolea
} else if (unique) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "reference '%s' repeated", ident.columnIdent().fqn()));
}
updateRowGranularity(reference.info().granularity());
return reference;
}

Expand Down Expand Up @@ -277,22 +272,6 @@ public WhereClause whereClause() {
return whereClause;
}

/**
* Updates the row granularity of this query if it is higher than the current row granularity.
*
* @param granularity the row granularity as seen by a reference
*/
protected RowGranularity updateRowGranularity(RowGranularity granularity) {
if (rowGranularity == null || rowGranularity.ordinal() < granularity.ordinal()) {
rowGranularity = granularity;
}
return rowGranularity;
}

public RowGranularity rowGranularity() {
return rowGranularity;
}

public List<Symbol> outputSymbols() {
return outputSymbols;
}
Expand Down
4 changes: 2 additions & 2 deletions sql/src/main/java/io/crate/planner/PlanNodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static CollectNode distributingCollect(AbstractDataAnalyzedStatement analysis,
ImmutableList<Projection> projections) {
CollectNode node = new CollectNode("distributing collect", analysis.table().getRouting(analysis.whereClause()));
node.whereClause(analysis.whereClause());
node.maxRowGranularity(analysis.rowGranularity());
node.maxRowGranularity(analysis.table().rowGranularity());
node.downStreamNodes(downstreamNodes);
node.toCollect(toCollect);
node.projections(projections);
Expand Down Expand Up @@ -111,7 +111,7 @@ static CollectNode collect(AbstractDataAnalyzedStatement analysis,
CollectNode node = new CollectNode("collect", routing);
node.whereClause(analysis.whereClause());
node.toCollect(toCollect);
node.maxRowGranularity(analysis.rowGranularity());
node.maxRowGranularity(analysis.table().rowGranularity());
node.projections(projections);
node.isPartitioned(analysis.table().isPartitioned());
setOutputTypes(node);
Expand Down
7 changes: 3 additions & 4 deletions sql/src/main/java/io/crate/planner/Planner.java
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,7 @@ private boolean hasOnlyGlobalCount(List<Symbol> symbols) {
}

private void groupBy(SelectAnalyzedStatement analysis, Plan plan, Context context) {

if (analysis.rowGranularity().ordinal() < RowGranularity.DOC.ordinal()
if (analysis.table().rowGranularity().ordinal() < RowGranularity.DOC.ordinal()
|| !requiresDistribution(analysis)) {
nonDistributedGroupBy(analysis, plan, context);
} else if (context.indexWriterProjection.isPresent()) {
Expand Down Expand Up @@ -695,7 +694,7 @@ private void nonDistributedGroupBy(SelectAnalyzedStatement analysis, Plan plan,
boolean groupedByClusteredPk = groupedByClusteredColumnOrPrimaryKeys(analysis);

int numAggregationSteps = 2;
if (analysis.rowGranularity() == RowGranularity.DOC) {
if (analysis.table().rowGranularity() == RowGranularity.DOC) {
/**
* this is only the case if the group by key is the clustered by column.
* collectNode has row-authority and there is no need to group again on the handler node
Expand Down Expand Up @@ -1090,7 +1089,7 @@ public Plan visitSelectAnalyzedStatement(SelectAnalyzedStatement statement, Cont

WhereClause whereClause = statement.whereClause();
if (!context.indexWriterProjection.isPresent()
&& statement.rowGranularity().ordinal() >= RowGranularity.DOC.ordinal() &&
&& statement.table().rowGranularity().ordinal() >= RowGranularity.DOC.ordinal() &&
statement.table().getRouting(whereClause).hasLocations() &&
statement.table().schemaInfo().name().equals(DocSchemaInfo.NAME)) {

Expand Down
2 changes: 1 addition & 1 deletion sql/src/test/java/io/crate/analyze/DeleteAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testDeleteWhere() throws Exception {
DeleteAnalyzedStatement.NestedDeleteAnalyzedStatement analysis = analyze("delete from users where name='Trillian'");
assertEquals(TEST_DOC_TABLE_IDENT, analysis.table().ident());

assertThat(analysis.rowGranularity(), is(RowGranularity.DOC));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.DOC));

Function whereClause = (Function)analysis.whereClause().query();
assertEquals(EqOperator.NAME, whereClause.info().ident().name());
Expand Down
19 changes: 13 additions & 6 deletions sql/src/test/java/io/crate/analyze/SelectAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void testOrderedSelect() throws Exception {

assertFalse(analysis.hasGroupBy());
assertTrue(analysis.orderBy().isSorted());
assertThat(analysis.rowGranularity(), is(RowGranularity.NODE));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.NODE));

assertEquals(1, analysis.outputSymbols().size());
assertEquals(1, analysis.orderBy().orderBySymbols().size());
Expand Down Expand Up @@ -235,7 +235,7 @@ public void testGroupedSelect() throws Exception {
assertEquals(analysis.table().ident(), SysNodesTableInfo.IDENT);
assertNull(analysis.limit());

assertThat(analysis.rowGranularity(), is(RowGranularity.NODE));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.NODE));
assertTrue(analysis.hasGroupBy());
assertEquals(2, analysis.outputSymbols().size());
assertEquals(1, analysis.groupBy().size());
Expand All @@ -252,7 +252,7 @@ public void testSimpleSelect() throws Exception {

assertFalse(analysis.hasGroupBy());

assertThat(analysis.rowGranularity(), is(RowGranularity.NODE));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.NODE));

assertEquals(SysNodesTableInfo.IDENT, analysis.table().ident());
assertEquals(1, analysis.outputSymbols().size());
Expand All @@ -266,7 +266,7 @@ public void testAggregationSelect() throws Exception {
SelectAnalyzedStatement analysis = analyze("select avg(load['5']) from sys.nodes");
assertEquals(SysNodesTableInfo.IDENT, analysis.table().ident());

assertThat(analysis.rowGranularity(), is(RowGranularity.NODE));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.NODE));

assertFalse(analysis.hasGroupBy());
assertEquals(1, analysis.outputSymbols().size());
Expand Down Expand Up @@ -298,7 +298,7 @@ public void testWhereSelect() throws Exception {
"where load['1'] = 1.2 or 1 >= load['5']");
assertEquals(SysNodesTableInfo.IDENT, analysis.table().ident());

assertThat(analysis.rowGranularity(), is(RowGranularity.NODE));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.NODE));

assertFalse(analysis.hasGroupBy());

Expand Down Expand Up @@ -656,7 +656,7 @@ public void test3ColPrimaryKeySetLiteral() throws Exception {
@Test
public void testGranularityWithSingleAggregation() throws Exception {
SelectAnalyzedStatement analysis = analyze("select count(*) from sys.nodes");
assertThat(analysis.rowGranularity(), is(RowGranularity.NODE));
assertThat(analysis.table().rowGranularity(), is(RowGranularity.NODE));
}

@Test
Expand Down Expand Up @@ -2017,4 +2017,11 @@ public void testClusteredByValueContainsComma() throws Exception {
assertThat(analysis.ids().size(), is(1));
assertThat(analysis.ids().get(0), is("a,b,c"));
}

@Test
public void testShardGranularityFromNodeGranularityTable() throws Exception {
expectedException.expect(UnsupportedOperationException.class);
expectedException.expectMessage("Cannot resolve reference 'sys.shards.id', reason: finer granularity than table 'sys.nodes'");
analyze("select sys.shards.id from sys.nodes");
}
}

0 comments on commit 641c803

Please sign in to comment.