|
| 1 | +package io.substrait.isthmus; |
| 2 | + |
| 3 | +import io.substrait.relation.Aggregate; |
| 4 | +import io.substrait.relation.ConsistentPartitionWindow; |
| 5 | +import io.substrait.relation.Cross; |
| 6 | +import io.substrait.relation.EmptyScan; |
| 7 | +import io.substrait.relation.Expand; |
| 8 | +import io.substrait.relation.ExtensionDdl; |
| 9 | +import io.substrait.relation.ExtensionLeaf; |
| 10 | +import io.substrait.relation.ExtensionMulti; |
| 11 | +import io.substrait.relation.ExtensionSingle; |
| 12 | +import io.substrait.relation.ExtensionTable; |
| 13 | +import io.substrait.relation.ExtensionWrite; |
| 14 | +import io.substrait.relation.Fetch; |
| 15 | +import io.substrait.relation.Filter; |
| 16 | +import io.substrait.relation.Join; |
| 17 | +import io.substrait.relation.LocalFiles; |
| 18 | +import io.substrait.relation.NamedDdl; |
| 19 | +import io.substrait.relation.NamedScan; |
| 20 | +import io.substrait.relation.NamedUpdate; |
| 21 | +import io.substrait.relation.NamedWrite; |
| 22 | +import io.substrait.relation.Project; |
| 23 | +import io.substrait.relation.RelVisitor; |
| 24 | +import io.substrait.relation.Set; |
| 25 | +import io.substrait.relation.Sort; |
| 26 | +import io.substrait.relation.VirtualTableScan; |
| 27 | +import io.substrait.relation.physical.HashJoin; |
| 28 | +import io.substrait.relation.physical.MergeJoin; |
| 29 | +import io.substrait.relation.physical.NestedLoopJoin; |
| 30 | +import io.substrait.util.EmptyVisitationContext; |
| 31 | +import org.apache.calcite.sql.SqlKind; |
| 32 | + |
| 33 | +/** |
| 34 | + * A visitor to infer the general SqlKind from the root of a Substrait Rel tree. Note: This infers |
| 35 | + * the general operation type, as the original SQL syntax is not preserved in the Substrait plan. |
| 36 | + */ |
| 37 | +public class SqlKindFromRel |
| 38 | + implements RelVisitor<SqlKind, EmptyVisitationContext, RuntimeException> { |
| 39 | + |
| 40 | + // Most common query operations map to SELECT. |
| 41 | + private static final SqlKind QUERY_KIND = SqlKind.SELECT; |
| 42 | + |
| 43 | + @Override |
| 44 | + public SqlKind visit(Aggregate aggregate, EmptyVisitationContext context) |
| 45 | + throws RuntimeException { |
| 46 | + |
| 47 | + return QUERY_KIND; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public SqlKind visit(EmptyScan emptyScan, EmptyVisitationContext context) |
| 52 | + throws RuntimeException { |
| 53 | + // An empty scan is typically the result of a query that returns no rows. |
| 54 | + return QUERY_KIND; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public SqlKind visit(Fetch fetch, EmptyVisitationContext context) throws RuntimeException { |
| 59 | + return QUERY_KIND; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public SqlKind visit(Filter filter, EmptyVisitationContext context) throws RuntimeException { |
| 64 | + return QUERY_KIND; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public SqlKind visit(Join join, EmptyVisitationContext context) throws RuntimeException { |
| 69 | + return SqlKind.JOIN; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public SqlKind visit(Set set, EmptyVisitationContext context) throws RuntimeException { |
| 74 | + switch (set.getSetOp()) { |
| 75 | + case UNION_ALL: |
| 76 | + case UNION_DISTINCT: |
| 77 | + return SqlKind.UNION; |
| 78 | + case INTERSECTION_PRIMARY: |
| 79 | + case INTERSECTION_MULTISET: |
| 80 | + case INTERSECTION_MULTISET_ALL: |
| 81 | + return SqlKind.INTERSECT; |
| 82 | + case MINUS_PRIMARY: |
| 83 | + case MINUS_PRIMARY_ALL: |
| 84 | + case MINUS_MULTISET: |
| 85 | + return SqlKind.EXCEPT; |
| 86 | + case UNKNOWN: |
| 87 | + default: |
| 88 | + return SqlKind.OTHER; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public SqlKind visit(NamedScan namedScan, EmptyVisitationContext context) |
| 94 | + throws RuntimeException { |
| 95 | + return QUERY_KIND; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public SqlKind visit(LocalFiles localFiles, EmptyVisitationContext context) |
| 100 | + throws RuntimeException { |
| 101 | + return QUERY_KIND; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public SqlKind visit(Project project, EmptyVisitationContext context) throws RuntimeException { |
| 106 | + return QUERY_KIND; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public SqlKind visit(Expand expand, EmptyVisitationContext context) throws RuntimeException { |
| 111 | + return QUERY_KIND; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public SqlKind visit(Sort sort, EmptyVisitationContext context) throws RuntimeException { |
| 116 | + return SqlKind.ORDER_BY; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public SqlKind visit(Cross cross, EmptyVisitationContext context) throws RuntimeException { |
| 121 | + return SqlKind.JOIN; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public SqlKind visit(VirtualTableScan virtualTableScan, EmptyVisitationContext context) |
| 126 | + throws RuntimeException { |
| 127 | + // A virtual table scan corresponds to a VALUES clause. |
| 128 | + return SqlKind.VALUES; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public SqlKind visit(ExtensionLeaf extensionLeaf, EmptyVisitationContext context) |
| 133 | + throws RuntimeException { |
| 134 | + return SqlKind.OTHER; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public SqlKind visit(ExtensionSingle extensionSingle, EmptyVisitationContext context) |
| 139 | + throws RuntimeException { |
| 140 | + return SqlKind.OTHER; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public SqlKind visit(ExtensionMulti extensionMulti, EmptyVisitationContext context) |
| 145 | + throws RuntimeException { |
| 146 | + return SqlKind.OTHER; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public SqlKind visit(ExtensionTable extensionTable, EmptyVisitationContext context) |
| 151 | + throws RuntimeException { |
| 152 | + return SqlKind.OTHER; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public SqlKind visit(HashJoin hashJoin, EmptyVisitationContext context) throws RuntimeException { |
| 157 | + return SqlKind.JOIN; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public SqlKind visit(MergeJoin mergeJoin, EmptyVisitationContext context) |
| 162 | + throws RuntimeException { |
| 163 | + return SqlKind.JOIN; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public SqlKind visit(NestedLoopJoin nestedLoopJoin, EmptyVisitationContext context) |
| 168 | + throws RuntimeException { |
| 169 | + return SqlKind.JOIN; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public SqlKind visit( |
| 174 | + ConsistentPartitionWindow consistentPartitionWindow, EmptyVisitationContext context) |
| 175 | + throws RuntimeException { |
| 176 | + return SqlKind.OVER; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public SqlKind visit(NamedWrite write, EmptyVisitationContext context) throws RuntimeException { |
| 181 | + switch (write.getOperation()) { |
| 182 | + case INSERT: |
| 183 | + return SqlKind.INSERT; |
| 184 | + case DELETE: |
| 185 | + return SqlKind.DELETE; |
| 186 | + case UPDATE: |
| 187 | + return SqlKind.UPDATE; |
| 188 | + case CTAS: |
| 189 | + return SqlKind.CREATE_TABLE; |
| 190 | + default: |
| 191 | + return SqlKind.OTHER; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public SqlKind visit(ExtensionWrite write, EmptyVisitationContext context) |
| 197 | + throws RuntimeException { |
| 198 | + return SqlKind.OTHER_DDL; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public SqlKind visit(NamedDdl ddl, EmptyVisitationContext context) throws RuntimeException { |
| 203 | + switch (ddl.getOperation()) { |
| 204 | + case CREATE: |
| 205 | + case CREATE_OR_REPLACE: |
| 206 | + if (ddl.getObject() == NamedDdl.DdlObject.TABLE) { |
| 207 | + return SqlKind.CREATE_TABLE; |
| 208 | + } else if (ddl.getObject() == NamedDdl.DdlObject.VIEW) { |
| 209 | + return SqlKind.CREATE_VIEW; |
| 210 | + } |
| 211 | + break; |
| 212 | + case DROP: |
| 213 | + case DROP_IF_EXIST: |
| 214 | + if (ddl.getObject() == NamedDdl.DdlObject.TABLE) { |
| 215 | + return SqlKind.DROP_TABLE; |
| 216 | + } else if (ddl.getObject() == NamedDdl.DdlObject.VIEW) { |
| 217 | + return SqlKind.DROP_VIEW; |
| 218 | + } |
| 219 | + break; |
| 220 | + case ALTER: |
| 221 | + if (ddl.getObject() == NamedDdl.DdlObject.TABLE) { |
| 222 | + return SqlKind.ALTER_TABLE; |
| 223 | + } else if (ddl.getObject() == NamedDdl.DdlObject.VIEW) { |
| 224 | + return SqlKind.ALTER_VIEW; |
| 225 | + } |
| 226 | + break; |
| 227 | + } |
| 228 | + return SqlKind.OTHER_DDL; |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public SqlKind visit(ExtensionDdl ddl, EmptyVisitationContext context) throws RuntimeException { |
| 233 | + return SqlKind.OTHER_DDL; |
| 234 | + } |
| 235 | + |
| 236 | + @Override |
| 237 | + public SqlKind visit(NamedUpdate update, EmptyVisitationContext context) throws RuntimeException { |
| 238 | + return SqlKind.UPDATE; |
| 239 | + } |
| 240 | +} |
0 commit comments