http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java index e5c70c0..92c2641 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java @@ -7,6 +7,10 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import org.apache.calcite.avatica.util.ByteString; +import org.apache.calcite.rel.RelCollation; +import org.apache.calcite.rel.RelCollations; +import org.apache.calcite.rel.RelFieldCollation; +import org.apache.calcite.rel.RelFieldCollation.Direction; import org.apache.calcite.rel.core.JoinRelType; import org.apache.calcite.rex.RexCall; import org.apache.calcite.rex.RexCorrelVariable; @@ -149,6 +153,28 @@ public class CalciteUtils { return ret; } + + public static RelCollation reverseCollation(RelCollation collation) { + if (collation.getFieldCollations().isEmpty()) + return collation; + + List<RelFieldCollation> fieldCollations = Lists.newArrayList(); + for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { + Direction dir = null; + switch (fieldCollation.direction) { + case ASCENDING: + dir = Direction.DESCENDING; + break; + case DESCENDING: + dir = Direction.ASCENDING; + break; + default: + assert false : "Shouldn't have come accross non Phoenix directions"; + } + fieldCollations.add(new RelFieldCollation(fieldCollation.getFieldIndex(), dir, fieldCollation.nullDirection)); + } + return RelCollations.of(fieldCollations); + } private static final Map<SqlKind, ExpressionFactory> EXPRESSION_MAP = Maps .newHashMapWithExpectedSize(ExpressionType.values().length);
http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java index a6c10b7..01913b9 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java @@ -20,7 +20,6 @@ import org.apache.phoenix.parse.TableName; import org.apache.phoenix.schema.MetaDataClient; import org.apache.phoenix.schema.PColumn; import org.apache.phoenix.schema.PTable; -import org.apache.phoenix.schema.PTable.IndexType; import org.apache.phoenix.schema.PTable.ViewType; import org.apache.phoenix.schema.PTableType; import org.apache.phoenix.schema.TableRef; @@ -49,11 +48,11 @@ import java.util.Set; */ public class PhoenixSchema implements Schema { public static final Factory FACTORY = new Factory(); - private static final String UNORDERED_SUFFIX = ":unordered"; + + public final PhoenixConnection pc; protected final String name; protected final String schemaName; - protected final PhoenixConnection pc; protected final MetaDataClient client; protected final Set<String> subSchemaNames; @@ -105,12 +104,7 @@ public class PhoenixSchema implements Schema { ImmutableList.<ColumnDef>of()), pc); final List<TableRef> tables = x.getTables(); assert tables.size() == 1; - final PTable pTable = tables.get(0).getTable(); - tableMap.put(tableName, pTable); - if (pTable.getBucketNum() != null || pTable.getIndexType() == IndexType.LOCAL) { - final String unorderedTableName = tableName + UNORDERED_SUFFIX; - tableMap.put(unorderedTableName, pTable); - } + tableMap.put(tableName, tables.get(0).getTable()); } else { String viewSql = rs.getString(PhoenixDatabaseMetaData.VIEW_STATEMENT); String viewType = rs.getString(PhoenixDatabaseMetaData.VIEW_TYPE); @@ -145,7 +139,7 @@ public class PhoenixSchema implements Schema { @Override public Table getTable(String name) { PTable table = tableMap.get(name); - return table == null ? null : new PhoenixTable(pc, table, !isUnorderedTableName(name)); + return table == null ? null : new PhoenixTable(pc, table); } @Override @@ -204,20 +198,9 @@ public class PhoenixSchema implements Schema { public void defineIndexesAsMaterializations(CalciteSchema calciteSchema) { List<String> path = calciteSchema.path(null); - for (Map.Entry<String, PTable> entry : tableMap.entrySet()) { - final String tableName = entry.getKey(); - final PTable table = entry.getValue(); - if (!isUnorderedTableName(tableName)) { - for (PTable index : table.getIndexes()) { - addMaterialization(table, index, path, calciteSchema); - } - } - } - for (Map.Entry<String, PTable> entry : tableMap.entrySet()) { - final String tableName = entry.getKey(); - final PTable table = entry.getValue(); - if (isUnorderedTableName(tableName)) { - addUnorderedAsMaterialization(tableName, table, path, calciteSchema); + for (PTable table : tableMap.values()) { + for (PTable index : table.getIndexes()) { + addMaterialization(table, index, path, calciteSchema); } } } @@ -239,21 +222,6 @@ public class PhoenixSchema implements Schema { calciteSchema, null, sb.toString(), path, index.getTableName().getString(), true, true); } - protected void addUnorderedAsMaterialization(String tableName, PTable table, List<String> path, - CalciteSchema calciteSchema) { - StringBuffer sb = new StringBuffer(); - sb.append("SELECT * FROM ") - .append("\"") - .append(table.getTableName().getString()) - .append("\""); - MaterializationService.instance().defineMaterialization( - calciteSchema, null, sb.toString(), path, tableName, true, true); - } - - private boolean isUnorderedTableName(String tableName) { - return tableName.endsWith(UNORDERED_SUFFIX); - } - private static class ViewDef { final String viewSql; final boolean updatable; http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixTable.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixTable.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixTable.java index 272cd47..cb07a7c 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixTable.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixTable.java @@ -2,7 +2,6 @@ package org.apache.phoenix.calcite; import java.util.List; -import org.apache.calcite.plan.RelOptCluster; import org.apache.calcite.plan.RelOptTable; import org.apache.calcite.rel.RelCollation; import org.apache.calcite.rel.RelCollationTraitDef; @@ -19,7 +18,6 @@ import org.apache.calcite.schema.TranslatableTable; import org.apache.calcite.schema.impl.AbstractTable; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.util.ImmutableBitSet; -import org.apache.phoenix.calcite.rel.PhoenixRel; import org.apache.phoenix.calcite.rel.PhoenixTableScan; import org.apache.phoenix.jdbc.PhoenixConnection; import org.apache.phoenix.schema.PColumn; @@ -42,25 +40,21 @@ public class PhoenixTable extends AbstractTable implements TranslatableTable { public final ImmutableBitSet pkBitSet; public final RelCollation collation; public final PhoenixConnection pc; - public final boolean requireRowKeyOrder; public static int getStartingColumnPosition(PTable pTable) { return (pTable.getBucketNum() == null ? 0 : 1) + (pTable.isMultiTenant() ? 1 : 0) + (pTable.getViewIndexId() == null ? 0 : 1); } - public PhoenixTable(PhoenixConnection pc, PTable pTable, boolean requireRowKeyOrder) { + public PhoenixTable(PhoenixConnection pc, PTable pTable) { this.pc = Preconditions.checkNotNull(pc); this.pTable = Preconditions.checkNotNull(pTable); - this.requireRowKeyOrder = requireRowKeyOrder; List<Integer> pkPositions = Lists.<Integer> newArrayList(); List<RelFieldCollation> fieldCollations = Lists.<RelFieldCollation> newArrayList(); - if (requireRowKeyOrder) { - for (PColumn column : pTable.getPKColumns()) { - int position = column.getPosition(); - SortOrder sortOrder = column.getSortOrder(); - pkPositions.add(position); - fieldCollations.add(new RelFieldCollation(position, sortOrder == SortOrder.ASC ? Direction.ASCENDING : Direction.DESCENDING)); - } + for (PColumn column : pTable.getPKColumns()) { + int position = column.getPosition(); + SortOrder sortOrder = column.getSortOrder(); + pkPositions.add(position); + fieldCollations.add(new RelFieldCollation(position, sortOrder == SortOrder.ASC ? Direction.ASCENDING : Direction.DESCENDING)); } this.pkBitSet = ImmutableBitSet.of(pkPositions); this.collation = RelCollationTraitDef.INSTANCE.canonize(RelCollations.of(fieldCollations)); @@ -105,10 +99,7 @@ public class PhoenixTable extends AbstractTable implements TranslatableTable { @Override public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable relOptTable) { - final RelOptCluster cluster = context.getCluster(); - // TODO Is there a better place to do this? - cluster.setMetadataProvider(PhoenixRel.METADATA_PROVIDER); - return PhoenixTableScan.create(cluster, relOptTable, null); + return PhoenixTableScan.create(context.getCluster(), relOptTable); } @Override @@ -132,9 +123,7 @@ public class PhoenixTable extends AbstractTable implements TranslatableTable { @Override public List<RelCollation> getCollations() { - return collation.getFieldCollations().isEmpty() ? - ImmutableList.<RelCollation>of() - : ImmutableList.<RelCollation>of(collation); + return ImmutableList.<RelCollation>of(collation); } @Override http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java index 677b966..a956eee 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java @@ -5,6 +5,7 @@ import java.util.List; import org.apache.calcite.adapter.enumerable.EnumerableRules; import org.apache.calcite.jdbc.CalcitePrepare; import org.apache.calcite.jdbc.CalciteSchema; +import org.apache.calcite.plan.RelOptCluster; import org.apache.calcite.plan.RelOptCostFactory; import org.apache.calcite.plan.RelOptPlanner; import org.apache.calcite.plan.RelOptRule; @@ -12,8 +13,11 @@ import org.apache.calcite.plan.RelTraitSet; import org.apache.calcite.prepare.CalcitePrepareImpl; import org.apache.calcite.prepare.Prepare.Materialization; import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.logical.LogicalSort; import org.apache.calcite.rel.rules.JoinCommuteRule; +import org.apache.calcite.rel.rules.SortProjectTransposeRule; import org.apache.calcite.rel.rules.SortUnionTransposeRule; +import org.apache.calcite.rex.RexBuilder; import org.apache.calcite.runtime.Hook; import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.parser.SqlParser; @@ -22,15 +26,19 @@ import org.apache.calcite.tools.Programs; import org.apache.calcite.util.Holder; import org.apache.calcite.util.Pair; import org.apache.phoenix.calcite.PhoenixSchema; -import org.apache.phoenix.calcite.metadata.PhoenixRelMetadataProvider; import org.apache.phoenix.calcite.parse.SqlCreateView; import org.apache.phoenix.calcite.parser.PhoenixParserImpl; +import org.apache.phoenix.calcite.rel.PhoenixRel; +import org.apache.phoenix.calcite.rel.PhoenixServerProject; +import org.apache.phoenix.calcite.rel.PhoenixTemporarySort; import org.apache.phoenix.calcite.rules.PhoenixCompactClientSortRule; import org.apache.phoenix.calcite.rules.PhoenixFilterScanMergeRule; -import org.apache.phoenix.calcite.rules.PhoenixInnerSortRemoveRule; +import org.apache.phoenix.calcite.rules.PhoenixForwardTableScanRule; import org.apache.phoenix.calcite.rules.PhoenixJoinSingleValueAggregateMergeRule; import org.apache.phoenix.calcite.rules.PhoenixMergeSortUnionRule; import org.apache.phoenix.calcite.rules.PhoenixOrderedAggregateRule; +import org.apache.phoenix.calcite.rules.PhoenixReverseTableScanRule; +import org.apache.phoenix.calcite.rules.PhoenixSortServerJoinTransposeRule; import com.google.common.base.Function; @@ -58,6 +66,14 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { } @Override + protected RelOptCluster createCluster(RelOptPlanner planner, + RexBuilder rexBuilder) { + RelOptCluster cluster = super.createCluster(planner, rexBuilder); + cluster.setMetadataProvider(PhoenixRel.METADATA_PROVIDER); + return cluster; + } + + @Override protected RelOptPlanner createPlanner( final CalcitePrepare.Context prepareContext, org.apache.calcite.plan.Context externalContext, @@ -69,16 +85,24 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { planner.addRule(JoinCommuteRule.SWAP_OUTER); planner.removeRule(SortUnionTransposeRule.INSTANCE); planner.addRule(SortUnionTransposeRule.MATCH_NULL_FETCH); + planner.addRule(new SortProjectTransposeRule( + PhoenixTemporarySort.class, + PhoenixServerProject.class, + "PhoenixSortProjectTransposeRule")); for (RelOptRule rule : this.defaultConverterRules) { planner.addRule(rule); } planner.addRule(PhoenixFilterScanMergeRule.INSTANCE); - planner.addRule(PhoenixCompactClientSortRule.SORT_SERVERAGGREGATE); + planner.addRule(PhoenixCompactClientSortRule.INSTANCE); planner.addRule(PhoenixJoinSingleValueAggregateMergeRule.INSTANCE); planner.addRule(PhoenixMergeSortUnionRule.INSTANCE); - planner.addRule(PhoenixInnerSortRemoveRule.INSTANCE); planner.addRule(PhoenixOrderedAggregateRule.INSTANCE); + planner.addRule(PhoenixSortServerJoinTransposeRule.INSTANCE); + planner.addRule(new PhoenixForwardTableScanRule(LogicalSort.class)); + planner.addRule(new PhoenixForwardTableScanRule(PhoenixTemporarySort.class)); + planner.addRule(new PhoenixReverseTableScanRule(LogicalSort.class)); + planner.addRule(new PhoenixReverseTableScanRule(PhoenixTemporarySort.class)); for (CalciteSchema schema : prepareContext.getRootSchema().getSubSchemaMap().values()) { if (schema.schema instanceof PhoenixSchema) { @@ -121,7 +145,7 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { // Second planner pass to do physical "tweaks". This the first time that // EnumerableCalcRel is introduced. - final Program program2 = Programs.hep(Programs.CALC_RULES, true, new PhoenixRelMetadataProvider());; + final Program program2 = Programs.hep(Programs.CALC_RULES, true, PhoenixRel.METADATA_PROVIDER);; Program p = Programs.sequence(program1, program2); input.getValue().set(p); http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java index 90606cd..d653627 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/metadata/PhoenixRelMdCollation.java @@ -1,6 +1,7 @@ package org.apache.phoenix.calcite.metadata; import java.util.List; + import org.apache.calcite.rel.RelCollation; import org.apache.calcite.rel.RelCollations; import org.apache.calcite.rel.RelNode; @@ -17,6 +18,7 @@ import org.apache.phoenix.calcite.rel.PhoenixCorrelate; import org.apache.phoenix.calcite.rel.PhoenixLimit; import org.apache.phoenix.calcite.rel.PhoenixMergeSortUnion; import org.apache.phoenix.calcite.rel.PhoenixServerJoin; +import org.apache.phoenix.calcite.rel.PhoenixTableScan; import com.google.common.collect.ImmutableList; @@ -27,6 +29,10 @@ public class PhoenixRelMdCollation { private PhoenixRelMdCollation() { } + public ImmutableList<RelCollation> collations(PhoenixTableScan tableScan) { + return ImmutableList.copyOf(tableScan.getCollationList()); + } + public ImmutableList<RelCollation> collations(PhoenixCorrelate correlate) { return ImmutableList.copyOf(correlate(correlate.getLeft(), correlate.getRight(), correlate.getJoinType())); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java index 17a4cea..681414b 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixAbstractAggregate.java @@ -1,6 +1,7 @@ package org.apache.phoenix.calcite.rel; import java.util.List; +import java.util.Set; import org.apache.calcite.plan.RelOptCluster; import org.apache.calcite.plan.RelOptCost; @@ -15,10 +16,9 @@ import org.apache.calcite.rel.core.Aggregate; import org.apache.calcite.rel.core.AggregateCall; import org.apache.calcite.util.ImmutableBitSet; import org.apache.calcite.util.ImmutableIntList; -import org.apache.calcite.util.Util; import org.apache.phoenix.calcite.CalciteUtils; -import org.apache.phoenix.compile.QueryPlan; import org.apache.phoenix.compile.GroupByCompiler.GroupBy; +import org.apache.phoenix.compile.QueryPlan; import org.apache.phoenix.compile.StatementContext; import org.apache.phoenix.coprocessor.BaseScannerRegionObserver; import org.apache.phoenix.execute.TupleProjectionPlan; @@ -34,6 +34,7 @@ import org.apache.phoenix.schema.RowKeyValueAccessor; import org.apache.phoenix.schema.TableRef; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; /** * Implementation of {@link org.apache.calcite.rel.core.Aggregate} @@ -50,22 +51,30 @@ abstract public class PhoenixAbstractAggregate extends Aggregate implements Phoe return call.getAggregation().getName().equals("SINGLE_VALUE"); } - public static boolean isOrderedGroupSet(ImmutableBitSet groupSet, RelNode child) { - List<Integer> ordinals = groupSet.asList(); + protected static boolean isOrderedGroupSet(ImmutableBitSet groupSet, RelNode child) { + if (groupSet.isEmpty()) { + return true; + } + + Set<Integer> ordinals = Sets.newHashSet(groupSet.asList()); List<RelCollation> collations = child.getTraitSet().getTraits(RelCollationTraitDef.INSTANCE); - boolean isOrderedGroupBy = ordinals.isEmpty(); - for (int i = 0; i < collations.size() && !isOrderedGroupBy; i++) { + for (int i = 0; i < collations.size(); i++) { + int count = 0; List<RelFieldCollation> fieldCollations = collations.get(i).getFieldCollations(); - List<Integer> fields = Lists.newArrayListWithExpectedSize(fieldCollations.size()); - for (RelFieldCollation fieldCollation : fieldCollations) { - fields.add(fieldCollation.getFieldIndex()); + if (fieldCollations.size() < ordinals.size()) { + continue; + } + for (RelFieldCollation fieldCollation : fieldCollations.subList(0, ordinals.size())) { + if (ordinals.contains(fieldCollation.getFieldIndex())) { + count++; + } } - if (Util.startsWith(fields, ordinals)) { - isOrderedGroupBy = true; + if (count == ordinals.size()) { + return true; } } - return isOrderedGroupBy; + return false; } public final boolean isOrderedGroupBy; @@ -93,7 +102,7 @@ abstract public class PhoenixAbstractAggregate extends Aggregate implements Phoe if (isSingleValueCheckAggregate(this)) return planner.getCostFactory().makeInfiniteCost(); - double orderedGroupByFactor = isOrderedGroupBy ? 0.8 : 1.0; + double orderedGroupByFactor = isOrderedGroupBy ? 0.5 : 1.0; return super.computeSelfCost(planner).multiplyBy(orderedGroupByFactor); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java index c62c328..49b8cdf 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientAggregate.java @@ -29,7 +29,7 @@ public class PhoenixClientAggregate extends PhoenixAbstractAggregate { ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) { RelOptCluster cluster = input.getCluster(); - RelTraitSet traits = cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION); + RelTraitSet traits = cluster.traitSetOf(PhoenixConvention.CLIENT); return new PhoenixClientAggregate(cluster, traits, input, indicator, groupSet, groupSets, aggCalls); } @@ -49,7 +49,7 @@ public class PhoenixClientAggregate extends PhoenixAbstractAggregate { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.CLIENT)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java index 9eba164..d51e9e8 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientJoin.java @@ -46,7 +46,7 @@ public class PhoenixClientJoin extends PhoenixAbstractJoin { RelOptCluster cluster = left.getCluster(); final JoinInfo joinInfo = JoinInfo.of(left, right, condition); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -77,8 +77,8 @@ public class PhoenixClientJoin extends PhoenixAbstractJoin { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getLeft().getConvention() != PhoenixRel.CLIENT_CONVENTION - || getRight().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getLeft().getConvention().satisfies(PhoenixConvention.GENERIC) + || !getRight().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); if (joinType == JoinRelType.RIGHT http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java index e170553..94552ab 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientProject.java @@ -24,7 +24,7 @@ public class PhoenixClientProject extends PhoenixAbstractProject { final List<? extends RexNode> projects, RelDataType rowType) { RelOptCluster cluster = input.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -47,7 +47,8 @@ public class PhoenixClientProject extends PhoenixAbstractProject { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.SERVERJOIN) + && !getInput().getConvention().satisfies(PhoenixConvention.CLIENT)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java index 0611a8c..401a3b7 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSemiJoin.java @@ -38,7 +38,7 @@ public class PhoenixClientSemiJoin extends PhoenixAbstractSemiJoin implements RelOptCluster cluster = left.getCluster(); final JoinInfo joinInfo = JoinInfo.of(left, right, condition); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -64,8 +64,8 @@ public class PhoenixClientSemiJoin extends PhoenixAbstractSemiJoin implements @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getLeft().getConvention() != PhoenixRel.CLIENT_CONVENTION - || getRight().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getLeft().getConvention().satisfies(PhoenixConvention.GENERIC) + || !getRight().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); if ((!leftKeys.isEmpty() && !RelCollations.contains(RelMetadataQuery.collations(getLeft()), leftKeys)) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java index f5a65df..bbc2005 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixClientSort.java @@ -27,7 +27,7 @@ public class PhoenixClientSort extends PhoenixAbstractSort { RelOptCluster cluster = input.getCluster(); collation = RelCollationTraitDef.INSTANCE.canonize(collation); RelTraitSet traits = - input.getTraitSet().replace(PhoenixRel.CLIENT_CONVENTION).replace(collation); + input.getTraitSet().replace(PhoenixConvention.CLIENT).replace(collation); return new PhoenixClientSort(cluster, traits, input, collation); } @@ -44,7 +44,7 @@ public class PhoenixClientSort extends PhoenixAbstractSort { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.CLIENT)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java index 15372bd..8105f88 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCompactClientSort.java @@ -21,7 +21,7 @@ public class PhoenixCompactClientSort extends PhoenixAbstractSort { RelOptCluster cluster = input.getCluster(); collation = RelCollationTraitDef.INSTANCE.canonize(collation); RelTraitSet traits = - input.getTraitSet().replace(PhoenixRel.CLIENT_CONVENTION).replace(collation); + input.getTraitSet().replace(PhoenixConvention.CLIENT).replace(collation); return new PhoenixCompactClientSort(cluster, traits, input, collation); } @@ -38,7 +38,7 @@ public class PhoenixCompactClientSort extends PhoenixAbstractSort { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.CLIENT)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixConvention.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixConvention.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixConvention.java new file mode 100644 index 0000000..5006f43 --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixConvention.java @@ -0,0 +1,47 @@ +package org.apache.phoenix.calcite.rel; + +import org.apache.calcite.plan.Convention; +import org.apache.calcite.plan.ConventionTraitDef; +import org.apache.calcite.plan.RelOptPlanner; +import org.apache.calcite.plan.RelTrait; +import org.apache.calcite.plan.RelTraitDef; + +public enum PhoenixConvention implements Convention { + + /** Generic convention*/ + GENERIC, + + /** Server convention*/ + SERVER, + + /** Server join convention*/ + SERVERJOIN, + + /** Client convention*/ + CLIENT; + + @Override + public RelTraitDef<?> getTraitDef() { + return ConventionTraitDef.INSTANCE; + } + + @Override + public boolean satisfies(RelTrait trait) { + return this == trait || trait == GENERIC; + } + + @Override + public void register(RelOptPlanner planner) { + } + + @Override + public Class<PhoenixRel> getInterface() { + return PhoenixRel.class; + } + + @Override + public String getName() { + return "PHOENIX_" + this.name(); + } + +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java index 6518700..ae868d3 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixCorrelate.java @@ -33,7 +33,7 @@ public class PhoenixCorrelate extends Correlate implements PhoenixRel { final SemiJoinType joinType) { RelOptCluster cluster = left.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -60,9 +60,9 @@ public class PhoenixCorrelate extends Correlate implements PhoenixRel { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getLeft().getConvention() != PhoenixRel.CLIENT_CONVENTION - || getRight().getConvention() != PhoenixRel.CLIENT_CONVENTION) - return planner.getCostFactory().makeInfiniteCost(); + if (!getLeft().getConvention().satisfies(PhoenixConvention.GENERIC) + || !getRight().getConvention().satisfies(PhoenixConvention.GENERIC)) + return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner).multiplyBy(PHOENIX_FACTOR); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java index a070d35..796ea00 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixFilter.java @@ -29,7 +29,7 @@ public class PhoenixFilter extends Filter implements PhoenixRel { public static PhoenixFilter create(final RelNode input, final RexNode condition) { RelOptCluster cluster = input.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -49,7 +49,7 @@ public class PhoenixFilter extends Filter implements PhoenixRel { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner).multiplyBy(PHOENIX_FACTOR); http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java index fc7637e..4497919 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixLimit.java @@ -29,7 +29,7 @@ public class PhoenixLimit extends SingleRel implements PhoenixRel { public static PhoenixLimit create(final RelNode input, RexNode offset, RexNode fetch) { RelOptCluster cluster = input.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -64,7 +64,7 @@ public class PhoenixLimit extends SingleRel implements PhoenixRel { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); double rowCount = RelMetadataQuery.getRowCount(this); http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java index 05b90ad..e23c03e 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixMergeSortUnion.java @@ -30,7 +30,7 @@ public class PhoenixMergeSortUnion extends Union implements PhoenixRel { final boolean all, final RelCollation collation) { RelOptCluster cluster = inputs.get(0).getCluster(); RelTraitSet traits = - cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSetOf(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -53,7 +53,7 @@ public class PhoenixMergeSortUnion extends Union implements PhoenixRel { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { for (RelNode input : getInputs()) { - if (input.getConvention() != PhoenixRel.CLIENT_CONVENTION + if (!input.getConvention().satisfies(PhoenixConvention.GENERIC) || !RelMetadataQuery.collations(input).contains(collation)) { return planner.getCostFactory().makeInfiniteCost(); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java index 7d269a8..f5e04be 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixRel.java @@ -2,7 +2,6 @@ package org.apache.phoenix.calcite.rel; import java.util.List; -import org.apache.calcite.plan.Convention; import org.apache.calcite.rel.RelNode; import org.apache.calcite.rel.metadata.RelMetadataProvider; import org.apache.calcite.util.ImmutableIntList; @@ -24,11 +23,7 @@ import org.apache.phoenix.schema.types.PDataType; * over streams of {@link org.apache.phoenix.schema.tuple.Tuple}s.</p> */ public interface PhoenixRel extends RelNode { - /** Calling conventions for relational operations that occur in Phoenix. */ - Convention SERVER_CONVENTION = new Convention.Impl("PHOENIX_SERVER", PhoenixRel.class); - Convention SERVERJOIN_CONVENTION = new Convention.Impl("PHOENIX_SERVERJOIN", PhoenixRel.class); - Convention CLIENT_CONVENTION = new Convention.Impl("PHOENIX_CLIENT", PhoenixRel.class); - + /** Metadata Provider for PhoenixRel */ RelMetadataProvider METADATA_PROVIDER = new PhoenixRelMetadataProvider(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java index d208bef..2eb0d35 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerAggregate.java @@ -25,7 +25,7 @@ public class PhoenixServerAggregate extends PhoenixAbstractAggregate { ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) { RelOptCluster cluster = input.getCluster(); - RelTraitSet traits = cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION); + RelTraitSet traits = cluster.traitSetOf(PhoenixConvention.CLIENT); return new PhoenixServerAggregate(cluster, traits, input, indicator, groupSet, groupSets, aggCalls); } @@ -43,8 +43,8 @@ public class PhoenixServerAggregate extends PhoenixAbstractAggregate { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.SERVER_CONVENTION - && getInput().getConvention() != PhoenixRel.SERVERJOIN_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.SERVER) + && !getInput().getConvention().satisfies(PhoenixConvention.SERVERJOIN)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java index 5419374..dcebb5d 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerJoin.java @@ -40,7 +40,7 @@ public class PhoenixServerJoin extends PhoenixAbstractJoin { Set<String> variablesStopped, boolean isSingleValueRhs) { RelOptCluster cluster = left.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.SERVERJOIN_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.SERVERJOIN) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -72,8 +72,8 @@ public class PhoenixServerJoin extends PhoenixAbstractJoin { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getLeft().getConvention() != PhoenixRel.SERVER_CONVENTION - || getRight().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getLeft().getConvention().satisfies(PhoenixConvention.SERVER) + || !getRight().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); if (joinType == JoinRelType.FULL || joinType == JoinRelType.RIGHT) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java index daa48f7..fbda9c6 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerProject.java @@ -24,7 +24,7 @@ public class PhoenixServerProject extends PhoenixAbstractProject { final List<? extends RexNode> projects, RelDataType rowType) { RelOptCluster cluster = input.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.SERVER_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.SERVER) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -47,7 +47,7 @@ public class PhoenixServerProject extends PhoenixAbstractProject { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.SERVER_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.SERVER)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java index b9a3576..928033b 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSemiJoin.java @@ -36,7 +36,7 @@ public class PhoenixServerSemiJoin extends PhoenixAbstractSemiJoin { final RelNode left, final RelNode right, RexNode condition) { RelOptCluster cluster = left.getCluster(); final RelTraitSet traits = - cluster.traitSet().replace(PhoenixRel.SERVERJOIN_CONVENTION) + cluster.traitSet().replace(PhoenixConvention.SERVERJOIN) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { @@ -64,8 +64,8 @@ public class PhoenixServerSemiJoin extends PhoenixAbstractSemiJoin { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getLeft().getConvention() != PhoenixRel.SERVER_CONVENTION - || getRight().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getLeft().getConvention().satisfies(PhoenixConvention.SERVER) + || !getRight().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); //TODO return infinite cost if RHS size exceeds memory limit. http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java index b43754c..783ac87 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixServerSort.java @@ -21,7 +21,7 @@ public class PhoenixServerSort extends PhoenixAbstractSort { RelOptCluster cluster = input.getCluster(); collation = RelCollationTraitDef.INSTANCE.canonize(collation); RelTraitSet traits = - input.getTraitSet().replace(PhoenixRel.CLIENT_CONVENTION).replace(collation); + input.getTraitSet().replace(PhoenixConvention.CLIENT).replace(collation); return new PhoenixServerSort(cluster, traits, input, collation); } @@ -38,8 +38,8 @@ public class PhoenixServerSort extends PhoenixAbstractSort { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.SERVER_CONVENTION - && getInput().getConvention() != PhoenixRel.SERVERJOIN_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.SERVER) + && !getInput().getConvention().satisfies(PhoenixConvention.SERVERJOIN)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner) http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java index 567ddc0..1933ccb 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTableScan.java @@ -39,6 +39,8 @@ import org.apache.phoenix.expression.LiteralExpression; import org.apache.phoenix.iterate.ParallelIteratorFactory; import org.apache.phoenix.jdbc.PhoenixStatement; import org.apache.phoenix.parse.SelectStatement; +import org.apache.phoenix.query.QueryServices; +import org.apache.phoenix.query.QueryServicesOptions; import org.apache.phoenix.schema.ColumnRef; import org.apache.phoenix.schema.KeyValueSchema.KeyValueSchemaBuilder; import org.apache.phoenix.schema.PColumn; @@ -46,36 +48,53 @@ import org.apache.phoenix.schema.PName; import org.apache.phoenix.schema.PTable; import org.apache.phoenix.schema.TableRef; import org.apache.phoenix.schema.types.PDataType; -import org.apache.phoenix.util.ScanUtil; import org.apache.phoenix.util.SchemaUtil; import com.google.common.base.Supplier; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; import com.google.common.collect.Lists; /** * Scan of a Phoenix table. */ public class PhoenixTableScan extends TableScan implements PhoenixRel { + public enum ScanOrder { + NONE, + FORWARD, + REVERSE, + } + public final RexNode filter; + public final ScanOrder scanOrder; + public final ScanRanges scanRanges; - private final ScanRanges scanRanges; - + public static PhoenixTableScan create(RelOptCluster cluster, final RelOptTable table) { + return create(cluster, table, null, + getDefaultScanOrder(table.unwrap(PhoenixTable.class))); + } + public static PhoenixTableScan create(RelOptCluster cluster, final RelOptTable table, - RexNode filter) { + RexNode filter, final ScanOrder scanOrder) { final RelTraitSet traits = - cluster.traitSetOf(PhoenixRel.SERVER_CONVENTION) + cluster.traitSetOf(PhoenixConvention.SERVER) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { - return table.unwrap(PhoenixTable.class).getStatistic().getCollations(); + if (scanOrder == ScanOrder.NONE) { + return ImmutableList.of(); + } + List<RelCollation> collations = table.getCollationList(); + return scanOrder == ScanOrder.FORWARD ? collations : reverse(collations); } }); - return new PhoenixTableScan(cluster, traits, table, filter); + return new PhoenixTableScan(cluster, traits, table, filter, scanOrder); } - private PhoenixTableScan(RelOptCluster cluster, RelTraitSet traits, RelOptTable table, RexNode filter) { + private PhoenixTableScan(RelOptCluster cluster, RelTraitSet traits, RelOptTable table, RexNode filter, ScanOrder scanOrder) { super(cluster, traits, table); this.filter = filter; + this.scanOrder = scanOrder; ScanRanges scanRanges = null; if (filter != null) { @@ -116,6 +135,30 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { } this.scanRanges = scanRanges; } + + private static ScanOrder getDefaultScanOrder(PhoenixTable table) { + //TODO why attribute value not correct in connectUsingModel?? + //return table.pc.getQueryServices().getProps().getBoolean( + // QueryServices.FORCE_ROW_KEY_ORDER_ATTRIB, + // QueryServicesOptions.DEFAULT_FORCE_ROW_KEY_ORDER) ? + // ScanOrder.FORWARD : ScanOrder.NONE; + return ScanOrder.NONE; + } + + private static List<RelCollation> reverse(List<RelCollation> collations) { + Builder<RelCollation> builder = ImmutableList.<RelCollation>builder(); + for (RelCollation collation : collations) { + builder.add(CalciteUtils.reverseCollation(collation)); + } + return builder.build(); + } + + public boolean isReverseScanEnabled() { + return table.unwrap(PhoenixTable.class).pc + .getQueryServices().getProps().getBoolean( + QueryServices.USE_REVERSE_SCAN_ATTRIB, + QueryServicesOptions.DEFAULT_USE_REVERSE_SCAN); + } @Override public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) { @@ -126,7 +169,8 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { @Override public RelWriter explainTerms(RelWriter pw) { return super.explainTerms(pw) - .itemIf("filter", filter, filter != null); + .itemIf("filter", filter, filter != null) + .itemIf("scanOrder", scanOrder, scanOrder != ScanOrder.NONE); } @Override @@ -146,7 +190,7 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { } else if (table.unwrap(PhoenixTable.class).getTable().getParentName() != null){ rowCount = addEpsilon(rowCount); } - if (requireRowKeyOrder()) { + if (scanOrder != ScanOrder.NONE) { // We don't want to make a big difference here. The idea is to avoid // forcing row key order whenever the order is absolutely useless. // E.g. in "select count(*) from t" we do not need the row key order; @@ -158,6 +202,9 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { // eventually be an AggregatePlan, in which the "forceRowKeyOrder" // flag takes no effect. rowCount = addEpsilon(rowCount); + if (scanOrder == ScanOrder.REVERSE) { + rowCount = addEpsilon(rowCount); + } } int fieldCount = this.table.getRowType().getFieldCount(); return planner.getCostFactory() @@ -174,6 +221,11 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { return rows; } + + @Override + public List<RelCollation> getCollationList() { + return getTraitSet().getTraits(RelCollationTraitDef.INSTANCE); + } @Override public QueryPlan implement(Implementor implementor) { @@ -210,11 +262,12 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { implementor.setTableRef(new TableRef(projectedTable)); } Integer limit = null; - OrderBy orderBy = OrderBy.EMPTY_ORDER_BY; + OrderBy orderBy = scanOrder == ScanOrder.NONE ? + OrderBy.EMPTY_ORDER_BY + : (scanOrder == ScanOrder.FORWARD ? + OrderBy.FWD_ROW_KEY_ORDER_BY + : OrderBy.REV_ROW_KEY_ORDER_BY); ParallelIteratorFactory iteratorFactory = null; - if (requireRowKeyOrder()) { - ScanUtil.setForceRowKeyOrder(context.getScan()); - } return new ScanPlan(context, select, tableRef, RowProjector.EMPTY_PROJECTOR, limit, orderBy, iteratorFactory, true, dynamicFilter); } catch (SQLException e) { throw new RuntimeException(e); @@ -247,10 +300,6 @@ public class PhoenixTableScan extends TableScan implements PhoenixRel { } } } - - private boolean requireRowKeyOrder() { - return table.unwrap(PhoenixTable.class).requireRowKeyOrder; - } private double addEpsilon(double d) { assert d >= 0d; http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTemporarySort.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTemporarySort.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTemporarySort.java new file mode 100644 index 0000000..5ab289a --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixTemporarySort.java @@ -0,0 +1,43 @@ +package org.apache.phoenix.calcite.rel; + +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptCost; +import org.apache.calcite.plan.RelOptPlanner; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.rel.RelCollation; +import org.apache.calcite.rel.RelCollationTraitDef; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rex.RexNode; +import org.apache.phoenix.compile.QueryPlan; + +public class PhoenixTemporarySort extends PhoenixAbstractSort { + + public static PhoenixTemporarySort create(RelNode input, RelCollation collation) { + RelOptCluster cluster = input.getCluster(); + collation = RelCollationTraitDef.INSTANCE.canonize(collation); + RelTraitSet traits = + input.getTraitSet().replace(PhoenixConvention.SERVER).replace(collation); + return new PhoenixTemporarySort(cluster, traits, input, collation); + } + + private PhoenixTemporarySort(RelOptCluster cluster, RelTraitSet traits, + RelNode child, RelCollation collation) { + super(cluster, traits, child, collation); + } + + @Override + public PhoenixTemporarySort copy(RelTraitSet traitSet, RelNode newInput, + RelCollation newCollation, RexNode offset, RexNode fetch) { + return create(newInput, newCollation); + } + + @Override + public RelOptCost computeSelfCost(RelOptPlanner planner) { + return planner.getCostFactory().makeInfiniteCost(); + } + + @Override + public QueryPlan implement(Implementor implementor) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixToClientConverter.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixToClientConverter.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixToClientConverter.java deleted file mode 100644 index f395787..0000000 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixToClientConverter.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.apache.phoenix.calcite.rel; - -import java.util.List; - -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelOptCost; -import org.apache.calcite.plan.RelOptPlanner; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.SingleRel; -import org.apache.phoenix.compile.QueryPlan; - -public class PhoenixToClientConverter extends SingleRel implements - PhoenixRel { - - public static PhoenixToClientConverter create(RelNode input) { - return new PhoenixToClientConverter( - input.getCluster(), - input.getTraitSet().replace(PhoenixRel.CLIENT_CONVENTION), - input); - } - - private PhoenixToClientConverter(RelOptCluster cluster, - RelTraitSet traits, RelNode input) { - super(cluster, traits, input); - } - - @Override - public PhoenixToClientConverter copy( - RelTraitSet traitSet, - List<RelNode> newInputs) { - return create(sole(newInputs)); - } - - @Override - public RelOptCost computeSelfCost(RelOptPlanner planner) { - return planner.getCostFactory().makeCost(0, 0, 0); - } - - @Override - public QueryPlan implement(Implementor implementor) { - return implementor.visitInput(0, (PhoenixRel) getInput()); - } - -} http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java index d39f3a6..71b7932 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUncollect.java @@ -21,7 +21,7 @@ public class PhoenixUncollect extends Uncollect implements PhoenixRel { public static PhoenixUncollect create(RelNode input) { RelOptCluster cluster = input.getCluster(); - RelTraitSet traits = cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION); + RelTraitSet traits = cluster.traitSetOf(PhoenixConvention.CLIENT); return new PhoenixUncollect(cluster, traits, input); } @@ -38,7 +38,7 @@ public class PhoenixUncollect extends Uncollect implements PhoenixRel { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { - if (getInput().getConvention() != PhoenixRel.CLIENT_CONVENTION) + if (!getInput().getConvention().satisfies(PhoenixConvention.GENERIC)) return planner.getCostFactory().makeInfiniteCost(); return super.computeSelfCost(planner).multiplyBy(PHOENIX_FACTOR); http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java index 8b0b616..8b9efb8 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixUnion.java @@ -25,7 +25,7 @@ public class PhoenixUnion extends Union implements PhoenixRel { public static PhoenixUnion create(List<RelNode> inputs, boolean all) { RelOptCluster cluster = inputs.get(0).getCluster(); - RelTraitSet traits = cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION); + RelTraitSet traits = cluster.traitSetOf(PhoenixConvention.CLIENT); return new PhoenixUnion(cluster, traits, inputs, all); } @@ -41,7 +41,7 @@ public class PhoenixUnion extends Union implements PhoenixRel { @Override public RelOptCost computeSelfCost(RelOptPlanner planner) { for (RelNode input : getInputs()) { - if (input.getConvention() != PhoenixRel.CLIENT_CONVENTION) { + if (!input.getConvention().satisfies(PhoenixConvention.GENERIC)) { return planner.getCostFactory().makeInfiniteCost(); } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java index 22284e6..e53be9b 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rel/PhoenixValues.java @@ -72,7 +72,7 @@ public class PhoenixValues extends Values implements PhoenixRel { public static PhoenixValues create(RelOptCluster cluster, final RelDataType rowType, final ImmutableList<ImmutableList<RexLiteral>> tuples) { final RelTraitSet traits = - cluster.traitSetOf(PhoenixRel.CLIENT_CONVENTION) + cluster.traitSetOf(PhoenixConvention.CLIENT) .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() { public List<RelCollation> get() { http://git-wip-us.apache.org/repos/asf/phoenix/blob/50e4406d/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixCompactClientSortRule.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixCompactClientSortRule.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixCompactClientSortRule.java index b0f3a9f..b7af899 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixCompactClientSortRule.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/rules/PhoenixCompactClientSortRule.java @@ -9,14 +9,13 @@ import org.apache.phoenix.calcite.rel.PhoenixServerAggregate; public class PhoenixCompactClientSortRule extends RelOptRule { - public static final PhoenixCompactClientSortRule SORT_SERVERAGGREGATE = - new PhoenixCompactClientSortRule("PhoenixCompactClientSortRule:sort_serveraggregate", PhoenixServerAggregate.class); + public static final PhoenixCompactClientSortRule INSTANCE = + new PhoenixCompactClientSortRule(); - public PhoenixCompactClientSortRule(String description, Class<? extends PhoenixRel> clazz) { + public PhoenixCompactClientSortRule() { super( operand(PhoenixClientSort.class, - operand(clazz, any())), - description); + operand(PhoenixServerAggregate.class, any()))); } @Override
