You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/11/13 23:49:57 UTC

[GitHub] [flink] walterddr commented on a change in pull request #9404: [FLINK-13667][ml] Add the utility class for the Table

walterddr commented on a change in pull request #9404: [FLINK-13667][ml] Add the utility class for the Table
URL: https://github.com/apache/flink/pull/9404#discussion_r346061336
 
 

 ##########
 File path: flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/utils/TableUtil.java
 ##########
 @@ -0,0 +1,429 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.ml.common.utils;
+
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.types.Row;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.flink.shaded.guava18.com.google.common.base.Joiner;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * Utility class for the operations on Table, such as the operations on column name, the operations on column type and
+ * so on.
+ */
+public class TableUtil {
+	/**
+	 * Return a lower temp table name in lower case and use "-" as connector character.
+	 *
+	 * @return tableName
+	 */
+	public static synchronized String getTempTableName() {
+		return ("temp_" + UUID.randomUUID().toString().replaceAll("-", "_"))
+			.toLowerCase();
+	}
+
+	/**
+	 * Find the index of <code>targetCol</code> in string array <code>tableCols</code>. It will ignore the case of the
+	 * tableCols.
+	 *
+	 * @param tableCols a string array among which to find the targetCol.
+	 * @param targetCol the targetCol to find.
+	 * @return the index of the targetCol, if not found, returns -1.
+	 */
+	public static int findColIndex(String[] tableCols, String targetCol) {
+		Preconditions.checkNotNull(targetCol, "targetCol is null!");
+		for (int i = 0; i < tableCols.length; i++) {
+			if (targetCol.equalsIgnoreCase(tableCols[i])) {
+				return i;
+			}
+		}
+		return -1;
+	}
+
+	/**
+	 * Find the indices of <code>targetCols</code> in string array <code>tableCols</code>. If
+	 * <code>targetCols</code> is
+	 * null, it will be replaced by the <code>tableCols</code>
+	 *
+	 * @param tableCols  a string array among which to find the targetCols.
+	 * @param targetCols the targetCols to find.
+	 * @return the indices of the targetCols.
+	 */
+	public static int[] findColIndices(String[] tableCols, String[] targetCols) {
+		if (targetCols == null) {
+			int[] indices = new int[tableCols.length];
+			for (int i = 0; i < tableCols.length; i++) {
+				indices[i] = i;
+			}
+			return indices;
+		}
+		int[] indices = new int[targetCols.length];
+		for (int i = 0; i < indices.length; i++) {
+			indices[i] = findColIndex(tableCols, targetCols[i]);
+		}
+		return indices;
+	}
+
+	/**
+	 * Find the index of <code>targetCol</code> from the <code>tableSchema</code>.
+	 *
+	 * @param tableSchema the TableSchema among which to find the targetCol.
+	 * @param targetCol   the targetCols to find.
+	 * @return the index of the targetCol.
+	 */
+	public static int findColIndex(TableSchema tableSchema, String targetCol) {
+		return findColIndex(tableSchema.getFieldNames(), targetCol);
+	}
+
+	/**
+	 * Find the indices of <code>targetCols</code> from the <code>tableSchema</code>.
+	 *
+	 * @param tableSchema the TableSchema among which to find the targetCols.
+	 * @param targetCols  the targetCols to find.
+	 * @return the indices of the targetCols.
+	 */
+	public static int[] findColIndices(TableSchema tableSchema, String[] targetCols) {
+		return findColIndices(tableSchema.getFieldNames(), targetCols);
+	}
+
+	/**
+	 * Find the types of the <code>targetCols</code>. If the targetCol not exist, return null.
+	 *
+	 * @param tableSchema TableSchema.
+	 * @param targetCols  the targetCols to find.
+	 * @return the corresponding types.
+	 */
+	public static TypeInformation[] findColTypes(TableSchema tableSchema, String[] targetCols) {
+		TypeInformation[] types = new TypeInformation[targetCols.length];
+		for (int i = 0; i < types.length; i++) {
 
 Review comment:
   echo previous comment - check null for `targetCols` missing

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services