OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MapDSqlAdvisorValidator.java
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.calcite.prepare;
18 
19 import org.apache.calcite.rel.type.RelDataType;
20 import org.apache.calcite.rel.type.RelDataTypeFactory;
21 import org.apache.calcite.runtime.CalciteException;
22 import org.apache.calcite.sql.SqlKind;
23 import org.apache.calcite.sql.SqlNode;
24 import org.apache.calcite.sql.SqlNodeList;
25 import org.apache.calcite.sql.SqlOperatorTable;
26 import org.apache.calcite.sql.SqlSelect;
27 import org.apache.calcite.sql.advise.SqlAdvisorValidator;
28 import org.apache.calcite.sql.validate.SqlConformance;
29 import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
30 import org.apache.calcite.sql.validate.SqlValidatorScope;
31 import org.apache.calcite.util.Util;
32 
33 import java.util.List;
34 
35 class MapDSqlAdvisorValidator extends SqlAdvisorValidator {
37  SqlOperatorTable opTab,
38  SqlValidatorCatalogReader catalogReader,
39  RelDataTypeFactory typeFactory,
40  SqlConformance conformance) {
41  super(opTab, catalogReader, typeFactory, conformance);
42  this.visibleTables = visibleTables;
43  }
44 
45  @Override
46  protected void validateGroupClause(SqlSelect select) {
47  try {
48  SqlNodeList groupList = select.getGroup();
49  if (groupList == null) {
50  return;
51  }
52  // Validate the group items so that completions are available for them.
53  // For some reason, the base class doesn't do it.
54  for (final SqlNode groupItem : groupList) {
55  final SqlValidatorScope groupScope = getGroupScope(select);
56  groupItem.validate(this, groupScope);
57  }
58  super.validateGroupClause(select);
59  } catch (CalciteException e) {
60  Util.swallow(e, TRACER);
61  }
62  }
63 
64  @Override
65  protected void validateFrom(
66  SqlNode node, RelDataType targetRowType, SqlValidatorScope scope) {
67  try {
68  // Must not return columns from a table which is not visible. Since column
69  // hints are returned without their table, we must keep track of visibility
70  // violations during validation.
71  if (node.getKind() == SqlKind.IDENTIFIER
72  && tableViolatesPermissions(node.toString())) {
74  }
75  super.validateFrom(node, targetRowType, scope);
76  } catch (CalciteException e) {
77  Util.swallow(e, TRACER);
78  }
79  }
80 
81  // Check if the given table name is invisible (per the permissions). The dummy
82  // table inserted by the partial parser is allowed (starts with underscore).
83  boolean tableViolatesPermissions(final String tableName) {
84  return !tableName.isEmpty() && Character.isAlphabetic(tableName.charAt(0))
85  && visibleTables.stream().noneMatch(
86  visibleTableName -> visibleTableName.equalsIgnoreCase(tableName));
87  }
88 
91  }
92 
93  private List<String> visibleTables;
94  private boolean violatedTablePermissions = false;
95 }
void validateFrom(SqlNode node, RelDataType targetRowType, SqlValidatorScope scope)
MapDSqlAdvisorValidator(List< String > visibleTables, SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory, SqlConformance conformance)