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


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/InsertBaseStatement.java:
##########
@@ -258,6 +270,65 @@ private static String getPathsStringForLog(Stream<? 
extends PartialPath> pathStr
 
   public void updateAfterSchemaValidation(MPPQueryContext context) throws 
QueryProcessException {}
 
+  public void setInsertTypeCastZone(ZoneId zoneId) {
+    insertTypeCastZone = zoneId;
+  }
+
+  public boolean isInsertTypeCastEnabled() {
+    return insertTypeCastZone != null;
+  }
+
+  protected boolean shouldFormatTimestampAsText(TSDataType source, TSDataType 
target) {
+    return isInsertTypeCastEnabled()
+        && !isForceTypeConversion()
+        && source == TSDataType.TIMESTAMP
+        && (target == TSDataType.TEXT || target == TSDataType.STRING);
+  }
+
+  protected Function<Object, Object> prepareInsertCast(TSDataType source, 
TSDataType target) {
+    if (!isInsertTypeCastEnabled()
+        || isForceTypeConversion()
+        || source == TSDataType.OBJECT
+        || target == TSDataType.OBJECT
+        || source == TSDataType.UNKNOWN
+        || target == TSDataType.UNKNOWN
+        || source == TSDataType.VECTOR
+        || target == TSDataType.VECTOR) {
+      return null;
+    }
+    final TypeServices.CastObjectInputService input =
+        
TypeServices.CAST_OBJECT_INPUT_SERVICE.call(Type.fromTsDataType(source));
+    final TypeServices.CastObjectValueService output =
+        
TypeServices.CAST_OBJECT_VALUE_SERVICE.call(Type.fromTsDataType(target));
+    // The column CAST parses BLOB values using their hexadecimal 
representation.
+    final boolean decodeBlob = source == TSDataType.BLOB && !target.isBinary();
+    // Tree-model column CAST uses the server time zone (see 
CastFunctionHelper).
+    final ZoneId castZone =
+        !isWriteToTable() && shouldFormatTimestampAsText(source, target)

Review Comment:
   **[P1] Use the tree query time zone for date casts**
   
   `CastFunctionHelper` passes the server default time zone to the tree query 
column transformer for all conversions. This branch only selects that zone for 
`TIMESTAMP -> TEXT/STRING`. With a UTC server and a `-08:00` session, inserting 
a typed `TIMESTAMP` value of `0` into a tree `DATE` series persists 
`1969-12-31`, while `SELECT CAST(source AS DATE)` on a `TIMESTAMP` series 
containing the same value returns `1970-01-01`.
   
   Please use the tree query zone for the new tree-model casts and add a 
regression test with distinct server and session zones. Table casts should 
continue to use the session zone.



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/crud/InsertRowStatement.java:
##########
@@ -250,9 +276,33 @@ public void transferType(ZoneId zoneId) throws 
QueryProcessException {
       // parse string value to specific type
       dataTypes[i] = measurementSchemas[i].getType();
       try {
+        if (values[i] instanceof PendingInsertCast) {
+          final PendingInsertCast pending = (PendingInsertCast) values[i];
+          values[i] = pending.value();
+          if (getColumnCategory(i) == TsTableColumnCategory.FIELD) {
+            if (pending.sourceType() == dataTypes[i]) {
+              if (dataTypes[i] == TSDataType.DATE) {
+                validateEncodedDate((Integer) pending.value());
+              }
+              continue;
+            }
+            final Function<Object, Object> cast =
+                prepareInsertCast(pending.sourceType(), dataTypes[i]);
+            if (cast != null) {
+              values[i] = cast.apply(pending.value());
+              continue;
+            }
+          }
+        }
         // if the type is binary and the value is already binary, do not 
convert
         if (values[i] != null && !(dataTypes[i].isBinary() && values[i] 
instanceof Binary)) {
-          values[i] = CommonUtils.parseValue(dataTypes[i], 
values[i].toString(), zoneId);
+          try {
+            values[i] = CommonUtils.parseValue(dataTypes[i], 
values[i].toString(), zoneId);
+          } catch (QueryProcessException e) {

Review Comment:
   **[P2] Try query CAST after a BLOB literal parse failure**
   
   With the option enabled, `INSERT INTO root.cast_demo.d(time, b) VALUES (1, 
'abc')` still rejects `b` when it is a `BLOB` series. 
`CommonUtils.parseBlobStringToByteArray` rejects the plain text with 
`IllegalArgumentException`, which bypasses this catch and goes straight to the 
outer partial-insert handling. Query `CAST` from `TEXT` to `BLOB` accepts the 
same bytes (`X'616263'`). This also affects untyped Session records.
   
   Route the expected BLOB parse failure through the CAST fallback and cover a 
plain text value written to a BLOB series through the untyped path.



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