OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MapDRelJsonWriter.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
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 org.apache.calcite.rel.externalize;
18 
19 import com.google.common.collect.ImmutableList;
20 
21 import org.apache.calcite.adapter.enumerable.EnumerableTableScan;
22 import org.apache.calcite.rel.RelNode;
23 import org.apache.calcite.rel.RelWriter;
24 import org.apache.calcite.rel.logical.LogicalAggregate;
25 import org.apache.calcite.rel.logical.LogicalTableModify;
26 import org.apache.calcite.rel.logical.LogicalTableScan;
27 import org.apache.calcite.rel.type.RelDataType;
28 import org.apache.calcite.sql.SqlExplainLevel;
29 import org.apache.calcite.util.JsonBuilder;
30 import org.apache.calcite.util.Pair;
31 
32 import java.util.ArrayList;
33 import java.util.IdentityHashMap;
34 import java.util.List;
35 import java.util.Map;
36 
42 public class MapDRelJsonWriter implements RelWriter {
43  //~ Instance fields ----------------------------------------------------------
44 
45  private final JsonBuilder jsonBuilder;
46  private final MapDRelJson relJson;
47  private final Map<RelNode, String> relIdMap = new IdentityHashMap<RelNode, String>();
48  private final List<Object> relList;
49  private final List<Pair<String, Object>> values = new ArrayList<Pair<String, Object>>();
50  private String previousId;
51 
52  //~ Constructors -------------------------------------------------------------
53 
54  public MapDRelJsonWriter() {
55  jsonBuilder = new JsonBuilder();
56  relList = jsonBuilder.list();
58  }
59 
60  //~ Methods ------------------------------------------------------------------
61 
62  protected void explain_(RelNode rel, List<Pair<String, Object>> values) {
63  final Map<String, Object> map = jsonBuilder.map();
64 
65  map.put("id", null); // ensure that id is the first attribute
66  map.put("relOp", relJson.classToTypeName(rel.getClass()));
67  if (rel instanceof EnumerableTableScan) {
68  RelDataType row_type = ((EnumerableTableScan) rel).getTable().getRowType();
69  List<String> field_names = row_type.getFieldNames();
70  map.put("fieldNames", field_names);
71  }
72  if (rel instanceof LogicalAggregate) {
73  map.put("fields", rel.getRowType().getFieldNames());
74  }
75  if (rel instanceof LogicalTableModify) {
76  // FIX-ME: What goes here?
77  }
78 
79  for (Pair<String, Object> value : values) {
80  if (value.right instanceof RelNode) {
81  continue;
82  }
83  put(map, value.left, value.right);
84  }
85  // omit 'inputs: ["3"]' if "3" is the preceding rel
86  final List<Object> list = explainInputs(rel.getInputs());
87  if (list.size() != 1 || !list.get(0).equals(previousId)) {
88  map.put("inputs", list);
89  }
90 
91  final String id = Integer.toString(relIdMap.size());
92  relIdMap.put(rel, id);
93  map.put("id", id);
94 
95  relList.add(map);
96  previousId = id;
97  }
98 
99  private void put(Map<String, Object> map, String name, Object value) {
100  map.put(name, relJson.toJson(value));
101  }
102 
103  private List<Object> explainInputs(List<RelNode> inputs) {
104  final List<Object> list = jsonBuilder.list();
105  for (RelNode input : inputs) {
106  String id = relIdMap.get(input);
107  if (id == null) {
108  input.explain(this);
109  id = previousId;
110  }
111  list.add(id);
112  }
113  return list;
114  }
115 
116  public final void explain(RelNode rel, List<Pair<String, Object>> valueList) {
117  explain_(rel, valueList);
118  }
119 
120  public SqlExplainLevel getDetailLevel() {
121  return SqlExplainLevel.ALL_ATTRIBUTES;
122  }
123 
124  public RelWriter input(String term, RelNode input) {
125  return this;
126  }
127 
128  public RelWriter item(String term, Object value) {
129  values.add(Pair.of(term, value));
130  return this;
131  }
132 
133  private List<Object> getList(List<Pair<String, Object>> values, String tag) {
134  for (Pair<String, Object> value : values) {
135  if (value.left.equals(tag)) {
136  // noinspection unchecked
137  return (List<Object>) value.right;
138  }
139  }
140  final List<Object> list = new ArrayList<Object>();
141  values.add(Pair.of(tag, (Object) list));
142  return list;
143  }
144 
145  public RelWriter itemIf(String term, Object value, boolean condition) {
146  if (condition) {
147  item(term, value);
148  }
149  return this;
150  }
151 
152  public RelWriter done(RelNode node) {
153  final List<Pair<String, Object>> valuesCopy = ImmutableList.copyOf(values);
154  values.clear();
155  explain_(node, valuesCopy);
156  return this;
157  }
158 
159  public boolean nest() {
160  return true;
161  }
162 
167  public String asString() {
168  return jsonBuilder.toJsonString(asJsonMap());
169  }
170 
171  public Map<String, Object> asJsonMap() {
172  final Map<String, Object> map = jsonBuilder.map();
173  map.put("rels", relList);
174  return map;
175  }
176 }
177 
178 // End RelJsonWriter.java
void explain_(RelNode rel, List< Pair< String, Object >> values)
List< Object > getList(List< Pair< String, Object >> values, String tag)
List< Object > explainInputs(List< RelNode > inputs)
final void explain(RelNode rel, List< Pair< String, Object >> valueList)
RelWriter itemIf(String term, Object value, boolean condition)
void put(Map< String, Object > map, String name, Object value)