http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreterRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreterRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreterRule.java deleted file mode 100644 index 99996fa..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreterRule.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.interpreter.BindableConvention; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; - -/** - * Planner rule that converts {@link org.apache.calcite.interpreter.BindableRel} - * to {@link org.apache.calcite.adapter.enumerable.EnumerableRel} by creating - * an {@link org.apache.calcite.adapter.enumerable.EnumerableInterpreter}. - */ -public class EnumerableInterpreterRule extends ConverterRule { - public static final EnumerableInterpreterRule INSTANCE = - new EnumerableInterpreterRule(); - - private EnumerableInterpreterRule() { - super(RelNode.class, BindableConvention.INSTANCE, - EnumerableConvention.INSTANCE, "EnumerableInterpreterRule"); - } - - //~ Methods ---------------------------------------------------------------- - - @Override public RelNode convert(RelNode rel) { - return EnumerableInterpreter.create(rel, 0.5d); - } -} - -// End EnumerableInterpreterRule.java
http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersect.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersect.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersect.java deleted file mode 100644 index 8462f65..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersect.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.Ord; -import org.apache.calcite.linq4j.tree.BlockBuilder; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.Intersect; -import org.apache.calcite.util.BuiltInMethod; - -import java.util.List; - -/** Implementation of {@link org.apache.calcite.rel.core.Intersect} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableIntersect extends Intersect implements EnumerableRel { - public EnumerableIntersect(RelOptCluster cluster, RelTraitSet traitSet, - List<RelNode> inputs, boolean all) { - super(cluster, traitSet, inputs, all); - assert !all; - } - - public EnumerableIntersect copy(RelTraitSet traitSet, List<RelNode> inputs, - boolean all) { - return new EnumerableIntersect(getCluster(), traitSet, inputs, all); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - final BlockBuilder builder = new BlockBuilder(); - Expression intersectExp = null; - for (Ord<RelNode> ord : Ord.zip(inputs)) { - EnumerableRel input = (EnumerableRel) ord.e; - final Result result = implementor.visitChild(this, ord.i, input, pref); - Expression childExp = - builder.append( - "child" + ord.i, - result.block); - - if (intersectExp == null) { - intersectExp = childExp; - } else { - intersectExp = - Expressions.call(intersectExp, - BuiltInMethod.INTERSECT.method, - Expressions.list(childExp) - .appendIfNotNull(result.physType.comparer())); - } - - // Once the first input has chosen its format, ask for the same for - // other inputs. - pref = pref.of(result.format); - } - - builder.add(intersectExp); - final PhysType physType = - PhysTypeImpl.of( - implementor.getTypeFactory(), - getRowType(), - pref.prefer(JavaRowFormat.CUSTOM)); - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableIntersect.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersectRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersectRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersectRule.java deleted file mode 100644 index f906d47..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableIntersectRule.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.Convention; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.rel.logical.LogicalIntersect; - -/** - * Rule to convert a - * {@link org.apache.calcite.rel.logical.LogicalIntersect} to an - * {@link EnumerableIntersect}. - */ -class EnumerableIntersectRule extends ConverterRule { - EnumerableIntersectRule() { - super(LogicalIntersect.class, Convention.NONE, - EnumerableConvention.INSTANCE, "EnumerableIntersectRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalIntersect intersect = (LogicalIntersect) rel; - if (intersect.all) { - return null; // INTERSECT ALL not implemented - } - final EnumerableConvention out = EnumerableConvention.INSTANCE; - final RelTraitSet traitSet = intersect.getTraitSet().replace(out); - return new EnumerableIntersect(rel.getCluster(), traitSet, - convertList(intersect.getInputs(), out), false); - } -} - -// End EnumerableIntersectRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java deleted file mode 100644 index cfd00a4..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoin.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.BlockBuilder; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -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.InvalidRelException; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.RelNodes; -import org.apache.calcite.rel.core.CorrelationId; -import org.apache.calcite.rel.core.EquiJoin; -import org.apache.calcite.rel.core.JoinInfo; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.rel.metadata.RelMetadataQuery; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.util.BuiltInMethod; -import org.apache.calcite.util.ImmutableIntList; -import org.apache.calcite.util.Util; - -import com.google.common.collect.ImmutableList; - -import java.util.Set; - -/** Implementation of {@link org.apache.calcite.rel.core.Join} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableJoin extends EquiJoin implements EnumerableRel { - /** Creates an EnumerableJoin. - * - * <p>Use {@link #create} unless you know what you're doing. */ - protected EnumerableJoin( - RelOptCluster cluster, - RelTraitSet traits, - RelNode left, - RelNode right, - RexNode condition, - ImmutableIntList leftKeys, - ImmutableIntList rightKeys, - Set<CorrelationId> variablesSet, - JoinRelType joinType) - throws InvalidRelException { - super( - cluster, - traits, - left, - right, - condition, - leftKeys, - rightKeys, - variablesSet, - joinType); - } - - @Deprecated // to be removed before 2.0 - protected EnumerableJoin(RelOptCluster cluster, RelTraitSet traits, - RelNode left, RelNode right, RexNode condition, ImmutableIntList leftKeys, - ImmutableIntList rightKeys, JoinRelType joinType, - Set<String> variablesStopped) throws InvalidRelException { - this(cluster, traits, left, right, condition, leftKeys, rightKeys, - CorrelationId.setOf(variablesStopped), joinType); - } - - /** Creates an EnumerableJoin. */ - public static EnumerableJoin create( - RelNode left, - RelNode right, - RexNode condition, - ImmutableIntList leftKeys, - ImmutableIntList rightKeys, - Set<CorrelationId> variablesSet, - JoinRelType joinType) - throws InvalidRelException { - final RelOptCluster cluster = left.getCluster(); - final RelTraitSet traitSet = - cluster.traitSetOf(EnumerableConvention.INSTANCE); - return new EnumerableJoin(cluster, traitSet, left, right, condition, - leftKeys, rightKeys, variablesSet, joinType); - } - - @Deprecated // to be removed before 2.0 - public static EnumerableJoin create( - RelNode left, - RelNode right, - RexNode condition, - ImmutableIntList leftKeys, - ImmutableIntList rightKeys, - JoinRelType joinType, - Set<String> variablesStopped) - throws InvalidRelException { - return create(left, right, condition, leftKeys, rightKeys, - CorrelationId.setOf(variablesStopped), joinType); - } - - @Override public EnumerableJoin copy(RelTraitSet traitSet, RexNode condition, - RelNode left, RelNode right, JoinRelType joinType, - boolean semiJoinDone) { - final JoinInfo joinInfo = JoinInfo.of(left, right, condition); - assert joinInfo.isEqui(); - try { - return new EnumerableJoin(getCluster(), traitSet, left, right, - condition, joinInfo.leftKeys, joinInfo.rightKeys, variablesSet, - joinType); - } catch (InvalidRelException e) { - // Semantic error not possible. Must be a bug. Convert to - // internal error. - throw new AssertionError(e); - } - } - - @Override public RelOptCost computeSelfCost(RelOptPlanner planner, - RelMetadataQuery mq) { - double rowCount = mq.getRowCount(this); - - // Joins can be flipped, and for many algorithms, both versions are viable - // and have the same cost. To make the results stable between versions of - // the planner, make one of the versions slightly more expensive. - switch (joinType) { - case RIGHT: - rowCount = addEpsilon(rowCount); - break; - default: - if (RelNodes.COMPARATOR.compare(left, right) > 0) { - rowCount = addEpsilon(rowCount); - } - } - - // Cheaper if the smaller number of rows is coming from the LHS. - // Model this by adding L log L to the cost. - final double rightRowCount = right.estimateRowCount(mq); - final double leftRowCount = left.estimateRowCount(mq); - if (Double.isInfinite(leftRowCount)) { - rowCount = leftRowCount; - } else { - rowCount += Util.nLogN(leftRowCount); - } - if (Double.isInfinite(rightRowCount)) { - rowCount = rightRowCount; - } else { - rowCount += rightRowCount; - } - return planner.getCostFactory().makeCost(rowCount, 0, 0); - } - - private double addEpsilon(double d) { - assert d >= 0d; - final double d0 = d; - if (d < 10) { - // For small d, adding 1 would change the value significantly. - d *= 1.001d; - if (d != d0) { - return d; - } - } - // For medium d, add 1. Keeps integral values integral. - ++d; - if (d != d0) { - return d; - } - // For large d, adding 1 might not change the value. Add .1%. - // If d is NaN, this still will probably not change the value. That's OK. - d *= 1.001d; - return d; - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - BlockBuilder builder = new BlockBuilder(); - final Result leftResult = - implementor.visitChild(this, 0, (EnumerableRel) left, pref); - Expression leftExpression = - builder.append( - "left", leftResult.block); - final Result rightResult = - implementor.visitChild(this, 1, (EnumerableRel) right, pref); - Expression rightExpression = - builder.append( - "right", rightResult.block); - final PhysType physType = - PhysTypeImpl.of( - implementor.getTypeFactory(), getRowType(), pref.preferArray()); - final PhysType keyPhysType = - leftResult.physType.project( - leftKeys, JavaRowFormat.LIST); - return implementor.result( - physType, - builder.append( - Expressions.call( - leftExpression, - BuiltInMethod.JOIN.method, - Expressions.list( - rightExpression, - leftResult.physType.generateAccessor(leftKeys), - rightResult.physType.generateAccessor(rightKeys), - EnumUtils.joinSelector(joinType, - physType, - ImmutableList.of( - leftResult.physType, rightResult.physType))) - .append( - Util.first(keyPhysType.comparer(), - Expressions.constant(null))) - .append( - Expressions.constant(joinType.generatesNullsOnLeft())) - .append( - Expressions.constant( - joinType.generatesNullsOnRight())))).toBlock()); - } - -} - -// End EnumerableJoin.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoinRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoinRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoinRule.java deleted file mode 100644 index 375568b..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableJoinRule.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.Convention; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.InvalidRelException; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.rel.core.JoinInfo; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.rel.logical.LogicalJoin; - -import java.util.ArrayList; -import java.util.List; - -/** Planner rule that converts a - * {@link org.apache.calcite.rel.logical.LogicalJoin} relational expression - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -class EnumerableJoinRule extends ConverterRule { - EnumerableJoinRule() { - super( - LogicalJoin.class, - Convention.NONE, - EnumerableConvention.INSTANCE, - "EnumerableJoinRule"); - } - - @Override public RelNode convert(RelNode rel) { - LogicalJoin join = (LogicalJoin) rel; - List<RelNode> newInputs = new ArrayList<>(); - for (RelNode input : join.getInputs()) { - if (!(input.getConvention() instanceof EnumerableConvention)) { - input = - convert( - input, - input.getTraitSet() - .replace(EnumerableConvention.INSTANCE)); - } - newInputs.add(input); - } - final RelOptCluster cluster = join.getCluster(); - final RelTraitSet traitSet = - join.getTraitSet().replace(EnumerableConvention.INSTANCE); - final RelNode left = newInputs.get(0); - final RelNode right = newInputs.get(1); - final JoinInfo info = JoinInfo.of(left, right, join.getCondition()); - if (!info.isEqui() && join.getJoinType() != JoinRelType.INNER) { - // EnumerableJoinRel only supports equi-join. We can put a filter on top - // if it is an inner join. - try { - return new EnumerableThetaJoin(cluster, traitSet, left, right, - join.getCondition(), join.getVariablesSet(), join.getJoinType()); - } catch (InvalidRelException e) { - EnumerableRules.LOGGER.debug(e.toString()); - return null; - } - } - RelNode newRel; - try { - newRel = new EnumerableJoin( - cluster, - join.getTraitSet().replace(EnumerableConvention.INSTANCE), - left, - right, - info.getEquiCondition(left, right, cluster.getRexBuilder()), - info.leftKeys, - info.rightKeys, - join.getVariablesSet(), - join.getJoinType()); - } catch (InvalidRelException e) { - EnumerableRules.LOGGER.debug(e.toString()); - return null; - } - if (!info.isEqui()) { - newRel = new EnumerableFilter(cluster, newRel.getTraitSet(), - newRel, info.getRemaining(cluster.getRexBuilder())); - } - return newRel; - } -} - -// End EnumerableJoinRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java deleted file mode 100644 index 827944f..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimit.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.BlockBuilder; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelCollation; -import org.apache.calcite.rel.RelCollationTraitDef; -import org.apache.calcite.rel.RelDistribution; -import org.apache.calcite.rel.RelDistributionTraitDef; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.RelWriter; -import org.apache.calcite.rel.SingleRel; -import org.apache.calcite.rel.metadata.RelMdCollation; -import org.apache.calcite.rel.metadata.RelMdDistribution; -import org.apache.calcite.rel.metadata.RelMetadataQuery; -import org.apache.calcite.rex.RexLiteral; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.util.BuiltInMethod; - -import com.google.common.base.Supplier; - -import java.util.List; - -/** Relational expression that applies a limit and/or offset to its input. */ -public class EnumerableLimit extends SingleRel implements EnumerableRel { - public final RexNode offset; - public final RexNode fetch; - - /** Creates an EnumerableLimit. - * - * <p>Use {@link #create} unless you know what you're doing. */ - public EnumerableLimit( - RelOptCluster cluster, - RelTraitSet traitSet, - RelNode input, - RexNode offset, - RexNode fetch) { - super(cluster, traitSet, input); - this.offset = offset; - this.fetch = fetch; - assert getConvention() instanceof EnumerableConvention; - assert getConvention() == input.getConvention(); - } - - /** Creates an EnumerableLimit. */ - public static EnumerableLimit create(final RelNode input, RexNode offset, - RexNode fetch) { - final RelOptCluster cluster = input.getCluster(); - final RelMetadataQuery mq = RelMetadataQuery.instance(); - final RelTraitSet traitSet = - cluster.traitSetOf(EnumerableConvention.INSTANCE) - .replaceIfs( - RelCollationTraitDef.INSTANCE, - new Supplier<List<RelCollation>>() { - public List<RelCollation> get() { - return RelMdCollation.limit(mq, input); - } - }) - .replaceIf(RelDistributionTraitDef.INSTANCE, - new Supplier<RelDistribution>() { - public RelDistribution get() { - return RelMdDistribution.limit(mq, input); - } - }); - return new EnumerableLimit(cluster, traitSet, input, offset, fetch); - } - - @Override public EnumerableLimit copy( - RelTraitSet traitSet, - List<RelNode> newInputs) { - return new EnumerableLimit( - getCluster(), - traitSet, - sole(newInputs), - offset, - fetch); - } - - @Override public RelWriter explainTerms(RelWriter pw) { - return super.explainTerms(pw) - .itemIf("offset", offset, offset != null) - .itemIf("fetch", fetch, fetch != null); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - final BlockBuilder builder = new BlockBuilder(); - final EnumerableRel child = (EnumerableRel) getInput(); - final Result result = implementor.visitChild(this, 0, child, pref); - final PhysType physType = - PhysTypeImpl.of( - implementor.getTypeFactory(), - getRowType(), - result.format); - - Expression v = builder.append("child", result.block); - if (offset != null) { - v = builder.append( - "offset", - Expressions.call( - v, - BuiltInMethod.SKIP.method, - Expressions.constant(RexLiteral.intValue(offset)))); - } - if (fetch != null) { - v = builder.append( - "fetch", - Expressions.call( - v, - BuiltInMethod.TAKE.method, - Expressions.constant(RexLiteral.intValue(fetch)))); - } - - builder.add( - Expressions.return_( - null, - v)); - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableLimit.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimitRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimitRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimitRule.java deleted file mode 100644 index 4f8ab10..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableLimitRule.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.RelOptRule; -import org.apache.calcite.plan.RelOptRuleCall; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.Sort; - -/** - * Rule to convert an {@link org.apache.calcite.rel.core.Sort} that has - * {@code offset} or {@code fetch} set to an - * {@link EnumerableLimit} - * on top of a "pure" {@code Sort} that has no offset or fetch. - */ -class EnumerableLimitRule extends RelOptRule { - EnumerableLimitRule() { - super( - operand(Sort.class, any()), - "EnumerableLimitRule"); - } - - @Override public void onMatch(RelOptRuleCall call) { - final Sort sort = call.rel(0); - if (sort.offset == null && sort.fetch == null) { - return; - } - final RelTraitSet traitSet = - sort.getTraitSet().replace(EnumerableConvention.INSTANCE); - RelNode input = sort.getInput(); - if (!sort.getCollation().getFieldCollations().isEmpty()) { - // Create a sort with the same sort key, but no offset or fetch. - input = sort.copy( - sort.getTraitSet(), - input, - sort.getCollation(), - null, - null); - } - RelNode x = convert( - input, - input.getTraitSet().replace(EnumerableConvention.INSTANCE)); - call.transformTo( - new EnumerableLimit( - sort.getCluster(), - traitSet, - x, - sort.offset, - sort.fetch)); - } -} - -// End EnumerableLimitRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoin.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoin.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoin.java deleted file mode 100644 index 95d6239..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoin.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.adapter.java.JavaTypeFactory; -import org.apache.calcite.linq4j.tree.BlockBuilder; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.linq4j.tree.ParameterExpression; -import org.apache.calcite.linq4j.tree.Types; -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.InvalidRelException; -import org.apache.calcite.rel.RelCollation; -import org.apache.calcite.rel.RelCollationTraitDef; -import org.apache.calcite.rel.RelCollations; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.CorrelationId; -import org.apache.calcite.rel.core.EquiJoin; -import org.apache.calcite.rel.core.JoinInfo; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.rel.metadata.RelMdCollation; -import org.apache.calcite.rel.metadata.RelMetadataQuery; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rex.RexLiteral; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.util.BuiltInMethod; -import org.apache.calcite.util.ImmutableIntList; -import org.apache.calcite.util.Pair; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -/** Implementation of {@link org.apache.calcite.rel.core.Join} in - * {@link EnumerableConvention enumerable calling convention} using - * a merge algorithm. */ -public class EnumerableMergeJoin extends EquiJoin implements EnumerableRel { - EnumerableMergeJoin( - RelOptCluster cluster, - RelTraitSet traits, - RelNode left, - RelNode right, - RexNode condition, - ImmutableIntList leftKeys, - ImmutableIntList rightKeys, - Set<CorrelationId> variablesSet, - JoinRelType joinType) - throws InvalidRelException { - super(cluster, traits, left, right, condition, leftKeys, rightKeys, - variablesSet, joinType); - final List<RelCollation> collations = - traits.getTraits(RelCollationTraitDef.INSTANCE); - assert collations == null || RelCollations.contains(collations, leftKeys); - } - - @Deprecated // to be removed before 2.0 - EnumerableMergeJoin(RelOptCluster cluster, RelTraitSet traits, RelNode left, - RelNode right, RexNode condition, ImmutableIntList leftKeys, - ImmutableIntList rightKeys, JoinRelType joinType, - Set<String> variablesStopped) throws InvalidRelException { - this(cluster, traits, left, right, condition, leftKeys, rightKeys, - CorrelationId.setOf(variablesStopped), joinType); - } - - public static EnumerableMergeJoin create(RelNode left, RelNode right, - RexLiteral condition, ImmutableIntList leftKeys, - ImmutableIntList rightKeys, JoinRelType joinType) - throws InvalidRelException { - final RelOptCluster cluster = right.getCluster(); - RelTraitSet traitSet = cluster.traitSet(); - if (traitSet.isEnabled(RelCollationTraitDef.INSTANCE)) { - final RelMetadataQuery mq = RelMetadataQuery.instance(); - final List<RelCollation> collations = - RelMdCollation.mergeJoin(mq, left, right, leftKeys, rightKeys); - traitSet = traitSet.replace(collations); - } - return new EnumerableMergeJoin(cluster, traitSet, left, right, condition, - leftKeys, rightKeys, ImmutableSet.<CorrelationId>of(), joinType); - } - - @Override public EnumerableMergeJoin copy(RelTraitSet traitSet, - RexNode condition, RelNode left, RelNode right, JoinRelType joinType, - boolean semiJoinDone) { - final JoinInfo joinInfo = JoinInfo.of(left, right, condition); - assert joinInfo.isEqui(); - try { - return new EnumerableMergeJoin(getCluster(), traitSet, left, right, - condition, joinInfo.leftKeys, joinInfo.rightKeys, variablesSet, - joinType); - } catch (InvalidRelException e) { - // Semantic error not possible. Must be a bug. Convert to - // internal error. - throw new AssertionError(e); - } - } - - @Override public RelOptCost computeSelfCost(RelOptPlanner planner, - RelMetadataQuery mq) { - // We assume that the inputs are sorted. The price of sorting them has - // already been paid. The cost of the join is therefore proportional to the - // input and output size. - final double rightRowCount = right.estimateRowCount(mq); - final double leftRowCount = left.estimateRowCount(mq); - final double rowCount = mq.getRowCount(this); - final double d = leftRowCount + rightRowCount + rowCount; - return planner.getCostFactory().makeCost(d, 0, 0); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - BlockBuilder builder = new BlockBuilder(); - final Result leftResult = - implementor.visitChild(this, 0, (EnumerableRel) left, pref); - final Expression leftExpression = - builder.append("left", leftResult.block); - final ParameterExpression left_ = - Expressions.parameter(leftResult.physType.getJavaRowType(), "left"); - final Result rightResult = - implementor.visitChild(this, 1, (EnumerableRel) right, pref); - final Expression rightExpression = - builder.append("right", rightResult.block); - final ParameterExpression right_ = - Expressions.parameter(rightResult.physType.getJavaRowType(), "right"); - final JavaTypeFactory typeFactory = implementor.getTypeFactory(); - final PhysType physType = - PhysTypeImpl.of(typeFactory, getRowType(), pref.preferArray()); - final List<Expression> leftExpressions = new ArrayList<>(); - final List<Expression> rightExpressions = new ArrayList<>(); - for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) { - final RelDataType keyType = - typeFactory.leastRestrictive( - ImmutableList.of( - left.getRowType().getFieldList().get(pair.left).getType(), - right.getRowType().getFieldList().get(pair.right).getType())); - final Type keyClass = typeFactory.getJavaClass(keyType); - leftExpressions.add( - Types.castIfNecessary(keyClass, - leftResult.physType.fieldReference(left_, pair.left))); - rightExpressions.add( - Types.castIfNecessary(keyClass, - rightResult.physType.fieldReference(right_, pair.right))); - } - final PhysType leftKeyPhysType = - leftResult.physType.project(leftKeys, JavaRowFormat.LIST); - final PhysType rightKeyPhysType = - rightResult.physType.project(rightKeys, JavaRowFormat.LIST); - return implementor.result( - physType, - builder.append( - Expressions.call( - BuiltInMethod.MERGE_JOIN.method, - Expressions.list( - leftExpression, - rightExpression, - Expressions.lambda( - leftKeyPhysType.record(leftExpressions), left_), - Expressions.lambda( - rightKeyPhysType.record(rightExpressions), right_), - EnumUtils.joinSelector(joinType, - physType, - ImmutableList.of( - leftResult.physType, rightResult.physType)), - Expressions.constant( - joinType.generatesNullsOnLeft()), - Expressions.constant( - joinType.generatesNullsOnRight())))).toBlock()); - } -} - -// End EnumerableMergeJoin.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoinRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoinRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoinRule.java deleted file mode 100644 index 222111c..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoinRule.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.Ord; -import org.apache.calcite.plan.Convention; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.InvalidRelException; -import org.apache.calcite.rel.RelCollation; -import org.apache.calcite.rel.RelCollations; -import org.apache.calcite.rel.RelFieldCollation; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.rel.core.JoinInfo; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.rel.logical.LogicalJoin; - -import com.google.common.collect.Lists; - -import java.util.List; - -/** Planner rule that converts a - * {@link org.apache.calcite.rel.logical.LogicalJoin} relational expression - * {@link EnumerableConvention enumerable calling convention}. - * - * @see org.apache.calcite.adapter.enumerable.EnumerableJoinRule - */ -class EnumerableMergeJoinRule extends ConverterRule { - EnumerableMergeJoinRule() { - super(LogicalJoin.class, - Convention.NONE, - EnumerableConvention.INSTANCE, - "EnumerableMergeJoinRule"); - } - - @Override public RelNode convert(RelNode rel) { - LogicalJoin join = (LogicalJoin) rel; - final JoinInfo info = - JoinInfo.of(join.getLeft(), join.getRight(), join.getCondition()); - if (join.getJoinType() != JoinRelType.INNER) { - // EnumerableMergeJoin only supports inner join. - // (It supports non-equi join, using a post-filter; see below.) - return null; - } - if (info.pairs().size() == 0) { - // EnumerableMergeJoin CAN support cartesian join, but disable it for now. - return null; - } - final List<RelNode> newInputs = Lists.newArrayList(); - final List<RelCollation> collations = Lists.newArrayList(); - int offset = 0; - for (Ord<RelNode> ord : Ord.zip(join.getInputs())) { - RelTraitSet traits = ord.e.getTraitSet() - .replace(EnumerableConvention.INSTANCE); - if (!info.pairs().isEmpty()) { - final List<RelFieldCollation> fieldCollations = Lists.newArrayList(); - for (int key : info.keys().get(ord.i)) { - fieldCollations.add( - new RelFieldCollation(key, - RelFieldCollation.Direction.ASCENDING, - RelFieldCollation.NullDirection.LAST)); - } - final RelCollation collation = RelCollations.of(fieldCollations); - collations.add(RelCollations.shift(collation, offset)); - traits = traits.replace(collation); - } - newInputs.add(convert(ord.e, traits)); - offset += ord.e.getRowType().getFieldCount(); - } - final RelNode left = newInputs.get(0); - final RelNode right = newInputs.get(1); - final RelOptCluster cluster = join.getCluster(); - RelNode newRel; - try { - RelTraitSet traits = join.getTraitSet() - .replace(EnumerableConvention.INSTANCE); - if (!collations.isEmpty()) { - traits = traits.replace(collations); - } - newRel = new EnumerableMergeJoin(cluster, - traits, - left, - right, - info.getEquiCondition(left, right, cluster.getRexBuilder()), - info.leftKeys, - info.rightKeys, - join.getVariablesSet(), - join.getJoinType()); - } catch (InvalidRelException e) { - EnumerableRules.LOGGER.debug(e.toString()); - return null; - } - if (!info.isEqui()) { - newRel = new EnumerableFilter(cluster, newRel.getTraitSet(), - newRel, info.getRemaining(cluster.getRexBuilder())); - } - return newRel; - } -} - -// End EnumerableMergeJoinRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinus.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinus.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinus.java deleted file mode 100644 index 7ed4edb..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinus.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.Ord; -import org.apache.calcite.linq4j.tree.BlockBuilder; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.Minus; -import org.apache.calcite.util.BuiltInMethod; - -import java.util.List; - -/** Implementation of {@link org.apache.calcite.rel.core.Minus} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableMinus extends Minus implements EnumerableRel { - public EnumerableMinus(RelOptCluster cluster, RelTraitSet traitSet, - List<RelNode> inputs, boolean all) { - super(cluster, traitSet, inputs, all); - assert !all; - } - - public EnumerableMinus copy(RelTraitSet traitSet, List<RelNode> inputs, - boolean all) { - return new EnumerableMinus(getCluster(), traitSet, inputs, all); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - final BlockBuilder builder = new BlockBuilder(); - Expression minusExp = null; - for (Ord<RelNode> ord : Ord.zip(inputs)) { - EnumerableRel input = (EnumerableRel) ord.e; - final Result result = implementor.visitChild(this, ord.i, input, pref); - Expression childExp = - builder.append( - "child" + ord.i, - result.block); - - if (minusExp == null) { - minusExp = childExp; - } else { - minusExp = - Expressions.call(minusExp, - BuiltInMethod.EXCEPT.method, - Expressions.list(childExp) - .appendIfNotNull(result.physType.comparer())); - } - - // Once the first input has chosen its format, ask for the same for - // other inputs. - pref = pref.of(result.format); - } - - builder.add(minusExp); - final PhysType physType = - PhysTypeImpl.of( - implementor.getTypeFactory(), - getRowType(), - pref.prefer(JavaRowFormat.CUSTOM)); - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableMinus.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinusRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinusRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinusRule.java deleted file mode 100644 index 4cbf263..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMinusRule.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.Convention; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.rel.logical.LogicalMinus; - -/** - * Rule to convert an {@link org.apache.calcite.rel.logical.LogicalMinus} to an - * {@link EnumerableMinus}. - */ -class EnumerableMinusRule extends ConverterRule { - EnumerableMinusRule() { - super(LogicalMinus.class, Convention.NONE, EnumerableConvention.INSTANCE, - "EnumerableMinusRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalMinus minus = (LogicalMinus) rel; - if (minus.all) { - return null; // EXCEPT ALL not implemented - } - final EnumerableConvention out = EnumerableConvention.INSTANCE; - final RelTraitSet traitSet = - rel.getTraitSet().replace( - EnumerableConvention.INSTANCE); - return new EnumerableMinus(rel.getCluster(), traitSet, - convertList(minus.getInputs(), out), false); - } -} - -// End EnumerableMinusRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProject.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProject.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProject.java deleted file mode 100644 index fa2b48b..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProject.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.RelOptCluster; -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.rel.core.Project; -import org.apache.calcite.rel.metadata.RelMdCollation; -import org.apache.calcite.rel.metadata.RelMetadataQuery; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.util.Util; - -import com.google.common.base.Supplier; - -import java.util.List; - -/** Implementation of {@link org.apache.calcite.rel.core.Project} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableProject extends Project implements EnumerableRel { - /** - * Creates an EnumerableProject. - * - * <p>Use {@link #create} unless you know what you're doing. - * - * @param cluster Cluster this relational expression belongs to - * @param traitSet Traits of this relational expression - * @param input Input relational expression - * @param projects List of expressions for the input columns - * @param rowType Output row type - */ - public EnumerableProject( - RelOptCluster cluster, - RelTraitSet traitSet, - RelNode input, - List<? extends RexNode> projects, - RelDataType rowType) { - super(cluster, traitSet, input, projects, rowType); - assert getConvention() instanceof EnumerableConvention; - } - - @Deprecated // to be removed before 2.0 - public EnumerableProject(RelOptCluster cluster, RelTraitSet traitSet, - RelNode input, List<? extends RexNode> projects, RelDataType rowType, - int flags) { - this(cluster, traitSet, input, projects, rowType); - Util.discard(flags); - } - - /** Creates an EnumerableProject, specifying row type rather than field - * names. */ - public static EnumerableProject create(final RelNode input, - final List<? extends RexNode> projects, RelDataType rowType) { - final RelOptCluster cluster = input.getCluster(); - final RelMetadataQuery mq = RelMetadataQuery.instance(); - final RelTraitSet traitSet = - cluster.traitSet().replace(EnumerableConvention.INSTANCE) - .replaceIfs(RelCollationTraitDef.INSTANCE, - new Supplier<List<RelCollation>>() { - public List<RelCollation> get() { - return RelMdCollation.project(mq, input, projects); - } - }); - return new EnumerableProject(cluster, traitSet, input, projects, rowType); - } - - public EnumerableProject copy(RelTraitSet traitSet, RelNode input, - List<RexNode> projects, RelDataType rowType) { - return new EnumerableProject(getCluster(), traitSet, input, - projects, rowType); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - // EnumerableCalcRel is always better - throw new UnsupportedOperationException(); - } -} - -// End EnumerableProject.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectRule.java deleted file mode 100644 index 3f998da..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectRule.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.Convention; -import org.apache.calcite.plan.RelOptUtil; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.rel.logical.LogicalProject; - -/** - * Rule to convert a {@link org.apache.calcite.rel.logical.LogicalProject} to an - * {@link EnumerableProject}. - */ -class EnumerableProjectRule extends ConverterRule { - EnumerableProjectRule() { - super(LogicalProject.class, RelOptUtil.PROJECT_PREDICATE, Convention.NONE, - EnumerableConvention.INSTANCE, "EnumerableProjectRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalProject project = (LogicalProject) rel; - return EnumerableProject.create( - convert(project.getInput(), - project.getInput().getTraitSet() - .replace(EnumerableConvention.INSTANCE)), - project.getProjects(), - project.getRowType()); - } -} - -// End EnumerableProjectRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectToCalcRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectToCalcRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectToCalcRule.java deleted file mode 100644 index 2e1a4a8..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableProjectToCalcRule.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.plan.RelOptRule; -import org.apache.calcite.plan.RelOptRuleCall; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rex.RexProgram; - -/** Variant of {@link org.apache.calcite.rel.rules.ProjectToCalcRule} for - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableProjectToCalcRule extends RelOptRule { - EnumerableProjectToCalcRule() { - super(operand(EnumerableProject.class, any())); - } - - public void onMatch(RelOptRuleCall call) { - final EnumerableProject project = call.rel(0); - final RelNode input = project.getInput(); - final RexProgram program = - RexProgram.create(input.getRowType(), - project.getProjects(), - null, - project.getRowType(), - project.getCluster().getRexBuilder()); - final EnumerableCalc calc = EnumerableCalc.create(input, program); - call.transformTo(calc); - } -} - -// End EnumerableProjectToCalcRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRel.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRel.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRel.java deleted file mode 100644 index 5f9b7d3..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRel.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.linq4j.tree.BlockStatement; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.RelFactories; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.rex.RexUtil; -import org.apache.calcite.sql.validate.SqlValidatorUtil; - -import java.util.List; - -/** - * A relational expression of one of the - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention} calling - * conventions. - */ -public interface EnumerableRel - extends RelNode { - RelFactories.FilterFactory FILTER_FACTORY = - new RelFactories.FilterFactory() { - public RelNode createFilter(RelNode child, RexNode condition) { - return EnumerableFilter.create(child, condition); - } - }; - - RelFactories.ProjectFactory PROJECT_FACTORY = - new RelFactories.ProjectFactory() { - public RelNode createProject(RelNode child, - List<? extends RexNode> projects, List<String> fieldNames) { - final RelOptCluster cluster = child.getCluster(); - final RelDataType rowType = - RexUtil.createStructType(cluster.getTypeFactory(), projects, - fieldNames, SqlValidatorUtil.F_SUGGESTER); - return EnumerableProject.create(child, projects, rowType); - } - }; - - //~ Methods ---------------------------------------------------------------- - - /** - * Creates a plan for this expression according to a calling convention. - * - * @param implementor Implementor - * @param pref Preferred representation for rows in result expression - * @return Plan for this expression according to a calling convention - */ - Result implement(EnumerableRelImplementor implementor, Prefer pref); - - /** Preferred physical type. */ - enum Prefer { - /** Records must be represented as arrays. */ - ARRAY, - /** Consumer would prefer that records are represented as arrays, but can - * accommodate records represented as objects. */ - ARRAY_NICE, - /** Records must be represented as objects. */ - CUSTOM, - /** Consumer would prefer that records are represented as objects, but can - * accommodate records represented as arrays. */ - CUSTOM_NICE, - /** Consumer has no preferred representation. */ - ANY; - - public JavaRowFormat preferCustom() { - return prefer(JavaRowFormat.CUSTOM); - } - - public JavaRowFormat preferArray() { - return prefer(JavaRowFormat.ARRAY); - } - - public JavaRowFormat prefer(JavaRowFormat format) { - switch (this) { - case CUSTOM: - return JavaRowFormat.CUSTOM; - case ARRAY: - return JavaRowFormat.ARRAY; - default: - return format; - } - } - - public Prefer of(JavaRowFormat format) { - switch (format) { - case ARRAY: - return ARRAY; - default: - return CUSTOM; - } - } - } - - /** Result of implementing an enumerable relational expression by generating - * Java code. */ - class Result { - public final BlockStatement block; - - /** - * Describes the Java type returned by this relational expression, and the - * mapping between it and the fields of the logical row type. - */ - public final PhysType physType; - public final JavaRowFormat format; - - public Result(BlockStatement block, PhysType physType, - JavaRowFormat format) { - this.block = block; - this.physType = physType; - this.format = format; - } - } -} - -// End EnumerableRel.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRelImplementor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRelImplementor.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRelImplementor.java deleted file mode 100644 index 6e9033c..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableRelImplementor.java +++ /dev/null @@ -1,535 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.calcite.adapter.enumerable; - -import org.apache.calcite.DataContext; -import org.apache.calcite.jdbc.JavaTypeFactoryImpl; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.function.Function1; -import org.apache.calcite.linq4j.tree.BlockBuilder; -import org.apache.calcite.linq4j.tree.BlockStatement; -import org.apache.calcite.linq4j.tree.Blocks; -import org.apache.calcite.linq4j.tree.ClassDeclaration; -import org.apache.calcite.linq4j.tree.ConditionalStatement; -import org.apache.calcite.linq4j.tree.ConstantExpression; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.linq4j.tree.MemberDeclaration; -import org.apache.calcite.linq4j.tree.MethodCallExpression; -import org.apache.calcite.linq4j.tree.NewArrayExpression; -import org.apache.calcite.linq4j.tree.NewExpression; -import org.apache.calcite.linq4j.tree.ParameterExpression; -import org.apache.calcite.linq4j.tree.Primitive; -import org.apache.calcite.linq4j.tree.Statement; -import org.apache.calcite.linq4j.tree.Types; -import org.apache.calcite.linq4j.tree.VisitorImpl; -import org.apache.calcite.rex.RexBuilder; -import org.apache.calcite.runtime.Bindable; -import org.apache.calcite.util.BuiltInMethod; - -import com.google.common.base.Function; -import com.google.common.collect.Collections2; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterables; -import com.google.common.collect.Maps; - -import java.io.Serializable; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Subclass of {@link org.apache.calcite.plan.RelImplementor} for relational - * operators of {@link EnumerableConvention} calling convention. - */ -public class EnumerableRelImplementor extends JavaRelImplementor { - /** Maximum number of arguments to a constructor. See - * <a href="https://issues.apache.org/jira/browse/CALCITE-1097">[CALCITE-1097] - * Exception when executing query with too many aggregation columns</a> for - * details. */ - private static final int MAX_CONSTRUCTOR_ARG_COUNT = 10; - - public final Map<String, Object> map; - private final Map<String, RexToLixTranslator.InputGetter> corrVars = - Maps.newHashMap(); - private final Map<Object, ParameterExpression> stashedParameters = - Maps.newIdentityHashMap(); - - protected final Function1<String, RexToLixTranslator.InputGetter> - allCorrelateVariables = - new Function1<String, RexToLixTranslator.InputGetter>() { - public RexToLixTranslator.InputGetter apply(String name) { - return getCorrelVariableGetter(name); - } - }; - - public EnumerableRelImplementor(RexBuilder rexBuilder, - Map<String, Object> internalParameters) { - super(rexBuilder); - this.map = internalParameters; - } - - public EnumerableRel.Result visitChild( - EnumerableRel parent, - int ordinal, - EnumerableRel child, - EnumerableRel.Prefer prefer) { - if (parent != null) { - assert child == parent.getInputs().get(ordinal); - } - return child.implement(this, prefer); - } - - public ClassDeclaration implementRoot(EnumerableRel rootRel, - EnumerableRel.Prefer prefer) { - final EnumerableRel.Result result = rootRel.implement(this, prefer); - final List<MemberDeclaration> memberDeclarations = new ArrayList<>(); - new TypeRegistrar(memberDeclarations).go(result); - - // The following is a workaround to - // http://jira.codehaus.org/browse/JANINO-169. Otherwise we'd remove the - // member variable, rename the "root0" parameter as "root", and reference it - // directly from inner classes. - final ParameterExpression root0_ = - Expressions.parameter(Modifier.FINAL, DataContext.class, "root0"); - - // This creates the following code - // final Integer v1stashed = (Integer) root.get("v1stashed") - // It is convenient for passing non-literal "compile-time" constants - final Collection<Statement> stashed = - Collections2.transform(stashedParameters.values(), - new Function<ParameterExpression, Statement>() { - public Statement apply(ParameterExpression input) { - return Expressions.declare(Modifier.FINAL, input, - Expressions.convert_( - Expressions.call(DataContext.ROOT, - BuiltInMethod.DATA_CONTEXT_GET.method, - Expressions.constant(input.name)), - input.type)); - } - }); - - final BlockStatement block = Expressions.block( - Iterables.concat( - ImmutableList.of( - Expressions.statement( - Expressions.assign(DataContext.ROOT, root0_))), - stashed, - result.block.statements)); - memberDeclarations.add( - Expressions.fieldDecl(0, DataContext.ROOT, null)); - - memberDeclarations.add( - Expressions.methodDecl( - Modifier.PUBLIC, - Enumerable.class, - BuiltInMethod.BINDABLE_BIND.method.getName(), - Expressions.list(root0_), - block)); - memberDeclarations.add( - Expressions.methodDecl(Modifier.PUBLIC, Class.class, - BuiltInMethod.TYPED_GET_ELEMENT_TYPE.method.getName(), - Collections.<ParameterExpression>emptyList(), - Blocks.toFunctionBlock( - Expressions.return_(null, - Expressions.constant(result.physType.getJavaRowType()))))); - return Expressions.classDecl(Modifier.PUBLIC, - "Baz", - null, - Collections.<Type>singletonList(Bindable.class), - memberDeclarations); - } - - private ClassDeclaration classDecl( - JavaTypeFactoryImpl.SyntheticRecordType type) { - ClassDeclaration classDeclaration = - Expressions.classDecl( - Modifier.PUBLIC | Modifier.STATIC, - type.getName(), - null, - ImmutableList.<Type>of(Serializable.class), - new ArrayList<MemberDeclaration>()); - - // For each field: - // public T0 f0; - // ... - for (Types.RecordField field : type.getRecordFields()) { - classDeclaration.memberDeclarations.add( - Expressions.fieldDecl( - field.getModifiers(), - Expressions.parameter( - field.getType(), field.getName()), - null)); - } - - // Constructor: - // Foo(T0 f0, ...) { this.f0 = f0; ... } - final BlockBuilder blockBuilder = new BlockBuilder(); - final List<ParameterExpression> parameters = new ArrayList<>(); - final ParameterExpression thisParameter = - Expressions.parameter(type, "this"); - - // Here a constructor without parameter is used because the generated - // code could cause error if number of fields is too large. - classDeclaration.memberDeclarations.add( - Expressions.constructorDecl( - Modifier.PUBLIC, - type, - parameters, - blockBuilder.toBlock())); - - // equals method(): - // public boolean equals(Object o) { - // if (this == o) return true; - // if (!(o instanceof MyClass)) return false; - // final MyClass that = (MyClass) o; - // return this.f0 == that.f0 - // && equal(this.f1, that.f1) - // ... - // } - final BlockBuilder blockBuilder2 = new BlockBuilder(); - final ParameterExpression thatParameter = - Expressions.parameter(type, "that"); - final ParameterExpression oParameter = - Expressions.parameter(Object.class, "o"); - blockBuilder2.add( - Expressions.ifThen( - Expressions.equal(thisParameter, oParameter), - Expressions.return_(null, Expressions.constant(true)))); - blockBuilder2.add( - Expressions.ifThen( - Expressions.not( - Expressions.typeIs(oParameter, type)), - Expressions.return_(null, Expressions.constant(false)))); - blockBuilder2.add( - Expressions.declare( - Modifier.FINAL, - thatParameter, - Expressions.convert_(oParameter, type))); - final List<Expression> conditions = new ArrayList<>(); - for (Types.RecordField field : type.getRecordFields()) { - conditions.add( - Primitive.is(field.getType()) - ? Expressions.equal( - Expressions.field(thisParameter, field.getName()), - Expressions.field(thatParameter, field.getName())) - : Expressions.call(BuiltInMethod.OBJECTS_EQUAL.method, - Expressions.field(thisParameter, field.getName()), - Expressions.field(thatParameter, field.getName()))); - } - blockBuilder2.add( - Expressions.return_(null, Expressions.foldAnd(conditions))); - classDeclaration.memberDeclarations.add( - Expressions.methodDecl( - Modifier.PUBLIC, - boolean.class, - "equals", - Collections.singletonList(oParameter), - blockBuilder2.toBlock())); - - // hashCode method: - // public int hashCode() { - // int h = 0; - // h = hash(h, f0); - // ... - // return h; - // } - final BlockBuilder blockBuilder3 = new BlockBuilder(); - final ParameterExpression hParameter = - Expressions.parameter(int.class, "h"); - final ConstantExpression constantZero = - Expressions.constant(0); - blockBuilder3.add( - Expressions.declare(0, hParameter, constantZero)); - for (Types.RecordField field : type.getRecordFields()) { - final Method method = BuiltInMethod.HASH.method; - blockBuilder3.add( - Expressions.statement( - Expressions.assign( - hParameter, - Expressions.call( - method.getDeclaringClass(), - method.getName(), - ImmutableList.of( - hParameter, - Expressions.field(thisParameter, field)))))); - } - blockBuilder3.add( - Expressions.return_(null, hParameter)); - classDeclaration.memberDeclarations.add( - Expressions.methodDecl( - Modifier.PUBLIC, - int.class, - "hashCode", - Collections.<ParameterExpression>emptyList(), - blockBuilder3.toBlock())); - - // compareTo method: - // public int compareTo(MyClass that) { - // int c; - // c = compare(this.f0, that.f0); - // if (c != 0) return c; - // ... - // return 0; - // } - final BlockBuilder blockBuilder4 = new BlockBuilder(); - final ParameterExpression cParameter = - Expressions.parameter(int.class, "c"); - final int mod = type.getRecordFields().size() == 1 ? Modifier.FINAL : 0; - blockBuilder4.add( - Expressions.declare(mod, cParameter, null)); - final ConditionalStatement conditionalStatement = - Expressions.ifThen( - Expressions.notEqual(cParameter, constantZero), - Expressions.return_(null, cParameter)); - for (Types.RecordField field : type.getRecordFields()) { - MethodCallExpression compareCall; - try { - final Method method = (field.nullable() - ? BuiltInMethod.COMPARE_NULLS_LAST - : BuiltInMethod.COMPARE).method; - compareCall = Expressions.call(method.getDeclaringClass(), - method.getName(), - Expressions.field(thisParameter, field), - Expressions.field(thatParameter, field)); - } catch (RuntimeException e) { - if (e.getCause() instanceof NoSuchMethodException) { - // Just ignore the field in compareTo - // "create synthetic record class" blindly creates compareTo for - // all the fields, however not all the records will actually be used - // as sorting keys (e.g. temporary state for aggregate calculation). - // In those cases it is fine if we skip the problematic fields. - continue; - } - throw e; - } - blockBuilder4.add( - Expressions.statement( - Expressions.assign( - cParameter, - compareCall))); - blockBuilder4.add(conditionalStatement); - } - blockBuilder4.add( - Expressions.return_(null, constantZero)); - classDeclaration.memberDeclarations.add( - Expressions.methodDecl( - Modifier.PUBLIC, - int.class, - "compareTo", - Collections.singletonList(thatParameter), - blockBuilder4.toBlock())); - - // toString method: - // public String toString() { - // return "{f0=" + f0 - // + ", f1=" + f1 - // ... - // + "}"; - // } - final BlockBuilder blockBuilder5 = new BlockBuilder(); - Expression expression5 = null; - for (Types.RecordField field : type.getRecordFields()) { - if (expression5 == null) { - expression5 = - Expressions.constant("{" + field.getName() + "="); - } else { - expression5 = - Expressions.add( - expression5, - Expressions.constant(", " + field.getName() + "=")); - } - expression5 = - Expressions.add( - expression5, - Expressions.field(thisParameter, field.getName())); - } - expression5 = - expression5 == null - ? Expressions.constant("{}") - : Expressions.add( - expression5, - Expressions.constant("}")); - blockBuilder5.add( - Expressions.return_( - null, - expression5)); - classDeclaration.memberDeclarations.add( - Expressions.methodDecl( - Modifier.PUBLIC, - String.class, - "toString", - Collections.<ParameterExpression>emptyList(), - blockBuilder5.toBlock())); - - return classDeclaration; - } - - /** - * Stashes a value for the executor. Given values are de-duplicated if - * identical (see {@link java.util.IdentityHashMap}). - * - * <p>For instance, to pass {@code ArrayList} to your method, you can use - * {@code Expressions.call(method, implementor.stash(arrayList))}. - * - * <p>For simple literals (strings, numbers) the result is equivalent to - * {@link org.apache.calcite.linq4j.tree.Expressions#constant(Object, java.lang.reflect.Type)}. - * - * <p>Note: the input value is held in memory as long as the statement - * is alive. If you are using just a subset of its content, consider creating - * a slimmer holder. - * - * @param input Value to be stashed - * @param clazz Java class type of the value when it is used - * @param <T> Java class type of the value when it is used - * @return Expression that will represent {@code input} in runtime - */ - public <T> Expression stash(T input, Class<? super T> clazz) { - // Well-known final classes that can be used as literals - if (input == null - || input instanceof String - || input instanceof Boolean - || input instanceof Byte - || input instanceof Short - || input instanceof Integer - || input instanceof Long - || input instanceof Float - || input instanceof Double - ) { - return Expressions.constant(input, clazz); - } - ParameterExpression cached = stashedParameters.get(input); - if (cached != null) { - return cached; - } - // "stashed" avoids name clash since this name will be used as the variable - // name at the very start of the method. - final String name = "v" + map.size() + "stashed"; - final ParameterExpression x = Expressions.variable(clazz, name); - map.put(name, input); - stashedParameters.put(input, x); - return x; - } - - public void registerCorrelVariable(final String name, - final ParameterExpression pe, - final BlockBuilder corrBlock, final PhysType physType) { - corrVars.put(name, new RexToLixTranslator.InputGetter() { - public Expression field(BlockBuilder list, int index, Type storageType) { - Expression fieldReference = - physType.fieldReference(pe, index, storageType); - return corrBlock.append(name + "_" + index, fieldReference); - } - }); - } - - public void clearCorrelVariable(String name) { - assert corrVars.containsKey(name) : "Correlation variable " + name - + " should be defined"; - corrVars.remove(name); - } - - public RexToLixTranslator.InputGetter getCorrelVariableGetter(String name) { - assert corrVars.containsKey(name) : "Correlation variable " + name - + " should be defined"; - return corrVars.get(name); - } - - public EnumerableRel.Result result(PhysType physType, BlockStatement block) { - return new EnumerableRel.Result( - block, physType, ((PhysTypeImpl) physType).format); - } - - /** Visitor that finds types in an {@link Expression} tree. */ - private static class TypeFinder extends VisitorImpl<Void> { - private final Collection<Type> types; - - TypeFinder(Collection<Type> types) { - this.types = types; - } - - @Override public Void visit(NewExpression newExpression) { - types.add(newExpression.type); - return super.visit(newExpression); - } - - @Override public Void visit(NewArrayExpression newArrayExpression) { - Type type = newArrayExpression.type; - for (;;) { - final Type componentType = Types.getComponentType(type); - if (componentType == null) { - break; - } - type = componentType; - } - types.add(type); - return super.visit(newArrayExpression); - } - - @Override public Void visit(ConstantExpression constantExpression) { - if (constantExpression.value instanceof Type) { - types.add((Type) constantExpression.value); - } - return super.visit(constantExpression); - } - } - - /** Adds a declaration of each synthetic type found in a code block. */ - private class TypeRegistrar { - private final List<MemberDeclaration> memberDeclarations; - private final Set<Type> seen = new HashSet<>(); - - TypeRegistrar(List<MemberDeclaration> memberDeclarations) { - this.memberDeclarations = memberDeclarations; - } - - private void register(Type type) { - if (!seen.add(type)) { - return; - } - if (type instanceof JavaTypeFactoryImpl.SyntheticRecordType) { - memberDeclarations.add( - classDecl((JavaTypeFactoryImpl.SyntheticRecordType) type)); - } - if (type instanceof ParameterizedType) { - for (Type type1 : ((ParameterizedType) type).getActualTypeArguments()) { - register(type1); - } - } - } - - public void go(EnumerableRel.Result result) { - final Set<Type> types = new LinkedHashSet<>(); - result.block.accept(new TypeFinder(types)); - types.add(result.physType.getJavaRowType()); - for (Type type : types) { - register(type); - } - } - } -} - -// End EnumerableRelImplementor.java
