Airblader commented on a change in pull request #17186:
URL: https://github.com/apache/flink/pull/17186#discussion_r704069593
##########
File path:
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/SpecificInputTypeStrategies.java
##########
@@ -46,6 +55,15 @@
public static final InputTypeStrategy CURRENT_WATERMARK =
new CurrentWatermarkInputTypeStrategy();
+ /** Input strategy for {@link BuiltInFunctionDefinitions#JSON_OBJECT}. */
+ public static final InputTypeStrategy JSON_OBJECT =
Review comment:
(Placed in-line at the BIFD, Spotless will format this to an absolute
mess of a pyramid)
##########
File path:
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Expressions.java
##########
@@ -562,6 +563,43 @@ public static ApiExpression withoutColumns(Object head,
Object... tail) {
return
apiCallAtLeastOneArgument(BuiltInFunctionDefinitions.WITHOUT_COLUMNS, head,
tail);
}
+ /**
+ * Builds a JSON string from a list of key-value pairs.
+ *
+ * <p>{@param keyValues} is an even-numbered list of alternating key/value
pairs. Note that keys
+ * must be non-{@code NULL} string literals, while values may be arbitrary
expressions.
+ *
+ * <p>The {@link JsonOnNull onNull} behavior defines how to treat {@code
NULL} values.
+ *
+ * <p>Note that this function only returns a JSON string, not an actual
JSON type. Not all JSON
+ * can currently be represented in Flink's type system.
+ *
+ * <p>Examples:
+ *
+ * <pre>{@code
+ * // "{}"
+ * jsonObject(JsonOnNull.NULL)
+ * // "{\"K1\":\"V1\",\"K2\":\"V2\"}"
+ * jsonObject(JsonOnNull.NULL, "K1", "V1", "K2", "V2")
+ *
+ * // Expressions as values
+ * jsonObject(JsonOnNull.NULL, "orderNo", $("orderId"))
+ *
+ * // ON NULL
+ * jsonObject(JsonOnNull.NULL, "K1", nullOf(DataTypes.STRING())) //
"{\"K1\":null}"
+ * jsonObject(JsonOnNull.ABSENT, "K1", nullOf(DataTypes.STRING())) // "{}"
+ *
+ * // "{\"K1\":\"{\\\"K2\\\":\\\"V\\\"}\"}"
+ * jsonObject(JsonOnNull.NULL, "K1", jsonObject(JsonOnNull.NULL, "K2",
"V"))
+ * }</pre>
+ */
+ public static ApiExpression jsonObject(JsonOnNull onNull, Object...
keyValues) {
Review comment:
FWIW, I was also toying around with providing a `Map<String, Object>`
rather than varargs as it forces the literal key type as well as the arity of
the passed arguments. But in Java 8 it's such a pain to construct such maps and
the varargs signature is much more convenient to use here.
##########
File path:
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Expressions.java
##########
@@ -562,6 +563,43 @@ public static ApiExpression withoutColumns(Object head,
Object... tail) {
return
apiCallAtLeastOneArgument(BuiltInFunctionDefinitions.WITHOUT_COLUMNS, head,
tail);
}
+ /**
+ * Builds a JSON string from a list of key-value pairs.
+ *
+ * <p>{@param keyValues} is an even-numbered list of alternating key/value
pairs. Note that keys
+ * must be non-{@code NULL} string literals, while values may be arbitrary
expressions.
+ *
+ * <p>The {@link JsonOnNull onNull} behavior defines how to treat {@code
NULL} values.
+ *
+ * <p>Note that this function only returns a JSON string, not an actual
JSON type. Not all JSON
+ * can currently be represented in Flink's type system.
+ *
+ * <p>Examples:
+ *
+ * <pre>{@code
+ * // "{}"
+ * jsonObject(JsonOnNull.NULL)
+ * // "{\"K1\":\"V1\",\"K2\":\"V2\"}"
+ * jsonObject(JsonOnNull.NULL, "K1", "V1", "K2", "V2")
+ *
+ * // Expressions as values
+ * jsonObject(JsonOnNull.NULL, "orderNo", $("orderId"))
+ *
+ * // ON NULL
+ * jsonObject(JsonOnNull.NULL, "K1", nullOf(DataTypes.STRING())) //
"{\"K1\":null}"
+ * jsonObject(JsonOnNull.ABSENT, "K1", nullOf(DataTypes.STRING())) // "{}"
+ *
+ * // "{\"K1\":\"{\\\"K2\\\":\\\"V\\\"}\"}"
+ * jsonObject(JsonOnNull.NULL, "K1", jsonObject(JsonOnNull.NULL, "K2",
"V"))
+ * }</pre>
+ */
+ public static ApiExpression jsonObject(JsonOnNull onNull, Object...
keyValues) {
Review comment:
I didn't give this one a signature without the leading argument for two
reasons: for one, I think the use case of this function is limited to examples
and mocking. The more important reason, however, is that the two signatures
would kind of clash. I think forcing Table API users to provide the on-null
behavior is OK.
--
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]