Skip to content

Commit

Permalink
[CALCITE-6574] SqlValidatorImpl Refactor: SqlQueryScopes
Browse files Browse the repository at this point in the history
Adding SqlQueryScopes to encapsulate the data for resolving
SqlNode to SqlValidatorScope and SqlValidatorNamespaces.  Moving
the api for resolving scopes from SqlValidator to SqlQueryScopes.
  • Loading branch information
jamesstarr committed Sep 11, 2024
1 parent 67405c3 commit 6c7659f
Show file tree
Hide file tree
Showing 31 changed files with 843 additions and 641 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public SqlCall permutedCall() {
@Override public RelDataType getOperandType(int ordinal) {
final SqlNode operand = call.operand(ordinal);
final RelDataType type = SqlTypeUtil.deriveType(this, operand);
final SqlValidatorNamespace namespace = validator.getNamespace(operand);
final SqlValidatorNamespace namespace = validator.getSqlQueryScopes().getNamespace(operand);
if (namespace != null) {
return namespace.getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ private PercentileDiscCallBinding(SqlValidator validator,

@Override public RelDataType getCollationType() {
final RelDataType type = SqlTypeUtil.deriveType(this, collationColumn);
final SqlValidatorNamespace namespace = super.getValidator().getNamespace(collationColumn);
final SqlValidatorNamespace namespace = super.getValidator().getSqlQueryScopes()
.getNamespace(collationColumn);
return namespace != null ? namespace.getType() : type;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ private void registerId(SqlIdentifier id, SqlValidatorScope scope) {

@Override protected void validateOver(SqlCall call, SqlValidatorScope scope) {
try {
final OverScope overScope = (OverScope) getOverScope(call);
final OverScope overScope = (OverScope) getSqlQueryScopes().getOverScope(call);
final SqlNode relation = call.operand(0);
validateFrom(relation, unknownType, scope);
final SqlNode window = call.operand(1);
SqlValidatorScope opScope = scopes.get(relation);
SqlValidatorScope opScope = getSqlQueryScopes().getScope(relation);
if (opScope == null) {
opScope = overScope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected SqlMultisetQueryConstructor(String name, SqlKind kind,
SqlSelect subSelect = call.operand(0);
subSelect.validateExpr(validator, scope);
final SqlValidatorNamespace ns =
requireNonNull(validator.getNamespace(subSelect),
requireNonNull(validator.getSqlQueryScopes().getNamespace(subSelect),
() -> "namespace is missing for " + subSelect);
final RelDataType rowType = requireNonNull(ns.getRowType(), "rowType");
final SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@ private static class LambdaFamilyOperandTypeChecker
&& !argFamilies.stream().allMatch(f -> f == SqlTypeFamily.ANY)) {
// Replace the parameter types in the lambda expression.
final SqlLambdaScope scope =
(SqlLambdaScope) validator.getLambdaScope(lambdaExpr);
(SqlLambdaScope) validator.getSqlQueryScopes().getLambdaScope(lambdaExpr);
for (int i = 0; i < argFamilies.size(); i++) {
final SqlNode param = lambdaExpr.getParameters().get(i);
final RelDataType type =
Expand Down Expand Up @@ -1794,7 +1794,7 @@ private static class LambdaRelOperandTypeChecker
// Replace the parameter types in the lambda expression.
final SqlValidator validator = callBinding.getValidator();
final SqlLambdaScope scope =
(SqlLambdaScope) validator.getLambdaScope(lambdaExpr);
(SqlLambdaScope) validator.getSqlQueryScopes().getLambdaScope(lambdaExpr);
for (int i = 0; i < argTypes.size(); i++) {
final SqlNode param = lambdaExpr.getParameters().get(i);
final RelDataType type = argTypes.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ && isMeasureExp(id)) {
if (scope instanceof AggregatingSelectScope) {
final SqlSelect select = (SqlSelect) scope.getNode();
SelectScope selectScope =
requireNonNull(validator.getRawSelectScope(select),
requireNonNull(validator.getSqlQueryScopes().getRawSelectScope(select),
() -> "rawSelectScope for " + scope.getNode());
List<SqlNode> selectList =
requireNonNull(selectScope.getExpandedSelectList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ protected AliasNamespace(
@Override public boolean supportsModality(SqlModality modality) {
final List<SqlNode> operands = call.getOperandList();
final SqlValidatorNamespace childNs =
validator.getNamespaceOrThrow(operands.get(0));
validator.getSqlQueryScopes().getNamespaceOrThrow(operands.get(0));
return childNs.supportsModality(modality);
}

@Override protected RelDataType validateImpl(RelDataType targetRowType) {
final List<SqlNode> operands = call.getOperandList();
final SqlValidatorNamespace childNs =
validator.getNamespaceOrThrow(operands.get(0));
validator.getSqlQueryScopes().getNamespaceOrThrow(operands.get(0));
final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
final RelDataType aliasedType;
if (operands.size() == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ protected void addColumnNames(

@Override public SqlValidatorScope getOperandScope(SqlCall call) {
if (call instanceof SqlSelect) {
return validator.getSelectScope((SqlSelect) call);
return validator.getSqlQueryScopes().getSelectScope((SqlSelect) call);
} else if (call instanceof SqlLambda) {
return validator.getLambdaScope((SqlLambda) call);
return validator.getSqlQueryScopes().getLambdaScope((SqlLambda) call);
}
return this;
}
Expand Down Expand Up @@ -673,7 +673,7 @@ public SqlValidatorScope getParent() {
return null;
case 1:
final SqlValidatorNamespace selectNs =
validator.getNamespaceOrThrow(select);
validator.getSqlQueryScopes().getNamespaceOrThrow(select);
return SqlQualified.create(this, 1, selectNs, identifier);
default:
// More than one column has this alias.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class JoinNamespace extends AbstractNamespace {

@Override protected RelDataType validateImpl(RelDataType targetRowType) {
RelDataType leftType =
validator.getNamespaceOrThrow(join.getLeft()).getRowType();
validator.getSqlQueryScopes().getNamespaceOrThrow(join.getLeft()).getRowType();
RelDataType rightType =
validator.getNamespaceOrThrow(join.getRight()).getRowType();
validator.getSqlQueryScopes().getNamespaceOrThrow(join.getRight()).getRowType();
final RelDataTypeFactory typeFactory = validator.getTypeFactory();
switch (join.getJoinType()) {
case LEFT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class OrderByScope extends DelegatingScope {
}

@Override public void findAllColumnNames(List<SqlMoniker> result) {
final SqlValidatorNamespace ns = validator.getNamespaceOrThrow(select);
final SqlValidatorNamespace ns = validator.getSqlQueryScopes().getNamespaceOrThrow(select);
addColumnNames(ns, result);
}

Expand All @@ -80,14 +80,15 @@ public class OrderByScope extends DelegatingScope {
}

@Override public @Nullable RelDataType resolveColumn(String name, SqlNode ctx) {
final SqlValidatorNamespace selectNs = validator.getNamespaceOrThrow(select);
final SqlValidatorNamespace selectNs =
validator.getSqlQueryScopes().getNamespaceOrThrow(select);
final RelDataType rowType = selectNs.getRowType();
final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
final RelDataTypeField field = nameMatcher.field(rowType, name);
if (field != null) {
return field.getType();
}
final SqlValidatorScope selectScope = validator.getSelectScope(select);
final SqlValidatorScope selectScope = validator.getSqlQueryScopes().getSelectScope(select);
return selectScope.resolveColumn(name, ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PivotScope(SqlValidatorScope parent, SqlPivot pivot) {
* scope only has one namespace, and it is anonymous. */
public SqlValidatorNamespace getChild() {
return requireNonNull(
validator.getNamespace(pivot.query),
validator.getSqlQueryScopes().getNamespace(pivot.query),
() -> "namespace for pivot.query " + pivot.query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public SelectNamespace(
final RelDataType rowType = this.getRowTypeSansSystemColumns();
final int field = SqlTypeUtil.findField(rowType, columnName);
SelectScope selectScope =
requireNonNull(validator.getRawSelectScope(select),
requireNonNull(validator.getSqlQueryScopes().getRawSelectScope(select),
() -> "rawSelectScope for " + select);
final SqlNode selectItem =
requireNonNull(selectScope.getExpandedSelectList(),
() -> "expandedSelectList for selectScope of " + select).get(field);
return validator.getSelectScope(select).getMonotonicity(selectItem);
return validator.getSqlQueryScopes().getSelectScope(select).getMonotonicity(selectItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected SetopNamespace(
}
for (SqlNode operand : call.getOperandList()) {
final SqlValidatorNamespace namespace =
requireNonNull(validator.getNamespace(operand),
requireNonNull(validator.getSqlQueryScopes().getNamespace(operand),
() -> "namespace for " + operand);
monotonicity =
combine(monotonicity,
Expand Down Expand Up @@ -103,7 +103,7 @@ private static SqlMonotonicity combine(@Nullable SqlMonotonicity m0,
case INTERSECT:
case EXCEPT:
final SqlValidatorScope scope =
requireNonNull(validator.scopes.get(call),
requireNonNull(validator.getSqlQueryScopes().getScope(call),
() -> "scope for " + call);
for (SqlNode operand : call.getOperandList()) {
if (!operand.isA(SqlKind.QUERY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static SqlValidatorScope getScope(SqlCallBinding callBinding) {
@API(since = "1.27", status = API.Status.EXPERIMENTAL)
public static SqlValidatorNamespace getNamespace(SqlCallBinding callBinding) {
return requireNonNull(
callBinding.getValidator().getNamespace(callBinding.getCall()),
callBinding.getValidator().getSqlQueryScopes().getNamespace(callBinding.getCall()),
() -> "scope is null for " + safeToString(callBinding));
}

Expand Down
Loading

0 comments on commit 6c7659f

Please sign in to comment.