JackieTien97 commented on code in PR #18725:
URL: https://github.com/apache/iotdb/pull/18725#discussion_r4253809517


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/LTTBTableFunction.java:
##########
@@ -0,0 +1,1291 @@
+/*
+ * 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.commons.udf.builtin.relational.tvf;
+
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.i18n.CommonMessages;
+import org.apache.iotdb.udf.api.exception.UDFException;
+import org.apache.iotdb.udf.api.relational.TableFunction;
+import org.apache.iotdb.udf.api.relational.access.Record;
+import org.apache.iotdb.udf.api.relational.table.MapTableFunctionHandle;
+import org.apache.iotdb.udf.api.relational.table.TableFunctionAnalysis;
+import org.apache.iotdb.udf.api.relational.table.TableFunctionHandle;
+import 
org.apache.iotdb.udf.api.relational.table.TableFunctionProcessorProvider;
+import org.apache.iotdb.udf.api.relational.table.argument.Argument;
+import org.apache.iotdb.udf.api.relational.table.argument.DescribedSchema;
+import org.apache.iotdb.udf.api.relational.table.argument.ScalarArgument;
+import org.apache.iotdb.udf.api.relational.table.argument.TableArgument;
+import 
org.apache.iotdb.udf.api.relational.table.processor.TableFunctionDataProcessor;
+import 
org.apache.iotdb.udf.api.relational.table.specification.ParameterSpecification;
+import 
org.apache.iotdb.udf.api.relational.table.specification.ScalarParameterSpecification;
+import 
org.apache.iotdb.udf.api.relational.table.specification.TableParameterSpecification;
+import org.apache.iotdb.udf.api.type.Type;
+
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Deque;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static 
org.apache.iotdb.udf.api.relational.table.argument.ScalarArgumentChecker.POSITIVE_LONG_CHECKER;
+
+/**
+ * Largest-Triangle-Three-Buckets (LTTB) downsampling as a built-in Table 
Model table function.
+ *
+ * <p>The function shares its table-argument conventions with {@link 
M4TableFunction}: {@code DATA}
+ * has set semantics, {@code TIMECOL} names the {@code TIMESTAMP} ordering 
column, partition columns
+ * are echoed into the proper output, and every other numeric column is a 
participant column that is
+ * downsampled independently. Two mutually exclusive execution modes exist:
+ *
+ * <ul>
+ *   <li><b>Target-count mode</b> ({@code N}): the whole partition is buffered 
and the standard LTTB
+ *       algorithm reduces each participant column to exactly {@code N} points 
(or all points when
+ *       the column has no more than {@code N} eligible points). The output 
uses a fixed {@code
+ *       window_index = 0}.
+ *   <li><b>Window/bucket mode</b> ({@code SIZE}, optional {@code 
SLIDE}/{@code ORIGIN}): windows
+ *       are built exactly like M4 (time windows for duration literals, count 
windows for integers).
+ *       Each window is one LTTB bucket that selects at most one point per 
participant column: the
+ *       anchor is the previously selected point of that column, the 
candidates are the eligible
+ *       points in the window, and the third vertex is the average point of 
the next window that
+ *       carries data for that column (or the window's own last eligible point 
for the final
+ *       window).
+ * </ul>
+ *
+ * <p>{@code NULL} participant values are ignored per column; every non-null 
value, including {@code
+ * NaN} and the infinities, participates in the selection and is reproduced in 
the output. Ties in
+ * the triangle area are broken deterministically by the earliest timestamp 
(stable input order).
+ *
+ * <p>The timestamps of the input rows of one partition must be unique. As 
soon as two rows of the
+ * same partition carry the same timestamp the query fails with a semantic 
error ({@code 701}); the
+ * check runs at the partition input boundary, before per-column {@code NULL} 
filtering and before
+ * window expansion, so a row that several overlapping windows share is still 
a single input row.
+ * Equal timestamps in different partitions remain valid. Since the input is 
ordered by {@code
+ * TIMECOL}, equal timestamps are adjacent, and the check compares each row 
with the previous one
+ * and carries the last seen timestamp across input blocks.
+ */
+public class LTTBTableFunction implements TableFunction {
+
+  public static final String DATA_PARAMETER_NAME = "DATA";
+  public static final String TIMECOL_PARAMETER_NAME = "TIMECOL";
+  public static final String N_PARAMETER_NAME = "N";
+  public static final String SIZE_PARAMETER_NAME = "SIZE";
+  public static final String SLIDE_PARAMETER_NAME = "SLIDE";
+  public static final String ORIGIN_PARAMETER_NAME = "ORIGIN";
+
+  /** Internal boolean argument injected by the analyzer: {@code true} if SIZE 
is a duration. */
+  public static final String WINDOW_MODE_PARAMETER_NAME = "__LTTB_WINDOW_MODE";
+
+  public static final long UNSPECIFIED_N = Long.MIN_VALUE;
+  public static final long UNSPECIFIED_SIZE = Long.MIN_VALUE;
+  public static final long UNSPECIFIED_SLIDE = Long.MIN_VALUE;
+  public static final long MIN_TARGET_COUNT = 3L;
+
+  /** Hard cap on eligible points buffered per participant column in one 
partition or window. */
+  public static final int MAX_BUFFERED_POINTS = 1 << 22;
+
+  /** Shared budget for point buffers and selection state across all columns 
of one processor. */
+  static final long MAX_BUFFER_BYTES = 64L * 1024 * 1024;

Review Comment:
   [config] `MAX_BUFFER_BYTES` is a compile-time constant. Please expose it as 
a hot-modifiable configuration parameter (following the same pattern used by 
other per-query memory limits in IoTDB) so operators can tune it for workloads 
with large partitions or many overlapping windows without restarting the node.
   
   In addition, when a query plan includes an LTTB node, this budget should be 
added to the query's estimated execution memory. Currently 
`TableFunctionOperator.calculateMaxPeekMemory()` and 
`calculateRetainedSizeAfterCallingNext()` do not account for LTTB's internal 
buffers, so the engine cannot reclaim or back-pressure against them under 
memory pressure.



##########
iotdb-core/node-commons/src/main/i18n/en/org/apache/iotdb/commons/i18n/CommonMessages.java:
##########
@@ -342,6 +342,36 @@ private CommonMessages() {}
       
EXCEPTION_XCORR_REQUIRES_EXACTLY_TWO_CALCULATION_COLUMNS_BUT_FOUND_ARG_2FF8EB0C 
=
           "XCorr requires exactly two calculation columns, but found %d.";
   public static final String EXCEPTION_COLUMN_LACK_OF_NAME = "the column in 
table lack of the name";
+  public static final String
+      
EXCEPTION_EXACTLY_ONE_OF_THE_N_AND_SIZE_ARGUMENTS_MUST_BE_SPECIFIED_FOR_LTTB_54AF0733
 =
+          "Exactly one of the N and SIZE arguments must be specified for 
LTTB.";
+  public static final String 
EXCEPTION_THE_N_ARGUMENT_OF_LTTB_MUST_BE_AT_LEAST_3_29CE2B87 =
+      "The N argument of LTTB must be at least 3.";
+  public static final String
+      EXCEPTION_THE_N_ARGUMENT_OF_LTTB_MUST_BE_A_POSITIVE_INTEGER_21795D8E =
+          "The N argument of LTTB must be a positive integer.";
+  public static final String
+      
EXCEPTION_THE_N_ARGUMENT_OF_LTTB_CANNOT_BE_COMBINED_WITH_THE_SLIDE_OR_ORIGIN_ARGUMENTS_C2FF1FE1
 =
+          "The N argument of LTTB cannot be combined with the SLIDE or ORIGIN 
arguments.";
+  public static final String
+      
EXCEPTION_INVALID_SCALAR_ARGUMENT_SIZE_SHOULD_BE_A_POSITIVE_VALUE_3ECF76E0 =
+          "Invalid scalar argument SIZE, should be a positive value";
+  public static final String
+      
EXCEPTION_LTTB_BUFFERS_AT_MOST_ARG_ELIGIBLE_POINTS_PER_COLUMN_IN_ONE_PARTITION_OR_WINDOW_4C746D82
 =
+          "LTTB buffers at most %d eligible points per column in one partition 
or window.";
+  public static final String
+      
EXCEPTION_LTTB_DOES_NOT_SUPPORT_DUPLICATE_TIMESTAMPS_BUT_TIMESTAMP_ARG_APPEARS_MORE_THAN_ONCE_IN_THE_SAME_PARTITION_7713FEDD
 =
+          "LTTB does not support duplicate timestamps, but timestamp %s 
appears more than once in"
+              + " the same partition.";
+  public static final String 
EXCEPTION_UNSUPPORTED_LTTB_VALUE_TYPE_ARG_3E60F6FE =
+      "Unsupported LTTB value type: %s";
+  public static final String 
EXCEPTION_LTTB_COUNT_WINDOW_BOUNDARIES_EXCEED_THE_INT64_RANGE =

Review Comment:
   [i18n] This constant name is missing its hash suffix. Following the project 
naming rule (`<PREFIX>_<NORMALIZED>_<HASH>`), it should be:
   
   ```
   EXCEPTION_LTTB_COUNT_WINDOW_BOUNDARIES_EXCEED_THE_INT64_RANGE_5BC980CA
   ```
   
   Please apply the same rename to the `zh` file to keep the two files in 
parity.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/LTTBTableFunction.java:
##########
@@ -0,0 +1,1291 @@
+/*
+ * 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.commons.udf.builtin.relational.tvf;
+
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.i18n.CommonMessages;
+import org.apache.iotdb.udf.api.exception.UDFException;
+import org.apache.iotdb.udf.api.relational.TableFunction;
+import org.apache.iotdb.udf.api.relational.access.Record;
+import org.apache.iotdb.udf.api.relational.table.MapTableFunctionHandle;
+import org.apache.iotdb.udf.api.relational.table.TableFunctionAnalysis;
+import org.apache.iotdb.udf.api.relational.table.TableFunctionHandle;
+import 
org.apache.iotdb.udf.api.relational.table.TableFunctionProcessorProvider;
+import org.apache.iotdb.udf.api.relational.table.argument.Argument;
+import org.apache.iotdb.udf.api.relational.table.argument.DescribedSchema;
+import org.apache.iotdb.udf.api.relational.table.argument.ScalarArgument;
+import org.apache.iotdb.udf.api.relational.table.argument.TableArgument;
+import 
org.apache.iotdb.udf.api.relational.table.processor.TableFunctionDataProcessor;
+import 
org.apache.iotdb.udf.api.relational.table.specification.ParameterSpecification;
+import 
org.apache.iotdb.udf.api.relational.table.specification.ScalarParameterSpecification;
+import 
org.apache.iotdb.udf.api.relational.table.specification.TableParameterSpecification;
+import org.apache.iotdb.udf.api.type.Type;
+
+import org.apache.tsfile.block.column.ColumnBuilder;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Deque;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static 
org.apache.iotdb.udf.api.relational.table.argument.ScalarArgumentChecker.POSITIVE_LONG_CHECKER;
+
+/**
+ * Largest-Triangle-Three-Buckets (LTTB) downsampling as a built-in Table 
Model table function.
+ *
+ * <p>The function shares its table-argument conventions with {@link 
M4TableFunction}: {@code DATA}
+ * has set semantics, {@code TIMECOL} names the {@code TIMESTAMP} ordering 
column, partition columns
+ * are echoed into the proper output, and every other numeric column is a 
participant column that is
+ * downsampled independently. Two mutually exclusive execution modes exist:
+ *
+ * <ul>
+ *   <li><b>Target-count mode</b> ({@code N}): the whole partition is buffered 
and the standard LTTB
+ *       algorithm reduces each participant column to exactly {@code N} points 
(or all points when
+ *       the column has no more than {@code N} eligible points). The output 
uses a fixed {@code
+ *       window_index = 0}.
+ *   <li><b>Window/bucket mode</b> ({@code SIZE}, optional {@code 
SLIDE}/{@code ORIGIN}): windows
+ *       are built exactly like M4 (time windows for duration literals, count 
windows for integers).
+ *       Each window is one LTTB bucket that selects at most one point per 
participant column: the
+ *       anchor is the previously selected point of that column, the 
candidates are the eligible
+ *       points in the window, and the third vertex is the average point of 
the next window that
+ *       carries data for that column (or the window's own last eligible point 
for the final
+ *       window).
+ * </ul>
+ *
+ * <p>{@code NULL} participant values are ignored per column; every non-null 
value, including {@code
+ * NaN} and the infinities, participates in the selection and is reproduced in 
the output. Ties in
+ * the triangle area are broken deterministically by the earliest timestamp 
(stable input order).
+ *
+ * <p>The timestamps of the input rows of one partition must be unique. As 
soon as two rows of the
+ * same partition carry the same timestamp the query fails with a semantic 
error ({@code 701}); the
+ * check runs at the partition input boundary, before per-column {@code NULL} 
filtering and before
+ * window expansion, so a row that several overlapping windows share is still 
a single input row.
+ * Equal timestamps in different partitions remain valid. Since the input is 
ordered by {@code
+ * TIMECOL}, equal timestamps are adjacent, and the check compares each row 
with the previous one
+ * and carries the last seen timestamp across input blocks.
+ */
+public class LTTBTableFunction implements TableFunction {
+
+  public static final String DATA_PARAMETER_NAME = "DATA";
+  public static final String TIMECOL_PARAMETER_NAME = "TIMECOL";
+  public static final String N_PARAMETER_NAME = "N";
+  public static final String SIZE_PARAMETER_NAME = "SIZE";
+  public static final String SLIDE_PARAMETER_NAME = "SLIDE";
+  public static final String ORIGIN_PARAMETER_NAME = "ORIGIN";
+
+  /** Internal boolean argument injected by the analyzer: {@code true} if SIZE 
is a duration. */
+  public static final String WINDOW_MODE_PARAMETER_NAME = "__LTTB_WINDOW_MODE";
+
+  public static final long UNSPECIFIED_N = Long.MIN_VALUE;
+  public static final long UNSPECIFIED_SIZE = Long.MIN_VALUE;
+  public static final long UNSPECIFIED_SLIDE = Long.MIN_VALUE;
+  public static final long MIN_TARGET_COUNT = 3L;
+
+  /** Hard cap on eligible points buffered per participant column in one 
partition or window. */
+  public static final int MAX_BUFFERED_POINTS = 1 << 22;
+
+  /** Shared budget for point buffers and selection state across all columns 
of one processor. */
+  static final long MAX_BUFFER_BYTES = 64L * 1024 * 1024;
+
+  public static final String MODE_PROPERTY = "__LTTB_MODE";
+  public static final String PARTITION_TYPES_PROPERTY = 
"__LTTB_PARTITION_TYPES";
+  public static final String PARTICIPANT_TYPES_PROPERTY = 
"__LTTB_PARTICIPANT_TYPES";
+
+  private static final String OUTPUT_WINDOW_START_COLUMN = "window_start";
+  private static final String OUTPUT_WINDOW_END_COLUMN = "window_end";
+  private static final String OUTPUT_WINDOW_INDEX_COLUMN = "window_index";
+  private static final String TIME_SUFFIX = "_time";
+  private static final int INITIAL_CAPACITY = 64;
+  private static final int NOT_SELECTED = -1;
+
+  /** Execution mode persisted in the {@link MapTableFunctionHandle}. */
+  public enum Mode {
+    TARGET_COUNT,
+    TIME_WINDOW,
+    COUNT_WINDOW
+  }
+
+  @Override
+  public List<ParameterSpecification> getArgumentsSpecifications() {
+    return Arrays.asList(
+        
TableParameterSpecification.builder().name(DATA_PARAMETER_NAME).setSemantics().build(),
+        ScalarParameterSpecification.builder()
+            .name(TIMECOL_PARAMETER_NAME)
+            .type(Type.STRING)
+            .build(),
+        ScalarParameterSpecification.builder()
+            .name(N_PARAMETER_NAME)
+            .type(Type.INT64)
+            .defaultValue(UNSPECIFIED_N)
+            .build(),
+        ScalarParameterSpecification.builder()
+            .name(SIZE_PARAMETER_NAME)
+            .type(Type.INT64)
+            .defaultValue(UNSPECIFIED_SIZE)
+            .addChecker(POSITIVE_LONG_CHECKER)
+            .build(),
+        ScalarParameterSpecification.builder()
+            .name(SLIDE_PARAMETER_NAME)
+            .type(Type.INT64)
+            .defaultValue(UNSPECIFIED_SLIDE)
+            .addChecker(POSITIVE_LONG_CHECKER)
+            .build(),
+        ScalarParameterSpecification.builder()
+            .name(ORIGIN_PARAMETER_NAME)
+            .type(Type.TIMESTAMP)
+            .defaultValue(0L)
+            .build());
+  }
+
+  @Override
+  public TableFunctionAnalysis analyze(Map<String, Argument> arguments) throws 
UDFException {
+    int timeColumnIndex =
+        WindowTVFUtils.checkOrderByColumn(arguments, DATA_PARAMETER_NAME, 
TIMECOL_PARAMETER_NAME);
+    TableArgument tableArgument = (TableArgument) 
arguments.get(DATA_PARAMETER_NAME);
+    List<Integer> partitionIndexes = 
WindowTVFUtils.getPartitionIndexes(tableArgument);
+    Set<Integer> excludedIndexes = new HashSet<>(partitionIndexes);
+    excludedIndexes.add(timeColumnIndex);
+
+    long n = getLong(arguments, N_PARAMETER_NAME);
+    long size = getLong(arguments, SIZE_PARAMETER_NAME);
+    long slide = getLong(arguments, SLIDE_PARAMETER_NAME);
+    boolean nSpecified = n != UNSPECIFIED_N;
+    boolean sizeSpecified = size != UNSPECIFIED_SIZE;
+    boolean slideSpecified = slide != UNSPECIFIED_SLIDE;
+    if (nSpecified == sizeSpecified) {
+      throw new SemanticException(
+          CommonMessages
+              
.EXCEPTION_EXACTLY_ONE_OF_THE_N_AND_SIZE_ARGUMENTS_MUST_BE_SPECIFIED_FOR_LTTB_54AF0733);
+    }
+
+    Mode mode;
+    if (nSpecified) {
+      if (slideSpecified) {
+        throw new SemanticException(
+            CommonMessages
+                
.EXCEPTION_THE_N_ARGUMENT_OF_LTTB_CANNOT_BE_COMBINED_WITH_THE_SLIDE_OR_ORIGIN_ARGUMENTS_C2FF1FE1);
+      }
+      if (n < MIN_TARGET_COUNT) {
+        throw new SemanticException(
+            
CommonMessages.EXCEPTION_THE_N_ARGUMENT_OF_LTTB_MUST_BE_AT_LEAST_3_29CE2B87);
+      }
+      mode = Mode.TARGET_COUNT;
+    } else {
+      if (size <= 0) {
+        throw new UDFException(
+            CommonMessages
+                
.EXCEPTION_INVALID_SCALAR_ARGUMENT_SIZE_SHOULD_BE_A_POSITIVE_VALUE_3ECF76E0);
+      }
+      if (!slideSpecified) {
+        slide = size;
+      } else if (slide <= 0) {
+        throw new UDFException(
+            CommonMessages
+                
.EXCEPTION_INVALID_SCALAR_ARGUMENT_SLIDE_SHOULD_BE_A_POSITIVE_VALUE_F019E091);
+      }
+      boolean isTimeWindow =
+          arguments.containsKey(WINDOW_MODE_PARAMETER_NAME)
+              && (boolean) ((ScalarArgument) 
arguments.get(WINDOW_MODE_PARAMETER_NAME)).getValue();
+      mode = isTimeWindow ? Mode.TIME_WINDOW : Mode.COUNT_WINDOW;
+    }
+
+    DescribedSchema.Builder schemaBuilder = new DescribedSchema.Builder();
+    if (mode == Mode.TIME_WINDOW) {
+      schemaBuilder
+          .addField(OUTPUT_WINDOW_START_COLUMN, Type.TIMESTAMP)
+          .addField(OUTPUT_WINDOW_END_COLUMN, Type.TIMESTAMP);
+    } else {
+      schemaBuilder.addField(OUTPUT_WINDOW_INDEX_COLUMN, Type.INT64);
+    }
+
+    List<Type> partitionTypes = new ArrayList<>();
+    for (int partitionIndex : partitionIndexes) {
+      Type type = tableArgument.getFieldTypes().get(partitionIndex);
+      partitionTypes.add(type);
+      
schemaBuilder.addField(tableArgument.getFieldNames().get(partitionIndex).get(), 
type);
+    }
+
+    // Rejects every non-numeric participant column with the shared "not 
allowed columns" message.
+    List<Integer> participantIndexes =
+        WindowTVFUtils.getCalculationIndexes(tableArgument, excludedIndexes, 
null);
+    if (participantIndexes.isEmpty()) {
+      throw new 
SemanticException(CommonMessages.EXCEPTION_NO_CALCULATE_COLUMNS);
+    }
+    List<Type> participantTypes = new ArrayList<>();
+    for (int participantIndex : participantIndexes) {
+      Type type = tableArgument.getFieldTypes().get(participantIndex);
+      String columnName = 
tableArgument.getFieldNames().get(participantIndex).get();
+      participantTypes.add(type);
+      schemaBuilder.addField(columnName + TIME_SUFFIX, Type.TIMESTAMP);
+      schemaBuilder.addField(columnName, type);
+    }
+
+    MapTableFunctionHandle.Builder handleBuilder =
+        new MapTableFunctionHandle.Builder()
+            .addProperty(MODE_PROPERTY, mode.name())
+            .addProperty(PARTITION_TYPES_PROPERTY, 
WindowTVFUtils.joinTypes(partitionTypes))
+            .addProperty(PARTICIPANT_TYPES_PROPERTY, 
WindowTVFUtils.joinTypes(participantTypes));
+    if (mode == Mode.TARGET_COUNT) {
+      handleBuilder.addProperty(N_PARAMETER_NAME, n);
+    } else {
+      handleBuilder.addProperty(SIZE_PARAMETER_NAME, 
size).addProperty(SLIDE_PARAMETER_NAME, slide);
+      if (mode == Mode.TIME_WINDOW) {
+        handleBuilder.addProperty(ORIGIN_PARAMETER_NAME, getLong(arguments, 
ORIGIN_PARAMETER_NAME));
+      }
+    }
+
+    List<Integer> requiredColumns = new ArrayList<>();
+    requiredColumns.add(timeColumnIndex);
+    requiredColumns.addAll(partitionIndexes);
+    requiredColumns.addAll(participantIndexes);
+
+    return TableFunctionAnalysis.builder()
+        .properColumnSchema(schemaBuilder.build())
+        .requireRecordSnapshot(false)
+        .requiredColumns(DATA_PARAMETER_NAME, requiredColumns)
+        .handle(handleBuilder.build())
+        .build();
+  }
+
+  private static long getLong(Map<String, Argument> arguments, String name) {
+    return (long) ((ScalarArgument) arguments.get(name)).getValue();
+  }
+
+  @Override
+  public TableFunctionHandle createTableFunctionHandle() {
+    return new MapTableFunctionHandle();
+  }
+
+  @Override
+  public TableFunctionProcessorProvider getProcessorProvider(
+      TableFunctionHandle tableFunctionHandle) {
+    MapTableFunctionHandle handle = (MapTableFunctionHandle) 
tableFunctionHandle;
+    Mode mode = Mode.valueOf((String) handle.getProperty(MODE_PROPERTY));
+    Type[] partitionTypes =
+        WindowTVFUtils.parseTypes((String) 
handle.getProperty(PARTITION_TYPES_PROPERTY));
+    Type[] participantTypes =
+        WindowTVFUtils.parseTypes((String) 
handle.getProperty(PARTICIPANT_TYPES_PROPERTY));
+
+    return new TableFunctionProcessorProvider() {
+      @Override
+      public TableFunctionDataProcessor getDataProcessor() {
+        ParticipantColumn[] participantColumns =
+            createParticipantColumns(participantTypes, partitionTypes.length + 
1);
+        switch (mode) {
+          case TARGET_COUNT:
+            return new TargetCountLTTBDataProcessor(
+                (long) handle.getProperty(N_PARAMETER_NAME), partitionTypes, 
participantColumns);
+          case TIME_WINDOW:
+            return new TimeWindowLTTBDataProcessor(
+                (long) handle.getProperty(SIZE_PARAMETER_NAME),
+                (long) handle.getProperty(SLIDE_PARAMETER_NAME),
+                (long) handle.getProperty(ORIGIN_PARAMETER_NAME),
+                partitionTypes,
+                participantColumns);
+          case COUNT_WINDOW:
+            return new CountWindowLTTBDataProcessor(
+                (long) handle.getProperty(SIZE_PARAMETER_NAME),
+                (long) handle.getProperty(SLIDE_PARAMETER_NAME),
+                partitionTypes,
+                participantColumns);
+          default:
+            throw new IllegalStateException(String.valueOf(mode));
+        }
+      }
+    };
+  }
+
+  private static ParticipantColumn[] createParticipantColumns(Type[] types, 
int firstInputIndex) {
+    ParticipantColumn[] columns = new ParticipantColumn[types.length];
+    for (int i = 0; i < types.length; i++) {
+      columns[i] = new ParticipantColumn(firstInputIndex + i, types[i]);
+    }
+    return columns;
+  }
+
+  // 
---------------------------------------------------------------------------------------------
+  // LTTB core
+  // 
---------------------------------------------------------------------------------------------
+
+  /**
+   * Standard LTTB selection over an ordered point sequence.
+   *
+   * <p>When {@code count <= n} every index is returned. Otherwise the first 
and last points are
+   * retained and the {@code count - 2} intermediate points are divided into 
{@code n - 2} buckets;
+   * bucket {@code i} covers {@code [floor(i*(count-2)/(n-2))+1, 
floor((i+1)*(count-2)/(n-2))+1)}.
+   * For every bucket the candidate forming the largest triangle with the 
previously selected point
+   * and the average point of the next bucket wins (the last bucket uses the 
final point). Ties are
+   * broken by the earliest timestamp.
+   *
+   * @return ascending indexes of the selected points
+   */
+  static int[] selectTargetCount(long[] times, double[] values, int count, 
long n) {
+    return selectTargetCount(times, values, null, count, n);
+  }
+
+  private static int[] selectTargetCount(
+      long[] times, double[] values, long[] integralValues, int count, long n) 
{
+    if (count <= n) {
+      int[] all = new int[count];
+      for (int i = 0; i < count; i++) {
+        all[i] = i;
+      }
+      return all;
+    }
+
+    int[] selected = new int[(int) n];
+    int selectedCount = 0;
+    selected[selectedCount++] = 0;
+    long bucketCount = n - 2;
+    long intermediate = count - 2L;
+    int anchor = 0;
+    for (long bucket = 0; bucket < bucketCount; bucket++) {
+      int currentStart = (int) (bucket * intermediate / bucketCount) + 1;
+      int currentEnd = (int) ((bucket + 1) * intermediate / bucketCount) + 1;
+
+      long anchorTime = times[anchor];
+      double anchorValue = values[anchor];
+      long integralAnchor = integralValues == null ? 0 : 
integralValues[anchor];
+      double avgTime;
+      double avgValue;
+      int nextEnd =
+          bucket == bucketCount - 1 ? count : (int) ((bucket + 2) * 
intermediate / bucketCount) + 1;
+      if (bucket == bucketCount - 1) {
+        avgTime = differenceAsDouble(times[count - 1], anchorTime);
+        avgValue = relativeValue(values, integralValues, count - 1, 
anchorValue, integralAnchor);
+      } else {
+        double sumTime = 0;
+        double sumValue = 0;
+        for (int i = currentEnd; i < nextEnd; i++) {
+          sumTime += differenceAsDouble(times[i], anchorTime);
+          sumValue += relativeValue(values, integralValues, i, anchorValue, 
integralAnchor);
+        }
+        int nextSize = nextEnd - currentEnd;
+        avgTime = sumTime / nextSize;
+        avgValue = sumValue / nextSize;
+      }
+
+      int pick =
+          argMaxArea(
+              times,
+              values,
+              integralValues,
+              currentStart,
+              currentEnd,
+              anchorTime,
+              anchorValue,
+              integralAnchor,
+              avgTime,
+              avgValue);
+      if (pick == NOT_SELECTED) {
+        pick =
+            argMaxAreaExactly(
+                times,
+                values,
+                integralValues,
+                currentStart,
+                currentEnd,
+                anchorTime,
+                anchorValue,
+                integralAnchor,
+                times,
+                values,
+                integralValues,
+                currentEnd,
+                nextEnd);
+      }
+      selected[selectedCount++] = pick;
+      anchor = pick;
+    }
+    selected[selectedCount] = count - 1;
+    return selected;
+  }
+
+  /**
+   * Returns the index in {@code [start, end)} maximizing the triangle area; 
earliest wins ties.
+   * {@code nextTime} and {@code nextValue} are relative to the anchor. 
Integral differences are
+   * computed before converting to double so large epochs and INT64 baselines 
retain small changes.
+   * Returns {@link #NOT_SELECTED} if intermediate arithmetic overflows, so 
the caller can compare
+   * the entire bucket using its original points and exact arithmetic.
+   */
+  private static int argMaxArea(
+      long[] times,
+      double[] values,
+      long[] integralValues,
+      int start,
+      int end,
+      long anchorTime,
+      double anchorValue,
+      long integralAnchor,
+      double nextTime,
+      double nextValue) {
+    int pick = start;
+    double maxArea = -1;
+    for (int i = start; i < end; i++) {
+      double area =
+          Math.abs(
+              nextTime * relativeValue(values, integralValues, i, anchorValue, 
integralAnchor)
+                  - differenceAsDouble(times[i], anchorTime) * nextValue);
+      if (!Double.isFinite(area)) {
+        return NOT_SELECTED;
+      }
+      if (area > maxArea) {
+        maxArea = area;
+        pick = i;
+      }
+    }
+    return pick;
+  }
+
+  /**
+   * Overflow fallback for finite input values. Recomputes every candidate so 
an overflowing area
+   * cannot tie with or hide a larger one. Multiplying all areas by the 
lookahead point count lets
+   * us use sums instead of dividing by that count, keeping the comparison 
exact even for recurring
+   * averages. The normal path does not allocate decimal values.
+   */
+  private static int argMaxAreaExactly(
+      long[] times,
+      double[] values,
+      long[] integralValues,
+      int start,
+      int end,
+      long anchorTime,
+      double anchorValue,
+      long integralAnchor,
+      long[] nextTimes,
+      double[] nextValues,
+      long[] nextIntegralValues,
+      int nextStart,
+      int nextEnd) {
+    BigDecimal timeOrigin = BigDecimal.valueOf(anchorTime);
+    BigDecimal valueOrigin =
+        integralValues == null ? new BigDecimal(anchorValue) : 
BigDecimal.valueOf(integralAnchor);
+    BigDecimal sumTime = BigDecimal.ZERO;
+    BigDecimal sumValue = BigDecimal.ZERO;
+    for (int i = nextStart; i < nextEnd; i++) {
+      sumTime = 
sumTime.add(BigDecimal.valueOf(nextTimes[i]).subtract(timeOrigin));
+      sumValue = sumValue.add(exactValue(nextValues, nextIntegralValues, 
i).subtract(valueOrigin));
+    }
+    int pick = start;
+    BigDecimal maxArea = null;
+    for (int i = start; i < end; i++) {
+      BigDecimal time = BigDecimal.valueOf(times[i]).subtract(timeOrigin);
+      BigDecimal value = exactValue(values, integralValues, 
i).subtract(valueOrigin);
+      BigDecimal area = 
sumTime.multiply(value).subtract(time.multiply(sumValue)).abs();
+      if (maxArea == null || area.compareTo(maxArea) > 0) {
+        maxArea = area;
+        pick = i;
+      }
+    }
+    return pick;
+  }
+
+  private static BigDecimal exactValue(double[] values, long[] integralValues, 
int index) {
+    // The constructor preserves the actual binary floating-point value, 
including tiny deltas.
+    return integralValues == null
+        ? new BigDecimal(values[index])

Review Comment:
   [P2 follow-up] `new BigDecimal(values[index])` throws 
`NumberFormatException` when the value is `NaN` or `±Infinity`.
   
   The fast path (`argMaxArea`, around line 453) returns `NOT_SELECTED` when 
the computed area is non-finite, which causes the caller to invoke 
`argMaxAreaExactly` as a fallback. The Javadoc for that method still says 
**"Overflow fallback for finite input values"**, and `new 
BigDecimal(Double.NaN)` throws `NumberFormatException` unconditionally — it 
does not produce a numeric result.
   
   The existing integration test only covers the short-series path (`M <= N`) 
where all points are returned as-is and no area is computed. A series longer 
than `N` where any bucket contains a `NaN` or `Infinity` participant will crash 
with an unchecked `NumberFormatException` at this line.
   
   Options:
   1. Guard `exactValue`: check `Double.isFinite(values[index])` and treat 
non-finite candidates as zero area (i.e. always pick the first point in the 
bucket when all candidates are non-finite), or skip them.
   2. Reject non-finite participant values at the point they enter the buffer 
(in `collect()`) with a clear 701 error.
   
   Either way, please add a regression test for the case where `N < M` and at 
least one candidate in a bucket is `NaN` or `Infinity`.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java:
##########
@@ -2899,7 +2899,10 @@ private boolean 
canSplitTableFunctionProcessor(TableFunctionProcessorNode node)
     if (!isPartitionedGroup(node.getChild())) {
       return false;
     }
-    return 
!TableBuiltinTableFunction.FFT.getFunctionName().equalsIgnoreCase(node.getName());
+    // LTTB is not mergeable: fragment-local samples cannot be combined into a 
globally correct
+    // result, so the whole partition must be gathered and ordered before the 
function runs.
+    return 
!TableBuiltinTableFunction.FFT.getFunctionName().equalsIgnoreCase(node.getName())
+        && 
!TableBuiltinTableFunction.LTTB.getFunctionName().equalsIgnoreCase(node.getName());

Review Comment:
   [planner] Please remove the LTTB name from this exclusion list.
   
   `ParallelizeGrouping` + `GroupNode` already guarantees that, when `PARTITION 
BY` covers the device-level partition keys, each fragment receives complete 
partitions before any set-semantic TVF runs. That is the only property LTTB 
needs — it is identical to what M4 relies on to remain splittable without any 
exclusion here.
   
   Excluding LTTB by name serialises all partitions through the coordinator 
unnecessarily and will degrade performance on large tables with many partitions.
   
   More broadly, the non-mergeability property should live in the planner 
rather than in a growing name-based list. Consider expressing it as an 
attribute on the TVF registration or on `TableFunctionProcessorNode` so future 
non-mergeable TVFs do not require a new entry here.



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