|
| 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 | +} |
0 commit comments