View Javadoc
1   package org.wikimedia.eventutilities.flink.formats.json;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.annotation.Nonnull;
7   import javax.annotation.Nullable;
8   import javax.annotation.ParametersAreNonnullByDefault;
9   
10  import org.apache.flink.table.api.DataTypes;
11  import org.apache.flink.table.types.DataType;
12  import org.wikimedia.eventutilities.core.event.types.SchemaConversions;
13  
14  /**
15   * Implementation of {@link SchemaConversions}
16   * that can convert to Flink Table API DataType.
17   * Used by JsonSchemaFlinkConverter.
18   *
19   * If you modify this class, please also augment {@link TypeInformationSchemaConversions} accordingly.
20   * To handle proper conversion between Table API and DataStream,
21   * Flink needs the types consistently converted to the same underlying representations.
22   */
23  @ParametersAreNonnullByDefault
24  public class DataTypeSchemaConversions implements SchemaConversions<DataType> {
25  
26      /**
27       * @return
28       *  {@link DataTypes#NULL}
29       */
30      @Override
31      public DataType typeNull() {
32          return DataTypes.NULL();
33      }
34  
35      /**
36       * @return
37       *  {@link DataTypes#BOOLEAN}
38       */
39      @Override
40      public DataType typeBoolean() {
41          return DataTypes.BOOLEAN();
42      }
43  
44      /**
45       * @return
46       *  {@link DataTypes#STRING}
47       */
48      @Override
49      public DataType typeString() {
50          return DataTypes.STRING();
51      }
52  
53      /**
54       * @return
55       *  {@link DataTypes#DOUBLE}
56       */
57      @Override
58      public DataType typeDecimal() {
59          return DataTypes.DOUBLE();
60      }
61  
62      /**
63       * Note that the default conversion of LogicalType BigIntType is Long,
64       * which is NOT the same as Flink Types.BIG_INT (in the DataStream API).
65       *
66       * @return
67       *  {@link DataTypes#BIGINT}
68       */
69      @Override
70      public DataType typeInteger() {
71          return DataTypes.BIGINT();
72      }
73  
74      /**
75       * @return
76       *  {@link DataTypes#TIMESTAMP_LTZ} with precision 3
77       */
78      @Override
79      public DataType typeTimestamp() {
80          return DataTypes.TIMESTAMP_LTZ(3);
81      }
82  
83      /**
84       * <code>elementsAreNullable</code> is ignored; all Flink Table API elements are nullable.
85       *
86       * @return
87       *  {@link DataTypes#ARRAY}
88       */
89      @Override
90      public DataType typeArray(
91          DataType elementType,
92          boolean elementsAreNullable
93      ) {
94          return DataTypes.ARRAY(elementType);
95      }
96  
97      /**
98       * {@code valuesAreNullable} is ignored; all Flink Table API elements are nullable.
99       *
100      * @return
101      *  {@link DataTypes#MAP}
102      */
103     @Override
104     public DataType typeMap(
105         DataType keyType,
106         DataType valueType,
107         boolean valuesAreNullable
108     ) {
109         return DataTypes.MAP(keyType, valueType);
110     }
111 
112     /**
113      * If a RowFields description is not null,
114      * it will be used as the {@link DataTypes.Field}'s description.
115      * RowField nullalble-ness is not relevant;
116      * all Flink Table API fields are nullable.
117      *
118      * @return
119      *  {@link DataTypes#ROW}
120      */
121     @Override
122     public DataType typeRow(List<RowField<DataType>> rowFields) {
123         List<DataTypes.Field> dataTypeFields = new ArrayList<>();
124 
125         for (RowField<DataType> rowField : rowFields) {
126             dataTypeFields.add(
127                 buildFieldDataType(
128                     rowField.getName(),
129                     rowField.getType(),
130                     rowField.getDescription()
131                 )
132             );
133         }
134 
135         return DataTypes.ROW(dataTypeFields);
136     }
137 
138     /**
139      * Helper for constructing a DataTypes.Field with name,
140      * DataType, and optional description.
141      */
142     @Nonnull
143     protected DataTypes.Field buildFieldDataType(
144         String name,
145         DataType dataType,
146         @Nullable String description
147     ) {
148         if (description != null) {
149             return DataTypes.FIELD(
150                 name,
151                 dataType,
152                 description
153             );
154         } else {
155             return DataTypes.FIELD(
156                 name,
157                 dataType
158             );
159         }
160     }
161 
162 }