-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FLINK-37175][table] Support JSON built-in function for JSON_OBJECT #26022
base: master
Are you sure you want to change the base?
Conversation
6cc5305
to
dc6eed5
Compare
@@ -861,9 +862,13 @@ public static ApiExpression withoutColumns(Object head, Object... tail) { | |||
* jsonObject(JsonOnNull.ABSENT, "K1", nullOf(DataTypes.STRING())) // "{}" | |||
* | |||
* // {"K1":{"K2":"V"}} | |||
* jsonObject(JsonOnNull.NULL, "K1", json('{"K2":"V"}')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* jsonObject(JsonOnNull.NULL, "K1", json('{"K2":"V"}')) | |
* jsonObject(JsonOnNull.NULL, "K1", json("{'K2':'V'}")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or escaped? not sure how strict the user-defined JSON needs to be? maybe we should check and document this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string doesn't have to be escaped, I've updated the javadoc for the java expression and added one example using string literals that show that there's no need to escape it
* // {"K":{"K2":{"K3":42}}}
* jsonObject(
* JsonOnNull.NULL,
* "K",
* json("""
* {
* "K2": {
* "K3": 42
* }
* }
* """))
We escape in the java code since java also uses "
to express strings. If using single quotes in python or string literals in java, there's no need to escape. If they're escaped, they are also processed properly as well and result in the same json object.
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Expressions.java
Outdated
Show resolved
Hide resolved
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Expressions.java
Outdated
Show resolved
Hide resolved
...e/flink-table-common/src/main/java/org/apache/flink/table/types/logical/LogicalTypeRoot.java
Outdated
Show resolved
Hide resolved
...-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/JsonGenerateUtils.scala
Outdated
Show resolved
Hide resolved
...-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/JsonGenerateUtils.scala
Outdated
Show resolved
Hide resolved
...flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/CodeGenUtils.scala
Outdated
Show resolved
Hide resolved
b6516b6
to
6cc950a
Compare
6cc950a
to
dccb782
Compare
1dea414
to
42db42a
Compare
Made the changes as we discussed @twalthr. One additional thing I've changed is that we not only parse the json, but convert the json back to string before storing it. I think that makes sense, so we optimize the storage space by getting rid of unnecessary whitespaces/line breaks and so on before returning the value. |
@flinkbot run azure |
What is the purpose of the change
It is currently not possible to declare a SQL string that contains existing JSON as valid JSON for JSON_OBJECT. Something like JSON_OBJECT(KEY 'K' VALUE '{"value": 42}') returns {"K", "{"value": 42}"}, where the value is a string a not a json object.
This PR adds support for the JSON() function. It's the initial support for it, until this function returns the JSON datatype (what we still don't have in flink).
Example:
Brief change log
Verifying this change
This change added tests for multiple uses cases of the function. Also added tests to make sure the fucnction is only called within JSON_OBJECT.
Does this pull request potentially affect one of the following parts:
@Public(Evolving)
: yesDocumentation