OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyDBView.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 com.mapd.calcite.parser;
18 
19 import static com.mapd.calcite.parser.HeavyDBParser.CURRENT_PARSER;
20 
22 
23 import org.apache.calcite.plan.RelOptTable;
25 import org.apache.calcite.rel.RelNode;
26 import org.apache.calcite.rel.RelRoot;
27 import org.apache.calcite.rel.type.RelDataType;
28 import org.apache.calcite.rel.type.RelDataTypeFactory;
29 import org.apache.calcite.schema.Schema;
30 import org.apache.calcite.schema.TranslatableTable;
31 import org.apache.calcite.sql.parser.SqlParseException;
32 import org.apache.calcite.tools.RelConversionException;
33 import org.apache.calcite.tools.ValidationException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 
37 import java.util.ArrayList;
38 
39 import ai.heavy.thrift.server.TTableDetails;
40 
41 public class HeavyDBView extends HeavyDBTable implements TranslatableTable {
42  final static Logger HEAVYDBLOGGER = LoggerFactory.getLogger(HeavyDBView.class);
43  private final String viewSql;
45  private RelRoot viewRelRoot;
46 
47  public HeavyDBView(String view_sql, TTableDetails ri, HeavyDBParser mp) {
48  super(ri);
49  this.viewSql = view_sql;
50  try {
51  HeavyDBParserOptions parserOptions = new HeavyDBParserOptions();
52  viewRelRoot = mp.queryToRelNode(viewSql, parserOptions);
53  accessObjects = mp.captureIdentifiers(viewSql, parserOptions.isLegacySyntax());
54  } catch (SqlParseException e) {
55  HEAVYDBLOGGER.error("error parsing view SQL: " + view_sql, e);
56  } catch (ValidationException ex) {
57  HEAVYDBLOGGER.error("error validating view SQL: " + view_sql, ex);
58  } catch (RelConversionException ex) {
59  HEAVYDBLOGGER.error("error doing Rel Conversion view SQL: " + view_sql, ex);
60  }
61  }
62 
63  public String toString() {
64  return "View SQL: " + viewSql + "\n"
65  + "Accessed Objects\n" + accessObjects;
66  }
67 
69  return accessObjects;
70  }
71 
72  String getViewSql() {
73  return viewSql;
74  }
75 
76  @Override
77  public Schema.TableType getJdbcTableType() {
78  return Schema.TableType.VIEW;
79  }
80 
81  @Override
82  public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable relOptTable) {
83  return viewRelRoot.rel;
84  }
85 
86  @Override
87  public RelDataType getRowType(RelDataTypeFactory rdtf) {
88  return viewRelRoot.validatedRowType;
89  }
90 }
RelDataType getRowType(RelDataTypeFactory rdtf)
SqlIdentifierCapturer getAccessedObjects()
HeavyDBView(String view_sql, TTableDetails ri, HeavyDBParser mp)
SqlIdentifierCapturer accessObjects
RelNode toRel(RelOptTable.ToRelContext context, RelOptTable relOptTable)