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 2020/04/06 12:22:15 UTC

[GitHub] [flink] twalthr commented on a change in pull request #11570: [FLINK-16830][table-api] Let users use Row/List/Map/Seq directly in Expression DSL

twalthr commented on a change in pull request #11570: [FLINK-16830][table-api] Let users use Row/List/Map/Seq directly in Expression DSL
URL: https://github.com/apache/flink/pull/11570#discussion_r404022471
 
 

 ##########
 File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/ApiExpressionUtils.java
 ##########
 @@ -52,14 +63,151 @@ private ApiExpressionUtils() {
 		// private
 	}
 
+	/**
+	 * Converts a given object to an expression.
+	 *
+	 * <p>It converts:
+	 * <ul>
+	 *     <li>{@link Row} to a call to a row constructor expression</li>
+	 *     <li>{@link Map} to a call to a map constructor expression</li>
+	 *     <li>{@link List} to a call to an array constructor expression</li>
+	 *     <li>arrays to a call to an array constructor expression</li>
+	 *     <li>Scala's {@code Seq} to an array constructor via reflection</li>
+	 *     <li>Scala's {@code Map} to a map constructor via reflection</li>
+	 *     <li>Scala's {@code BigDecimal} to a DECIMAL literal</li>
+	 *     <li>if none of the above applies, the function tries to convert the object
+	 *          to a value literal with {@link #valueLiteral(Object)}</li>
+	 * </ul>
+	 *
+	 * @param expression An object to convert to an expression
+	 */
 	public static Expression objectToExpression(Object expression) {
 		if (expression instanceof ApiExpression) {
 			return ((ApiExpression) expression).toExpr();
 		} else if (expression instanceof Expression) {
 			return (Expression) expression;
+		} else if (expression instanceof Row) {
+			return convertRow((Row) expression);
+		} else if (expression instanceof Map) {
+			return convertJavaMap((Map<?, ?>) expression);
+		} else if (expression instanceof List) {
+			return convertJavaList((List<?>) expression);
+		} else if (expression.getClass().isArray()) {
+			return convertArray(expression);
 		} else {
-			return valueLiteral(expression);
+			return convertScala(expression).orElseGet(() -> valueLiteral(expression));
+		}
+	}
+
+	private static Expression convertRow(Row expression) {
+		List<Expression> fields = IntStream.range(0, expression.getArity())
+			.mapToObj(expression::getField)
+			.map(ApiExpressionUtils::objectToExpression)
+			.collect(Collectors.toList());
+
+		return ApiExpressionUtils.unresolvedCall(BuiltInFunctionDefinitions.ROW, fields);
 
 Review comment:
   nit: we are still in the same class and don't need to fully qualify the method

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