slinkydeveloper commented on a change in pull request #17381:
URL: https://github.com/apache/flink/pull/17381#discussion_r719145175



##########
File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/DataType.java
##########
@@ -115,6 +149,167 @@ public int hashCode() {
 
     // 
--------------------------------------------------------------------------------------------
 
+    /**
+     * Returns the first-level field names for the provided {{@link DataType}}.
+     *
+     * <p>Note: this method returns an empty list for every {{@link DataType}} 
not instance of
+     * {{@link FieldsDataType}}.
+     */
+    public static List<String> getFieldNames(DataType dataType) {
+        return dataType.logicalType.accept(
+                new LogicalTypeDefaultVisitor<List<String>>() {
+
+                    @Override
+                    public List<String> visit(StructuredType structuredType) {
+                        return structuredType.getAttributes().stream()
+                                
.map(StructuredType.StructuredAttribute::getName)
+                                .collect(Collectors.toList());
+                    }
+
+                    @Override
+                    public List<String> visit(RowType rowType) {
+                        return rowType.getFieldNames();
+                    }
+
+                    @Override
+                    public List<String> visit(DistinctType distinctType) {
+                        return distinctType.getSourceType().accept(this);
+                    }
+
+                    @Override
+                    protected List<String> defaultMethod(LogicalType 
logicalType) {
+                        return Collections.emptyList();
+                    }
+                });
+    }
+
+    /**
+     * Returns the first-level field data types for the provided {{@link 
DataType}}.
+     *
+     * <p>Note: this method returns an empty list for every {{@link DataType}} 
not instance of
+     * {{@link FieldsDataType}}.
+     */
+    public static List<DataType> getFieldDataTypes(DataType dataType) {
+        return dataType.accept(
+                new DataTypeDefaultVisitor<List<DataType>>() {
+                    @Override
+                    public List<DataType> visit(FieldsDataType fieldsDataType) 
{
+                        return fieldsDataType.getChildren();
+                    }
+
+                    @Override
+                    protected List<DataType> defaultMethod(DataType dataType) {
+                        return Collections.emptyList();
+                    }
+                });
+    }
+
+    /**
+     * Returns the count of the first-level fields for the provided {{@link 
DataType}}.
+     *
+     * <p>Note: this method returns {{@code 0}} for every {{@link DataType}} 
not instance of {{@link
+     * FieldsDataType}}.
+     */
+    public static int getFieldCount(DataType dataType) {
+        return getFieldDataTypes(dataType).size();
+    }
+
+    /**
+     * Projects a (possibly nested) row data type by returning a new data type 
that only includes
+     * fields of the given index paths.
+     *
+     * <p>Note: Index paths allow for arbitrary deep nesting. For example, 
{@code [[0, 2, 1], ...]}
+     * specifies to include the 2nd field of the 3rd field of the 1st field in 
the top-level row.
+     * Sometimes, it may get name conflicts when extract fields from the row 
field. Considering the
+     * the path is unique to extract fields, it makes sense to use the path to 
the fields with
+     * delimiter `_` as the new name of the field. For example, the new name 
of the field `b` in the
+     * row `a` is `a_b` rather than `b`. But it may still gets name conflicts 
in some situation,
+     * such as the field `a_b` in the top level schema. In such situation, it 
will use the postfix
+     * in the format '_$%d' to resolve the name conflicts.
+     */
+    public static DataType projectFields(DataType dataType, int[][] 
indexPaths) {
+        return DataTypeUtils.projectRow(dataType, indexPaths);
+    }
+
+    /**
+     * Projects a (possibly nested) row data type by returning a new data type 
that only includes
+     * fields of the given indices.
+     *
+     * <p>Note: This method only projects (possibly nested) fields in the 
top-level row.
+     */
+    public static DataType projectFields(DataType dataType, int[] indexes) {
+        return DataTypeUtils.projectRow(dataType, indexes);
+    }
+
+    /**
+     * Returns an ordered list of fields starting from the provided {{@link 
DataType}}.
+     *
+     * <p>Note: this method returns an empty list if the provided data type is 
not an instance of
+     * {{@link FieldsDataType}} or its logical type is not an instance of 
{{@link RowType}}.
+     */
+    public static List<DataTypes.Field> getFields(DataType dataType) {

Review comment:
       I've found out a simpler way to implement it from the other static 
methods included in this pr




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