Skip to content

Commit 6c7659f

Browse files
committed
[CALCITE-6574] SqlValidatorImpl Refactor: SqlQueryScopes
Adding SqlQueryScopes to encapsulate the data for resolving SqlNode to SqlValidatorScope and SqlValidatorNamespaces. Moving the api for resolving scopes from SqlValidator to SqlQueryScopes.
1 parent 67405c3 commit 6c7659f

31 files changed

+843
-641
lines changed

core/src/main/java/org/apache/calcite/sql/SqlCallBinding.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public SqlCall permutedCall() {
355355
@Override public RelDataType getOperandType(int ordinal) {
356356
final SqlNode operand = call.operand(ordinal);
357357
final RelDataType type = SqlTypeUtil.deriveType(this, operand);
358-
final SqlValidatorNamespace namespace = validator.getNamespace(operand);
358+
final SqlValidatorNamespace namespace = validator.getSqlQueryScopes().getNamespace(operand);
359359
if (namespace != null) {
360360
return namespace.getType();
361361
}

core/src/main/java/org/apache/calcite/sql/SqlWithinGroupOperator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ private PercentileDiscCallBinding(SqlValidator validator,
123123

124124
@Override public RelDataType getCollationType() {
125125
final RelDataType type = SqlTypeUtil.deriveType(this, collationColumn);
126-
final SqlValidatorNamespace namespace = super.getValidator().getNamespace(collationColumn);
126+
final SqlValidatorNamespace namespace = super.getValidator().getSqlQueryScopes()
127+
.getNamespace(collationColumn);
127128
return namespace != null ? namespace.getType() : type;
128129
}
129130
}

core/src/main/java/org/apache/calcite/sql/advise/SqlAdvisorValidator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ private void registerId(SqlIdentifier id, SqlValidatorScope scope) {
169169

170170
@Override protected void validateOver(SqlCall call, SqlValidatorScope scope) {
171171
try {
172-
final OverScope overScope = (OverScope) getOverScope(call);
172+
final OverScope overScope = (OverScope) getSqlQueryScopes().getOverScope(call);
173173
final SqlNode relation = call.operand(0);
174174
validateFrom(relation, unknownType, scope);
175175
final SqlNode window = call.operand(1);
176-
SqlValidatorScope opScope = scopes.get(relation);
176+
SqlValidatorScope opScope = getSqlQueryScopes().getScope(relation);
177177
if (opScope == null) {
178178
opScope = overScope;
179179
}

core/src/main/java/org/apache/calcite/sql/fun/SqlMultisetQueryConstructor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected SqlMultisetQueryConstructor(String name, SqlKind kind,
8686
SqlSelect subSelect = call.operand(0);
8787
subSelect.validateExpr(validator, scope);
8888
final SqlValidatorNamespace ns =
89-
requireNonNull(validator.getNamespace(subSelect),
89+
requireNonNull(validator.getSqlQueryScopes().getNamespace(subSelect),
9090
() -> "namespace is missing for " + subSelect);
9191
final RelDataType rowType = requireNonNull(ns.getRowType(), "rowType");
9292
final SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);

core/src/main/java/org/apache/calcite/sql/type/OperandTypes.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ private static class LambdaFamilyOperandTypeChecker
17321732
&& !argFamilies.stream().allMatch(f -> f == SqlTypeFamily.ANY)) {
17331733
// Replace the parameter types in the lambda expression.
17341734
final SqlLambdaScope scope =
1735-
(SqlLambdaScope) validator.getLambdaScope(lambdaExpr);
1735+
(SqlLambdaScope) validator.getSqlQueryScopes().getLambdaScope(lambdaExpr);
17361736
for (int i = 0; i < argFamilies.size(); i++) {
17371737
final SqlNode param = lambdaExpr.getParameters().get(i);
17381738
final RelDataType type =
@@ -1794,7 +1794,7 @@ private static class LambdaRelOperandTypeChecker
17941794
// Replace the parameter types in the lambda expression.
17951795
final SqlValidator validator = callBinding.getValidator();
17961796
final SqlLambdaScope scope =
1797-
(SqlLambdaScope) validator.getLambdaScope(lambdaExpr);
1797+
(SqlLambdaScope) validator.getSqlQueryScopes().getLambdaScope(lambdaExpr);
17981798
for (int i = 0; i < argTypes.size(); i++) {
17991799
final SqlNode param = lambdaExpr.getParameters().get(i);
18001800
final RelDataType type = argTypes.get(i);

core/src/main/java/org/apache/calcite/sql/validate/AggChecker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ && isMeasureExp(id)) {
147147
if (scope instanceof AggregatingSelectScope) {
148148
final SqlSelect select = (SqlSelect) scope.getNode();
149149
SelectScope selectScope =
150-
requireNonNull(validator.getRawSelectScope(select),
150+
requireNonNull(validator.getSqlQueryScopes().getRawSelectScope(select),
151151
() -> "rawSelectScope for " + scope.getNode());
152152
List<SqlNode> selectList =
153153
requireNonNull(selectScope.getExpandedSelectList(),

core/src/main/java/org/apache/calcite/sql/validate/AliasNamespace.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ protected AliasNamespace(
7474
@Override public boolean supportsModality(SqlModality modality) {
7575
final List<SqlNode> operands = call.getOperandList();
7676
final SqlValidatorNamespace childNs =
77-
validator.getNamespaceOrThrow(operands.get(0));
77+
validator.getSqlQueryScopes().getNamespaceOrThrow(operands.get(0));
7878
return childNs.supportsModality(modality);
7979
}
8080

8181
@Override protected RelDataType validateImpl(RelDataType targetRowType) {
8282
final List<SqlNode> operands = call.getOperandList();
8383
final SqlValidatorNamespace childNs =
84-
validator.getNamespaceOrThrow(operands.get(0));
84+
validator.getSqlQueryScopes().getNamespaceOrThrow(operands.get(0));
8585
final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
8686
final RelDataType aliasedType;
8787
if (operands.size() == 2) {

core/src/main/java/org/apache/calcite/sql/validate/DelegatingScope.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ protected void addColumnNames(
234234

235235
@Override public SqlValidatorScope getOperandScope(SqlCall call) {
236236
if (call instanceof SqlSelect) {
237-
return validator.getSelectScope((SqlSelect) call);
237+
return validator.getSqlQueryScopes().getSelectScope((SqlSelect) call);
238238
} else if (call instanceof SqlLambda) {
239-
return validator.getLambdaScope((SqlLambda) call);
239+
return validator.getSqlQueryScopes().getLambdaScope((SqlLambda) call);
240240
}
241241
return this;
242242
}
@@ -673,7 +673,7 @@ public SqlValidatorScope getParent() {
673673
return null;
674674
case 1:
675675
final SqlValidatorNamespace selectNs =
676-
validator.getNamespaceOrThrow(select);
676+
validator.getSqlQueryScopes().getNamespaceOrThrow(select);
677677
return SqlQualified.create(this, 1, selectNs, identifier);
678678
default:
679679
// More than one column has this alias.

core/src/main/java/org/apache/calcite/sql/validate/JoinNamespace.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class JoinNamespace extends AbstractNamespace {
4242

4343
@Override protected RelDataType validateImpl(RelDataType targetRowType) {
4444
RelDataType leftType =
45-
validator.getNamespaceOrThrow(join.getLeft()).getRowType();
45+
validator.getSqlQueryScopes().getNamespaceOrThrow(join.getLeft()).getRowType();
4646
RelDataType rightType =
47-
validator.getNamespaceOrThrow(join.getRight()).getRowType();
47+
validator.getSqlQueryScopes().getNamespaceOrThrow(join.getRight()).getRowType();
4848
final RelDataTypeFactory typeFactory = validator.getTypeFactory();
4949
switch (join.getJoinType()) {
5050
case LEFT:

core/src/main/java/org/apache/calcite/sql/validate/OrderByScope.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class OrderByScope extends DelegatingScope {
6363
}
6464

6565
@Override public void findAllColumnNames(List<SqlMoniker> result) {
66-
final SqlValidatorNamespace ns = validator.getNamespaceOrThrow(select);
66+
final SqlValidatorNamespace ns = validator.getSqlQueryScopes().getNamespaceOrThrow(select);
6767
addColumnNames(ns, result);
6868
}
6969

@@ -80,14 +80,15 @@ public class OrderByScope extends DelegatingScope {
8080
}
8181

8282
@Override public @Nullable RelDataType resolveColumn(String name, SqlNode ctx) {
83-
final SqlValidatorNamespace selectNs = validator.getNamespaceOrThrow(select);
83+
final SqlValidatorNamespace selectNs =
84+
validator.getSqlQueryScopes().getNamespaceOrThrow(select);
8485
final RelDataType rowType = selectNs.getRowType();
8586
final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
8687
final RelDataTypeField field = nameMatcher.field(rowType, name);
8788
if (field != null) {
8889
return field.getType();
8990
}
90-
final SqlValidatorScope selectScope = validator.getSelectScope(select);
91+
final SqlValidatorScope selectScope = validator.getSqlQueryScopes().getSelectScope(select);
9192
return selectScope.resolveColumn(name, ctx);
9293
}
9394

core/src/main/java/org/apache/calcite/sql/validate/PivotScope.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public PivotScope(SqlValidatorScope parent, SqlPivot pivot) {
3939
* scope only has one namespace, and it is anonymous. */
4040
public SqlValidatorNamespace getChild() {
4141
return requireNonNull(
42-
validator.getNamespace(pivot.query),
42+
validator.getSqlQueryScopes().getNamespace(pivot.query),
4343
() -> "namespace for pivot.query " + pivot.query);
4444
}
4545

core/src/main/java/org/apache/calcite/sql/validate/SelectNamespace.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public SelectNamespace(
7171
final RelDataType rowType = this.getRowTypeSansSystemColumns();
7272
final int field = SqlTypeUtil.findField(rowType, columnName);
7373
SelectScope selectScope =
74-
requireNonNull(validator.getRawSelectScope(select),
74+
requireNonNull(validator.getSqlQueryScopes().getRawSelectScope(select),
7575
() -> "rawSelectScope for " + select);
7676
final SqlNode selectItem =
7777
requireNonNull(selectScope.getExpandedSelectList(),
7878
() -> "expandedSelectList for selectScope of " + select).get(field);
79-
return validator.getSelectScope(select).getMonotonicity(selectItem);
79+
return validator.getSqlQueryScopes().getSelectScope(select).getMonotonicity(selectItem);
8080
}
8181
}

core/src/main/java/org/apache/calcite/sql/validate/SetopNamespace.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected SetopNamespace(
6767
}
6868
for (SqlNode operand : call.getOperandList()) {
6969
final SqlValidatorNamespace namespace =
70-
requireNonNull(validator.getNamespace(operand),
70+
requireNonNull(validator.getSqlQueryScopes().getNamespace(operand),
7171
() -> "namespace for " + operand);
7272
monotonicity =
7373
combine(monotonicity,
@@ -103,7 +103,7 @@ private static SqlMonotonicity combine(@Nullable SqlMonotonicity m0,
103103
case INTERSECT:
104104
case EXCEPT:
105105
final SqlValidatorScope scope =
106-
requireNonNull(validator.scopes.get(call),
106+
requireNonNull(validator.getSqlQueryScopes().getScope(call),
107107
() -> "scope for " + call);
108108
for (SqlNode operand : call.getOperandList()) {
109109
if (!operand.isA(SqlKind.QUERY)) {

core/src/main/java/org/apache/calcite/sql/validate/SqlNonNullableAccessors.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static SqlValidatorScope getScope(SqlCallBinding callBinding) {
104104
@API(since = "1.27", status = API.Status.EXPERIMENTAL)
105105
public static SqlValidatorNamespace getNamespace(SqlCallBinding callBinding) {
106106
return requireNonNull(
107-
callBinding.getValidator().getNamespace(callBinding.getCall()),
107+
callBinding.getValidator().getSqlQueryScopes().getNamespace(callBinding.getCall()),
108108
() -> "scope is null for " + safeToString(callBinding));
109109
}
110110

0 commit comments

Comments
 (0)