Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6574] SqlValidatorImpl Refactor: SqlQueryScopes #3958

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.getScopeMap().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().getScopeMap()
.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) getScopeMap().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 = getScopeMap().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.getScopeMap().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.getScopeMap().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.getScopeMap().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 @@ -235,4 +235,8 @@ protected RelDataType toStruct(RelDataType type, @Nullable SqlNode unnest) {
.add(SqlValidatorUtil.alias(requireNonNull(unnest, "unnest"), 0), type)
.build();
}

protected ScopeMap getScopeMap() {
return getValidator().getScopeMap();
}
}
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.getScopeMap().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));
getScopeMap().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));
getScopeMap().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 getScopeMap().getSelectScope((SqlSelect) call);
} else if (call instanceof SqlLambda) {
return validator.getLambdaScope((SqlLambda) call);
return getScopeMap().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);
getScopeMap().getNamespaceOrThrow(select);
return SqlQualified.create(this, 1, selectNs, identifier);
default:
// More than one column has this alias.
Expand All @@ -682,6 +682,10 @@ public SqlValidatorScope getParent() {
}
}

protected ScopeMap getScopeMap() {
return validator.getScopeMap();
}

/** Returns the number of columns in the SELECT clause that have {@code name}
* as their implicit (e.g. {@code t.name}) or explicit (e.g.
* {@code t.c as name}) 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();
getScopeMap().getNamespaceOrThrow(join.getLeft()).getRowType();
RelDataType rightType =
validator.getNamespaceOrThrow(join.getRight()).getRowType();
getScopeMap().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 = getScopeMap().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 =
getScopeMap().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 = getScopeMap().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),
getScopeMap().getNamespace(pivot.query),
() -> "namespace for pivot.query " + pivot.query);
}

Expand Down
Loading
Loading