Skip to content

[FLINK-37695][table] Fix parsing for built-in function JSON in JSON_OBJECT for all positions #26474

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gustavodemorais
Copy link
Contributor

@gustavodemorais gustavodemorais commented Apr 17, 2025

What is the purpose of the change

Parsing for a JSON() function call fails if its position is not the first param of JSON_OBJECT.

SELECT JSON_OBJECT('key' VALUE JSON('{}'), 'key_2' VALUE JSON('{}'));

Outputs:

The JSON() function is currently only supported inside a JSON_OBJECT() or JSON_ARRAY() function. Example: JSON_OBJECT('a', JSON('{\"key\": \"value\"}')) or JSON_ARRAY(JSON('{\"key\": \"value\"}'))

This is incorrect, we were too strict with the limitations. We're strictly allowing json to be called only as the second param in the function, when it should be allowed for every second.

Brief change log

(for example:)

  • Check if the JSON call is for any of the VALUES param inside a JSON_OBJECT and not only the first one

Verifying this change

This change added tests and can be verified as follows:

  • Added unit tests

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (no)
  • The serializers: (no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
  • The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (no)
  • If yes, how is the feature documented? (already covered by docs / JavaDocs)

@flinkbot
Copy link
Collaborator

flinkbot commented Apr 17, 2025

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

Comment on lines 491 to 492
if isJsonFunctionOperand(operand) &&
(isJsonArrayOperand(call) || i == 2 && isJsonObjectOperand(call)) =>
(isJsonArrayOperand(call) || isJsonObjectOperand(call) && inValuesParam(i)) =>
Copy link
Contributor

@snuyanzin snuyanzin Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder whether we can extract the whole if condition into method something like isJsonOperand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a helper function called isSupportedJsonOperand

* Determines if the parameter index is % 2, which means checking that we only allow JSON calls
* for the VALUE parameter of a JSON_OBJECT call.
*/
def inValuesParam(i: Int): Boolean = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can drop this method if we extract the whole if condition into a method.

Also about naming: it might make confused some people since in SQL there is also VALUES and this method has nothing to do with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it to inValuesParamOfJsonObject? I think the code is more readable if you read inValuesParamOfJsonObject as part of the if condition than i%2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants