You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "EricJoy2048 (via GitHub)" <gi...@apache.org> on 2023/09/01 08:04:51 UTC

[GitHub] [seatunnel] EricJoy2048 commented on a diff in pull request #5402: [Improve][transforms-v2][SQL] Support can use column.a, column['a'], and column[0] to get the data

EricJoy2048 commented on code in PR #5402:
URL: https://github.com/apache/seatunnel/pull/5402#discussion_r1312706884


##########
seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/sql_transform/sql_complex_type.conf:
##########
@@ -0,0 +1,100 @@
+#
+# 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.
+#
+######
+###### This config file is a demonstration of streaming processing in seatunnel config
+######
+
+env {
+  execution.parallelism = 1
+  job.mode = "BATCH"
+}
+
+source {
+    FakeSource {
+      result_table_name="t1"
+      schema = {
+        fields {
+          c_string = "string"
+          c_array = "array<string>"
+          c_map = "map<string,string>"
+          c_row {
+            c_int = "int"
+            c_string = "string"
+            next_row {
+              c_int = "int"
+              c_string = "string"
+            }
+          }
+        }
+      }
+      rows = [
+        {
+          kind = INSERT
+          fields = ["cs",["ca1","ca2","ca3"],{"name":"zs","age":"18"},[1,"sh",[2,"bj"]]]
+        }
+      ]
+    }
+}
+
+transform {
+  Sql {
+    source_table_name = "t1"
+    result_table_name = "t2"
+    query = "select c_string as c1,c_array[0] as c2,c_map['name'] as c3,c_row.next_row.c_string as c4 from t1"

Review Comment:
   Can you add this example to document `docs/en/transform-v2/sql.md`?



##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLType.java:
##########
@@ -87,8 +93,57 @@ public SeaTunnelDataType<?> getExpressionType(Expression expression) {
             return BasicType.STRING_TYPE;
         }
         if (expression instanceof Column) {
-            String columnName = ((Column) expression).getColumnName();
-            return inputRowType.getFieldType(inputRowType.indexOf(columnName));
+            Column column = (Column) expression;
+            String columnName = column.getColumnName();
+            Table table = column.getTable();
+            if (null == table) {
+                return inputRowType.getFieldType(inputRowType.indexOf(columnName));
+            } else {
+                List<String> keys =
+                        Arrays.stream(table.getFullyQualifiedName().split("\\."))
+                                .collect(Collectors.toList());
+                keys.add(columnName);
+
+                int currentIndex = inputRowType.indexOf(keys.get(0));
+                SeaTunnelDataType<?> currentDataType = inputRowType.getFieldType(currentIndex);
+                SeaTunnelRowType currentRowType;
+
+                for (int i = 1; i < keys.size(); i++) {
+                    if (currentDataType instanceof SeaTunnelRowType) {
+                        currentRowType = (SeaTunnelRowType) currentDataType;
+                        currentIndex = currentRowType.indexOf(keys.get(i));
+                        if (i == keys.size() - 1) {
+                            return currentRowType.getFieldType(currentIndex);
+                        }
+                        currentDataType = currentRowType.getFieldType(currentIndex);
+                    } else {
+                        throw new IllegalArgumentException(

Review Comment:
   Use `TransformException` better?



##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLType.java:
##########
@@ -87,8 +93,57 @@ public SeaTunnelDataType<?> getExpressionType(Expression expression) {
             return BasicType.STRING_TYPE;
         }
         if (expression instanceof Column) {
-            String columnName = ((Column) expression).getColumnName();
-            return inputRowType.getFieldType(inputRowType.indexOf(columnName));
+            Column column = (Column) expression;
+            String columnName = column.getColumnName();
+            Table table = column.getTable();
+            if (null == table) {
+                return inputRowType.getFieldType(inputRowType.indexOf(columnName));
+            } else {
+                List<String> keys =
+                        Arrays.stream(table.getFullyQualifiedName().split("\\."))
+                                .collect(Collectors.toList());
+                keys.add(columnName);
+
+                int currentIndex = inputRowType.indexOf(keys.get(0));
+                SeaTunnelDataType<?> currentDataType = inputRowType.getFieldType(currentIndex);
+                SeaTunnelRowType currentRowType;
+
+                for (int i = 1; i < keys.size(); i++) {
+                    if (currentDataType instanceof SeaTunnelRowType) {
+                        currentRowType = (SeaTunnelRowType) currentDataType;
+                        currentIndex = currentRowType.indexOf(keys.get(i));
+                        if (i == keys.size() - 1) {
+                            return currentRowType.getFieldType(currentIndex);
+                        }
+                        currentDataType = currentRowType.getFieldType(currentIndex);
+                    } else {
+                        throw new IllegalArgumentException(

Review Comment:
   Use `TransformException` better?



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org