Skip to content

Commit

Permalink
Adding sqlCluster as an override parameter for SqlValidatorConfig. Th…
Browse files Browse the repository at this point in the history
…is allows

for overriding validateFeature.

Removing unused getter from ValidatorAggStuff.
  • Loading branch information
jamesstarr committed Sep 17, 2024
1 parent d0250bb commit 6757458
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,12 @@ default Config withNakedMeasures(boolean nakedMeasures) {
return ScopeMapImpl.Factory.DEFAULT;
}

/** Configure a custom {@link SqlCluster}. */
Config withSqlCluster(SqlCluster sqlCluster);

/** Returns a custom SqlCluster. If not set, a new one is created. */
@Nullable SqlCluster sqlCluster();

/** Set a factory for name space builder to allow for custom behavior downstream. */
Config withNamespaceBuilderFactory(NamespaceBuilder.Factory factory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ protected SqlValidatorImpl(
this.typeCoercion = typeCoercion;
this.scopeMap = config.scopeMapFactory().create(catalogReader);
this.namespaceBuilder = config.namespaceBuilderFactory().create(this);
this.sqlCluster = new SqlCluster(opTab, catalogReader, typeFactory);
SqlCluster customCluster = config.sqlCluster();
this.sqlCluster = null == customCluster
? new SqlCluster(opTab, catalogReader, typeFactory)
: customCluster;
this.validatorAggStuff = new ValidatorAggStuff(sqlCluster, scopeMap);
this.scopePopulator =
new ScopePopulator(scopeMap, namespaceBuilder, sqlCluster, validatorAggStuff,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ protected boolean isOverAggregateWindow(SqlNode node) {
return aggFinder.findAgg(SqlNonNullableAccessors.getSelectList(select));
}


public AggFinder getAggFinder() {
return aggFinder;
}

public AggFinder getAggOrOverFinder() {
return aggOrOverFinder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.apache.calcite.runtime.Feature;
import org.apache.calcite.sql.SqlOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.validate.SqlCluster;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
import org.apache.calcite.sql.validate.SqlValidatorImpl;
import org.apache.calcite.sql.validate.SqlValidatorUtil;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.jupiter.api.Test;
Expand All @@ -43,7 +46,7 @@ class SqlValidatorFeatureTest extends SqlValidatorTestCase {

@Override public SqlValidatorFixture fixture() {
return super.fixture()
.withFactory(f -> f.withValidator(FeatureValidator::new));
.withFactory(f -> f.withValidator(this::createValidator));
}

@Test void testDistinct() {
Expand Down Expand Up @@ -106,35 +109,33 @@ private void checkFeature(String sql, Feature feature) {
}
}

//~ Inner Classes ----------------------------------------------------------

/** Extension to {@link SqlValidatorImpl} that validates features. */
public class FeatureValidator extends SqlValidatorImpl {
protected FeatureValidator(
SqlOperatorTable opTab,
SqlValidatorCatalogReader catalogReader,
RelDataTypeFactory typeFactory,
Config config) {
super(opTab, catalogReader, typeFactory, config);
}

protected void validateFeature(
Feature feature,
SqlParserPos pos) {
requireNonNull(pos, "pos");
if (feature.equals(disabledFeature)) {
CalciteException ex =
new CalciteException(
FEATURE_DISABLED,
null);
throw new CalciteContextException(
"location",
ex,
pos.getLineNum(),
pos.getColumnNum(),
pos.getEndLineNum(),
pos.getEndColumnNum());
private SqlValidator createValidator(SqlOperatorTable opTab,
SqlValidatorCatalogReader catalogReader,
RelDataTypeFactory typeFactory,
SqlValidator.Config config) {
SqlCluster sqlCluster = new SqlCluster(opTab, catalogReader, typeFactory) {
/** Extension to {@link SqlValidatorImpl} that validates features. */
@Override public void validateFeature(Feature feature, SqlParserPos pos) {
requireNonNull(pos, "pos");
if (feature.equals(disabledFeature)) {
CalciteException ex =
new CalciteException(
FEATURE_DISABLED,
null);
throw new CalciteContextException(
"location",
ex,
pos.getLineNum(),
pos.getColumnNum(),
pos.getEndLineNum(),
pos.getEndColumnNum());
}
}
}
};
return SqlValidatorUtil.newValidator(
opTab,
catalogReader,
typeFactory,
config.withSqlCluster(sqlCluster));
}
}

0 comments on commit 6757458

Please sign in to comment.