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


##########
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:
   Changed an unavailable projector to throw `TagFilterEvaluationException` 
instead of silently returning null. The callers already check availability and 
handle the unfiltered fallback. A null projection result now only means that a 
prepared projector found no matching view data. Added a regression test for the 
unavailable case.
   
   Commit: 7caec10c275.



##########
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:
   Removed the schema/name/type reconstruction: the result now reuses the 
original schema descriptors and column categories. Only selected row data and 
null bitmaps need compaction when the selected rows contain gaps.
   
   Commit: 7caec10c275.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to