Skip to content

Commit 0ac0384

Browse files
committed
[feature][dingo-exec] Add generate code for window
1 parent 8f04962 commit 0ac0384

File tree

15 files changed

+1493
-11
lines changed

15 files changed

+1493
-11
lines changed

dingo-calcite/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ dependencies {
4040
implementation group: 'io.dingodb.expr', name: 'dingo-expr-coding', version: 'dingo-expr'.v()
4141
implementation group: 'io.dingodb.expr', name: 'dingo-expr-common', version: 'dingo-expr'.v()
4242
implementation group: 'io.dropwizard.metrics', name: 'metrics-core', version: 'metrics-core'.v()
43+
implementation group: 'org.codehaus.janino', name:'janino', version: '3.1.8'
44+
implementation group: 'org.codehaus.janino', name:'commons-compiler', version: '3.1.8'
4345

4446
api group: 'io.dingodb.expr', name: 'dingo-expr-parser', version: 'dingo-expr'.v()
4547
api group: 'io.dingodb.expr', name: 'dingo-expr-rel', version: 'dingo-expr'.v()

dingo-calcite/src/main/java/io/dingodb/calcite/rel/DingoWindow.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.dingodb.calcite.rel;
1818

19-
import com.sun.istack.internal.Nullable;
2019
import io.dingodb.calcite.visitor.DingoRelVisitor;
2120
import org.apache.calcite.adapter.enumerable.PhysType;
2221
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
@@ -43,14 +42,19 @@ public DingoWindow(
4342
super(cluster, traitSet, hints, input, constants, rowType, groups);
4443
}
4544

46-
private static class WindowRelInputGetter
45+
@Override
46+
public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
47+
return new DingoWindow(this.getCluster(), traitSet, this.hints, sole(inputs), constants, rowType, groups);
48+
}
49+
50+
public static class WindowRelInputGetter
4751
implements RexToLixTranslator.InputGetter {
4852
private final Expression row;
4953
private final PhysType rowPhysType;
5054
private final int actualInputFieldCount;
5155
private final List<Expression> constants;
5256

53-
private WindowRelInputGetter(Expression row,
57+
public WindowRelInputGetter(Expression row,
5458
PhysType rowPhysType, int actualInputFieldCount,
5559
List<Expression> constants) {
5660
this.row = row;
@@ -59,7 +63,7 @@ private WindowRelInputGetter(Expression row,
5963
this.constants = constants;
6064
}
6165

62-
@Override public Expression field(BlockBuilder list, int index, @Nullable Type storageType) {
66+
@Override public Expression field(BlockBuilder list, int index, Type storageType) {
6367
if (index < actualInputFieldCount) {
6468
Expression current = list.append("current", row);
6569
return rowPhysType.fieldReference(current, index, storageType);

dingo-calcite/src/main/java/io/dingodb/calcite/rel/dingo/DingoStreamingConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public double estimateRowCount(@NonNull RelMetadataQuery mq) {
5757
Set<DingoRelPartition> partitions = getStreaming().getPartitions();
5858
Set<DingoRelPartition> inputPartitions = inputStreaming.getPartitions();
5959
this.rowCount = rowCount;
60-
assert partitions != null && inputPartitions != null;
61-
if (partitions.size() > inputPartitions.size()) {
60+
if (partitions == null || inputPartitions == null) {
61+
return rowCount;
62+
} else if (partitions.size() > inputPartitions.size()) {
6263
for (int i = 0; i < partitions.size() - inputPartitions.size(); ++i) {
6364
rowCount /= 1.000001d;
6465
}

dingo-calcite/src/main/java/io/dingodb/calcite/rule/DingoRules.java

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import io.dingodb.calcite.rule.dingo.DingoPhysicalRules;
21+
import io.dingodb.calcite.rule.dingo.DingoWindowRule;
22+
import org.apache.calcite.config.CalciteSystemProperty;
2123
import org.apache.calcite.plan.RelOptRule;
24+
import org.apache.calcite.plan.volcano.AbstractConverter;
2225
import org.apache.calcite.rel.core.Correlate;
2326
import org.apache.calcite.rel.rules.CoreRules;
27+
import org.apache.calcite.rel.rules.DateRangeRules;
28+
import org.apache.calcite.rel.rules.JoinPushThroughJoinRule;
29+
import org.apache.calcite.rel.rules.PruneEmptyRules;
2430

2531
import java.util.List;
2632

@@ -193,6 +199,78 @@ public final class DingoRules {
193199

194200
public static final DingoFilterReduceExpressionsRule FILTER_REDUCE_EXPRESSIONS_RULE
195201
= DingoFilterReduceExpressionsRule.Config.DEFAULT.toRule();
202+
public static final DingoWindowRule DINGO_WINDOW_RULE =
203+
DingoWindowRule.DEFAULT.toRule(DingoWindowRule.class);
204+
205+
public static final List<RelOptRule> BASE_RULES = ImmutableList.of(
206+
CoreRules.AGGREGATE_STAR_TABLE,
207+
CoreRules.AGGREGATE_PROJECT_STAR_TABLE,
208+
CalciteSystemProperty.COMMUTE.value()
209+
? CoreRules.JOIN_ASSOCIATE
210+
: CoreRules.PROJECT_MERGE,
211+
CoreRules.FILTER_SCAN,
212+
CoreRules.PROJECT_FILTER_TRANSPOSE,
213+
CoreRules.FILTER_PROJECT_TRANSPOSE,
214+
CoreRules.FILTER_INTO_JOIN,
215+
CoreRules.JOIN_PUSH_EXPRESSIONS,
216+
CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES,
217+
CoreRules.AGGREGATE_EXPAND_WITHIN_DISTINCT,
218+
CoreRules.AGGREGATE_CASE_TO_FILTER,
219+
CoreRules.AGGREGATE_REDUCE_FUNCTIONS,
220+
CoreRules.FILTER_AGGREGATE_TRANSPOSE,
221+
CoreRules.PROJECT_WINDOW_TRANSPOSE,
222+
CoreRules.MATCH,
223+
CoreRules.JOIN_COMMUTE,
224+
JoinPushThroughJoinRule.RIGHT,
225+
JoinPushThroughJoinRule.LEFT,
226+
//CoreRules.SORT_PROJECT_TRANSPOSE,
227+
//CoreRules.SORT_JOIN_TRANSPOSE,
228+
//CoreRules.SORT_REMOVE_CONSTANT_KEYS,
229+
//CoreRules.SORT_UNION_TRANSPOSE,
230+
CoreRules.EXCHANGE_REMOVE_CONSTANT_KEYS,
231+
CoreRules.SORT_EXCHANGE_REMOVE_CONSTANT_KEYS);
232+
233+
public static final List<RelOptRule> ABSTRACT_RELATIONAL_RULES = ImmutableList.of(
234+
CoreRules.FILTER_INTO_JOIN,
235+
CoreRules.JOIN_CONDITION_PUSH,
236+
AbstractConverter.ExpandConversionRule.INSTANCE,
237+
CoreRules.JOIN_COMMUTE,
238+
CoreRules.PROJECT_TO_SEMI_JOIN,
239+
CoreRules.JOIN_ON_UNIQUE_TO_SEMI_JOIN,
240+
CoreRules.JOIN_TO_SEMI_JOIN,
241+
CoreRules.AGGREGATE_REMOVE,
242+
CoreRules.UNION_TO_DISTINCT,
243+
CoreRules.PROJECT_REMOVE,
244+
CoreRules.PROJECT_AGGREGATE_MERGE,
245+
CoreRules.AGGREGATE_JOIN_TRANSPOSE,
246+
CoreRules.AGGREGATE_MERGE,
247+
CoreRules.AGGREGATE_PROJECT_MERGE,
248+
CoreRules.CALC_REMOVE
249+
//CoreRules.SORT_REMOVE
250+
);
251+
252+
public static final List<RelOptRule> ABSTRACT_RULES = ImmutableList.of(
253+
CoreRules.AGGREGATE_ANY_PULL_UP_CONSTANTS,
254+
CoreRules.UNION_PULL_UP_CONSTANTS,
255+
PruneEmptyRules.UNION_INSTANCE,
256+
PruneEmptyRules.INTERSECT_INSTANCE,
257+
PruneEmptyRules.MINUS_INSTANCE,
258+
PruneEmptyRules.PROJECT_INSTANCE,
259+
PruneEmptyRules.FILTER_INSTANCE,
260+
PruneEmptyRules.SORT_INSTANCE,
261+
PruneEmptyRules.AGGREGATE_INSTANCE,
262+
PruneEmptyRules.JOIN_LEFT_INSTANCE,
263+
PruneEmptyRules.JOIN_RIGHT_INSTANCE,
264+
PruneEmptyRules.SORT_FETCH_ZERO_INSTANCE,
265+
PruneEmptyRules.EMPTY_TABLE_INSTANCE,
266+
CoreRules.UNION_MERGE,
267+
CoreRules.INTERSECT_MERGE,
268+
CoreRules.MINUS_MERGE,
269+
CoreRules.PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW,
270+
CoreRules.FILTER_MERGE,
271+
DateRangeRules.FILTER_INSTANCE,
272+
CoreRules.INTERSECT_TO_DISTINCT);
273+
196274

197275
private static final List<RelOptRule> rules = ImmutableList.of(
198276
CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES,
@@ -258,8 +336,8 @@ public final class DingoRules {
258336
DINGO_DOCUMENT_JOIN_RULE,
259337
DINGO_DOCUMENT_PROJECT_RULE,
260338
DINGO_DOCUMENT_FILTER_RULE,
261-
DOCUMENT_INDEX_RANGE_SCAN_RULE
262-
339+
DOCUMENT_INDEX_RANGE_SCAN_RULE,
340+
DINGO_WINDOW_RULE
263341
);
264342

265343
private DingoRules() {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2021 DataCanvas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.dingodb.calcite.utils;
18+
19+
import org.apache.calcite.adapter.enumerable.EnumUtils;
20+
import org.apache.calcite.adapter.java.JavaTypeFactory;
21+
import org.apache.calcite.rel.type.RelDataType;
22+
import org.apache.calcite.rel.type.RelDataTypeField;
23+
import org.apache.calcite.rex.RexNode;
24+
import org.checkerframework.checker.nullness.qual.Nullable;
25+
26+
import java.lang.reflect.Type;
27+
import java.util.AbstractList;
28+
import java.util.List;
29+
30+
import static java.util.Objects.requireNonNull;
31+
32+
public class DingoEnumUtils {
33+
public static List<RelDataType> fieldRowTypes(
34+
final RelDataType inputRowType,
35+
final @Nullable List<? extends RexNode> extraInputs,
36+
final List<Integer> argList) {
37+
final List<RelDataTypeField> inputFields = inputRowType.getFieldList();
38+
return new AbstractList<RelDataType>() {
39+
@Override public RelDataType get(int index) {
40+
final int arg = argList.get(index);
41+
return arg < inputFields.size()
42+
? inputFields.get(arg).getType()
43+
: requireNonNull(extraInputs, "extraInputs")
44+
.get(arg - inputFields.size()).getType();
45+
}
46+
@Override public int size() {
47+
return argList.size();
48+
}
49+
};
50+
}
51+
52+
public static Type javaClass(
53+
JavaTypeFactory typeFactory, RelDataType type) {
54+
final Type clazz = typeFactory.getJavaClass(type);
55+
return clazz instanceof Class ? clazz : Object[].class;
56+
}
57+
58+
public static List<Type> fieldTypes(
59+
final JavaTypeFactory typeFactory,
60+
final List<? extends RelDataType> inputTypes) {
61+
return new AbstractList<Type>() {
62+
@Override public Type get(int index) {
63+
return javaClass(typeFactory, inputTypes.get(index));
64+
}
65+
@Override public int size() {
66+
return inputTypes.size();
67+
}
68+
};
69+
}
70+
71+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021 DataCanvas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.dingodb.calcite.utils;
18+
19+
import org.apache.calcite.adapter.enumerable.JavaRowFormat;
20+
import org.apache.calcite.adapter.enumerable.PhysType;
21+
import org.apache.calcite.linq4j.tree.BlockStatement;
22+
23+
public class DingoRelResult {
24+
public final BlockStatement block;
25+
26+
/**
27+
* Describes the Java type returned by this relational expression, and the
28+
* mapping between it and the fields of the logical row type.
29+
*/
30+
public final PhysType physType;
31+
public final JavaRowFormat format;
32+
33+
public DingoRelResult(BlockStatement block, PhysType physType,
34+
JavaRowFormat format) {
35+
this.block = block;
36+
this.physType = physType;
37+
this.format = format;
38+
}
39+
}

0 commit comments

Comments
 (0)