17 package org.apache.calcite.rel.externalize;
19 import com.google.common.collect.ImmutableList;
21 import org.apache.calcite.rel.RelNode;
22 import org.apache.calcite.rel.RelWriter;
23 import org.apache.calcite.rel.core.TableScan;
24 import org.apache.calcite.rel.hint.RelHint;
25 import org.apache.calcite.rel.logical.*;
26 import org.apache.calcite.rel.type.RelDataType;
27 import org.apache.calcite.sql.SqlExplainLevel;
29 import org.apache.calcite.util.Pair;
32 import java.util.stream.Collectors;
44 private final Map<RelNode, String>
relIdMap =
new IdentityHashMap<RelNode, String>();
46 private final List<Pair<String, Object>>
values =
new ArrayList<Pair<String, Object>>();
60 final Map<String, Object> map = jsonBuilder.map();
63 map.put(
"relOp", relJson.classToTypeName(rel.getClass()));
64 if (rel instanceof TableScan) {
65 RelDataType row_type = ((TableScan) rel).getTable().getRowType();
66 List<String> field_names = row_type.getFieldNames();
67 map.put(
"fieldNames", field_names);
69 if (rel instanceof LogicalAggregate) {
70 map.put(
"fields", rel.getRowType().getFieldNames());
72 if (rel instanceof LogicalTableModify) {
81 for (Pair<String, Object> value :
values) {
82 if (value.right instanceof RelNode) {
85 put(map, value.left, value.right);
89 if (list.size() != 1 || !list.get(0).equals(
previousId)) {
90 map.put(
"inputs", list);
93 final String
id = Integer.toString(relIdMap.size());
94 relIdMap.put(rel, id);
101 private void put(Map<String, Object> map, String
name, Object value) {
102 map.put(
name, relJson.toJson(value));
106 final List<Object> list = jsonBuilder.list();
107 for (RelNode
input : inputs) {
108 String
id = relIdMap.get(
input);
119 if (rel instanceof LogicalTableScan) {
120 LogicalTableScan node = (LogicalTableScan) rel;
121 if (!node.getHints().isEmpty()) {
126 }
else if (rel instanceof LogicalAggregate) {
127 LogicalAggregate node = (LogicalAggregate) rel;
128 if (!node.getHints().isEmpty()) {
133 }
else if (rel instanceof LogicalJoin) {
134 LogicalJoin node = (LogicalJoin) rel;
135 if (!node.getHints().isEmpty()) {
140 }
else if (rel instanceof LogicalProject) {
141 LogicalProject node = (LogicalProject) rel;
142 if (!node.getHints().isEmpty()) {
147 }
else if (rel instanceof LogicalCalc) {
148 LogicalCalc node = (LogicalCalc) rel;
149 if (!node.getHints().isEmpty()) {
159 List<String> explained =
new ArrayList<>();
160 if (rel instanceof LogicalTableScan) {
161 LogicalTableScan node = (LogicalTableScan) rel;
162 node.getHints().stream().forEach(s -> explained.add(s.toString().toLowerCase()));
163 }
else if (rel instanceof LogicalAggregate) {
164 LogicalAggregate node = (LogicalAggregate) rel;
165 node.getHints().stream().forEach(s -> explained.add(s.toString().toLowerCase()));
166 }
else if (rel instanceof LogicalJoin) {
167 LogicalJoin node = (LogicalJoin) rel;
168 node.getHints().stream().forEach(s -> explained.add(s.toString().toLowerCase()));
169 }
else if (rel instanceof LogicalProject) {
170 LogicalProject node = (LogicalProject) rel;
171 node.getHints().stream().forEach(s -> explained.add(s.toString().toLowerCase()));
172 }
else if (rel instanceof LogicalCalc) {
173 LogicalCalc node = (LogicalCalc) rel;
174 node.getHints().stream().forEach(s -> explained.add(s.toString().toLowerCase()));
176 return explained.stream().collect(Collectors.joining(
"|"));
179 public final void explain(RelNode rel, List<Pair<String, Object>> valueList) {
184 return SqlExplainLevel.ALL_ATTRIBUTES;
191 public RelWriter
item(String term, Object value) {
192 values.add(Pair.of(term, value));
196 private List<Object>
getList(List<Pair<String, Object>>
values, String tag) {
197 for (Pair<String, Object> value :
values) {
198 if (value.left.equals(tag)) {
200 return (List<Object>) value.right;
203 final List<Object> list =
new ArrayList<Object>();
204 values.add(Pair.of(tag, (Object) list));
208 public RelWriter
itemIf(String term, Object value,
boolean condition) {
215 public RelWriter
done(RelNode node) {
216 final List<Pair<String, Object>> valuesCopy = ImmutableList.copyOf(
values);
231 return jsonBuilder.toJsonString(
asJsonMap());
235 final Map<String, Object> map = jsonBuilder.map();
String explainHints(RelNode rel)
RelWriter itemIf(String term, Object value, boolean condition)
final EscapedStringJsonBuilder jsonBuilder
void explain_(RelNode rel, List< Pair< String, Object >> values)
RelWriter input(String term, RelNode input)
final void explain(RelNode rel, List< Pair< String, Object >> valueList)
SqlExplainLevel getDetailLevel()
Map< String, Object > asJsonMap()
final Map< RelNode, String > relIdMap
final HeavyDBRelJson relJson
List< Object > explainInputs(List< RelNode > inputs)
final List< Object > relList
RelWriter done(RelNode node)
boolean deliverHints(RelNode rel)
final List< Pair< String, Object > > values
List< Object > getList(List< Pair< String, Object >> values, String tag)
RelWriter item(String term, Object value)
void put(Map< String, Object > map, String name, Object value)