Caideyipi commented on code in PR #18639: URL: https://github.com/apache/iotdb/pull/18639#discussion_r4281311464
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/tagfilter/TabletTagFilter.java: ########## @@ -0,0 +1,362 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.subscription.tagfilter; + +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Identifier; +import org.apache.iotdb.db.i18n.DataNodeMiscMessages; +import org.apache.iotdb.db.pipe.event.common.tablet.PipeTabletUtils; + +import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.enums.ColumnCategory; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.utils.Binary; +import org.apache.tsfile.write.record.Tablet; +import org.apache.tsfile.write.schema.IMeasurementSchema; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; + +/** Filters rows of a table-model Tablet according to TAG column values. */ +public class TabletTagFilter { + + private static final String DELIMITED_FIELD_PREFIX = "D:"; + private static final String UNDELIMITED_FIELD_PREFIX = "U:"; + + private TabletTagFilter() { + // utility class + } + + public static Tablet filter(final Tablet tablet, final TagFilterMatcher matcher) { + return filter(tablet, matcher, null); + } + + public static Tablet filter( + final Tablet tablet, final TagFilterMatcher matcher, final String databaseName) { + if (Objects.isNull(tablet)) { + return null; + } + + final TagFilterMatcher effectiveMatcher = + Objects.nonNull(matcher) ? matcher : TagFilterMatcher.matchAll(); + effectiveMatcher.throwIfFailure(); + if (effectiveMatcher.isMatchAll()) { + return tablet; + } + if (effectiveMatcher.isMatchNone() || tablet.getRowSize() <= 0) { + return null; + } + + final List<IMeasurementSchema> schemas = tablet.getSchemas(); + final Object[] values = tablet.getValues(); + if (Objects.isNull(schemas) || schemas.isEmpty()) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLET_SCHEMA_IS_MISSING_164075B0); + } + if (Objects.isNull(values) || values.length < schemas.size()) { + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_TABLET_VALUE_COLUMNS_ARE_INCOMPLETE_DCD09F3C); + } + + final List<ColumnCategory> categories = getColumnCategories(tablet, schemas.size()); + validateTabletStructure(tablet, schemas, values, categories); + validateBinding(effectiveMatcher, databaseName, tablet.getTableName(), schemas, categories); + final Map<String, Integer> resolvedFields = + resolveReferencedTagColumns(effectiveMatcher.getReferencedFields(), schemas, categories); + + final List<Integer> selectedRows = new ArrayList<>(); + try { + for (int rowIndex = 0; rowIndex < tablet.getRowSize(); rowIndex++) { + final int currentRowIndex = rowIndex; + if (effectiveMatcher.matches( + identifier -> + getTagValue(tablet, currentRowIndex, resolvedFields.get(toFieldKey(identifier))))) { + selectedRows.add(rowIndex); + } + } + } catch (final RuntimeException e) { + if (e instanceof TagFilterEvaluationException) { + throw e; + } + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_FAILED_TO_EVALUATE_A_TABLET_ROW_7E4E94CE, e); + } + + if (selectedRows.isEmpty()) { + return null; + } + if (selectedRows.size() == tablet.getRowSize()) { + return tablet; + } + return copySelectedRows(tablet, schemas, categories, selectedRows); + } + + private static void validateBinding( + final TagFilterMatcher matcher, + final String databaseName, + final String tableName, + final List<IMeasurementSchema> schemas, + final List<ColumnCategory> categories) { + if (!matcher.isBindingEnforced()) { + return; + } + final TagFilterMatcher.TableBinding binding = + matcher.getTableBindings().get(TagFilterMatcher.TableKey.of(databaseName, tableName)); + if (binding == null) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLE_BINDING_IS_NOT_AVAILABLE_882C5F2F); + } + if (binding.isFailed()) { + throw TagFilterEvaluationException.schemaBinding(binding.getFailureReason()); + } + for (final Identifier identifier : matcher.getReferencedFields()) { + final String normalizedName = identifier.getValue().toLowerCase(Locale.ROOT); + if (!binding.getTagNames().contains(normalizedName)) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_REFERENCED_COLUMN_IS_NOT_A_TAG_COLUMN_33FA34BC); + } + } + // A runtime tablet must carry category metadata consistent with the binding. Missing or + // changed categories are structural/schema failures, never SQL NULL values. + for (int i = 0; i < schemas.size(); i++) { + final String name = schemas.get(i).getMeasurementName(); + if (binding.getAllNames().contains(name.toLowerCase(Locale.ROOT)) + && categories.get(i) != ColumnCategory.TAG + && matcher.getReferencedFields().stream() + .anyMatch(identifier -> identifier.getValue().equalsIgnoreCase(name))) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_REFERENCED_COLUMN_CATEGORY_CHANGED_564594D8); + } + } + } + + private static Map<String, Integer> resolveReferencedTagColumns( + final List<Identifier> referencedFields, + final List<IMeasurementSchema> schemas, + final List<ColumnCategory> categories) { + final Map<String, Integer> exactTagColumns = new HashMap<>(); + final Map<String, Integer> normalizedTagColumns = new HashMap<>(); + final Map<String, ColumnCategory> exactColumns = new HashMap<>(); + final Map<String, ColumnCategory> normalizedColumns = new HashMap<>(); + for (int i = 0; i < schemas.size(); i++) { + final IMeasurementSchema schema = schemas.get(i); + if (Objects.isNull(schema) || Objects.isNull(schema.getMeasurementName())) { + continue; + } + exactColumns.put(schema.getMeasurementName(), categories.get(i)); + normalizedColumns.put( + schema.getMeasurementName().toLowerCase(Locale.ROOT), categories.get(i)); + if (categories.get(i) != ColumnCategory.TAG) { + continue; + } + exactTagColumns.put(schema.getMeasurementName(), i); + normalizedTagColumns.put(schema.getMeasurementName().toLowerCase(Locale.ROOT), i); + } + + final Map<String, Integer> result = new HashMap<>(); + for (final Identifier identifier : referencedFields) { + final Integer columnIndex = + identifier.isDelimited() + ? exactTagColumns.get(identifier.getValue()) + : normalizedTagColumns.get(identifier.getValue().toLowerCase(Locale.ROOT)); + if (Objects.isNull(columnIndex)) { + final ColumnCategory category = + identifier.isDelimited() + ? exactColumns.get(identifier.getValue()) + : normalizedColumns.get(identifier.getValue().toLowerCase(Locale.ROOT)); + if (category != null && category != ColumnCategory.TAG) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_REFERENCED_COLUMN_IS_NOT_A_TAG_COLUMN_33FA34BC); + } + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_REFERENCED_TAG_COLUMN_IS_MISSING_55E07377); + } + result.put(toFieldKey(identifier), columnIndex); + } + return result; + } + + private static String getTagValue( + final Tablet tablet, final int rowIndex, final Integer columnIndex) { + if (Objects.isNull(columnIndex) || tablet.isNull(rowIndex, columnIndex)) { + return null; + } + final Object value = tablet.getValue(rowIndex, columnIndex); + if (value instanceof Binary) { + return ((Binary) value).getStringValue(TSFileConfig.STRING_CHARSET); + } + return Objects.nonNull(value) ? String.valueOf(value) : null; + } + + private static Tablet copySelectedRows( + final Tablet tablet, + final List<IMeasurementSchema> schemas, + final List<ColumnCategory> categories, + final List<Integer> selectedRows) { + final List<String> columnNames = new ArrayList<>(schemas.size()); + final List<TSDataType> dataTypes = new ArrayList<>(schemas.size()); + for (final IMeasurementSchema schema : schemas) { + if (Objects.isNull(schema) + || Objects.isNull(schema.getMeasurementName()) + || Objects.isNull(schema.getType())) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLET_MEASUREMENT_SCHEMA_IS_INCOMPLETE_6A813472); + } + columnNames.add(schema.getMeasurementName()); + dataTypes.add(schema.getType()); + } + + final Tablet result = + new Tablet(tablet.getTableName(), columnNames, dataTypes, categories, selectedRows.size()); + try { + for (int targetRow = 0; targetRow < selectedRows.size(); targetRow++) { + final int sourceRow = selectedRows.get(targetRow); + PipeTabletUtils.putTimestamp(result, targetRow, tablet.getTimestamp(sourceRow)); + for (int columnIndex = 0; columnIndex < schemas.size(); columnIndex++) { + if (tablet.isNull(sourceRow, columnIndex)) { + PipeTabletUtils.markNullValue(result, targetRow, columnIndex); + } else { + final Object value = tablet.getValue(sourceRow, columnIndex); + if (Objects.isNull(value)) { + PipeTabletUtils.markNullValue(result, targetRow, columnIndex); + } else { + PipeTabletUtils.putValue( + result, targetRow, columnIndex, dataTypes.get(columnIndex), value); + } + } + } + } + return result; + } catch (final RuntimeException e) { + if (e instanceof TagFilterEvaluationException) { + throw e; + } + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_FAILED_TO_COMPACT_FILTERED_TABLET_5D4AD8AA, e); + } + } + + private static List<ColumnCategory> getColumnCategories( + final Tablet tablet, final int columnCount) { + final List<ColumnCategory> categories = tablet.getColumnTypes(); + if (Objects.isNull(categories) || categories.size() < columnCount) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLET_COLUMN_CATEGORIES_ARE_MISSING_2C660532); + } + final List<ColumnCategory> result = new ArrayList<>(columnCount); + for (int i = 0; i < columnCount; i++) { + if (Objects.isNull(categories.get(i))) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLET_COLUMN_CATEGORY_IS_MISSING_A812B500); + } + result.add(categories.get(i)); + } + return result; + } + + private static void validateTabletStructure( + final Tablet tablet, + final List<IMeasurementSchema> schemas, + final Object[] values, + final List<ColumnCategory> categories) { + if (tablet.getTimestamps() == null || tablet.getTimestamps().length < tablet.getRowSize()) { + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_TABLET_TIMESTAMPS_ARE_INCOMPLETE_24F8CE6F); + } + final org.apache.tsfile.utils.BitMap[] bitMaps = tablet.getBitMaps(); + if (bitMaps != null && bitMaps.length < schemas.size()) { + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_TABLET_BITMAPS_ARE_INCOMPLETE_7BBC8035); + } + for (int i = 0; i < schemas.size(); i++) { + final IMeasurementSchema schema = schemas.get(i); + if (schema == null || schema.getMeasurementName() == null || schema.getType() == null) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLET_MEASUREMENT_SCHEMA_IS_INCOMPLETE_6A813472); + } + if (values[i] == null) { + // A column whose every row is NULL may legitimately omit its value array and represent + // the values solely through a bitmap. It still has a complete schema and must remain + // distinguishable from a missing or malformed column. + if (!isEntirelyNull(bitMaps, i, tablet.getRowSize())) { + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_TABLET_VALUE_COLUMN_IS_INCOMPLETE_845721FE); + } + continue; + } + if (!hasArrayLength(values[i], tablet.getRowSize())) { + throw new TagFilterEvaluationException( + DataNodeMiscMessages.EXCEPTION_TABLET_VALUE_COLUMN_IS_INCOMPLETE_845721FE); + } + if (categories.get(i) == null) { + throw TagFilterEvaluationException.schemaBinding( + DataNodeMiscMessages.EXCEPTION_TABLET_COLUMN_CATEGORY_IS_MISSING_A812B500); + } + } Review Comment: Removed the full Tablet structural scan, value-array length/type checks, per-cell null validation, and repeated schema/category validation. Upstream conversion supplies the structural guarantees. The filter retains predicate-specific checks for missing bindings, missing referenced columns, and non-TAG columns. It also uses primitive row indexes and copies typed arrays directly during compaction. Commit: 7caec10c275. ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/tagfilter/TagFilterEvaluator.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.subscription.tagfilter; + +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.BooleanLiteral; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.CommonQueryAstVisitor; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.ComparisonExpression; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Expression; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.FunctionCall; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Identifier; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.InListExpression; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.InPredicate; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.IsNullPredicate; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.LikePredicate; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.LogicalExpression; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Node; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.NotExpression; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.StringLiteral; +import org.apache.iotdb.db.i18n.DataNodeMiscMessages; + +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.regex.Pattern; + +/** Evaluates a validated tag-filter with SQL three-valued NULL semantics. */ +public class TagFilterEvaluator + implements CommonQueryAstVisitor< + TagFilterEvaluator.TruthValue, TagFilterEvaluator.ValueProvider> { + + enum TruthValue { + TRUE, + FALSE, + UNKNOWN + } + + private final Map<Expression, Pattern> compiledPatterns; + + private TagFilterEvaluator() { + this(Collections.emptyMap()); + } + + private TagFilterEvaluator(final Map<Expression, Pattern> compiledPatterns) { + this.compiledPatterns = compiledPatterns; + } + + @FunctionalInterface + public interface ValueProvider { + + String getValue(Identifier identifier); + } + + public static boolean evaluate(final Expression expression, final ValueProvider valueProvider) { + final TagFilterValidator.ValidationResult validation = + TagFilterValidator.validateAndCompile(expression); + return evaluate(expression, valueProvider, validation.getCompiledPatterns()); + } + + public static boolean evaluate( + final Expression expression, + final ValueProvider valueProvider, + final Map<Expression, Pattern> compiledPatterns) { + return new TagFilterEvaluator(compiledPatterns).process(expression, valueProvider) + == TruthValue.TRUE; + } + + @Override + public TruthValue visitNode(final Node node, final ValueProvider context) { + throw new IllegalArgumentException( + String.format( + DataNodeMiscMessages.UNSUPPORTED_EXPRESSION_FMT, node.getClass().getSimpleName())); + } + + @Override + public TruthValue visitBooleanLiteral(final BooleanLiteral node, final ValueProvider context) { + return node.getValue() ? TruthValue.TRUE : TruthValue.FALSE; + } + + @Override + public TruthValue visitLogicalExpression( + final LogicalExpression node, final ValueProvider context) { + boolean hasUnknown = false; + if (node.getOperator() == LogicalExpression.Operator.AND) { + for (final Expression term : node.getTerms()) { + final TruthValue result = process(term, context); + if (result == TruthValue.FALSE) { + return TruthValue.FALSE; + } + hasUnknown |= result == TruthValue.UNKNOWN; + } Review Comment: We need to continue after UNKNOWN to preserve SQL three-valued logic: `UNKNOWN AND FALSE` is FALSE, and `UNKNOWN OR TRUE` is TRUE. In particular, `NOT (UNKNOWN AND FALSE)` must match. Kept short-circuiting on FALSE for AND and TRUE for OR, documented why UNKNOWN is not decisive, and added regression assertions for both operand orders and NOT. These assertions also caught and fixed the shared parser's false rejection of valid `NOT (...)` expressions. Commit: 7caec10c275. ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/tagfilter/TagFilterMatcher.java: ########## @@ -0,0 +1,421 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.subscription.tagfilter; + +import org.apache.iotdb.commons.pipe.datastructure.pattern.TablePattern; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Expression; +import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Identifier; +import org.apache.iotdb.commons.schema.table.TsTable; +import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory; +import org.apache.iotdb.commons.schema.table.column.TsTableColumnSchema; +import org.apache.iotdb.db.i18n.DataNodeMiscMessages; +import org.apache.iotdb.rpc.subscription.config.TopicConfig; +import org.apache.iotdb.rpc.subscription.config.TopicConstant; +import org.apache.iotdb.rpc.subscription.exception.SubscriptionException; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.regex.Pattern; + +/** Immutable parsed tag-filter cached per topic. */ +public class TagFilterMatcher { + + public enum RuntimeStatus { + MATCH_ALL, + MATCH_NONE, + ACTIVE, + ERROR + } + + private static final TagFilterMatcher MATCH_NONE = + new TagFilterMatcher( + null, Collections.emptyList(), Collections.emptyMap(), State.MATCH_NONE, null, false); + + private final Expression expression; + private final List<Identifier> referencedFields; + private final Map<Expression, Pattern> compiledPatterns; + private final State state; + private final Throwable failure; + private final Map<TableKey, TableBinding> tableBindings; + private final boolean bindingEnforced; + + private enum State { + MATCH_ALL, + MATCH_NONE, + ACTIVE, + FAILURE + } + + private TagFilterMatcher( + final Expression expression, + final List<Identifier> referencedFields, + final Map<Expression, Pattern> compiledPatterns, + final State state, + final Throwable failure, + final boolean bindingEnforced) { + this( + expression, + referencedFields, + compiledPatterns, + state, + failure, + Collections.emptyMap(), + bindingEnforced); + } + + private TagFilterMatcher( + final Expression expression, + final List<Identifier> referencedFields, + final Map<Expression, Pattern> compiledPatterns, + final State state, + final Throwable failure, + final Map<TableKey, TableBinding> tableBindings, + final boolean bindingEnforced) { + this.expression = expression; + this.referencedFields = referencedFields; + this.compiledPatterns = compiledPatterns; + this.state = state; + this.failure = failure; + this.tableBindings = tableBindings; + this.bindingEnforced = bindingEnforced; + } + + private static final TagFilterMatcher MATCH_ALL = + new TagFilterMatcher( + null, Collections.emptyList(), Collections.emptyMap(), State.MATCH_ALL, null, false); + + public static TagFilterMatcher matchAll() { + return MATCH_ALL; + } + + public static TagFilterMatcher matchNone() { + return MATCH_NONE; + } + + public static TagFilterMatcher failure(final Throwable failure) { + return new TagFilterMatcher( + null, + Collections.emptyList(), + Collections.emptyMap(), + State.FAILURE, + Objects.nonNull(failure) ? failure : new IllegalStateException(), + false); + } + + public static TagFilterMatcher fromTopicConfig(final TopicConfig topicConfig) + throws SubscriptionException { + if (Objects.isNull(topicConfig) + || !topicConfig.isTableTopic() + || topicConfig.isTagFilterTrivial()) { + return matchAll(); + } + + final Expression expression; + final TagFilterValidator.ValidationResult validation; + try { + expression = new TagFilterParser().parse(topicConfig.getTagFilter()); + validation = TagFilterValidator.validateAndCompile(expression); + } catch (final IllegalArgumentException e) { + throw new SubscriptionException( + String.format( + DataNodeMiscMessages.EXCEPTION_INVALID_TAG_FILTER_ARG_E4B1C1C6, e.getMessage()), + e); + } + return new TagFilterMatcher( + expression, + validation.getReferencedFields(), + validation.getCompiledPatterns(), + State.ACTIVE, + null, + false); + } + + /** + * Parses a filter and validates every currently visible table selected by the topic pattern. An + * empty table map is deliberately allowed for a wildcard topic because the table may be created + * after the topic; a null map means that a required concrete schema is unavailable. + */ + public static TagFilterMatcher fromTopicConfig( + final TopicConfig topicConfig, final Map<String, Map<String, TsTable>> tables) + throws SubscriptionException { + final TagFilterMatcher matcher = fromTopicConfig(topicConfig); + if (matcher.isMatchAll()) { + return matcher; + } + if (tables == null) { + throw new SubscriptionException( + DataNodeMiscMessages.EXCEPTION_TABLE_SCHEMA_IS_NOT_AVAILABLE_FOR_TAG_FILTER_993AB728); + } + + final TablePattern tablePattern = + new TablePattern( + true, + topicConfig.getStringOrDefault( + TopicConstant.DATABASE_KEY, TopicConstant.DATABASE_DEFAULT_VALUE), + topicConfig.getStringOrDefault( + TopicConstant.TABLE_KEY, TopicConstant.TABLE_DEFAULT_VALUE)); + final Map<TableKey, TableBinding> bindings = new HashMap<>(); + for (final Map.Entry<String, Map<String, TsTable>> databaseEntry : tables.entrySet()) { + if (!tablePattern.matchesDatabase(databaseEntry.getKey())) { + continue; + } + for (final TsTable table : databaseEntry.getValue().values()) { + if (table == null || !tablePattern.matchesTable(table.getTableName())) { + continue; + } + final TableBinding binding = bindTable(matcher.referencedFields, table); + if (binding.isFailed()) { + throw new SubscriptionException(binding.getFailureReason()); + } + bindings.put(TableKey.of(databaseEntry.getKey(), table.getTableName()), binding); + } + } + final boolean bindingEnforced = + isLiteralTopicPattern( + topicConfig.getStringOrDefault( + TopicConstant.DATABASE_KEY, TopicConstant.DATABASE_DEFAULT_VALUE)) + && isLiteralTopicPattern( + topicConfig.getStringOrDefault( + TopicConstant.TABLE_KEY, TopicConstant.TABLE_DEFAULT_VALUE)); + return matcher.withBindings(bindings, bindingEnforced); + } + + private static boolean isLiteralTopicPattern(final String pattern) { + final String regexMetaCharacters = ".*+?[](){}\\|^$"; + return Objects.nonNull(pattern) + && pattern.chars().noneMatch(c -> regexMetaCharacters.indexOf((char) c) >= 0); + } + + private static TableBinding bindTable( + final List<Identifier> referencedFields, final TsTable table) { + final java.util.Set<String> tagNames = new java.util.HashSet<>(); Review Comment: Added normal imports for `Set`, `HashSet`, and `Locale`, and removed their fully qualified references throughout the class. Commit: 7caec10c275. ########## iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/subscription/meta/topic/TopicMeta.java: ########## @@ -396,6 +405,74 @@ public static void validateOwnerProgression( } } + private static Map<String, String> canonicalizeTopicAttributes( + final Map<String, String> topicAttributes) { + final Map<String, String> canonicalAttributes = new HashMap<>(); + if (Objects.isNull(topicAttributes)) { + return canonicalAttributes; + } + topicAttributes.forEach( + (key, value) -> canonicalAttributes.put(canonicalizeTopicAttributeKey(key), value)); + return canonicalAttributes; + } + + private static void canonicalizeTopicAttributesInPlace(final Map<String, String> attributes) { + final Map<String, String> canonicalAttributes = canonicalizeTopicAttributes(attributes); + attributes.clear(); + attributes.putAll(canonicalAttributes); + } + + private static String canonicalizeTopicAttributeKey(final String key) { + if (Objects.isNull(key)) { + return null; + } + final String normalizedKey = key.trim().toLowerCase(Locale.ROOT); + switch (normalizedKey) { + case TopicConstant.PATH_KEY: + return TopicConstant.PATH_KEY; + case TopicConstant.PATTERN_KEY: + return TopicConstant.PATTERN_KEY; + case TopicConstant.DATABASE_KEY: + return TopicConstant.DATABASE_KEY; + case TopicConstant.TABLE_KEY: + return TopicConstant.TABLE_KEY; + case TopicConstant.COLUMN_FILTER_KEY: + return TopicConstant.COLUMN_FILTER_KEY; + case TopicConstant.TAG_FILTER_KEY: + return TopicConstant.TAG_FILTER_KEY; + case TopicConstant.RETENTION_BYTES_KEY: + return TopicConstant.RETENTION_BYTES_KEY; + case TopicConstant.RETENTION_MS_KEY: + return TopicConstant.RETENTION_MS_KEY; + case TopicConstant.START_TIME_KEY: + return TopicConstant.START_TIME_KEY; + case TopicConstant.END_TIME_KEY: + return TopicConstant.END_TIME_KEY; Review Comment: This canonicalizes case-insensitive built-in topic keys before creation, ALTER merging, and deserialization. For example, ALTER with `TAG-FILTER` must replace an existing `tag-filter` rather than leave two values that different getters could read inconsistently. Replaced the switch with the shared `TopicProperty` registry, documented the purpose, and added tests for mixed-case ALTER and both deserialization paths. Extension keys and predicate values retain their original case. Commit: 7caec10c275. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
