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


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/TableLogicalPlanner.java:
##########
@@ -656,17 +664,50 @@ private RelationPlan createRelationPlan(final CopyTo 
statement, final Analysis a
         .infer(analysis, innerQueryRelationPlan, 
innerQueryRespDatasetHeader.getColumnHeaders());
     
statement.getOptions().check(innerQueryRespDatasetHeader.getColumnHeaders());
 
+    PlanNode copyToSource = innerQueryRelationPlan.getRoot();
+    if (statement.getOptions() instanceof CopyToTsFileOptions tsFileOptions
+        && !tsFileOptions.isGenerateNewTimeColumn()) {
+      List<ColumnHeader> columnHeaders = 
innerQueryRespDatasetHeader.getColumnHeaders();
+      List<Symbol> outputSymbols = outputNode.getOutputSymbols();
+      Map<String, Symbol> columnSymbols = new HashMap<>();
+      for (int i = 0; i < columnHeaders.size(); i++) {
+        columnSymbols.put(columnHeaders.get(i).getColumnName(), 
outputSymbols.get(i));
+      }
+      Map<Symbol, SortOrder> orderings = new LinkedHashMap<>();
+      for (String tagColumn : tsFileOptions.getTargetTagColumns()) {
+        orderings.put(columnSymbols.get(tagColumn), SortOrder.ASC_NULLS_FIRST);
+      }
+      orderings.put(
+          columnSymbols.get(tsFileOptions.getTargetTimeColumn()), 
SortOrder.ASC_NULLS_FIRST);
+      // Sort the complete inner query result, after its LIMIT and OFFSET.
+      copyToSource =
+          new SortNode(

Review Comment:
   [P2] Handle COPY sources without a table scan in the sort optimization
   
   Adding this sort makes previously valid no-FROM exports fail during logical 
optimization. For example:
   
   ```sql
   COPY (SELECT CAST(1 AS TIMESTAMP) AS time, 1 AS v)
   TO 'constant.tsfile' (MEMORY_THRESHOLD 1000000);
   ```
   
   This now returns `305 / INTERNAL_SERVER_ERROR` because 
`TransformSortToStreamSort.visitSort()` dereferences a null 
`DeviceTableScanNode`. I reproduced this on a live 1C1D instance; `EXPLAIN` and 
`EXPLAIN ANALYZE` wrappers fail as well, while a comparison using the baseline 
planner accepts the plain COPY query. Please make the sort optimization handle 
sources without a device scan, or safely eliminate unnecessary single-row 
sorts, and add regression coverage.



##########
iotdb-core/relational-grammar/src/main/antlr4/org/apache/iotdb/db/relational/grammar/sql/RelationalSql.g4:
##########
@@ -968,8 +968,8 @@ copyToStatementOption
 // ------------------------------------------- Query Statement 
---------------------------------------------------------
 queryStatement
     : query                                                        
#statementDefault
-    | EXPLAIN ('(' FORMAT identifier ')')? (query | executeStatement | 
executeImmediateStatement) #explain
-    | EXPLAIN ANALYZE VERBOSE? ('(' FORMAT identifier ')')? (query | 
executeStatement | executeImmediateStatement) #explainAnalyze
+    | EXPLAIN ('(' FORMAT identifier ')')? (query | copyToStatement | 
executeStatement | executeImmediateStatement) #explain
+    | EXPLAIN ANALYZE VERBOSE? ('(' FORMAT identifier ')')? (query | 
copyToStatement | executeStatement | executeImmediateStatement) #explainAnalyze

Review Comment:
   [P2] Update the existing EXPLAIN parser-error assertions
   
   Adding `COPY` to these alternatives changes the expected-token lists in 
parser errors, but `IoTDBInsertQueryIT.testExplain` still asserts the complete 
old messages at lines 464 and 476. `EXPLAIN INSERT ...` now reports `Expecting: 
'(', 'ANALYZE', 'COPY', 'EXECUTE', <query>`, so the first assertion fails; the 
`EXPLAIN ANALYZE INSERT ...` assertion has the same mismatch. This is already 
failing the [Windows (2) table-model CI 
job](https://github.com/apache/iotdb/actions/runs/39385979976/job/118505878007).
 Please update both assertions, or assert the stable parts of the parser error.



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