OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyDBSqlAdvisorValidator.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.SqlValidatorCatalogReader;
29 import org.apache.calcite.sql.validate.SqlValidatorScope;
30 import org.apache.calcite.util.Util;
31 
32 import java.util.List;
33 
34 class HeavyDBSqlAdvisorValidator extends SqlAdvisorValidator {
36  SqlOperatorTable opTab,
37  SqlValidatorCatalogReader catalogReader,
38  RelDataTypeFactory typeFactory,
39  Config config) {
40  super(opTab, catalogReader, typeFactory, config);
41  this.visibleTables = visibleTables;
42  }
43 
44  @Override
45  protected void validateGroupClause(SqlSelect select) {
46  try {
47  SqlNodeList groupList = select.getGroup();
48  if (groupList == null) {
49  return;
50  }
51  // Validate the group items so that completions are available for them.
52  // For some reason, the base class doesn't do it.
53  for (final SqlNode groupItem : groupList) {
54  final SqlValidatorScope groupScope = getGroupScope(select);
55  groupItem.validate(this, groupScope);
56  }
57  super.validateGroupClause(select);
58  } catch (CalciteException e) {
59  Util.swallow(e, TRACER);
60  }
61  }
62 
63  @Override
64  protected void validateFrom(
65  SqlNode node, RelDataType targetRowType, SqlValidatorScope scope) {
66  try {
67  // Must not return columns from a table which is not visible. Since column
68  // hints are returned without their table, we must keep track of visibility
69  // violations during validation.
70  if (node.getKind() == SqlKind.IDENTIFIER
71  && tableViolatesPermissions(node.toString())) {
73  }
74  super.validateFrom(node, targetRowType, scope);
75  } catch (CalciteException e) {
76  Util.swallow(e, TRACER);
77  }
78  }
79 
80  // Check if the given table name is invisible (per the permissions). The dummy
81  // table inserted by the partial parser is allowed (starts with underscore).
82  boolean tableViolatesPermissions(final String tableName) {
83  return !tableName.isEmpty() && Character.isAlphabetic(tableName.charAt(0))
84  && visibleTables.stream().noneMatch(
85  visibleTableName -> visibleTableName.equalsIgnoreCase(tableName));
86  }
87 
90  }
91 
92  private List<String> visibleTables;
93  private boolean violatedTablePermissions = false;
94 }
HeavyDBSqlAdvisorValidator(List< String > visibleTables, SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory, Config config)
void validateFrom(SqlNode node, RelDataType targetRowType, SqlValidatorScope scope)