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


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java:
##########
@@ -2899,6 +2899,10 @@ private boolean 
canSplitTableFunctionProcessor(TableFunctionProcessorNode node)
     if (!isPartitionedGroup(node.getChild())) {
       return false;
     }
+    // LTTB needs whole partitions, which the partitioned GroupNode above this 
node already
+    // guarantees: every fragment receives complete partitions and the 
ordering is preserved, so a
+    // fragment-local sample is a valid standard LTTB result for that 
partition and no exclusion is
+    // needed. FFT is excluded because its transform over the whole partition 
is not splittable.

Review Comment:
   [planner] Please restore the LTTB exclusion and merge-sort the input when 
the function is not split
   
   My earlier comment (r4253809521) was wrong. A partitioned `GroupNode` groups 
and orders rows *within each branch*. It does not make a partition *complete* 
within one branch. `ParallelizeGrouping` keeps the `GroupNode` whenever its 
partition keys are a prefix of the child's keys (`checkPrefixMatch`), and 
`canSplitTableFunctionProcessor` only checks for a partitioned `GroupNode`. The 
comment added here states the same wrong guarantee.
   
   **The split gives wrong results at 213393e.** I ran an end-to-end check on 
1C1D with four data regions (`CUSTOM` policy, 4 data region groups per 
database). The setup had two partitions, `tag3 = 'a'` and `'b'`, with 8 devices 
each and interleaved timestamps, and used `N => 4`. Results were compared with 
a reference LTTB.
   
   ```sql
   SELECT * FROM LTTB(DATA => (SELECT time, tag3, s1, row_number() OVER 
(PARTITION BY tag3, dev ORDER BY time) AS rn FROM t)
                      PARTITION BY tag3 ORDER BY time, TIMECOL => 'time', N => 
4)
   ```
   
   - One LTTB runs per region, so every partition returns 16 rows instead of 4.
   - A `SESSION` input partitioned by `(tag3, dev)` gives the same result.
   - When two devices of one partition share timestamps, no 701 is raised.
   - A direct scan with `PARTITION BY tag3` is correct.
   
   **Restoring the exclusion alone is not enough.**
   
   - The non-split branch of `visitTableFunctionProcessor` (lines 2886–2888) 
merges the children using the ordering that `nodeOrderingMap` holds for the 
first child.
   - `visitGroup` does not register the per-branch `GroupNode`s it creates 
(line 519). The lookup therefore returns null, and the children are merged by a 
`CollectNode` instead of a `MergeSort`.
   - `PartitionRecognizer.findNextDifferentRowIndex` assumes grouped input: a 
block whose last row has the current partition key is taken whole into the 
current partition.
   
   With the exclusion restored, the query above runs a single LTTB, but over 
mixed partitions. I checked this with `N => 1000`, which returns every row a 
partition receives:
   
   - the LTTB for partition `a` received 8 of its 32 rows;
   - the LTTB for `b` received 56 rows, 24 of them from partition `a`, and its 
timestamps went backwards 6 times;
   - shared timestamps still raise no 701.
   
   `FFT`, which is already excluded, is planned the same way.
   
   **Please, in this PR:**
   
   1. Restore the exclusion, and replace the comment at lines 2902–2905.
   2. Merge-sort the children in the non-split branch using the `GroupNode`'s 
ordering. This is valid because every child is either a per-branch `GroupNode` 
or a node whose ordering starts with that ordering:
   
      ```java
      } else if (!canSplitPushDown) {
        OrderingScheme childOrdering =
            node.getChild() instanceof GroupNode
                ? ((GroupNode) node.getChild()).getOrderingScheme()
                : nodeOrderingMap.get(childrenNodes.get(0).getPlanNodeId());
        node.setChild(mergeChildrenViaCollectOrMergeSort(childOrdering, 
childrenNodes));
      ```
   
      With both changes, these inputs are planned as `TableFunctionProcessor -> 
MergeSort`. On 1C1D and 1C3D, the direct scan, the window-function input and 
the `SESSION` input then all match the reference, and shared timestamps raise 
701. All 24 tests in `TableFunctionTest` pass.
   3. Add an IT for this setup:
      - call `setDataRegionGroupExtensionPolicy("CUSTOM")` and 
`setDefaultDataRegionGroupNumPerDatabase(4)` before `initClusterEnvironment()`;
      - put several devices in each partition;
      - compare the results for the window-function and `SESSION` inputs with 
the direct-scan result;
      - add a partition whose devices share timestamps; it must fail with 701.
   
   The split rule itself is a separate planner issue. It also affects M4: over 
the window-function input above, M4 with a single count window returns 15 rows 
per partition instead of at most 4, with or without these changes. 
`visitWindowFunction` and `visitPatternRecognition` use the same `GroupNode` 
test. We'll fix the split rule in a separate PR, and the LTTB exclusion can be 
removed after that.



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/TableFunctionTest.java:
##########
@@ -842,4 +843,161 @@ public void testM4RejectsDescendingOrderBy() {
           e.getMessage());
     }
   }
+
+  // Aliases use the planner's actual symbol names so the same matcher also 
resolves them through
+  // the ExchangeNodes of the distributed plan (which bind symbols to their 
own names).
+  private static final ImmutableMap<String, String> TABLE1_COLUMNS =
+      ImmutableMap.<String, String>builder()
+          .put("time", "time")
+          .put("tag1_0", "tag1")
+          .put("tag2_1", "tag2")
+          .put("tag3_2", "tag3")
+          .put("attr1_3", "attr1")
+          .put("attr2_4", "attr2")
+          .put("s1_5", "s1")
+          .put("s2_6", "s2")
+          .put("s3_7", "s3")
+          .buildOrThrow();
+
+  private static final String LTTB_DATA_ARGUMENT =
+      "DATA => table1 PARTITION BY (tag1, tag2, tag3, attr1, attr2) ORDER BY 
time";
+
+  private static MapTableFunctionHandle.Builder lttbHandle(String mode) {
+    return new MapTableFunctionHandle.Builder()
+        .addProperty(LTTBTableFunction.MODE_PROPERTY, mode)
+        .addProperty(
+            LTTBTableFunction.PARTITION_TYPES_PROPERTY, 
"STRING,STRING,STRING,STRING,STRING")
+        .addProperty(LTTBTableFunction.PARTICIPANT_TYPES_PROPERTY, 
"INT64,INT64,DOUBLE");
+  }
+
+  private static TableFunctionProcessorMatcher.Builder lttbMatcher(
+      TableFunctionProcessorMatcher.Builder builder, String... windowColumns) {
+    ImmutableList.Builder<String> properOutputs = ImmutableList.builder();
+    properOutputs.add(windowColumns);
+    properOutputs.add(
+        "lttb_tag1",
+        "lttb_tag2",
+        "lttb_tag3",
+        "lttb_attr1",
+        "lttb_attr2",
+        "lttb_s1_time",
+        "lttb_s1",
+        "lttb_s2_time",
+        "lttb_s2",
+        "lttb_s3_time",
+        "lttb_s3");
+    return builder
+        .name("lttb")
+        .properOutputs(properOutputs.build().toArray(new String[0]))
+        .requiredSymbols(
+            "time", "tag1_0", "tag2_1", "tag3_2", "attr1_3", "attr2_4", 
"s1_5", "s2_6", "s3_7");
+  }
+
+  @Test
+  public void testLTTBTargetCountMode() {
+    PlanTester planTester = new PlanTester();
+    String sql = "SELECT * FROM LTTB(" + LTTB_DATA_ARGUMENT + ", TIMECOL => 
'time', N => 100)";
+    LogicalQueryPlan logicalQueryPlan = planTester.createPlan(sql);
+    PlanMatchPattern tableScan = tableScan("testdb.table1", TABLE1_COLUMNS);
+
+    Consumer<TableFunctionProcessorMatcher.Builder> tableFunctionMatcher =
+        builder ->
+            lttbMatcher(builder, "window_index")
+                .handle(
+                    lttbHandle("TARGET_COUNT")
+                        .addProperty(LTTBTableFunction.N_PARAMETER_NAME, 100L)
+                        .build());
+
+    // PARTITION BY several columns plans a GroupNode (a SortNode subclass) 
below the function
+    assertPlan(
+        logicalQueryPlan, anyTree(tableFunctionProcessor(tableFunctionMatcher, 
group(tableScan))));
+    // LTTB is not mergeable: the whole partition is gathered and ordered 
before the function runs
+    assertPlan(
+        planTester.getFragmentPlan(0),
+        output(
+            tableFunctionProcessor(
+                tableFunctionMatcher, mergeSort(exchange(), exchange(), 
exchange()))));

Review Comment:
   [test] This assertion fails at 213393e
   
   ```
   TableFunctionTest.testLTTBTargetCountMode:915 Plan does not match
   ```
   
   5cdb336 removed the LTTB exclusion. Since then, fragment 0 is `Output -> 
Collect` over the exchanges, with one LTTB per fragment. Once the exclusion is 
restored (see the planner comment), the plan is `Output -> 
TableFunctionProcessor -> MergeSort(Exchange x3)` again, and this test passes 
unchanged.
   
   Please also add a planner test for the window-function input from the 
planner comment. Its fragment 0 should be `Output -> TableFunctionProcessor -> 
MergeSort` over the exchanges. Without the merge fix, the merge is a `Collect`.



##########
iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/TableFunctionProcessorProvider.java:
##########
@@ -43,4 +43,16 @@ default TableFunctionLeafProcessor getSplitProcessor() {
     throw new UnsupportedOperationException(
         UdfApiMessages.TABLE_FUNCTION_DOES_NOT_PROCESS_LEAF_DATA);
   }
+
+  /**
+   * The bytes the processors of this provider may hold in their own buffers 
while a partition is
+   * being processed, above what {@code process} returns. The engine adds this 
to the estimated
+   * memory of the {@linkplain TableFunctionDataProcessor} operator, so a 
table function that
+   * accumulates input (rather than emitting a result per record) is visible 
to memory management
+   * and can be back-pressured. The default is {@code 0} for table functions 
that keep no state
+   * across calls.
+   */
+  default long getEstimatedMaxBufferedBytes() {
+    return 0L;
+  }

Review Comment:
   [api] Please don't add this to the public UDF API
   
   `TableFunctionProcessorProvider` is part of `udf-api`, which user-defined 
table functions implement. A new method here is a public API change that we 
would have to support for every UDF from now on, and it exposes engine memory 
accounting to user code. Please revert this file.
   
   The engine can get the value internally instead. 
`TableOperatorGenerator.visitTableFunctionProcessor` (calc-commons, lines 
1756–1758) already resolves the `TableFunction` and calls 
`getProcessorProvider(handle)`. From there it can:
   
   1. get the budget from the built-in LTTB provider, for example through an 
internal interface in node-commons or an `instanceof` check (user-defined 
functions get 0);
   2. pass that value to the `TableFunctionOperator` constructor.
   
   The value it passes should be the same one the processors enforce; see the 
`TableFunctionOperator` comment.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/LTTBTableFunction.java:
##########
@@ -0,0 +1,1387 @@
+/*
+ * 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.conf.CommonDescriptor;
+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).
+ *       {@code SIZE} defines a bucket, not the input range of a separate 
standard LTTB invocation.
+ *       One anchor chain is maintained per partition and participant column: 
for the current bucket
+ *       {@code A} is the point selected by the preceding bucket of that 
column ({@code B} in turn
+ *       becomes the anchor of the following bucket), {@code B} ranges over 
the current bucket's
+ *       eligible points, and {@code C} is the average point of the next 
bucket that carries data
+ *       for that column (or the bucket's own last eligible point when there 
is none). Overlapping
+ *       buckets ({@code SLIDE < SIZE}) do not restart the chain, and the 
state is independent
+ *       between partitions and between participant columns.
+ * </ul>
+ *
+ * <p>Bucket mode emits one row per window that carries input, and at most one 
selected point per
+ * participant column in a row; a column with no eligible point in a window 
emits {@code NULL} in
+ * its place, and a window in which every participant column is {@code NULL} 
still emits a single
+ * all-{@code NULL} row (padding by output position). Bucket mode does 
<em>not</em> preserve the
+ * first and last eligible points: an endpoint used as {@code A} or as {@code 
C} is a triangle
+ * vertex, not a selected point, so it appears in the output only when it also 
wins its bucket. A
+ * bucket holding just one or two eligible points therefore still contributes 
a single row per
+ * column: with two points the areas are equal and the tie resolves to the 
earlier one. First/last
+ * preservation and the return-all-points case apply to target-count mode only.
+ *
+ * <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. It is
+   * the default of {@code lttb_buffer_size_in_bytes}; operators can tune the 
live value with that
+   * hot-reloadable configuration parameter for workloads with large 
partitions or many overlapping
+   * windows.
+   */
+  public static final long DEFAULT_MAX_BUFFER_BYTES = 64L * 1024 * 1024;

Review Comment:
   [docs] The defaults and comments of `lttb_buffer_size_in_bytes` don't match 
the code
   
   **The 64 MiB default.** This Javadoc calls 64 MiB "the default of 
`lttb_buffer_size_in_bytes`", but the effective default is `min(32 MiB, 
operator memory / query_thread_count / 2)`. 
`IoTDBDescriptor.loadFixedSizeLimitForQuery` sets that value, maps values ≤ 0 
to it, and `iotdb-system.properties.template` documents it. 64 MiB only shows 
up in two places:
   
   - the initial value in `CommonConfig` (line 573). By contrast, 
`sortBufferSize`, which the template says this parameter matches, starts at 32 
MiB;
   - the ≤ 0 fallback in `maxBufferBytes()`. A DataNode never reaches it after 
loading its properties.
   
   Please keep a single default, and drop or align the extra constant. 
`LTTBRegressionTest` and `LTTBWindowMemoryTest` reference the constant too.
   
   **The `maxBufferBytes()` Javadoc (lines 151–157).** It says "Processors read 
this once at construction, so a reload applies to queries that start 
afterwards". That isn't what happens. A processor is constructed per partition, 
so a reload reaches the next partition of a query that is already running; see 
the `TableFunctionOperator` comment. Please describe the actual rule once the 
value is resolved per provider.
   
   **`TableFunctionOperator`.** The comments at lines 298 and 312 should 
describe the new mechanism.
   
   **Optional: the error message.** "LTTB buffers exceed the per-processor 
memory limit of %d bytes." could name `lttb_buffer_size_in_bytes`, so users 
know what to tune. That needs a new constant name and hash in both `en` and 
`zh`.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/LTTBTableFunction.java:
##########
@@ -0,0 +1,1387 @@
+/*
+ * 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.conf.CommonDescriptor;
+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).
+ *       {@code SIZE} defines a bucket, not the input range of a separate 
standard LTTB invocation.
+ *       One anchor chain is maintained per partition and participant column: 
for the current bucket
+ *       {@code A} is the point selected by the preceding bucket of that 
column ({@code B} in turn
+ *       becomes the anchor of the following bucket), {@code B} ranges over 
the current bucket's
+ *       eligible points, and {@code C} is the average point of the next 
bucket that carries data
+ *       for that column (or the bucket's own last eligible point when there 
is none). Overlapping
+ *       buckets ({@code SLIDE < SIZE}) do not restart the chain, and the 
state is independent
+ *       between partitions and between participant columns.
+ * </ul>
+ *
+ * <p>Bucket mode emits one row per window that carries input, and at most one 
selected point per
+ * participant column in a row; a column with no eligible point in a window 
emits {@code NULL} in
+ * its place, and a window in which every participant column is {@code NULL} 
still emits a single
+ * all-{@code NULL} row (padding by output position). Bucket mode does 
<em>not</em> preserve the
+ * first and last eligible points: an endpoint used as {@code A} or as {@code 
C} is a triangle
+ * vertex, not a selected point, so it appears in the output only when it also 
wins its bucket. A
+ * bucket holding just one or two eligible points therefore still contributes 
a single row per
+ * column: with two points the areas are equal and the tie resolves to the 
earlier one. First/last
+ * preservation and the return-all-points case apply to target-count mode only.
+ *
+ * <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. It is
+   * the default of {@code lttb_buffer_size_in_bytes}; operators can tune the 
live value with that
+   * hot-reloadable configuration parameter for workloads with large 
partitions or many overlapping
+   * windows.
+   */
+  public static final long DEFAULT_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
+  }
+
+  /**
+   * The live per-processor buffer budget, read from the hot-reloadable {@code
+   * lttb_buffer_size_in_bytes} configuration parameter. Values that are not 
positive fall back to
+   * {@link #DEFAULT_MAX_BUFFER_BYTES} so a misconfigured node still bounds 
memory instead of
+   * buffering without limit. Processors read this once at construction, so a 
reload applies to
+   * queries that start afterwards.
+   */
+  public static long maxBufferBytes() {
+    long configured = 
CommonDescriptor.getInstance().getConfig().getLttbBufferSize();
+    return configured > 0 ? configured : DEFAULT_MAX_BUFFER_BYTES;
+  }
+
+  @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));
+        }
+      }
+
+      /**
+       * Every LTTB processor buffers the points of the partition (or of the 
windows it keeps open)
+       * until it can select the output points, so the whole per-processor 
budget is reportable as
+       * memory the operator holds while the partition is processed.
+       */
+      @Override
+      public long getEstimatedMaxBufferedBytes() {
+        return maxBufferBytes();
+      }
+    };
+  }
+
+  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;
+  }
+
+  /**
+   * Area of the triangle formed by the candidate {@code index}, the anchor 
and the lookahead point,
+   * or {@code -1} when no finite area exists. Areas are non-negative, so 
{@code -1} unambiguously
+   * marks a candidate that cannot be scored.
+   *
+   * <p>A {@code NaN} or infinite participant value yields a non-finite 
(undefined) area rather than
+   * an overflow: no arithmetic can make it finite. Such a candidate never 
beats a finite one, so a
+   * bucket whose candidates are all non-finite keeps its earliest point.
+   */
+  private static double boundedArea(
+      long[] times,
+      double[] values,
+      long[] integralValues,
+      int index,
+      long anchorTime,
+      double anchorValue,
+      long integralAnchor,
+      double nextTime,
+      double nextValue) {
+    double area =
+        Math.abs(
+            nextTime * relativeValue(values, integralValues, index, 
anchorValue, integralAnchor)
+                - differenceAsDouble(times[index], anchorTime) * nextValue);
+    return Double.isFinite(area) ? area : -1;
+  }
+
+  /** Whether the participant value at {@code index} is representable as an 
exact decimal. */
+  private static boolean hasExactValue(double[] values, long[] integralValues, 
int index) {
+    return integralValues != null || Double.isFinite(values[index]);
+  }
+
+  private static int argMaxArea(
+      long[] times,
+      double[] values,
+      long[] integralValues,
+      int start,
+      int end,
+      long anchorTime,
+      double anchorValue,
+      long integralAnchor,
+      double nextTime,
+      double nextValue) {
+    // The exact fallback rebuilds the lookahead sum from the original points, 
so a non-finite
+    // average over representable points is still recoverable; only the anchor 
origin has to be
+    // representable, because it is converted to BigDecimal directly. A 
non-finite anchor value can
+    // only arise from non-finite input, which no arithmetic can repair.
+    boolean exactIsPossible = Double.isFinite(anchorValue);

Review Comment:
   [non-finite] The selection degenerates when a triangle vertex is non-finite
   
   Non-finite values are now eligible points (lines 93–95), so they can become 
the anchor `A` or enter the lookahead `C`. When either is non-finite, every 
area in the bucket is non-finite and `boundedArea` maps each one to −1. 
`exactIsPossible` is false, or the exact path has no finite lookahead point, so 
`pick` stays at `start`. The bucket then returns its earliest point regardless 
of the data.
   
   The examples below use one DOUBLE column with t = 0..5, at 213393e. The last 
column shows the result when the non-finite values are replaced by finite ones.
   
   | input | args | result | with finite values instead |
   |---|---|---|---|
   | `NaN, 1, 2, 9, 0, 0` | `N => 3` | t0:NaN, **t1:1**, t5:0 | (t0 = 0) t0:0, 
**t3:9**, t5:0 |
   | `NaN, 1, 9, 2, 0, 0` | `SIZE => 3` | 0\|**t0:NaN**, 1\|t3:2 | (t0 = 0) 
0\|**t2:9**, 1\|t3:2 |
   | `0, NaN, NaN, 1, 9, 0` | `N => 4` | t0:0, t1:NaN, **t3:1**, t5:0 | (5, 5) 
t0:0, t1:5, **t4:9**, t5:0 |
   | `0, 1, 9, NaN, NaN, 0` | `N => 4` | t0:0, **t1:1**, t3:NaN, t5:0 | (5, 5) 
t0:0, **t2:9**, t4:5, t5:0 |
   
   These rows show three separate cases:
   
   1. **A non-finite first point as `A`** (rows 1–2). In row 1 the peak t3:9 is 
lost. In row 2, window 0 returns the NaN anchor itself; a time window behaves 
the same. `±Infinity` behaves like NaN: `Infinity, 1, 2, 9, 0, 0` with `N => 3` 
gives t0:Infinity, t1:1, t5:0.
   2. **A non-finite selected point as the next `A`** (row 3). An all-NaN 
bucket can only return a NaN point. That point becomes the anchor of the next 
bucket, which then returns t3:1 instead of the peak t4:9.
   3. **No finite point in the next bucket, so `C` is undefined** (row 4). The 
current bucket returns t1:1 instead of t2:9.
   
   The comment at line 514 treats this as unavoidable ("no arithmetic can 
repair"). However, the geometry can avoid non-finite vertices without changing 
which points are eligible for output. For example:
   
   - `A` is the most recent finite selected point, or the first finite eligible 
point;
   - `C` is the average of the finite points of the next bucket that has one, 
using the same skip rule as for buckets without eligible points; if there is 
none, `C` is the last finite eligible point;
   - a bucket with no finite candidate still returns its earliest point, but 
does not move `A`.
   
   With these rules, all four cases above select the peak. Whatever rule you 
choose, please state it in the class Javadoc and add expected-result tests for 
the three cases, in both `N` and `SIZE` mode.
   
   The endpoint rule in the [semantics] comment does not change this. A 
non-finite first or last point is still returned, but it should not become `A` 
or `C`.



##########
integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBLTTBTableFunctionIT.java:
##########
@@ -0,0 +1,261 @@
+/*
+ * 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.relational.it.db.it;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.TableClusterIT;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.Connection;
+import java.sql.Statement;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
+import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
+import static org.junit.Assert.fail;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({TableLocalStandaloneIT.class, TableClusterIT.class})

Review Comment:
   [tests] Remaining integration-test gaps
   
   I should have included these in my previous review. At 213393e the IT still 
lacks the following:
   
   1. **SLIDE.** No test checks results for `SLIDE < SIZE` or `SLIDE > SIZE`, 
in either time or count windows. `SLIDE < SIZE` gives overlapping windows that 
continue the anchor chain (issue point 1). `SLIDE` only appears in 
`testInvalidParameterCombinations`, and 
`testOverlappingWindowsContinueTheAnchorChain` only covers the processor.
   2. **ORIGIN.** Only the epoch is used (lines 126 and 150). Please add an 
`ORIGIN` that is not aligned with the data, so that the window boundaries 
actually shift.
   3. **TIMECOL.** No LTTB test at any level (IT, planner or UT) covers an 
invalid `TIMECOL`, such as a non-`TIMESTAMP` column or a column that does not 
exist. The PR description lists a non-`TIMESTAMP` `TIMECOL` as rejected at 
analysis time.
   4. **Duplicate timestamps (issue point 4).** Duplicates across input blocks 
are only tested at checker level (`testTimestampUniquenessSpansInputBlocks`), 
and the IT duplicates the rows of a single device with `UNION ALL`. No test 
covers equal timestamps that come from different devices of one partition, e.g. 
`PARTITION BY` a tag that two devices share. Please add this case with the 
multi-region setup from the planner comment, for a direct scan and for the 
window-function and `SESSION` inputs. It must report 701 with both 
`-PTableSimpleIT` and `-PTableClusterIT`.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/LTTBTableFunction.java:
##########
@@ -0,0 +1,1387 @@
+/*
+ * 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.conf.CommonDescriptor;
+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).
+ *       {@code SIZE} defines a bucket, not the input range of a separate 
standard LTTB invocation.
+ *       One anchor chain is maintained per partition and participant column: 
for the current bucket
+ *       {@code A} is the point selected by the preceding bucket of that 
column ({@code B} in turn
+ *       becomes the anchor of the following bucket), {@code B} ranges over 
the current bucket's
+ *       eligible points, and {@code C} is the average point of the next 
bucket that carries data
+ *       for that column (or the bucket's own last eligible point when there 
is none). Overlapping
+ *       buckets ({@code SLIDE < SIZE}) do not restart the chain, and the 
state is independent
+ *       between partitions and between participant columns.
+ * </ul>
+ *
+ * <p>Bucket mode emits one row per window that carries input, and at most one 
selected point per
+ * participant column in a row; a column with no eligible point in a window 
emits {@code NULL} in
+ * its place, and a window in which every participant column is {@code NULL} 
still emits a single
+ * all-{@code NULL} row (padding by output position). Bucket mode does 
<em>not</em> preserve the
+ * first and last eligible points: an endpoint used as {@code A} or as {@code 
C} is a triangle
+ * vertex, not a selected point, so it appears in the output only when it also 
wins its bucket. A
+ * bucket holding just one or two eligible points therefore still contributes 
a single row per
+ * column: with two points the areas are equal and the tie resolves to the 
earlier one. First/last
+ * preservation and the return-all-points case apply to target-count mode only.

Review Comment:
   [semantics] `SIZE` mode must return the first and last points, and a window 
without eligible points must not emit a row
   
   Thanks for making the boundary rules explicit. Two of them need to change.
   
   **1. Return the first and last eligible points, as the original LTTB does.**
   
   At 213393e the first eligible point is only the initial `A`, and the last 
one is only the final `C`. As a triangle vertex, a point has zero area, so 
neither endpoint is returned unless it is alone in its window. The last point 
is the most recent reading, so a chart built with `SIZE => 1m` never shows it.
   
   Real SQL results at 213393e (t = 0, 1, 2, ...):
   
   | input | `SIZE` | 213393e | `N` mode |
   |---|---|---|---|
   | `0,1,0,1,0,9` | 3 | w0 t2:0; w1 t4:0 | `N => 3`: t0:0, t4:0, t5:9 |
   | `9,0,1,0,1,0` | 3 | w0 t1:0; w1 t4:1 | `N => 3`: t0:9, t1:0, t5:0 |
   | `0,9` | 3 | w0 t0:0 | `N => 3`: t0:0, t1:9 |
   | `1,2,1,2,1,2,1,9` | 4 | w0 t2:1; w1 t6:1 | `N => 4`: t0:1, t1:2, t6:1, 
t7:9 |
   
   Please define `SIZE` mode as standard LTTB whose buckets are the windows:
   
   - **Endpoints.** Per partition and participant column, the first eligible 
point `F` and the last eligible point `L` that fall in a window are buckets of 
their own and are always returned, as in the original algorithm.
   - **Window buckets.** Every window contributes one bucket made of its other 
eligible points. The anchor chain starts at `F`. For a window bucket:
     - `A` is the previously selected point;
     - `C` is the average point of the next window bucket that has points, or 
`L` when there is none;
     - ties go to the earliest point;
     - a window bucket without points is skipped.
   - **Output rows.** `F` is reported in the first window that contains it, and 
`L` in the last one. Such a window emits up to two rows, or three when `F` and 
`L` share it. Rows are padded by position, as in M4.
   - **Overlapping windows** keep one anchor chain, as settled in the issue.
   
   Expected results under this rule, computed with a reference implementation 
of it (count windows). Except for the `SLIDE` row, the time-window form (`SIZE 
=> 3ms`, `ORIGIN` at the epoch) gives the same results. With `SLIDE < SIZE`, 
the time windows start one slide before the first point, so the layout differs:
   
   | input | arguments | expected |
   |---|---|---|
   | `0,1,0,1,0,9` | `SIZE => 3` | w0 t0:0, t1:1; w1 t4:0, t5:9 |
   | `9,0,1,0,1,0` | `SIZE => 3` | w0 t0:9, t1:0; w1 t4:1, t5:0 |
   | `0,9` | `SIZE => 3` | w0 t0:0, t1:9 |
   | `1,2,1,2,1,2,1,9` | `SIZE => 4` | w0 t0:1, t1:2; w1 t6:1, t7:9 (same as `N 
=> 4`) |
   | `0,1,0,1,0,9` | `SIZE => 6` | w0 t0:0, t4:0, t5:9 (same as `N => 3`) |
   | `0,1,0,1,0,9` | `SIZE => 3, SLIDE => 2` | w0 t0:0, t1:1; w1 t2:0; w2 t4:0, 
t5:9 |
   | `metrics` d1 `0,1,8,2,0,0` | `SIZE => 2` | w0 t0:0, t1:1; w1 t2:8; w2 
t4:0, t5:0 |
   | `sparse` `0,1,2,NULL,NULL,NULL,0,0,0` | `SIZE => 3` | w0 t0:0, t2:2; w2 
t6:0, t8:0 |
   
   `L` is only known at the end of the partition. A window can therefore be 
finalized only once neither it nor its next window bucket can end with `L`, 
that is, after a later eligible point of the column arrives or the partition 
ends. This adds one eligible point of lookahead to the existing next-bucket 
lookahead.
   
   **2. A window or partition without any eligible point emits no row, as in 
M4.**
   
   At 213393e a window whose participant columns are all NULL still emits one 
all-NULL row, and in `N` mode an all-NULL partition does too. M4 emits no row 
for such a window (`M4TableFunction.outputWindow`, `rowCount == 0`). Both 
functions share the argument and window conventions, so their results should 
agree.
   
   The all-NULL row carries no point. It only says that the window had input 
rows, and that depends on whether the input happens to contain all-NULL rows: 
`WHERE v IS NOT NULL` removes them, while a window without input rows emits 
nothing anyway. For `0,5,1,NULL,NULL,NULL` with `SIZE => 3`, M4 returns window 
0 only; this PR adds an all-NULL row for window 1.
   
   Please emit as many rows as the column with the most selected points, and no 
row when that number is 0, in both modes. Today `rowCount` starts at 1 in 
`TargetCountLTTBDataProcessor.finish` (line 975), and `emitWindow` always 
writes a row (line 1246).
   
   **Updates.** Please update the class Javadoc and the PR description, and 
these tests:
   
   - `testCountWindowAndTimeWindowOutput`: endpoints.
   - `testWindowSkipsNullBucketForLookahead`: window 1 emits no row.
   - `testTargetCountPreservesEndpointsAndAlignsColumns`: the `d2` row.
   - `testDuplicateTimestampsInDifferentPartitionsRemainValid`: `d2` has no 
value at t = 0, so it would no longer show a second partition. Please give it a 
non-NULL value.
   - The UTs that pin the current rule, such as 
`testTwoEligiblePointsInOneBucketEmitOneRow`, 
`testWindowWithNoEligiblePointsStillEmitsARow` and 
`testOverlappingWindowsContinueTheAnchorChain`.



##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/function/TableFunctionOperator.java:
##########
@@ -101,6 +104,7 @@ public TableFunctionOperator(
         new PartitionRecognizer(
             partitionChannels, requiredChannels, passThroughChannels, 
inputDataTypes);
     this.isDeclaredAsPassThrough = isDeclaredAsPassThrough;
+    this.estimatedMaxBufferedBytes = 
processorProvider.getEstimatedMaxBufferedBytes();

Review Comment:
   [memory] The reserved budget can differ from the budget the processors 
enforce
   
   The estimate is read once, here, when the operator is built. The enforced 
limit, however, is read again for every partition:
   
   - this operator creates a new processor per partition (line 181);
   - each processor calls `new BufferMemoryBudget(maxBufferBytes())` 
(`LTTBTableFunction.java` lines 938 and 1076);
   - `maxBufferBytes()` reads the live `lttb_buffer_size_in_bytes`.
   
   So a hot reload during a running query changes the limit for every partition 
that starts afterwards, while the reservation keeps the old value.
   
   A probe shows the gap. With the value at 1 MiB, the estimate is 1,048,576. 
After `setLttbBufferSize(4 MiB)`, which is what a hot reload does, the 
processor created for the next partition only fails at row 131,072, with "LTTB 
buffers exceed the per-processor memory limit of 4194304 bytes". That is 4 MiB 
buffered against a 1 MiB reservation.
   
   Please resolve the budget once, for example when the LTTB provider is 
created, and use that same value for every processor of the provider and for 
this estimate. The two are then equal by construction.



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