jt2594838 commented on code in PR #18639:
URL: https://github.com/apache/iotdb/pull/18639#discussion_r4217720815


##########
iotdb-client/subscription/src/test/java/org/apache/iotdb/session/subscription/consumer/base/SubscriptionConsumerMultiProviderPollTest.java:
##########
@@ -93,14 +94,41 @@ public void testPollBacksOffAfterAllProvidersAreEmpty() 
throws SubscriptionExcep
     }
   }
 
+  @Test
+  public void testFilteredTabletBatchIsAckedWithoutReturningEmptyMessage()
+      throws SubscriptionException {
+    final TestPullConsumer consumer = new TestPullConsumer(1, 0, true);
+    try {
+      consumer.open();
+      consumer.subscribeTopic();
+
+      final List<SubscriptionMessage> messages = consumer.pollForTest(1_000L);
+
+      Assert.assertTrue(messages.isEmpty());
+      Assert.assertEquals(1, consumer.getAcknowledgedCommitContexts().size());
+      Assert.assertEquals(
+          new SubscriptionCommitContext(1, 0, TOPIC, CONSUMER_GROUP_ID, 0L),
+          consumer.getAcknowledgedCommitContexts().get(0));

Review Comment:
   Why do you call the test `WithoutReturningEmptyMessage` but the messages are 
empty?



##########
iotdb-core/confignode/src/main/i18n/zh/org/apache/iotdb/confignode/i18n/ConfigNodeMessages.java:
##########
@@ -718,6 +718,15 @@ private ConfigNodeMessages() {}
   public static final String
       
EXCEPTION_FAILED_TO_CREATE_OR_ALTER_TOPIC_ARG_AND_ARG_ARE_ONLY_SUPPORTED_FOR_INCREMENTAL_TOPICS_D86CEA8E
 =
           "创建或修改 topic 失败,%s 和 %s 仅支持 incremental 模式的 topic";
+  public static final String
+      
EXCEPTION_FAILED_TO_CREATE_OR_ALTER_TOPIC_DUPLICATE_ARG_ATTRIBUTES_ARE_NOT_ALLOWED_27315578
 =
+          "\u521b\u5efa\u6216\u4fee\u6539 Topic 
\u5931\u8d25\uff0c\u4e0d\u5141\u8bb8\u91cd\u590d\u7684 %s \u5c5e\u6027";
+  public static final String
+      
EXCEPTION_FAILED_TO_CREATE_OR_ALTER_TOPIC_ARG_IS_ONLY_SUPPORTED_FOR_TABLE_TOPICS_A5126607
 =
+          "\u521b\u5efa\u6216\u4fee\u6539 Topic \u5931\u8d25\uff0c%s 
\u4ec5\u652f\u6301\u8868\u6a21\u578b Topic";
+  public static final String
+      
EXCEPTION_FAILED_TO_CREATE_OR_ALTER_TOPIC_ARG_SHOULD_NOT_BE_EMPTY_767B1148 =
+          "\u521b\u5efa\u6216\u4fee\u6539 Topic \u5931\u8d25\uff0c%s 
\u4e0d\u5e94\u4e3a\u7a7a";

Review Comment:
   Mind this



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/agent/SubscriptionBrokerAgent.java:
##########
@@ -815,6 +821,156 @@ public void dropColumnFilter(final String topicName) {
     LOGGER.info(DataNodeMiscMessages.SUBSCRIPTION_DROP_COLUMN_FILTER, 
topicName);
   }
 
+  public void refreshTagFilter(final String topicName, final TopicConfig 
topicConfig) {
+    final ITableCache tableCache = DataNodeTableCache.getInstance();
+    if (Objects.isNull(topicConfig)
+        || !topicConfig.isTableTopic()
+        || topicConfig.isTagFilterTrivial()) {
+      cacheTagFilterMatcher(
+          topicName, TagFilterMatcher.matchAll(), 
tableCache.getInstanceVersion());
+      return;
+    }
+
+    for (int attempt = 0; attempt < TAG_FILTER_SCHEMA_SNAPSHOT_MAX_RETRIES; 
attempt++) {
+      final long versionBeforeBinding = tableCache.getInstanceVersion();
+      final TagFilterMatcher matcher;
+      try {
+        final Map<String, Map<String, TsTable>> bindingTables =
+            getTagFilterBindingTables(topicConfig);
+        if (bindingTables == null) {
+          if (versionBeforeBinding != tableCache.getInstanceVersion()) {
+            continue;
+          }
+          cacheTagFilterMatcher(
+              topicName,
+              TagFilterMatcher.failure(
+                  new SubscriptionException(
+                      DataNodeMiscMessages
+                          
.EXCEPTION_TABLE_SCHEMA_IS_NOT_AVAILABLE_FOR_TAG_FILTER_993AB728)),
+              versionBeforeBinding);
+          LOGGER.info(
+              DataNodeMiscMessages
+                  
.LOG_SUBSCRIPTION_TABLE_SCHEMA_IS_NOT_AVAILABLE_FOR_TAG_FILTER_TOPIC_ARG_E864C98F,
+              topicName);
+          return;
+        }

Review Comment:
   Better to give a clearer message about why the schema is not available.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/agent/SubscriptionTopicAgent.java:
##########
@@ -152,6 +157,35 @@ static boolean shouldRefreshColumnFilter(final TopicMeta 
oldMeta, final TopicMet
                     newConfig, TopicConstant.TABLE_KEY, 
TopicConstant.TABLE_DEFAULT_VALUE)));
   }
 
+  static boolean shouldRefreshTagFilter(final TopicMeta oldMeta, final 
TopicMeta newMeta) {
+    if (Objects.isNull(newMeta) || !newMeta.getConfig().isTableTopic()) {
+      return false;
+    }
+    if (Objects.isNull(oldMeta) || !oldMeta.getConfig().isTableTopic()) {
+      return true;
+    }
+
+    final TopicConfig oldConfig = oldMeta.getConfig();
+    final TopicConfig newConfig = newMeta.getConfig();
+    return !Objects.equals(
+            normalizeTagFilterValue(oldConfig.getTagFilter()),
+            normalizeTagFilterValue(newConfig.getTagFilter()))
+        || !Objects.equals(
+            normalizeTagFilterValue(
+                getAttributeIgnoreCase(
+                    oldConfig, TopicConstant.DATABASE_KEY, 
TopicConstant.DATABASE_DEFAULT_VALUE)),
+            normalizeTagFilterValue(
+                getAttributeIgnoreCase(
+                    newConfig, TopicConstant.DATABASE_KEY, 
TopicConstant.DATABASE_DEFAULT_VALUE)))

Review Comment:
   The name `normalizeTagFilterValue` is not precise since it is applied to 
other properties.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java:
##########
@@ -100,25 +126,22 @@ protected boolean constructBatch(final 
TabletInsertionEvent event) {
           (PipeInsertNodeTabletInsertionEvent) event;
       final boolean isTableModel = 
insertNodeTabletInsertionEvent.isTableModelEvent();
       final List<Tablet> tablets = 
insertNodeTabletInsertionEvent.convertToTablets();
-      final List<Tablet> retainedTablets = new ArrayList<>(tablets.size());
-      final List<Boolean> retainedAlignedFlags = new 
ArrayList<>(tablets.size());
+      final List<TabletTransformResult> retainedTablets = new 
ArrayList<>(tablets.size());
       for (int i = 0; i < tablets.size(); ++i) {
-        Tablet tablet = tablets.get(i);
+        final Tablet tablet = tablets.get(i);
         if (isTabletEmpty(tablet)) {
           continue;
         }
-        if (isTableModel) {
-          tablet =
-              pruneTableModelTablet(
-                  tablet, 
insertNodeTabletInsertionEvent.getTableModelDatabaseName());
-          if (isTabletEmpty(tablet)) {
-            continue;
-          }
-        }
-        retainedTablets.add(tablet);
-        if (!isTableModel) {
-          
retainedAlignedFlags.add(insertNodeTabletInsertionEvent.isAligned(i));
+        final TabletTransformResult transformedTablet =
+            transformTablet(
+                isTableModel ? 
insertNodeTabletInsertionEvent.getTableModelDatabaseName() : null,
+                tablet,
+                isTableModel,
+                !isTableModel && insertNodeTabletInsertionEvent.isAligned(i));

Review Comment:
   Why is ' isAligned ' false when `isTableModel` is true?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java:
##########
@@ -100,25 +126,22 @@ protected boolean constructBatch(final 
TabletInsertionEvent event) {
           (PipeInsertNodeTabletInsertionEvent) event;
       final boolean isTableModel = 
insertNodeTabletInsertionEvent.isTableModelEvent();
       final List<Tablet> tablets = 
insertNodeTabletInsertionEvent.convertToTablets();
-      final List<Tablet> retainedTablets = new ArrayList<>(tablets.size());
-      final List<Boolean> retainedAlignedFlags = new 
ArrayList<>(tablets.size());
+      final List<TabletTransformResult> retainedTablets = new 
ArrayList<>(tablets.size());
       for (int i = 0; i < tablets.size(); ++i) {
-        Tablet tablet = tablets.get(i);
+        final Tablet tablet = tablets.get(i);
         if (isTabletEmpty(tablet)) {
           continue;
         }
-        if (isTableModel) {
-          tablet =
-              pruneTableModelTablet(
-                  tablet, 
insertNodeTabletInsertionEvent.getTableModelDatabaseName());
-          if (isTabletEmpty(tablet)) {
-            continue;
-          }
-        }
-        retainedTablets.add(tablet);
-        if (!isTableModel) {
-          
retainedAlignedFlags.add(insertNodeTabletInsertionEvent.isAligned(i));
+        final TabletTransformResult transformedTablet =
+            transformTablet(
+                isTableModel ? 
insertNodeTabletInsertionEvent.getTableModelDatabaseName() : null,

Review Comment:
   Is it possible to let getTableModelDatabaseName return null when it is not a 
table model event?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionFilterSnapshot.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.event.batch;
+
+import org.apache.iotdb.db.i18n.DataNodeMiscMessages;
+import org.apache.iotdb.db.subscription.agent.SubscriptionAgent;
+import org.apache.iotdb.db.subscription.broker.SubscriptionPrefetchingQueue;
+import org.apache.iotdb.db.subscription.columnfilter.ColumnFilterMatcher;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterEvaluationException;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterMatcher;
+import org.apache.iotdb.rpc.subscription.config.TopicConfig;
+import org.apache.iotdb.rpc.subscription.config.TopicConstant;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+
+/** Immutable topic-filter state used by every event in one subscription 
batch. */
+final class SubscriptionFilterSnapshot {
+
+  private static final int MAX_CAPTURE_ATTEMPTS = 3;
+
+  private final boolean tableModel;
+  private final TopicConfig topicConfig;
+  private final Map<String, String> filteringAttributes;
+  private final TagFilterMatcher tagFilterMatcher;
+  private final ColumnFilterMatcher columnFilterMatcher;
+
+  private SubscriptionFilterSnapshot(
+      final boolean tableModel,
+      final TopicConfig topicConfig,
+      final TagFilterMatcher tagFilterMatcher,
+      final ColumnFilterMatcher columnFilterMatcher) {
+    this.tableModel = tableModel;
+    this.topicConfig = topicConfig;
+    this.filteringAttributes = filteringAttributes(topicConfig);
+    this.tagFilterMatcher = tagFilterMatcher;
+    this.columnFilterMatcher = columnFilterMatcher;
+  }
+
+  static SubscriptionFilterSnapshot capture(final SubscriptionPrefetchingQueue 
queue) {
+    final boolean tableModel =
+        SubscriptionAgent.consumer().isTableModel(queue.getConsumerGroupId());
+    for (int attempt = 0; attempt < MAX_CAPTURE_ATTEMPTS; attempt++) {
+      final TopicConfig topicConfig = getCurrentTopicConfig(queue, tableModel);
+      if (Objects.isNull(topicConfig)) {
+        throw new TagFilterEvaluationException(
+            DataNodeMiscMessages
+                
.EXCEPTION_TOPIC_CONFIGURATION_IS_NOT_AVAILABLE_FOR_TAG_FILTER_1E023E3A);
+      }
+
+      final TagFilterMatcher tagFilterMatcher =
+          SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), 
tableModel);
+      tagFilterMatcher.throwIfFailure();
+      final ColumnFilterMatcher columnFilterMatcher =
+          
SubscriptionAgent.broker().getColumnFilterMatcher(queue.getTopicName(), 
tableModel);
+
+      final TopicConfig currentTopicConfig = getCurrentTopicConfig(queue, 
tableModel);
+      if (Objects.nonNull(currentTopicConfig)
+          && 
filteringAttributes(topicConfig).equals(filteringAttributes(currentTopicConfig))
+          && tagFilterMatcher
+              == 
SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), tableModel)
+          && columnFilterMatcher
+              == SubscriptionAgent.broker()
+                  .getColumnFilterMatcher(queue.getTopicName(), tableModel)) {

Review Comment:
   Can we simply add a version in the topic config?
   The version may be divided into several sub-classes, like filter version, 
retention version, etc.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/sink/payload/evolvable/batch/PipeTabletEventTsFileBatch.java:
##########
@@ -127,50 +150,58 @@ protected boolean constructBatch(final 
TabletInsertionEvent event) {
       if (retainedTablets.isEmpty()) {
         return false;
       }
-      
increaseTotalBufferSizeAndUpdateMemoryBlock(calculateTabletsSizeInBytes(retainedTablets));
-      for (int i = 0; i < retainedTablets.size(); ++i) {
-        final Tablet tablet = retainedTablets.get(i);
-        if (isTableModel) {
+      increaseTotalBufferSizeAndUpdateMemoryBlock(
+          calculateTabletsSizeInBytes(
+              retainedTablets.stream()
+                  .map(TabletTransformResult::getTablet)
+                  .collect(java.util.stream.Collectors.toList())));

Review Comment:
   mind the import



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionTreeViewProjector.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.event.batch;
+
+import org.apache.iotdb.commons.pipe.config.constant.PipeSourceConstant;
+import org.apache.iotdb.commons.schema.table.TreeViewSchema;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
+import org.apache.iotdb.db.subscription.columnfilter.TreeViewTabletProjector;
+import org.apache.iotdb.rpc.subscription.config.TopicConfig;
+import org.apache.iotdb.rpc.subscription.config.TopicConstant;
+
+import org.apache.tsfile.write.record.Tablet;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+/**
+ * Resolves and applies the table projection for tree-model events captured by 
a Tree View topic.
+ */
+final class SubscriptionTreeViewProjector {
+
+  private final TopicConfig topicConfig;
+
+  private boolean initialized;
+  private TreeViewTabletProjector projector;
+
+  SubscriptionTreeViewProjector(final TopicConfig topicConfig) {
+    this.topicConfig = topicConfig;
+  }
+
+  synchronized boolean prepare() {
+    if (initialized) {
+      return true;
+    }
+    if (Objects.isNull(topicConfig) || !topicConfig.isTableTopic()) {
+      initialized = true;
+      return true;
+    }
+
+    final String database =
+        topicConfig.getStringOrDefault(
+            TopicConstant.DATABASE_KEY, TopicConstant.DATABASE_DEFAULT_VALUE);
+    final String tableName =
+        topicConfig.getStringOrDefault(TopicConstant.TABLE_KEY, 
TopicConstant.TABLE_DEFAULT_VALUE);
+    if (isDefaultTopicPattern(database, TopicConstant.DATABASE_DEFAULT_VALUE)
+        || isDefaultTopicPattern(tableName, TopicConstant.TABLE_DEFAULT_VALUE)
+        || !isLiteralTopicPattern(database)
+        || !isLiteralTopicPattern(tableName)) {
+      initialized = true;
+      return true;
+    }
+
+    if (!isTreeCapturedByTopic(topicConfig)
+        && topicConfig.isColumnFilterTrivial()
+        && topicConfig.isTagFilterTrivial()) {
+      initialized = true;
+      return true;
+    }
+
+    final TsTable table = DataNodeTableCache.getInstance().getTable(database, 
tableName, false);
+    if (Objects.isNull(table)) {
+      return false;
+    }
+    if (TreeViewSchema.isTreeViewTable(table)) {
+      projector = new TreeViewTabletProjector(database, table);
+    }
+    initialized = true;
+    return true;
+  }
+
+  Tablet project(final Tablet tablet) {
+    return Objects.nonNull(projector) ? projector.project(tablet) : null;
+  }

Review Comment:
   Why does a null projector return null?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionFilterSnapshot.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.event.batch;
+
+import org.apache.iotdb.db.i18n.DataNodeMiscMessages;
+import org.apache.iotdb.db.subscription.agent.SubscriptionAgent;
+import org.apache.iotdb.db.subscription.broker.SubscriptionPrefetchingQueue;
+import org.apache.iotdb.db.subscription.columnfilter.ColumnFilterMatcher;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterEvaluationException;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterMatcher;
+import org.apache.iotdb.rpc.subscription.config.TopicConfig;
+import org.apache.iotdb.rpc.subscription.config.TopicConstant;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+
+/** Immutable topic-filter state used by every event in one subscription 
batch. */
+final class SubscriptionFilterSnapshot {
+
+  private static final int MAX_CAPTURE_ATTEMPTS = 3;
+
+  private final boolean tableModel;
+  private final TopicConfig topicConfig;
+  private final Map<String, String> filteringAttributes;
+  private final TagFilterMatcher tagFilterMatcher;
+  private final ColumnFilterMatcher columnFilterMatcher;
+
+  private SubscriptionFilterSnapshot(
+      final boolean tableModel,
+      final TopicConfig topicConfig,
+      final TagFilterMatcher tagFilterMatcher,
+      final ColumnFilterMatcher columnFilterMatcher) {
+    this.tableModel = tableModel;
+    this.topicConfig = topicConfig;
+    this.filteringAttributes = filteringAttributes(topicConfig);
+    this.tagFilterMatcher = tagFilterMatcher;
+    this.columnFilterMatcher = columnFilterMatcher;
+  }
+
+  static SubscriptionFilterSnapshot capture(final SubscriptionPrefetchingQueue 
queue) {
+    final boolean tableModel =
+        SubscriptionAgent.consumer().isTableModel(queue.getConsumerGroupId());
+    for (int attempt = 0; attempt < MAX_CAPTURE_ATTEMPTS; attempt++) {
+      final TopicConfig topicConfig = getCurrentTopicConfig(queue, tableModel);
+      if (Objects.isNull(topicConfig)) {
+        throw new TagFilterEvaluationException(
+            DataNodeMiscMessages
+                
.EXCEPTION_TOPIC_CONFIGURATION_IS_NOT_AVAILABLE_FOR_TAG_FILTER_1E023E3A);
+      }
+
+      final TagFilterMatcher tagFilterMatcher =
+          SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), 
tableModel);
+      tagFilterMatcher.throwIfFailure();
+      final ColumnFilterMatcher columnFilterMatcher =
+          
SubscriptionAgent.broker().getColumnFilterMatcher(queue.getTopicName(), 
tableModel);
+
+      final TopicConfig currentTopicConfig = getCurrentTopicConfig(queue, 
tableModel);
+      if (Objects.nonNull(currentTopicConfig)
+          && 
filteringAttributes(topicConfig).equals(filteringAttributes(currentTopicConfig))
+          && tagFilterMatcher
+              == 
SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), tableModel)
+          && columnFilterMatcher
+              == SubscriptionAgent.broker()
+                  .getColumnFilterMatcher(queue.getTopicName(), tableModel)) {
+        return new SubscriptionFilterSnapshot(
+            tableModel,
+            new TopicConfig(new HashMap<>(topicConfig.getAttribute())),
+            tagFilterMatcher,
+            columnFilterMatcher);
+      }
+    }
+
+    throw new TagFilterEvaluationException(
+        DataNodeMiscMessages
+            
.EXCEPTION_TOPIC_CONFIGURATION_CHANGED_WHILE_CAPTURING_TAG_FILTER_SNAPSHOT_984CEB1B);
+  }
+
+  boolean isCurrent(final SubscriptionPrefetchingQueue queue) {
+    if (!tableModel) {
+      return true;
+    }
+    final TopicConfig currentTopicConfig = getCurrentTopicConfig(queue, true);
+    return Objects.nonNull(currentTopicConfig)
+        && filteringAttributes.equals(filteringAttributes(currentTopicConfig))
+        && tagFilterMatcher
+            == 
SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), true)
+        && columnFilterMatcher
+            == 
SubscriptionAgent.broker().getColumnFilterMatcher(queue.getTopicName(), true);
+  }
+
+  TopicConfig getTopicConfig() {
+    return topicConfig;
+  }
+
+  TagFilterMatcher getTagFilterMatcher() {
+    return tagFilterMatcher;
+  }
+
+  ColumnFilterMatcher getColumnFilterMatcher() {
+    return columnFilterMatcher;
+  }
+
+  boolean hasNonTrivialFilter() {
+    return tableModel && (!tagFilterMatcher.isMatchAll() || 
!columnFilterMatcher.isMatchAll());
+  }
+
+  private static TopicConfig getCurrentTopicConfig(
+      final SubscriptionPrefetchingQueue queue, final boolean tableModel) {
+    return SubscriptionAgent.topic()
+        .getTopicConfigs(Collections.singleton(queue.getTopicName()), 
tableModel)
+        .get(queue.getTopicName());
+  }

Review Comment:
   Can we add an override to avoid the intermediate map?



##########
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());

Review Comment:
   Why do we need a copy here?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionFilterSnapshot.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.event.batch;
+
+import org.apache.iotdb.db.i18n.DataNodeMiscMessages;
+import org.apache.iotdb.db.subscription.agent.SubscriptionAgent;
+import org.apache.iotdb.db.subscription.broker.SubscriptionPrefetchingQueue;
+import org.apache.iotdb.db.subscription.columnfilter.ColumnFilterMatcher;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterEvaluationException;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterMatcher;
+import org.apache.iotdb.rpc.subscription.config.TopicConfig;
+import org.apache.iotdb.rpc.subscription.config.TopicConstant;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+
+/** Immutable topic-filter state used by every event in one subscription 
batch. */
+final class SubscriptionFilterSnapshot {
+
+  private static final int MAX_CAPTURE_ATTEMPTS = 3;
+
+  private final boolean tableModel;
+  private final TopicConfig topicConfig;
+  private final Map<String, String> filteringAttributes;
+  private final TagFilterMatcher tagFilterMatcher;
+  private final ColumnFilterMatcher columnFilterMatcher;
+
+  private SubscriptionFilterSnapshot(
+      final boolean tableModel,
+      final TopicConfig topicConfig,
+      final TagFilterMatcher tagFilterMatcher,
+      final ColumnFilterMatcher columnFilterMatcher) {
+    this.tableModel = tableModel;
+    this.topicConfig = topicConfig;
+    this.filteringAttributes = filteringAttributes(topicConfig);
+    this.tagFilterMatcher = tagFilterMatcher;
+    this.columnFilterMatcher = columnFilterMatcher;
+  }
+
+  static SubscriptionFilterSnapshot capture(final SubscriptionPrefetchingQueue 
queue) {
+    final boolean tableModel =
+        SubscriptionAgent.consumer().isTableModel(queue.getConsumerGroupId());
+    for (int attempt = 0; attempt < MAX_CAPTURE_ATTEMPTS; attempt++) {
+      final TopicConfig topicConfig = getCurrentTopicConfig(queue, tableModel);
+      if (Objects.isNull(topicConfig)) {
+        throw new TagFilterEvaluationException(
+            DataNodeMiscMessages
+                
.EXCEPTION_TOPIC_CONFIGURATION_IS_NOT_AVAILABLE_FOR_TAG_FILTER_1E023E3A);
+      }
+
+      final TagFilterMatcher tagFilterMatcher =
+          SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), 
tableModel);
+      tagFilterMatcher.throwIfFailure();
+      final ColumnFilterMatcher columnFilterMatcher =
+          
SubscriptionAgent.broker().getColumnFilterMatcher(queue.getTopicName(), 
tableModel);
+
+      final TopicConfig currentTopicConfig = getCurrentTopicConfig(queue, 
tableModel);
+      if (Objects.nonNull(currentTopicConfig)
+          && 
filteringAttributes(topicConfig).equals(filteringAttributes(currentTopicConfig))
+          && tagFilterMatcher
+              == 
SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), tableModel)
+          && columnFilterMatcher
+              == SubscriptionAgent.broker()
+                  .getColumnFilterMatcher(queue.getTopicName(), tableModel)) {
+        return new SubscriptionFilterSnapshot(
+            tableModel,
+            new TopicConfig(new HashMap<>(topicConfig.getAttribute())),
+            tagFilterMatcher,
+            columnFilterMatcher);
+      }
+    }
+
+    throw new TagFilterEvaluationException(
+        DataNodeMiscMessages
+            
.EXCEPTION_TOPIC_CONFIGURATION_CHANGED_WHILE_CAPTURING_TAG_FILTER_SNAPSHOT_984CEB1B);
+  }
+
+  boolean isCurrent(final SubscriptionPrefetchingQueue queue) {
+    if (!tableModel) {
+      return true;
+    }
+    final TopicConfig currentTopicConfig = getCurrentTopicConfig(queue, true);
+    return Objects.nonNull(currentTopicConfig)
+        && filteringAttributes.equals(filteringAttributes(currentTopicConfig))
+        && tagFilterMatcher
+            == 
SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), true)
+        && columnFilterMatcher
+            == 
SubscriptionAgent.broker().getColumnFilterMatcher(queue.getTopicName(), true);
+  }
+
+  TopicConfig getTopicConfig() {
+    return topicConfig;
+  }
+
+  TagFilterMatcher getTagFilterMatcher() {
+    return tagFilterMatcher;
+  }
+
+  ColumnFilterMatcher getColumnFilterMatcher() {
+    return columnFilterMatcher;
+  }
+
+  boolean hasNonTrivialFilter() {
+    return tableModel && (!tagFilterMatcher.isMatchAll() || 
!columnFilterMatcher.isMatchAll());
+  }
+
+  private static TopicConfig getCurrentTopicConfig(
+      final SubscriptionPrefetchingQueue queue, final boolean tableModel) {
+    return SubscriptionAgent.topic()
+        .getTopicConfigs(Collections.singleton(queue.getTopicName()), 
tableModel)
+        .get(queue.getTopicName());
+  }
+
+  private static Map<String, String> filteringAttributes(final TopicConfig 
topicConfig) {
+    if (Objects.isNull(topicConfig) || 
Objects.isNull(topicConfig.getAttribute())) {
+      return Collections.emptyMap();
+    }
+    final Map<String, String> result = new HashMap<>();
+    topicConfig
+        .getAttribute()
+        .forEach(
+            (key, value) -> {
+              if (Objects.isNull(key)) {
+                return;
+              }
+              final String normalizedKey = key.trim().toLowerCase(Locale.ROOT);
+              if (!isOwnerAttribute(normalizedKey)) {
+                result.put(normalizedKey, value);
+              }
+            });
+    return result;
+  }
+
+  private static boolean isOwnerAttribute(final String key) {
+    return TopicConstant.OWNER_ID_KEY.equals(key)
+        || TopicConstant.OWNER_EPOCH_KEY.equals(key)
+        || TopicConstant.MAX_OWNER_EPOCH_KEY.equals(key)
+        || TopicConstant.OWNER_LEASE_DURATION_MS_KEY.equals(key);
+  }

Review Comment:
   New attributes will be added even if they are not.
   May use an enum class to refactor the keys and provide an 
`isFilteringProperty` method.



##########
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;
+  }

Review Comment:
   Is it possible to skip the validation and use the original array?



##########
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:
   What is this for?
   



##########
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;

Review Comment:
   If selectedRows is a prefix, is it possible to use a shallow copy of the 
tablet, in which we only change the row count and preserve other members?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionFilterSnapshot.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.event.batch;
+
+import org.apache.iotdb.db.i18n.DataNodeMiscMessages;
+import org.apache.iotdb.db.subscription.agent.SubscriptionAgent;
+import org.apache.iotdb.db.subscription.broker.SubscriptionPrefetchingQueue;
+import org.apache.iotdb.db.subscription.columnfilter.ColumnFilterMatcher;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterEvaluationException;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterMatcher;
+import org.apache.iotdb.rpc.subscription.config.TopicConfig;
+import org.apache.iotdb.rpc.subscription.config.TopicConstant;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+
+/** Immutable topic-filter state used by every event in one subscription 
batch. */
+final class SubscriptionFilterSnapshot {
+
+  private static final int MAX_CAPTURE_ATTEMPTS = 3;
+
+  private final boolean tableModel;
+  private final TopicConfig topicConfig;
+  private final Map<String, String> filteringAttributes;
+  private final TagFilterMatcher tagFilterMatcher;
+  private final ColumnFilterMatcher columnFilterMatcher;
+
+  private SubscriptionFilterSnapshot(
+      final boolean tableModel,
+      final TopicConfig topicConfig,
+      final TagFilterMatcher tagFilterMatcher,
+      final ColumnFilterMatcher columnFilterMatcher) {
+    this.tableModel = tableModel;
+    this.topicConfig = topicConfig;
+    this.filteringAttributes = filteringAttributes(topicConfig);
+    this.tagFilterMatcher = tagFilterMatcher;
+    this.columnFilterMatcher = columnFilterMatcher;
+  }
+
+  static SubscriptionFilterSnapshot capture(final SubscriptionPrefetchingQueue 
queue) {
+    final boolean tableModel =
+        SubscriptionAgent.consumer().isTableModel(queue.getConsumerGroupId());
+    for (int attempt = 0; attempt < MAX_CAPTURE_ATTEMPTS; attempt++) {
+      final TopicConfig topicConfig = getCurrentTopicConfig(queue, tableModel);
+      if (Objects.isNull(topicConfig)) {
+        throw new TagFilterEvaluationException(
+            DataNodeMiscMessages
+                
.EXCEPTION_TOPIC_CONFIGURATION_IS_NOT_AVAILABLE_FOR_TAG_FILTER_1E023E3A);
+      }
+
+      final TagFilterMatcher tagFilterMatcher =
+          SubscriptionAgent.broker().getTagFilterMatcher(queue.getTopicName(), 
tableModel);
+      tagFilterMatcher.throwIfFailure();
+      final ColumnFilterMatcher columnFilterMatcher =
+          
SubscriptionAgent.broker().getColumnFilterMatcher(queue.getTopicName(), 
tableModel);
+
+      final TopicConfig currentTopicConfig = getCurrentTopicConfig(queue, 
tableModel);

Review Comment:
   Why is there no `columnFilterMatcher .throwIfFailure`?



##########
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:
   Too many validations in this class.
   The tablet coming from upper streams should have already been validated.
   Check the necessity of the checks here and do not let unnecessary checks 
slow down and complicate the process.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/event/batch/SubscriptionFilterSnapshot.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.event.batch;
+
+import org.apache.iotdb.db.i18n.DataNodeMiscMessages;
+import org.apache.iotdb.db.subscription.agent.SubscriptionAgent;
+import org.apache.iotdb.db.subscription.broker.SubscriptionPrefetchingQueue;
+import org.apache.iotdb.db.subscription.columnfilter.ColumnFilterMatcher;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterEvaluationException;
+import org.apache.iotdb.db.subscription.tagfilter.TagFilterMatcher;
+import org.apache.iotdb.rpc.subscription.config.TopicConfig;
+import org.apache.iotdb.rpc.subscription.config.TopicConstant;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+
+/** Immutable topic-filter state used by every event in one subscription 
batch. */
+final class SubscriptionFilterSnapshot {
+
+  private static final int MAX_CAPTURE_ATTEMPTS = 3;
+
+  private final boolean tableModel;
+  private final TopicConfig topicConfig;
+  private final Map<String, String> filteringAttributes;

Review Comment:
   What are the key and value?



##########
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();

Review Comment:
   Mind the import



##########
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:
   Break on first unknown?



##########
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:
   Mind the import



-- 
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]

Reply via email to