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

Wip scope populator #3965

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.calcite.config.CalciteConnectionConfig;
import org.apache.calcite.prepare.CalciteCatalogReader;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.validate.SqlCluster;
import org.apache.calcite.sql.validate.SqlValidatorImpl;

import com.google.common.collect.ImmutableList;
Expand All @@ -45,8 +46,9 @@ public class ContextSqlValidator extends SqlValidatorImpl {
* @param mutable Whether to get the mutable schema.
*/
public ContextSqlValidator(CalcitePrepare.Context context, boolean mutable) {
super(SqlStdOperatorTable.instance(), getCatalogReader(context, mutable),
context.getTypeFactory(), Config.DEFAULT);
super(
new SqlCluster(SqlStdOperatorTable.instance(), getCatalogReader(context, mutable),
context.getTypeFactory()), Config.DEFAULT);
}

private static CalciteCatalogReader getCatalogReader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlInsert;
import org.apache.calcite.sql.SqlOperatorTable;
import org.apache.calcite.sql.validate.SqlCluster;
import org.apache.calcite.sql.validate.SqlValidatorImpl;

/** Validator.
Expand All @@ -29,7 +30,7 @@ public class CalciteSqlValidator extends SqlValidatorImpl {
public CalciteSqlValidator(SqlOperatorTable opTab,
CalciteCatalogReader catalogReader, JavaTypeFactory typeFactory,
Config config) {
super(opTab, catalogReader, typeFactory, config);
super(new SqlCluster(opTab, catalogReader, typeFactory), config);
}

@Override protected RelDataType getLogicalSourceRowType(
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlCallBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public SqlCallBinding(SqlValidator validator, SqlValidatorScope scope,
}
return n;
}
return validator.isAggregate(select) ? 0 : -1;
return validator.getValidatorAggStuff().isAggregate(select) ? 0 : -1;
}

/**
Expand Down 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 @@ -28,6 +28,7 @@
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.sql.validate.OverScope;
import org.apache.calcite.sql.validate.SelectScope;
import org.apache.calcite.sql.validate.SqlCluster;
import org.apache.calcite.sql.validate.SqlModality;
import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
import org.apache.calcite.sql.validate.SqlValidatorImpl;
Expand Down Expand Up @@ -67,7 +68,7 @@ public SqlAdvisorValidator(
SqlValidatorCatalogReader catalogReader,
RelDataTypeFactory typeFactory,
Config config) {
super(opTab, catalogReader, typeFactory, config);
super(new SqlCluster(opTab, catalogReader, typeFactory), config);
}

//~ Methods ----------------------------------------------------------------
Expand Down Expand Up @@ -169,11 +170,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 All @@ -199,7 +200,4 @@ private void registerId(SqlIdentifier id, SqlValidatorScope scope) {
return true;
}

@Override protected boolean shouldAllowOverRelation() {
return true; // no reason not to be lenient
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public SqlAbstractGroupFunction(String name,
final SelectScope selectScope =
requireNonNull(SqlValidatorUtil.getEnclosingSelectScope(scope));
final SqlSelect select = selectScope.getNode();
if (!validator.isAggregate(select)) {
if (!validator.getValidatorAggStuff().isAggregate(select)) {
throw validator.newValidationError(call,
Static.RESOURCE.groupingInAggregate(getName()));
}
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
Loading
Loading