Skip to content

Commit 16e17e3

Browse files
committed
[feature][dingo-calcite] Support window function
1 parent c6fcab7 commit 16e17e3

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed

dingo-calcite/src/main/java/io/dingodb/calcite/DingoParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ public RelNode optimize(RelNode relNode) {
414414
List<RelOptRule> rules = DingoRules.rules();
415415
ImmutableList.Builder<RelOptRule> builder = ImmutableList.builder();
416416
builder.addAll(rules);
417+
418+
builder.addAll(DingoRules.ABSTRACT_RELATIONAL_RULES);
419+
builder.addAll(DingoRules.ABSTRACT_RULES);
420+
builder.addAll(DingoRules.BASE_RULES);
421+
417422
if (!context.getConfig().topDownOpt()) {
418423
// This is needed for `IterativeRuleDriver`.
419424
builder.add(AbstractConverter.ExpandConversionRule.INSTANCE);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.rel;
18+
19+
import com.sun.istack.internal.Nullable;
20+
import io.dingodb.calcite.visitor.DingoRelVisitor;
21+
import org.apache.calcite.adapter.enumerable.PhysType;
22+
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
23+
import org.apache.calcite.linq4j.tree.BlockBuilder;
24+
import org.apache.calcite.linq4j.tree.Expression;
25+
import org.apache.calcite.plan.RelOptCluster;
26+
import org.apache.calcite.plan.RelTraitSet;
27+
import org.apache.calcite.rel.RelNode;
28+
import org.apache.calcite.rel.core.Window;
29+
import org.apache.calcite.rel.hint.RelHint;
30+
import org.apache.calcite.rel.type.RelDataType;
31+
import org.apache.calcite.rex.RexLiteral;
32+
import org.checkerframework.checker.nullness.qual.NonNull;
33+
34+
import java.lang.reflect.Type;
35+
import java.util.List;
36+
37+
public class DingoWindow extends Window implements DingoRel {
38+
39+
public DingoWindow(
40+
RelOptCluster cluster, RelTraitSet traitSet, List<RelHint> hints, RelNode input,
41+
List<RexLiteral> constants, RelDataType rowType, List<Group> groups
42+
) {
43+
super(cluster, traitSet, hints, input, constants, rowType, groups);
44+
}
45+
46+
private static class WindowRelInputGetter
47+
implements RexToLixTranslator.InputGetter {
48+
private final Expression row;
49+
private final PhysType rowPhysType;
50+
private final int actualInputFieldCount;
51+
private final List<Expression> constants;
52+
53+
private WindowRelInputGetter(Expression row,
54+
PhysType rowPhysType, int actualInputFieldCount,
55+
List<Expression> constants) {
56+
this.row = row;
57+
this.rowPhysType = rowPhysType;
58+
this.actualInputFieldCount = actualInputFieldCount;
59+
this.constants = constants;
60+
}
61+
62+
@Override public Expression field(BlockBuilder list, int index, @Nullable Type storageType) {
63+
if (index < actualInputFieldCount) {
64+
Expression current = list.append("current", row);
65+
return rowPhysType.fieldReference(current, index, storageType);
66+
}
67+
return constants.get(index - actualInputFieldCount);
68+
}
69+
}
70+
71+
72+
@Override
73+
public <T> T accept(@NonNull DingoRelVisitor<T> visitor) {
74+
return visitor.visit(this);
75+
}
76+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.rule.dingo;
18+
19+
import io.dingodb.calcite.rel.DingoWindow;
20+
import io.dingodb.calcite.rel.logical.LogicalScanWithRelOp;
21+
import io.dingodb.calcite.traits.DingoConvention;
22+
import org.apache.calcite.plan.Convention;
23+
import org.apache.calcite.plan.RelTraitSet;
24+
import org.apache.calcite.rel.RelNode;
25+
import org.apache.calcite.rel.convert.ConverterRule;
26+
import org.apache.calcite.rel.core.Window;
27+
import org.apache.calcite.rel.logical.LogicalWindow;
28+
import org.checkerframework.checker.nullness.qual.Nullable;
29+
30+
public class DingoWindowRule extends ConverterRule {
31+
public static final Config DEFAULT = Config.INSTANCE
32+
.withConversion(
33+
LogicalWindow.class,
34+
Convention.NONE,
35+
DingoConvention.INSTANCE,
36+
"DingoWindowRule"
37+
)
38+
.withRuleFactory(DingoWindowRule::new);
39+
40+
protected DingoWindowRule(Config config) {
41+
super(config);
42+
}
43+
44+
@Override
45+
public @Nullable RelNode convert(RelNode rel) {
46+
final Window winAgg = (Window) rel;
47+
final RelTraitSet traitSet =
48+
winAgg.getTraitSet().replace(DingoConvention.INSTANCE);
49+
final RelNode child = winAgg.getInput();
50+
final RelNode convertedChild =
51+
convert(child,
52+
child.getTraitSet().replace(DingoConvention.INSTANCE));
53+
return new DingoWindow(rel.getCluster(), traitSet, winAgg.getHints(), convertedChild,
54+
winAgg.getConstants(), winAgg.getRowType(), winAgg.groups);
55+
}
56+
}

dingo-calcite/src/main/java/io/dingodb/calcite/visitor/DingoJobVisitor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import io.dingodb.calcite.rel.DingoUnion;
4545
import io.dingodb.calcite.rel.DingoValues;
4646
import io.dingodb.calcite.rel.DingoVector;
47+
import io.dingodb.calcite.rel.DingoWindow;
4748
import io.dingodb.calcite.rel.DocumentStreamConvertor;
4849
import io.dingodb.calcite.rel.VectorStreamConvertor;
4950
import io.dingodb.calcite.rel.dingo.DingoDocumentScanFilter;
@@ -97,6 +98,7 @@
9798
import io.dingodb.calcite.visitor.function.DingoValuesVisitFun;
9899
import io.dingodb.calcite.visitor.function.DingoVectorStreamingVisitFun;
99100
import io.dingodb.calcite.visitor.function.DingoVectorVisitFun;
101+
import io.dingodb.calcite.visitor.function.DingoWindowVisitFun;
100102
import io.dingodb.common.ExecuteVariables;
101103
import io.dingodb.common.Location;
102104
import io.dingodb.common.log.LogUtils;
@@ -419,4 +421,8 @@ public Collection<Vertex> visit(@NonNull DingoDocumentScanFilter dingoDocumentSc
419421
return DingoDocumentScanFilterVisitFun.visit(job, idGenerator, currentLocation, this, transaction, dingoDocumentScanFilter);
420422
}
421423

424+
public Collection<Vertex> visit(@NonNull DingoWindow dingoWindow) {
425+
return DingoWindowVisitFun.visit(job, idGenerator, currentLocation, this, transaction, dingoWindow);
426+
}
427+
422428
}

dingo-calcite/src/main/java/io/dingodb/calcite/visitor/DingoRelVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import io.dingodb.calcite.rel.DingoUnion;
4545
import io.dingodb.calcite.rel.DingoValues;
4646
import io.dingodb.calcite.rel.DingoVector;
47+
import io.dingodb.calcite.rel.DingoWindow;
4748
import io.dingodb.calcite.rel.DocumentStreamConvertor;
4849
import io.dingodb.calcite.rel.VectorStreamConvertor;
4950
import io.dingodb.calcite.rel.dingo.DingoDocumentScanFilter;
@@ -142,4 +143,6 @@ public interface DingoRelVisitor<T> {
142143

143144
T visit(DingoDocumentScanFilter documentIndexRangeScan);
144145

146+
T visit(DingoWindow dingoWindow);
147+
145148
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.visitor.function;
18+
19+
import io.dingodb.calcite.rel.DingoWindow;
20+
import io.dingodb.calcite.rel.dingo.DingoDocumentScanFilter;
21+
import io.dingodb.calcite.visitor.DingoJobVisitor;
22+
import io.dingodb.common.Location;
23+
import io.dingodb.exec.base.IdGenerator;
24+
import io.dingodb.exec.base.Job;
25+
import io.dingodb.exec.dag.Vertex;
26+
import io.dingodb.exec.transaction.base.ITransaction;
27+
import org.checkerframework.checker.nullness.qual.NonNull;
28+
29+
import java.util.Collection;
30+
31+
public class DingoWindowVisitFun {
32+
33+
private DingoWindowVisitFun() {
34+
}
35+
36+
public static @NonNull Collection<Vertex> visit(
37+
Job job,
38+
@NonNull IdGenerator idGenerator,
39+
Location currentLocation,
40+
DingoJobVisitor visitor,
41+
ITransaction transaction,
42+
@NonNull DingoWindow rel
43+
) {
44+
return null;
45+
}
46+
}

0 commit comments

Comments
 (0)