/
EnumerableCalc.java
273 lines (255 loc) · 10.5 KB
/
EnumerableCalc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* 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.RelOptPredicateList;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelCollation;
import org.apache.calcite.rel.RelCollationTraitDef;
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.sql.validate.SqlConformance;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.apache.calcite.util.BuiltInMethod;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;
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 = cluster.getMetadataQuery();
final RelTraitSet traitSet = cluster.traitSet()
.replace(EnumerableConvention.INSTANCE)
.replaceIfs(RelCollationTraitDef.INSTANCE,
() -> RelMdCollation.calc(mq, input, program))
.replaceIf(RelDistributionTraitDef.INSTANCE,
() -> 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 RelMetadataQuery mq = RelMetadataQuery.instance();
final RelOptPredicateList predicates = mq.getPulledUpPredicates(child);
final RexSimplify simplify =
new RexSimplify(rexBuilder, predicates, 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, implementor.getConformance());
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();
final SqlConformance conformance =
(SqlConformance) implementor.map.getOrDefault("_conformance",
SqlConformanceEnum.DEFAULT);
List<Expression> expressions =
RexToLixTranslator.translateProjects(
program,
typeFactory,
conformance,
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