http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java deleted file mode 100644 index 60408c6..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java +++ /dev/null @@ -1,269 +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.Ord; -import org.apache.calcite.linq4j.function.Function2; -import org.apache.calcite.linq4j.tree.BlockStatement; -import org.apache.calcite.linq4j.tree.ConstantUntypedNull; -import org.apache.calcite.linq4j.tree.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.linq4j.tree.MethodDeclaration; -import org.apache.calcite.linq4j.tree.ParameterExpression; -import org.apache.calcite.linq4j.tree.Primitive; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeField; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.util.BuiltInMethod; - -import com.google.common.base.Function; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; - -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; -import java.util.AbstractList; -import java.util.ArrayList; -import java.util.List; - -/** - * Utilities for generating programs in the Enumerable (functional) - * style. - */ -public class EnumUtils { - - private static final Function<RexNode, Type> REX_TO_INTERNAL_TYPE = - new Function<RexNode, Type>() { - public Type apply(RexNode node) { - return toInternal(node.getType()); - } - }; - - private EnumUtils() {} - - static final boolean BRIDGE_METHODS = true; - - static final List<ParameterExpression> NO_PARAMS = - ImmutableList.of(); - - static final List<Expression> NO_EXPRS = - ImmutableList.of(); - - public static final List<String> LEFT_RIGHT = - ImmutableList.of("left", "right"); - - /** Declares a method that overrides another method. */ - public static MethodDeclaration overridingMethodDecl(Method method, - Iterable<ParameterExpression> parameters, - BlockStatement body) { - return Expressions.methodDecl( - method.getModifiers() & ~Modifier.ABSTRACT, - method.getReturnType(), - method.getName(), - parameters, - body); - } - - static Type javaClass( - JavaTypeFactory typeFactory, RelDataType type) { - final Type clazz = typeFactory.getJavaClass(type); - return clazz instanceof Class ? clazz : Object[].class; - } - - static Class javaRowClass( - JavaTypeFactory typeFactory, RelDataType type) { - if (type.isStruct() && type.getFieldCount() == 1) { - type = type.getFieldList().get(0).getType(); - } - final Type clazz = typeFactory.getJavaClass(type); - return clazz instanceof Class ? (Class) clazz : Object[].class; - } - - static List<Type> fieldTypes( - final JavaTypeFactory typeFactory, - final List<? extends RelDataType> inputTypes) { - return new AbstractList<Type>() { - public Type get(int index) { - return EnumUtils.javaClass(typeFactory, inputTypes.get(index)); - } - public int size() { - return inputTypes.size(); - } - }; - } - - static List<RelDataType> fieldRowTypes( - final RelDataType inputRowType, - final List<? extends RexNode> extraInputs, - final List<Integer> argList) { - final List<RelDataTypeField> inputFields = inputRowType.getFieldList(); - return new AbstractList<RelDataType>() { - public RelDataType get(int index) { - final int arg = argList.get(index); - return arg < inputFields.size() - ? inputFields.get(arg).getType() - : extraInputs.get(arg - inputFields.size()).getType(); - } - public int size() { - return argList.size(); - } - }; - } - - static Expression joinSelector(JoinRelType joinType, PhysType physType, - List<PhysType> inputPhysTypes) { - // A parameter for each input. - final List<ParameterExpression> parameters = new ArrayList<>(); - - // Generate all fields. - final List<Expression> expressions = new ArrayList<>(); - final int outputFieldCount = physType.getRowType().getFieldCount(); - for (Ord<PhysType> ord : Ord.zip(inputPhysTypes)) { - final PhysType inputPhysType = - ord.e.makeNullable(joinType.generatesNullsOn(ord.i)); - // If input item is just a primitive, we do not generate specialized - // primitive apply override since it won't be called anyway - // Function<T> always operates on boxed arguments - final ParameterExpression parameter = - Expressions.parameter(Primitive.box(inputPhysType.getJavaRowType()), - EnumUtils.LEFT_RIGHT.get(ord.i)); - parameters.add(parameter); - if (expressions.size() == outputFieldCount) { - // For instance, if semi-join needs to return just the left inputs - break; - } - final int fieldCount = inputPhysType.getRowType().getFieldCount(); - for (int i = 0; i < fieldCount; i++) { - Expression expression = - inputPhysType.fieldReference(parameter, i, - physType.getJavaFieldType(expressions.size())); - if (joinType.generatesNullsOn(ord.i)) { - expression = - Expressions.condition( - Expressions.equal(parameter, Expressions.constant(null)), - Expressions.constant(null), - expression); - } - expressions.add(expression); - } - } - return Expressions.lambda( - Function2.class, - physType.record(expressions), - parameters); - } - - /** Converts from internal representation to JDBC representation used by - * arguments of user-defined functions. For example, converts date values from - * {@code int} to {@link java.sql.Date}. */ - static Expression fromInternal(Expression e, Class<?> targetType) { - if (e == ConstantUntypedNull.INSTANCE) { - return e; - } - if (!(e.getType() instanceof Class)) { - return e; - } - if (targetType.isAssignableFrom((Class) e.getType())) { - return e; - } - if (targetType == java.sql.Date.class) { - return Expressions.call(BuiltInMethod.INTERNAL_TO_DATE.method, e); - } - if (targetType == java.sql.Time.class) { - return Expressions.call(BuiltInMethod.INTERNAL_TO_TIME.method, e); - } - if (targetType == java.sql.Timestamp.class) { - return Expressions.call(BuiltInMethod.INTERNAL_TO_TIMESTAMP.method, e); - } - if (Primitive.is(e.type) - && Primitive.isBox(targetType)) { - // E.g. e is "int", target is "Long", generate "(long) e". - return Expressions.convert_(e, - Primitive.ofBox(targetType).primitiveClass); - } - return e; - } - - static List<Expression> fromInternal(Class<?>[] targetTypes, - List<Expression> expressions) { - final List<Expression> list = new ArrayList<>(); - for (int i = 0; i < expressions.size(); i++) { - list.add(fromInternal(expressions.get(i), targetTypes[i])); - } - return list; - } - - static Type fromInternal(Type type) { - if (type == java.sql.Date.class || type == java.sql.Time.class) { - return int.class; - } - if (type == java.sql.Timestamp.class) { - return long.class; - } - return type; - } - - static Type toInternal(RelDataType type) { - switch (type.getSqlTypeName()) { - case DATE: - case TIME: - return type.isNullable() ? Integer.class : int.class; - case TIMESTAMP: - return type.isNullable() ? Long.class : long.class; - default: - return null; // we don't care; use the default storage type - } - } - - static List<Type> internalTypes(List<? extends RexNode> operandList) { - return Lists.transform(operandList, REX_TO_INTERNAL_TYPE); - } - - static Expression enforce(final Type storageType, - final Expression e) { - if (storageType != null && e.type != storageType) { - if (e.type == java.sql.Date.class) { - if (storageType == int.class) { - return Expressions.call(BuiltInMethod.DATE_TO_INT.method, e); - } - if (storageType == Integer.class) { - return Expressions.call(BuiltInMethod.DATE_TO_INT_OPTIONAL.method, e); - } - } else if (e.type == java.sql.Time.class) { - if (storageType == int.class) { - return Expressions.call(BuiltInMethod.TIME_TO_INT.method, e); - } - if (storageType == Integer.class) { - return Expressions.call(BuiltInMethod.TIME_TO_INT_OPTIONAL.method, e); - } - } else if (e.type == java.sql.Timestamp.class) { - if (storageType == long.class) { - return Expressions.call(BuiltInMethod.TIMESTAMP_TO_LONG.method, e); - } - if (storageType == Long.class) { - return Expressions.call(BuiltInMethod.TIMESTAMP_TO_LONG_OPTIONAL.method, e); - } - } - } - return e; - } -} - -// End EnumUtils.java
http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregate.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregate.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregate.java deleted file mode 100644 index c6b74e2..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregate.java +++ /dev/null @@ -1,468 +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.enumerable.impl.AggAddContextImpl; -import org.apache.calcite.adapter.enumerable.impl.AggResultContextImpl; -import org.apache.calcite.adapter.java.JavaTypeFactory; -import org.apache.calcite.jdbc.JavaTypeFactoryImpl; -import org.apache.calcite.linq4j.Ord; -import org.apache.calcite.linq4j.function.Function0; -import org.apache.calcite.linq4j.function.Function1; -import org.apache.calcite.linq4j.function.Function2; -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.RelTraitSet; -import org.apache.calcite.prepare.CalcitePrepareImpl; -import org.apache.calcite.rel.InvalidRelException; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.Aggregate; -import org.apache.calcite.rel.core.AggregateCall; -import org.apache.calcite.rel.type.RelDataType; -import org.apache.calcite.rel.type.RelDataTypeField; -import org.apache.calcite.rex.RexInputRef; -import org.apache.calcite.rex.RexNode; -import org.apache.calcite.sql.SqlAggFunction; -import org.apache.calcite.util.BuiltInMethod; -import org.apache.calcite.util.ImmutableBitSet; -import org.apache.calcite.util.Pair; -import org.apache.calcite.util.Util; - -import com.google.common.collect.Lists; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** Implementation of {@link org.apache.calcite.rel.core.Aggregate} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableAggregate extends Aggregate implements EnumerableRel { - public EnumerableAggregate( - RelOptCluster cluster, - RelTraitSet traitSet, - RelNode child, - boolean indicator, - ImmutableBitSet groupSet, - List<ImmutableBitSet> groupSets, - List<AggregateCall> aggCalls) - throws InvalidRelException { - super(cluster, traitSet, child, indicator, groupSet, groupSets, aggCalls); - assert getConvention() instanceof EnumerableConvention; - - for (AggregateCall aggCall : aggCalls) { - if (aggCall.isDistinct()) { - throw new InvalidRelException( - "distinct aggregation not supported"); - } - AggImplementor implementor2 = - RexImpTable.INSTANCE.get(aggCall.getAggregation(), false); - if (implementor2 == null) { - throw new InvalidRelException( - "aggregation " + aggCall.getAggregation() + " not supported"); - } - } - } - - @Override public EnumerableAggregate copy(RelTraitSet traitSet, RelNode input, - boolean indicator, ImmutableBitSet groupSet, - List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) { - try { - return new EnumerableAggregate(getCluster(), traitSet, input, indicator, - groupSet, groupSets, aggCalls); - } catch (InvalidRelException e) { - // Semantic error not possible. Must be a bug. Convert to - // internal error. - throw new AssertionError(e); - } - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - final JavaTypeFactory typeFactory = implementor.getTypeFactory(); - final BlockBuilder builder = new BlockBuilder(); - final EnumerableRel child = (EnumerableRel) getInput(); - final Result result = implementor.visitChild(this, 0, child, pref); - Expression childExp = - builder.append( - "child", - result.block); - final RelDataType inputRowType = getInput().getRowType(); - - final PhysType physType = - PhysTypeImpl.of( - typeFactory, getRowType(), pref.preferCustom()); - - // final Enumerable<Employee> child = <<child adapter>>; - // Function1<Employee, Integer> keySelector = - // new Function1<Employee, Integer>() { - // public Integer apply(Employee a0) { - // return a0.deptno; - // } - // }; - // Function1<Employee, Object[]> accumulatorInitializer = - // new Function1<Employee, Object[]>() { - // public Object[] apply(Employee a0) { - // return new Object[] {0, 0}; - // } - // }; - // Function2<Object[], Employee, Object[]> accumulatorAdder = - // new Function2<Object[], Employee, Object[]>() { - // public Object[] apply(Object[] a1, Employee a0) { - // a1[0] = ((Integer) a1[0]) + 1; - // a1[1] = ((Integer) a1[1]) + a0.salary; - // return a1; - // } - // }; - // Function2<Integer, Object[], Object[]> resultSelector = - // new Function2<Integer, Object[], Object[]>() { - // public Object[] apply(Integer a0, Object[] a1) { - // return new Object[] { a0, a1[0], a1[1] }; - // } - // }; - // return childEnumerable - // .groupBy( - // keySelector, accumulatorInitializer, accumulatorAdder, - // resultSelector); - // - // or, if key has 0 columns, - // - // return childEnumerable - // .aggregate( - // accumulatorInitializer.apply(), - // accumulatorAdder, - // resultSelector); - // - // with a slightly different resultSelector; or if there are no aggregate - // functions - // - // final Enumerable<Employee> child = <<child adapter>>; - // Function1<Employee, Integer> keySelector = - // new Function1<Employee, Integer>() { - // public Integer apply(Employee a0) { - // return a0.deptno; - // } - // }; - // EqualityComparer<Employee> equalityComparer = - // new EqualityComparer<Employee>() { - // boolean equal(Employee a0, Employee a1) { - // return a0.deptno; - // } - // }; - // return child - // .distinct(equalityComparer); - - final PhysType inputPhysType = result.physType; - - ParameterExpression parameter = - Expressions.parameter(inputPhysType.getJavaRowType(), "a0"); - - final PhysType keyPhysType = - inputPhysType.project(groupSet.asList(), getGroupType() != Group.SIMPLE, - JavaRowFormat.LIST); - final int groupCount = getGroupCount(); - final int indicatorCount = getIndicatorCount(); - - final List<AggImpState> aggs = new ArrayList<>(aggCalls.size()); - for (Ord<AggregateCall> call : Ord.zip(aggCalls)) { - aggs.add(new AggImpState(call.i, call.e, false)); - } - - // Function0<Object[]> accumulatorInitializer = - // new Function0<Object[]>() { - // public Object[] apply() { - // return new Object[] {0, 0}; - // } - // }; - final List<Expression> initExpressions = new ArrayList<>(); - final BlockBuilder initBlock = new BlockBuilder(); - - final List<Type> aggStateTypes = new ArrayList<>(); - for (final AggImpState agg : aggs) { - agg.context = - new AggContext() { - public SqlAggFunction aggregation() { - return agg.call.getAggregation(); - } - - public RelDataType returnRelType() { - return agg.call.type; - } - - public Type returnType() { - return EnumUtils.javaClass(typeFactory, returnRelType()); - } - - public List<? extends RelDataType> parameterRelTypes() { - return EnumUtils.fieldRowTypes(inputRowType, null, - agg.call.getArgList()); - } - - public List<? extends Type> parameterTypes() { - return EnumUtils.fieldTypes(typeFactory, - parameterRelTypes()); - } - }; - List<Type> state = - agg.implementor.getStateType(agg.context); - - if (state.isEmpty()) { - continue; - } - - aggStateTypes.addAll(state); - - final List<Expression> decls = new ArrayList<>(state.size()); - for (int i = 0; i < state.size(); i++) { - String aggName = "a" + agg.aggIdx; - if (CalcitePrepareImpl.DEBUG) { - aggName = Util.toJavaId(agg.call.getAggregation().getName(), 0) - .substring("ID$0$".length()) + aggName; - } - Type type = state.get(i); - ParameterExpression pe = - Expressions.parameter(type, - initBlock.newName(aggName + "s" + i)); - initBlock.add(Expressions.declare(0, pe, null)); - decls.add(pe); - } - agg.state = decls; - initExpressions.addAll(decls); - agg.implementor.implementReset(agg.context, - new AggResultContextImpl(initBlock, decls)); - } - - final PhysType accPhysType = - PhysTypeImpl.of( - typeFactory, - typeFactory.createSyntheticType(aggStateTypes)); - - - if (accPhysType.getJavaRowType() instanceof JavaTypeFactoryImpl.SyntheticRecordType) { - // We have to initialize the SyntheticRecordType instance this way, to avoid using - // class constructor with too many parameters. - JavaTypeFactoryImpl.SyntheticRecordType synType = - (JavaTypeFactoryImpl.SyntheticRecordType) accPhysType.getJavaRowType(); - final ParameterExpression record0_ = - Expressions.parameter(accPhysType.getJavaRowType(), "record0"); - initBlock.add(Expressions.declare(0, record0_, null)); - initBlock.add( - Expressions.statement( - Expressions.assign( - record0_, - Expressions.new_(accPhysType.getJavaRowType())))); - List<Types.RecordField> fieldList = synType.getRecordFields(); - for (int i = 0; i < initExpressions.size(); i++) { - Expression right = initExpressions.get(i); - initBlock.add( - Expressions.statement( - Expressions.assign( - Expressions.field( - record0_, - fieldList.get(i)), - right))); - } - initBlock.add(record0_); - } else { - initBlock.add(accPhysType.record(initExpressions)); - } - - final Expression accumulatorInitializer = - builder.append( - "accumulatorInitializer", - Expressions.lambda( - Function0.class, - initBlock.toBlock())); - - // Function2<Object[], Employee, Object[]> accumulatorAdder = - // new Function2<Object[], Employee, Object[]>() { - // public Object[] apply(Object[] acc, Employee in) { - // acc[0] = ((Integer) acc[0]) + 1; - // acc[1] = ((Integer) acc[1]) + in.salary; - // return acc; - // } - // }; - final BlockBuilder builder2 = new BlockBuilder(); - final ParameterExpression inParameter = - Expressions.parameter(inputPhysType.getJavaRowType(), "in"); - final ParameterExpression acc_ = - Expressions.parameter(accPhysType.getJavaRowType(), "acc"); - for (int i = 0, stateOffset = 0; i < aggs.size(); i++) { - final AggImpState agg = aggs.get(i); - - final int stateSize = agg.state.size(); - final List<Expression> accumulator = new ArrayList<>(stateSize); - for (int j = 0; j < stateSize; j++) { - accumulator.add(accPhysType.fieldReference(acc_, j + stateOffset)); - } - agg.state = accumulator; - - stateOffset += stateSize; - - AggAddContext addContext = - new AggAddContextImpl(builder2, accumulator) { - public List<RexNode> rexArguments() { - List<RelDataTypeField> inputTypes = - inputPhysType.getRowType().getFieldList(); - List<RexNode> args = new ArrayList<>(); - for (int index : agg.call.getArgList()) { - args.add(RexInputRef.of(index, inputTypes)); - } - return args; - } - - public RexNode rexFilterArgument() { - return agg.call.filterArg < 0 - ? null - : RexInputRef.of(agg.call.filterArg, - inputPhysType.getRowType()); - } - - public RexToLixTranslator rowTranslator() { - return RexToLixTranslator.forAggregation(typeFactory, - currentBlock(), - new RexToLixTranslator.InputGetterImpl( - Collections.singletonList( - Pair.of((Expression) inParameter, inputPhysType)))) - .setNullable(currentNullables()); - } - }; - - agg.implementor.implementAdd(agg.context, addContext); - } - builder2.add(acc_); - final Expression accumulatorAdder = - builder.append( - "accumulatorAdder", - Expressions.lambda( - Function2.class, - builder2.toBlock(), - acc_, - inParameter)); - - // Function2<Integer, Object[], Object[]> resultSelector = - // new Function2<Integer, Object[], Object[]>() { - // public Object[] apply(Integer key, Object[] acc) { - // return new Object[] { key, acc[0], acc[1] }; - // } - // }; - final BlockBuilder resultBlock = new BlockBuilder(); - final List<Expression> results = Expressions.list(); - final ParameterExpression key_; - if (groupCount == 0) { - key_ = null; - } else { - final Type keyType = keyPhysType.getJavaRowType(); - key_ = Expressions.parameter(keyType, "key"); - for (int j = 0; j < groupCount + indicatorCount; j++) { - results.add( - keyPhysType.fieldReference(key_, j)); - } - } - for (final AggImpState agg : aggs) { - results.add( - agg.implementor.implementResult(agg.context, - new AggResultContextImpl(resultBlock, agg.state))); - } - resultBlock.add(physType.record(results)); - if (getGroupType() != Group.SIMPLE) { - final List<Expression> list = Lists.newArrayList(); - for (ImmutableBitSet set : groupSets) { - list.add( - inputPhysType.generateSelector(parameter, groupSet.asList(), - set.asList(), keyPhysType.getFormat())); - } - final Expression keySelectors_ = - builder.append("keySelectors", - Expressions.call(BuiltInMethod.ARRAYS_AS_LIST.method, - list)); - final Expression resultSelector = - builder.append("resultSelector", - Expressions.lambda(Function2.class, - resultBlock.toBlock(), - key_, - acc_)); - builder.add( - Expressions.return_(null, - Expressions.call( - BuiltInMethod.GROUP_BY_MULTIPLE.method, - Expressions.list(childExp, - keySelectors_, - accumulatorInitializer, - accumulatorAdder, - resultSelector) - .appendIfNotNull(keyPhysType.comparer())))); - } else if (groupCount == 0) { - final Expression resultSelector = - builder.append( - "resultSelector", - Expressions.lambda( - Function1.class, - resultBlock.toBlock(), - acc_)); - builder.add( - Expressions.return_( - null, - Expressions.call( - BuiltInMethod.SINGLETON_ENUMERABLE.method, - Expressions.call( - childExp, - BuiltInMethod.AGGREGATE.method, - Expressions.call(accumulatorInitializer, "apply"), - accumulatorAdder, - resultSelector)))); - } else if (aggCalls.isEmpty() - && groupSet.equals( - ImmutableBitSet.range(child.getRowType().getFieldCount()))) { - builder.add( - Expressions.return_( - null, - Expressions.call( - inputPhysType.convertTo(childExp, physType), - BuiltInMethod.DISTINCT.method, - Expressions.<Expression>list() - .appendIfNotNull(physType.comparer())))); - } else { - final Expression keySelector_ = - builder.append("keySelector", - inputPhysType.generateSelector(parameter, - groupSet.asList(), - keyPhysType.getFormat())); - final Expression resultSelector_ = - builder.append("resultSelector", - Expressions.lambda(Function2.class, - resultBlock.toBlock(), - key_, - acc_)); - builder.add( - Expressions.return_(null, - Expressions.call(childExp, - BuiltInMethod.GROUP_BY2.method, - Expressions.list(keySelector_, - accumulatorInitializer, - accumulatorAdder, - resultSelector_) - .appendIfNotNull(keyPhysType.comparer())))); - } - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableAggregate.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregateRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregateRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregateRule.java deleted file mode 100644 index e29b9fc..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableAggregateRule.java +++ /dev/null @@ -1,56 +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.InvalidRelException; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.rel.logical.LogicalAggregate; - -/** - * Rule to convert a {@link org.apache.calcite.rel.logical.LogicalAggregate} - * to an {@link EnumerableAggregate}. - */ -class EnumerableAggregateRule extends ConverterRule { - EnumerableAggregateRule() { - super(LogicalAggregate.class, Convention.NONE, - EnumerableConvention.INSTANCE, "EnumerableAggregateRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalAggregate agg = (LogicalAggregate) rel; - final RelTraitSet traitSet = - agg.getTraitSet().replace(EnumerableConvention.INSTANCE); - try { - return new EnumerableAggregate( - rel.getCluster(), - traitSet, - convert(agg.getInput(), EnumerableConvention.INSTANCE), - agg.indicator, - agg.getGroupSet(), - agg.getGroupSets(), - agg.getAggCallList()); - } catch (InvalidRelException e) { - EnumerableRules.LOGGER.debug(e.toString()); - return null; - } - } -} - -// End EnumerableAggregateRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBindable.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBindable.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBindable.java deleted file mode 100644 index 2ad069e..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableBindable.java +++ /dev/null @@ -1,102 +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.interpreter.BindableConvention; -import org.apache.calcite.interpreter.BindableRel; -import org.apache.calcite.interpreter.Node; -import org.apache.calcite.interpreter.Row; -import org.apache.calcite.interpreter.Sink; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.Enumerator; -import org.apache.calcite.plan.ConventionTraitDef; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterImpl; -import org.apache.calcite.rel.convert.ConverterRule; -import org.apache.calcite.runtime.ArrayBindable; -import org.apache.calcite.runtime.Bindable; - -import com.google.common.collect.ImmutableMap; - -import java.util.List; - -/** - * Relational expression that converts an enumerable input to interpretable - * calling convention. - * - * @see org.apache.calcite.adapter.enumerable.EnumerableConvention - * @see org.apache.calcite.interpreter.BindableConvention - */ -public class EnumerableBindable extends ConverterImpl implements BindableRel { - protected EnumerableBindable(RelOptCluster cluster, RelNode input) { - super(cluster, ConventionTraitDef.INSTANCE, - cluster.traitSetOf(BindableConvention.INSTANCE), input); - } - - @Override public EnumerableBindable copy(RelTraitSet traitSet, - List<RelNode> inputs) { - return new EnumerableBindable(getCluster(), sole(inputs)); - } - - public Class<Object[]> getElementType() { - return Object[].class; - } - - public Enumerable<Object[]> bind(DataContext dataContext) { - final ImmutableMap<String, Object> map = ImmutableMap.of(); - final Bindable bindable = EnumerableInterpretable.toBindable(map, null, - (EnumerableRel) getInput(), EnumerableRel.Prefer.ARRAY); - final ArrayBindable arrayBindable = EnumerableInterpretable.box(bindable); - return arrayBindable.bind(dataContext); - } - - public Node implement(final InterpreterImplementor implementor) { - return new Node() { - public void run() throws InterruptedException { - final Sink sink = - implementor.relSinks.get(EnumerableBindable.this).get(0); - final Enumerable<Object[]> enumerable = bind(implementor.dataContext); - final Enumerator<Object[]> enumerator = enumerable.enumerator(); - while (enumerator.moveNext()) { - sink.send(Row.asCopy(enumerator.current())); - } - } - }; - } - - /** - * Rule that converts any enumerable relational expression to bindable. - */ - public static class EnumerableToBindableConverterRule extends ConverterRule { - public static final EnumerableToBindableConverterRule INSTANCE = - new EnumerableToBindableConverterRule(); - - private EnumerableToBindableConverterRule() { - super(EnumerableRel.class, EnumerableConvention.INSTANCE, - BindableConvention.INSTANCE, "EnumerableToBindableConverterRule"); - } - - @Override public RelNode convert(RelNode rel) { - return new EnumerableBindable(rel.getCluster(), rel); - } - } -} - -// End EnumerableBindable.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalc.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalc.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalc.java deleted file mode 100644 index a23dcfe..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalc.java +++ /dev/null @@ -1,274 +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.adapter.java.JavaTypeFactory; -import org.apache.calcite.linq4j.Enumerator; -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.Expression; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.linq4j.tree.MemberDeclaration; -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.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.core.Calc; -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.RexBuilder; -import org.apache.calcite.rex.RexProgram; -import org.apache.calcite.rex.RexSimplify; -import org.apache.calcite.rex.RexUtil; -import org.apache.calcite.util.BuiltInMethod; -import org.apache.calcite.util.Pair; -import org.apache.calcite.util.Util; - -import com.google.common.base.Supplier; -import com.google.common.collect.ImmutableList; - -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; -import java.util.Collections; -import java.util.List; - -import static org.apache.calcite.adapter.enumerable.EnumUtils.BRIDGE_METHODS; -import static org.apache.calcite.adapter.enumerable.EnumUtils.NO_EXPRS; -import static org.apache.calcite.adapter.enumerable.EnumUtils.NO_PARAMS; - -/** Implementation of {@link org.apache.calcite.rel.core.Calc} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableCalc extends Calc implements EnumerableRel { - /** - * Creates an EnumerableCalc. - * - * <p>Use {@link #create} unless you know what you're doing. - */ - public EnumerableCalc(RelOptCluster cluster, - RelTraitSet traitSet, - RelNode input, - RexProgram program) { - super(cluster, traitSet, input, program); - assert getConvention() instanceof EnumerableConvention; - assert !program.containsAggs(); - } - - @Deprecated // to be removed before 2.0 - public EnumerableCalc( - RelOptCluster cluster, - RelTraitSet traitSet, - RelNode input, - RexProgram program, - List<RelCollation> collationList) { - this(cluster, traitSet, input, program); - Util.discard(collationList); - } - - /** Creates an EnumerableCalc. */ - public static EnumerableCalc create(final RelNode input, - final RexProgram program) { - 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.calc(mq, input, program); - } - }) - .replaceIf(RelDistributionTraitDef.INSTANCE, - new Supplier<RelDistribution>() { - public RelDistribution get() { - return RelMdDistribution.calc(mq, input, program); - } - }); - return new EnumerableCalc(cluster, traitSet, input, program); - } - - @Override public EnumerableCalc copy(RelTraitSet traitSet, RelNode child, - RexProgram program) { - // we do not need to copy program; it is immutable - return new EnumerableCalc(getCluster(), traitSet, child, program); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - final JavaTypeFactory typeFactory = implementor.getTypeFactory(); - final BlockBuilder builder = new BlockBuilder(); - final EnumerableRel child = (EnumerableRel) getInput(); - - final Result result = - implementor.visitChild(this, 0, child, pref); - - final PhysType physType = - PhysTypeImpl.of( - typeFactory, getRowType(), pref.prefer(result.format)); - - // final Enumerable<Employee> inputEnumerable = <<child adapter>>; - // return new Enumerable<IntString>() { - // Enumerator<IntString> enumerator() { - // return new Enumerator<IntString>() { - // public void reset() { - // ... - Type outputJavaType = physType.getJavaRowType(); - final Type enumeratorType = - Types.of( - Enumerator.class, outputJavaType); - Type inputJavaType = result.physType.getJavaRowType(); - ParameterExpression inputEnumerator = - Expressions.parameter( - Types.of( - Enumerator.class, inputJavaType), - "inputEnumerator"); - Expression input = - RexToLixTranslator.convert( - Expressions.call( - inputEnumerator, - BuiltInMethod.ENUMERATOR_CURRENT.method), - inputJavaType); - - final RexBuilder rexBuilder = getCluster().getRexBuilder(); - final RexSimplify simplify = - new RexSimplify(rexBuilder, false, RexUtil.EXECUTOR); - final RexProgram program = this.program.normalize(rexBuilder, simplify); - - BlockStatement moveNextBody; - if (program.getCondition() == null) { - moveNextBody = - Blocks.toFunctionBlock( - Expressions.call( - inputEnumerator, - BuiltInMethod.ENUMERATOR_MOVE_NEXT.method)); - } else { - final BlockBuilder builder2 = new BlockBuilder(); - Expression condition = - RexToLixTranslator.translateCondition( - program, - typeFactory, - builder2, - new RexToLixTranslator.InputGetterImpl( - Collections.singletonList( - Pair.of(input, result.physType))), - implementor.allCorrelateVariables); - builder2.add( - Expressions.ifThen( - condition, - Expressions.return_( - null, Expressions.constant(true)))); - moveNextBody = - Expressions.block( - Expressions.while_( - Expressions.call( - inputEnumerator, - BuiltInMethod.ENUMERATOR_MOVE_NEXT.method), - builder2.toBlock()), - Expressions.return_( - null, - Expressions.constant(false))); - } - - final BlockBuilder builder3 = new BlockBuilder(); - List<Expression> expressions = - RexToLixTranslator.translateProjects( - program, - typeFactory, - builder3, - physType, - DataContext.ROOT, - new RexToLixTranslator.InputGetterImpl( - Collections.singletonList( - Pair.of(input, result.physType))), - implementor.allCorrelateVariables); - builder3.add( - Expressions.return_( - null, physType.record(expressions))); - BlockStatement currentBody = - builder3.toBlock(); - - final Expression inputEnumerable = - builder.append( - "inputEnumerable", result.block, false); - final Expression body = - Expressions.new_( - enumeratorType, - NO_EXPRS, - Expressions.list( - Expressions.fieldDecl( - Modifier.PUBLIC - | Modifier.FINAL, - inputEnumerator, - Expressions.call( - inputEnumerable, - BuiltInMethod.ENUMERABLE_ENUMERATOR.method)), - EnumUtils.overridingMethodDecl( - BuiltInMethod.ENUMERATOR_RESET.method, - NO_PARAMS, - Blocks.toFunctionBlock( - Expressions.call( - inputEnumerator, - BuiltInMethod.ENUMERATOR_RESET.method))), - EnumUtils.overridingMethodDecl( - BuiltInMethod.ENUMERATOR_MOVE_NEXT.method, - NO_PARAMS, - moveNextBody), - EnumUtils.overridingMethodDecl( - BuiltInMethod.ENUMERATOR_CLOSE.method, - NO_PARAMS, - Blocks.toFunctionBlock( - Expressions.call( - inputEnumerator, - BuiltInMethod.ENUMERATOR_CLOSE.method))), - Expressions.methodDecl( - Modifier.PUBLIC, - BRIDGE_METHODS - ? Object.class - : outputJavaType, - "current", - NO_PARAMS, - currentBody))); - builder.add( - Expressions.return_( - null, - Expressions.new_( - BuiltInMethod.ABSTRACT_ENUMERABLE_CTOR.constructor, - // TODO: generics - // Collections.singletonList(inputRowType), - NO_EXPRS, - ImmutableList.<MemberDeclaration>of( - Expressions.methodDecl( - Modifier.PUBLIC, - enumeratorType, - BuiltInMethod.ENUMERABLE_ENUMERATOR.method.getName(), - NO_PARAMS, - Blocks.toFunctionBlock(body)))))); - return implementor.result(physType, builder.toBlock()); - } - - public RexProgram getProgram() { - return program; - } -} - -// End EnumerableCalc.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalcRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalcRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalcRule.java deleted file mode 100644 index b94ff57..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCalcRule.java +++ /dev/null @@ -1,47 +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.LogicalCalc; - -/** - * Rule to convert a {@link org.apache.calcite.rel.logical.LogicalCalc} to an - * {@link EnumerableCalc}. - */ -class EnumerableCalcRule extends ConverterRule { - EnumerableCalcRule() { - // The predicate ensures that if there's a multiset, FarragoMultisetSplitter - // will work on it first. - super(LogicalCalc.class, RelOptUtil.CALC_PREDICATE, Convention.NONE, - EnumerableConvention.INSTANCE, "EnumerableCalcRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalCalc calc = (LogicalCalc) rel; - final RelNode input = calc.getInput(); - return EnumerableCalc.create( - convert(input, - input.getTraitSet().replace(EnumerableConvention.INSTANCE)), - calc.getProgram()); - } -} - -// End EnumerableCalcRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java deleted file mode 100644 index 59c2776..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java +++ /dev/null @@ -1,71 +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.RelNode; -import org.apache.calcite.rel.core.Collect; -import org.apache.calcite.util.BuiltInMethod; - -/** Implementation of {@link org.apache.calcite.rel.core.Collect} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableCollect extends Collect implements EnumerableRel { - public EnumerableCollect(RelOptCluster cluster, RelTraitSet traitSet, - RelNode child, String fieldName) { - super(cluster, traitSet, child, fieldName); - assert getConvention() instanceof EnumerableConvention; - assert getConvention() == child.getConvention(); - } - - @Override public EnumerableCollect copy(RelTraitSet traitSet, - RelNode newInput) { - return new EnumerableCollect(getCluster(), traitSet, newInput, fieldName); - } - - 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, Prefer.ARRAY); - final PhysType physType = - PhysTypeImpl.of( - implementor.getTypeFactory(), - getRowType(), - JavaRowFormat.LIST); - - // final Enumerable<Employee> child = <<child adapter>>; - // final List<Employee> list = child.toList(); - Expression child_ = - builder.append( - "child", result.block); - Expression list_ = - builder.append("list", - Expressions.call(child_, - BuiltInMethod.ENUMERABLE_TO_LIST.method)); - - builder.add( - Expressions.return_(null, - Expressions.call( - BuiltInMethod.SINGLETON_ENUMERABLE.method, list_))); - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableCollect.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollectRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollectRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollectRule.java deleted file mode 100644 index 4ab49b4..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollectRule.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.core.Collect; - -/** - * Rule to convert an {@link org.apache.calcite.rel.core.Collect} to an - * {@link EnumerableCollect}. - */ -class EnumerableCollectRule extends ConverterRule { - EnumerableCollectRule() { - super(Collect.class, Convention.NONE, EnumerableConvention.INSTANCE, - "EnumerableCollectRule"); - } - - public RelNode convert(RelNode rel) { - final Collect collect = (Collect) rel; - final RelTraitSet traitSet = - collect.getTraitSet().replace(EnumerableConvention.INSTANCE); - final RelNode input = collect.getInput(); - return new EnumerableCollect( - rel.getCluster(), - traitSet, - convert(input, - input.getTraitSet().replace(EnumerableConvention.INSTANCE)), - collect.getFieldName()); - } -} - -// End EnumerableCollectRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableConvention.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableConvention.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableConvention.java deleted file mode 100644 index ab3976f..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableConvention.java +++ /dev/null @@ -1,69 +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.ConventionTraitDef; -import org.apache.calcite.plan.RelOptPlanner; -import org.apache.calcite.plan.RelTrait; -import org.apache.calcite.plan.RelTraitDef; -import org.apache.calcite.plan.RelTraitSet; - -/** - * Family of calling conventions that return results as an - * {@link org.apache.calcite.linq4j.Enumerable}. - */ -public enum EnumerableConvention implements Convention { - INSTANCE; - - /** Cost of an enumerable node versus implementing an equivalent node in a - * "typical" calling convention. */ - public static final double COST_MULTIPLIER = 1.0d; - - @Override public String toString() { - return getName(); - } - - public Class getInterface() { - return EnumerableRel.class; - } - - public String getName() { - return "ENUMERABLE"; - } - - public RelTraitDef getTraitDef() { - return ConventionTraitDef.INSTANCE; - } - - public boolean satisfies(RelTrait trait) { - return this == trait; - } - - public void register(RelOptPlanner planner) {} - - public boolean canConvertConvention(Convention toConvention) { - return false; - } - - public boolean useAbstractConvertersForConversion(RelTraitSet fromTraits, - RelTraitSet toTraits) { - return false; - } -} - -// End EnumerableConvention.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelate.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelate.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelate.java deleted file mode 100644 index 6085520..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelate.java +++ /dev/null @@ -1,117 +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.linq4j.tree.ParameterExpression; -import org.apache.calcite.linq4j.tree.Primitive; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.Correlate; -import org.apache.calcite.rel.core.CorrelationId; -import org.apache.calcite.rel.core.JoinRelType; -import org.apache.calcite.sql.SemiJoinType; -import org.apache.calcite.util.BuiltInMethod; -import org.apache.calcite.util.ImmutableBitSet; - -import com.google.common.collect.ImmutableList; - -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; - -/** Implementation of {@link org.apache.calcite.rel.core.Correlate} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableCorrelate extends Correlate - implements EnumerableRel { - - public EnumerableCorrelate(RelOptCluster cluster, RelTraitSet traits, - RelNode left, RelNode right, - CorrelationId correlationId, - ImmutableBitSet requiredColumns, SemiJoinType joinType) { - super(cluster, traits, left, right, correlationId, requiredColumns, - joinType); - } - - @Override public EnumerableCorrelate copy(RelTraitSet traitSet, - RelNode left, RelNode right, CorrelationId correlationId, - ImmutableBitSet requiredColumns, SemiJoinType joinType) { - return new EnumerableCorrelate(getCluster(), - traitSet, left, right, correlationId, requiredColumns, joinType); - } - - public Result implement(EnumerableRelImplementor implementor, - Prefer pref) { - final BlockBuilder builder = new BlockBuilder(); - final Result leftResult = - implementor.visitChild(this, 0, (EnumerableRel) left, pref); - Expression leftExpression = - builder.append( - "left", leftResult.block); - - final BlockBuilder corrBlock = new BlockBuilder(); - Type corrVarType = leftResult.physType.getJavaRowType(); - ParameterExpression corrRef; // correlate to be used in inner loop - ParameterExpression corrArg; // argument to correlate lambda (must be boxed) - if (!Primitive.is(corrVarType)) { - corrArg = - Expressions.parameter(Modifier.FINAL, - corrVarType, getCorrelVariable()); - corrRef = corrArg; - } else { - corrArg = - Expressions.parameter(Modifier.FINAL, - Primitive.box(corrVarType), "$box" + getCorrelVariable()); - corrRef = (ParameterExpression) corrBlock.append(getCorrelVariable(), - Expressions.unbox(corrArg)); - } - - implementor.registerCorrelVariable(getCorrelVariable(), corrRef, - corrBlock, leftResult.physType); - - final Result rightResult = - implementor.visitChild(this, 1, (EnumerableRel) right, pref); - - implementor.clearCorrelVariable(getCorrelVariable()); - - corrBlock.add(rightResult.block); - - final PhysType physType = - PhysTypeImpl.of( - implementor.getTypeFactory(), - getRowType(), - pref.prefer(JavaRowFormat.CUSTOM)); - - Expression selector = - EnumUtils.joinSelector( - joinType.returnsJustFirstInput() ? joinType.toJoinType() - : JoinRelType.INNER, physType, - ImmutableList.of(leftResult.physType, rightResult.physType)); - - builder.append( - Expressions.call(leftExpression, BuiltInMethod.CORRELATE_JOIN.method, - Expressions.constant(joinType.toLinq4j()), - Expressions.lambda(corrBlock.toBlock(), corrArg), - selector)); - - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableCorrelate.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelateRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelateRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelateRule.java deleted file mode 100644 index eb08a27..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelateRule.java +++ /dev/null @@ -1,51 +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.LogicalCorrelate; - -/** - * Implementation of nested loops over enumerable inputs. - */ -public class EnumerableCorrelateRule extends ConverterRule { - EnumerableCorrelateRule() { - super(LogicalCorrelate.class, Convention.NONE, - EnumerableConvention.INSTANCE, "EnumerableCorrelateRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalCorrelate c = (LogicalCorrelate) rel; - final RelTraitSet traitSet = - c.getTraitSet().replace(EnumerableConvention.INSTANCE); - return new EnumerableCorrelate( - rel.getCluster(), - traitSet, - convert(c.getLeft(), c.getLeft().getTraitSet() - .replace(EnumerableConvention.INSTANCE)), - convert(c.getRight(), c.getRight().getTraitSet() - .replace(EnumerableConvention.INSTANCE)), - c.getCorrelationId(), - c.getRequiredColumns(), - c.getJoinType()); - } -} - -// End EnumerableCorrelateRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilter.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilter.java deleted file mode 100644 index 894ff16..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilter.java +++ /dev/null @@ -1,87 +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.RelDistribution; -import org.apache.calcite.rel.RelDistributionTraitDef; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.core.Filter; -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.RexNode; - -import com.google.common.base.Supplier; - -import java.util.List; - -/** Implementation of {@link org.apache.calcite.rel.core.Filter} in - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableFilter - extends Filter - implements EnumerableRel { - /** Creates an EnumerableFilter. - * - * <p>Use {@link #create} unless you know what you're doing. */ - public EnumerableFilter( - RelOptCluster cluster, - RelTraitSet traitSet, - RelNode child, - RexNode condition) { - super(cluster, traitSet, child, condition); - assert getConvention() instanceof EnumerableConvention; - } - - /** Creates an EnumerableFilter. */ - public static EnumerableFilter create(final RelNode input, - RexNode condition) { - 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.filter(mq, input); - } - }) - .replaceIf(RelDistributionTraitDef.INSTANCE, - new Supplier<RelDistribution>() { - public RelDistribution get() { - return RelMdDistribution.filter(mq, input); - } - }); - return new EnumerableFilter(cluster, traitSet, input, condition); - } - - public EnumerableFilter copy(RelTraitSet traitSet, RelNode input, - RexNode condition) { - return new EnumerableFilter(getCluster(), traitSet, input, condition); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - // EnumerableCalc is always better - throw new UnsupportedOperationException(); - } -} - -// End EnumerableFilter.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterRule.java deleted file mode 100644 index 51fcdd4..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterRule.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.LogicalFilter; - -/** - * Rule to convert a {@link org.apache.calcite.rel.logical.LogicalFilter} to an - * {@link EnumerableFilter}. - */ -class EnumerableFilterRule extends ConverterRule { - EnumerableFilterRule() { - super(LogicalFilter.class, RelOptUtil.FILTER_PREDICATE, Convention.NONE, - EnumerableConvention.INSTANCE, "EnumerableFilterRule"); - } - - public RelNode convert(RelNode rel) { - final LogicalFilter filter = (LogicalFilter) rel; - return new EnumerableFilter(rel.getCluster(), - rel.getTraitSet().replace(EnumerableConvention.INSTANCE), - convert(filter.getInput(), - filter.getInput().getTraitSet() - .replace(EnumerableConvention.INSTANCE)), - filter.getCondition()); - } -} - -// End EnumerableFilterRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterToCalcRule.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterToCalcRule.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterToCalcRule.java deleted file mode 100644 index 9a61124..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableFilterToCalcRule.java +++ /dev/null @@ -1,52 +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.rel.type.RelDataType; -import org.apache.calcite.rex.RexBuilder; -import org.apache.calcite.rex.RexProgram; -import org.apache.calcite.rex.RexProgramBuilder; - -/** Variant of {@link org.apache.calcite.rel.rules.FilterToCalcRule} for - * {@link org.apache.calcite.adapter.enumerable.EnumerableConvention enumerable calling convention}. */ -public class EnumerableFilterToCalcRule extends RelOptRule { - EnumerableFilterToCalcRule() { - super(operand(EnumerableFilter.class, any())); - } - - public void onMatch(RelOptRuleCall call) { - final EnumerableFilter filter = call.rel(0); - final RelNode input = filter.getInput(); - - // Create a program containing a filter. - final RexBuilder rexBuilder = filter.getCluster().getRexBuilder(); - final RelDataType inputRowType = input.getRowType(); - final RexProgramBuilder programBuilder = - new RexProgramBuilder(inputRowType, rexBuilder); - programBuilder.addIdentity(); - programBuilder.addCondition(filter.getCondition()); - final RexProgram program = programBuilder.getProgram(); - - final EnumerableCalc calc = EnumerableCalc.create(input, program); - call.transformTo(calc); - } -} - -// End EnumerableFilterToCalcRule.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java deleted file mode 100644 index 19d5f2a..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java +++ /dev/null @@ -1,205 +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.avatica.Helper; -import org.apache.calcite.interpreter.InterpretableConvention; -import org.apache.calcite.interpreter.InterpretableRel; -import org.apache.calcite.interpreter.Interpreter; -import org.apache.calcite.interpreter.Node; -import org.apache.calcite.interpreter.Row; -import org.apache.calcite.interpreter.Sink; -import org.apache.calcite.jdbc.CalcitePrepare; -import org.apache.calcite.linq4j.AbstractEnumerable; -import org.apache.calcite.linq4j.Enumerable; -import org.apache.calcite.linq4j.Enumerator; -import org.apache.calcite.linq4j.tree.ClassDeclaration; -import org.apache.calcite.linq4j.tree.Expressions; -import org.apache.calcite.plan.ConventionTraitDef; -import org.apache.calcite.plan.RelOptCluster; -import org.apache.calcite.plan.RelTraitSet; -import org.apache.calcite.prepare.CalcitePrepareImpl; -import org.apache.calcite.rel.RelNode; -import org.apache.calcite.rel.convert.ConverterImpl; -import org.apache.calcite.runtime.ArrayBindable; -import org.apache.calcite.runtime.Bindable; -import org.apache.calcite.runtime.Hook; -import org.apache.calcite.runtime.Typed; -import org.apache.calcite.runtime.Utilities; -import org.apache.calcite.util.Util; - -import org.codehaus.commons.compiler.CompileException; -import org.codehaus.commons.compiler.CompilerFactoryFactory; -import org.codehaus.commons.compiler.IClassBodyEvaluator; -import org.codehaus.commons.compiler.ICompilerFactory; - -import java.io.IOException; -import java.io.StringReader; -import java.util.List; -import java.util.Map; - -/** - * Relational expression that converts an enumerable input to interpretable - * calling convention. - * - * @see EnumerableConvention - * @see org.apache.calcite.interpreter.BindableConvention - */ -public class EnumerableInterpretable extends ConverterImpl - implements InterpretableRel { - protected EnumerableInterpretable(RelOptCluster cluster, RelNode input) { - super(cluster, ConventionTraitDef.INSTANCE, - cluster.traitSetOf(InterpretableConvention.INSTANCE), input); - } - - @Override public EnumerableInterpretable copy(RelTraitSet traitSet, - List<RelNode> inputs) { - return new EnumerableInterpretable(getCluster(), sole(inputs)); - } - - public Node implement(final InterpreterImplementor implementor) { - final Bindable bindable = toBindable(implementor.internalParameters, - implementor.spark, (EnumerableRel) getInput(), - EnumerableRel.Prefer.ARRAY); - final ArrayBindable arrayBindable = box(bindable); - final Enumerable<Object[]> enumerable = - arrayBindable.bind(implementor.dataContext); - return new EnumerableNode(enumerable, implementor.interpreter, this); - } - - public static Bindable toBindable(Map<String, Object> parameters, - CalcitePrepare.SparkHandler spark, EnumerableRel rel, - EnumerableRel.Prefer prefer) { - EnumerableRelImplementor relImplementor = - new EnumerableRelImplementor(rel.getCluster().getRexBuilder(), - parameters); - - final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer); - String s = Expressions.toString(expr.memberDeclarations, "\n", false); - - if (CalcitePrepareImpl.DEBUG) { - Util.debugCode(System.out, s); - } - - Hook.JAVA_PLAN.run(s); - - try { - if (spark != null && spark.enabled()) { - return spark.compile(expr, s); - } else { - return getBindable(expr, s, rel.getRowType().getFieldCount()); - } - } catch (Exception e) { - throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n" - + s, e); - } - } - - static ArrayBindable getArrayBindable(ClassDeclaration expr, String s, - int fieldCount) throws CompileException, IOException { - Bindable bindable = getBindable(expr, s, fieldCount); - return box(bindable); - } - - static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount) - throws CompileException, IOException { - ICompilerFactory compilerFactory; - try { - compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(); - } catch (Exception e) { - throw new IllegalStateException( - "Unable to instantiate java compiler", e); - } - IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator(); - cbe.setClassName(expr.name); - cbe.setExtendedClass(Utilities.class); - cbe.setImplementedInterfaces( - fieldCount == 1 - ? new Class[] {Bindable.class, Typed.class} - : new Class[] {ArrayBindable.class}); - cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader()); - if (CalcitePrepareImpl.DEBUG) { - // Add line numbers to the generated janino class - cbe.setDebuggingInformation(true, true, true); - } - return (Bindable) cbe.createInstance(new StringReader(s)); - } - - /** Converts a bindable over scalar values into an array bindable, with each - * row as an array of 1 element. */ - static ArrayBindable box(final Bindable bindable) { - if (bindable instanceof ArrayBindable) { - return (ArrayBindable) bindable; - } - return new ArrayBindable() { - public Class<Object[]> getElementType() { - return Object[].class; - } - - public Enumerable<Object[]> bind(DataContext dataContext) { - final Enumerable<?> enumerable = bindable.bind(dataContext); - return new AbstractEnumerable<Object[]>() { - public Enumerator<Object[]> enumerator() { - final Enumerator<?> enumerator = enumerable.enumerator(); - return new Enumerator<Object[]>() { - public Object[] current() { - return new Object[] {enumerator.current()}; - } - - public boolean moveNext() { - return enumerator.moveNext(); - } - - public void reset() { - enumerator.reset(); - } - - public void close() { - enumerator.close(); - } - }; - } - }; - } - }; - } - - /** Interpreter node that reads from an {@link Enumerable}. - * - * <p>From the interpreter's perspective, it is a leaf node. */ - private static class EnumerableNode implements Node { - private final Enumerable<Object[]> enumerable; - private final Sink sink; - - public EnumerableNode(Enumerable<Object[]> enumerable, - Interpreter interpreter, EnumerableInterpretable rel) { - this.enumerable = enumerable; - this.sink = interpreter.sink(rel); - } - - public void run() throws InterruptedException { - final Enumerator<Object[]> enumerator = enumerable.enumerator(); - while (enumerator.moveNext()) { - Object[] values = enumerator.current(); - sink.send(Row.of(values)); - } - } - } -} - -// End EnumerableInterpretable.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreter.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreter.java deleted file mode 100644 index 1c53483..0000000 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpreter.java +++ /dev/null @@ -1,104 +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.interpreter.Interpreter; -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.RelNode; -import org.apache.calcite.rel.SingleRel; -import org.apache.calcite.rel.metadata.RelMetadataQuery; -import org.apache.calcite.util.BuiltInMethod; - -import java.util.List; - -/** Relational expression that executes its children using an interpreter. - * - * <p>Although quite a few kinds of {@link org.apache.calcite.rel.RelNode} can - * be interpreted, this is only created by default for - * {@link org.apache.calcite.schema.FilterableTable} and - * {@link org.apache.calcite.schema.ProjectableFilterableTable}. - */ -public class EnumerableInterpreter extends SingleRel - implements EnumerableRel { - private final double factor; - - /** - * Creates an EnumerableInterpreter. - * - * <p>Use {@link #create} unless you know what you're doing. - * - * @param cluster Cluster - * @param traitSet Traits - * @param input Input relation - * @param factor Cost multiply factor - */ - public EnumerableInterpreter(RelOptCluster cluster, RelTraitSet traitSet, - RelNode input, double factor) { - super(cluster, traitSet, input); - assert getConvention() instanceof EnumerableConvention; - this.factor = factor; - } - - /** - * Creates an EnumerableInterpreter. - * - * @param input Input relation - * @param factor Cost multiply factor - */ - public static EnumerableInterpreter create(RelNode input, double factor) { - final RelTraitSet traitSet = input.getTraitSet() - .replace(EnumerableConvention.INSTANCE); - return new EnumerableInterpreter(input.getCluster(), traitSet, input, - factor); - } - - @Override public RelOptCost computeSelfCost(RelOptPlanner planner, - RelMetadataQuery mq) { - return super.computeSelfCost(planner, mq).multiplyBy(factor); - } - - @Override public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) { - return new EnumerableInterpreter(getCluster(), traitSet, sole(inputs), - factor); - } - - public Result implement(EnumerableRelImplementor implementor, Prefer pref) { - final JavaTypeFactory typeFactory = implementor.getTypeFactory(); - final BlockBuilder builder = new BlockBuilder(); - final PhysType physType = - PhysTypeImpl.of(typeFactory, getRowType(), JavaRowFormat.ARRAY); - final Expression interpreter_ = builder.append("interpreter", - Expressions.new_(Interpreter.class, - implementor.getRootExpression(), - implementor.stash(getInput(), RelNode.class))); - final Expression sliced_ = - getRowType().getFieldCount() == 1 - ? Expressions.call(BuiltInMethod.SLICE0.method, interpreter_) - : interpreter_; - builder.add(sliced_); - return implementor.result(physType, builder.toBlock()); - } -} - -// End EnumerableInterpreter.java
