Skip to content

Commit 3eaaa15

Browse files
committed
feat(isthmus): update to calcite 1.39
Signed-off-by: MBWhite <[email protected]>
1 parent b7abddd commit 3eaaa15

File tree

4 files changed

+159
-103
lines changed

4 files changed

+159
-103
lines changed

Diff for: gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ com.github.vlsi.vlsi-release-plugins.version=1.74
1515

1616
# library version
1717
antlr.version=4.13.1
18-
calcite.version=1.38.0
18+
calcite.version=1.39.0
1919
guava.version=32.1.3-jre
2020
immutables.version=2.10.1
2121
jackson.version=2.16.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package io.substrait.isthmus;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Optional;
7+
import java.util.stream.Collectors;
8+
9+
import org.apache.calcite.jdbc.CalciteSchema;
10+
import org.apache.calcite.rel.type.RelDataTypeFactory;
11+
import org.apache.calcite.schema.Table;
12+
import org.apache.calcite.schema.impl.AbstractSchema;
13+
import org.apache.calcite.schema.lookup.Lookup;
14+
15+
import io.substrait.relation.NamedScan;
16+
import io.substrait.relation.Rel;
17+
import io.substrait.relation.RelCopyOnWriteVisitor;
18+
import io.substrait.type.NamedStruct;
19+
20+
/**
21+
* A subclass of the Calcite Schema for creation from a Substrait relation
22+
*
23+
* <p>Implementation note:
24+
*
25+
* <p>The external Isthmus API can take a function that will return the table schema when needed,
26+
* rather than it being available up front.
27+
*
28+
* <p>This was implemented by a special subclass of the Calcite simple schema. Since this was
29+
* changed in Calcite 1.39.0; it failed to work; the protected methods it extended from changed.
30+
*
31+
* <p>The new feature of the Calcite schema is the 'lazy' or delayed lookup of tables. This
32+
* feature has not be exploited here
33+
*/
34+
public class SubstraitCalciteSchema extends AbstractSchema {
35+
36+
private Map<String, Table> tables;
37+
38+
protected SubstraitCalciteSchema(Map<String, Table> tables) {
39+
this.tables = tables;
40+
}
41+
42+
@Override
43+
protected Map<String, Table> getTableMap() {
44+
return tables;
45+
}
46+
47+
@Override
48+
public Lookup<Table> tables() {
49+
return super.tables();
50+
}
51+
52+
/**
53+
* Turn this into a root Calciteschema Choice of settings is based on current isthmus behaviour
54+
*/
55+
public CalciteSchema getRootSchema() {
56+
return CalciteSchema.createRootSchema(false, false, "", this);
57+
}
58+
59+
public static Builder builder() {
60+
return new Builder();
61+
}
62+
63+
/**
64+
* Builder class to assist with creating the CalciteSchema
65+
*
66+
* <p>Can be created from a Rel or a Lookup function
67+
*/
68+
public static class Builder {
69+
70+
private Rel rel;
71+
private RelDataTypeFactory typeFactory;
72+
private TypeConverter typeConverter;
73+
74+
public Builder withTypeFactory(RelDataTypeFactory typeFactory) {
75+
this.typeFactory = typeFactory;
76+
return this;
77+
}
78+
79+
public Builder withTypeConverter(TypeConverter typeConverter) {
80+
this.typeConverter = typeConverter;
81+
return this;
82+
}
83+
84+
public Builder withSubstraitRel(Rel rel) {
85+
this.rel = rel;
86+
return this;
87+
}
88+
89+
public SubstraitCalciteSchema build() {
90+
if (typeConverter == null) {
91+
throw new IllegalArgumentException("'TypeConverter' must be specified");
92+
}
93+
94+
if (typeFactory == null) {
95+
throw new IllegalArgumentException("'TypeFactory' must be specified");
96+
}
97+
98+
if (rel == null) {
99+
throw new IllegalArgumentException("'rel' must be specified");
100+
}
101+
102+
// If there are any named structs within the relation, gather these and convert
103+
// them to a map of tables
104+
// index by name; note that the name of the table is 'un-namespaced' here.
105+
// This was the existing logic so it has not been altered.
106+
Map<List<String>, NamedStruct> tableMap = NamedStructGatherer.gatherTables(rel);
107+
108+
Map<String, Table> tables =
109+
tableMap.entrySet().stream()
110+
.map(
111+
entry -> {
112+
var id = entry.getKey();
113+
var name = id.get(id.size() - 1);
114+
var table = entry.getValue();
115+
var value =
116+
new SqlConverterBase.DefinedTable(
117+
name,
118+
typeFactory,
119+
typeConverter.toCalcite(typeFactory, table.struct(), table.names()));
120+
return Map.entry(name, value);
121+
})
122+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
123+
124+
return new SubstraitCalciteSchema(tables);
125+
}
126+
}
127+
128+
private static final class NamedStructGatherer extends RelCopyOnWriteVisitor<RuntimeException> {
129+
Map<List<String>, NamedStruct> tableMap;
130+
131+
private NamedStructGatherer() {
132+
super();
133+
this.tableMap = new HashMap<>();
134+
}
135+
136+
public static Map<List<String>, NamedStruct> gatherTables(Rel rel) {
137+
var visitor = new NamedStructGatherer();
138+
rel.accept(visitor);
139+
return visitor.tableMap;
140+
}
141+
142+
@Override
143+
public Optional<Rel> visit(NamedScan namedScan) {
144+
Optional<Rel> result = super.visit(namedScan);
145+
146+
List<String> tableName = namedScan.getNames();
147+
tableMap.put(tableName, namedScan.getInitialSchema());
148+
149+
return result;
150+
}
151+
}
152+
}

Diff for: isthmus/src/main/java/io/substrait/isthmus/SubstraitToCalcite.java

+6-47
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22

33
import io.substrait.extension.SimpleExtension;
44
import io.substrait.plan.Plan;
5-
import io.substrait.relation.NamedScan;
65
import io.substrait.relation.Rel;
7-
import io.substrait.relation.RelCopyOnWriteVisitor;
8-
import io.substrait.type.NamedStruct;
96
import java.util.ArrayList;
10-
import java.util.HashMap;
117
import java.util.List;
12-
import java.util.Map;
13-
import java.util.Optional;
14-
import java.util.function.Function;
158
import org.apache.calcite.jdbc.CalciteSchema;
16-
import org.apache.calcite.jdbc.LookupCalciteSchema;
179
import org.apache.calcite.rel.RelNode;
1810
import org.apache.calcite.rel.RelRoot;
1911
import org.apache.calcite.rel.type.RelDataType;
2012
import org.apache.calcite.rel.type.RelDataTypeFactory;
2113
import org.apache.calcite.rel.type.RelDataTypeField;
22-
import org.apache.calcite.schema.Table;
2314
import org.apache.calcite.sql.SqlKind;
2415
import org.apache.calcite.tools.Frameworks;
2516
import org.apache.calcite.tools.RelBuilder;
@@ -57,19 +48,12 @@ public SubstraitToCalcite(
5748
* <p>Override this method to customize schema extraction.
5849
*/
5950
protected CalciteSchema toSchema(Rel rel) {
60-
Map<List<String>, NamedStruct> tableMap = NamedStructGatherer.gatherTables(rel);
61-
Function<List<String>, Table> lookup =
62-
id -> {
63-
NamedStruct table = tableMap.get(id);
64-
if (table == null) {
65-
return null;
66-
}
67-
return new SqlConverterBase.DefinedTable(
68-
id.get(id.size() - 1),
69-
typeFactory,
70-
typeConverter.toCalcite(typeFactory, table.struct(), table.names()));
71-
};
72-
return LookupCalciteSchema.createRootSchema(lookup);
51+
return SubstraitCalciteSchema.builder()
52+
.withSubstraitRel(rel)
53+
.withTypeFactory(typeFactory)
54+
.withTypeConverter(typeConverter)
55+
.build()
56+
.getRootSchema();
7357
}
7458

7559
/**
@@ -179,29 +163,4 @@ private Pair<Integer, RelDataType> renameFields(
179163
return Pair.of(currentIndex, type);
180164
}
181165
}
182-
183-
private static class NamedStructGatherer extends RelCopyOnWriteVisitor<RuntimeException> {
184-
Map<List<String>, NamedStruct> tableMap;
185-
186-
private NamedStructGatherer() {
187-
super();
188-
this.tableMap = new HashMap<>();
189-
}
190-
191-
public static Map<List<String>, NamedStruct> gatherTables(Rel rel) {
192-
var visitor = new NamedStructGatherer();
193-
rel.accept(visitor);
194-
return visitor.tableMap;
195-
}
196-
197-
@Override
198-
public Optional<Rel> visit(NamedScan namedScan) {
199-
Optional<Rel> result = super.visit(namedScan);
200-
201-
List<String> tableName = namedScan.getNames();
202-
tableMap.put(tableName, namedScan.getInitialSchema());
203-
204-
return result;
205-
}
206-
}
207166
}

Diff for: isthmus/src/main/java/org/apache/calcite/jdbc/LookupCalciteSchema.java

-55
This file was deleted.

0 commit comments

Comments
 (0)