Skip to content

Commit

Permalink
Access columns by ordinal, not name
Browse files Browse the repository at this point in the history
  • Loading branch information
julianhyde committed Feb 17, 2024
1 parent 87e5486 commit 833370f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@
* <p>Used to flag individual columns as 'must-filter'.
*/
public interface SemanticTable {
/** Returns the filter expression for column {@code columnName}
* if it is a must-filter column,
/** Returns the filter expression for {@code column}
* if it is a {@link #mustFilter(int) must-filter} column,
* or null if it is not a must-filter column.
*
* @see #mustFilter(String) */
@Nullable String getFilter(String columnName);
* @param column Column ordinal (0-based)
*
* @throws IndexOutOfBoundsException if column ordinal is out of range */
default @Nullable String getFilter(int column) {
return null;
}

/** Returns whether column {@code columnName} must be filtered in any query
* that references this table. */
boolean mustFilter(String columnName);
/** Returns whether {@code column} must be filtered in any query
* that references this table.
*
* @param column Column ordinal (0-based)
*
* @throws IndexOutOfBoundsException if column ordinal is out of range */
default boolean mustFilter(int column) {
return getFilter(column) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private TableNamespace(SqlValidatorImpl validator, SqlValidatorTable table,
.ifPresent(semanticTable ->
this.mustFilterFields =
table.getRowType().getFieldList().stream()
.filter(f -> semanticTable.mustFilter(f.getName()))
.map(RelDataTypeField::getIndex)
.filter(semanticTable::mustFilter)
.collect(toImmutableBitSet()));

if (extendedFields.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,39 +984,40 @@ protected abstract RexNode getConstraint(RexBuilder rexBuilder,
*/
public static class MustFilterMockTable
extends MockTable implements SemanticTable {

private final Map<String, String> mustFilterFields;
private final Map<String, String> fieldFilters;

MustFilterMockTable(MockCatalogReader catalogReader, String catalogName,
String schemaName, String name, boolean stream, boolean temporal,
double rowCount, @Nullable ColumnResolver resolver,
InitializerExpressionFactory initializerExpressionFactory,
Map<String, String> mustFilterFields) {
Map<String, String> fieldFilters) {
super(catalogReader, catalogName, schemaName, name, stream, temporal,
rowCount, resolver, initializerExpressionFactory);
this.mustFilterFields = ImmutableMap.copyOf(mustFilterFields);
this.fieldFilters = ImmutableMap.copyOf(fieldFilters);
}

/** Creates a MustFilterMockTable that implements {@link SemanticTable}. */
public static MustFilterMockTable create(MockCatalogReader catalogReader,
MockSchema schema, String name, boolean stream, double rowCount,
@Nullable ColumnResolver resolver,
InitializerExpressionFactory initializerExpressionFactory,
boolean temporal, Map<String, String> mustFilterFields) {
boolean temporal, Map<String, String> fieldFilters) {
MustFilterMockTable table =
new MustFilterMockTable(catalogReader, schema.getCatalogName(),
schema.name, name, stream, temporal, rowCount, resolver,
initializerExpressionFactory, mustFilterFields);
initializerExpressionFactory, fieldFilters);
schema.addTable(name);
return table;
}

@Override public @Nullable String getFilter(String columnName) {
return mustFilterFields.get(columnName);
@Override public @Nullable String getFilter(int column) {
String columnName = columnList.get(column).getKey();
return fieldFilters.get(columnName);
}

@Override public boolean mustFilter(String columnName) {
return mustFilterFields.containsKey(columnName);
@Override public boolean mustFilter(int column) {
String columnName = columnList.get(column).getKey();
return fieldFilters.containsKey(columnName);
}
}

Expand Down

0 comments on commit 833370f

Please sign in to comment.