Skip to content

Commit a84d028

Browse files
Merge pull request #1882 from taozhi8833998/feat-json-object-flink
feat: support json object in flinksql
2 parents 3996769 + 02423f5 commit a84d028

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

pegjs/flinksql.pegjs

+27
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,32 @@ position_func_clause
29142914
};
29152915
}
29162916

2917+
json_object_func_arg
2918+
= key:literal_string __ 'VALUE'i __ value:or_and_expr __ on:(KW_ON __ 'NULL'i __ ('NULL'i / 'ABSENT'i))? {
2919+
return {
2920+
type: 'json_object_arg',
2921+
expr: {
2922+
key,
2923+
value,
2924+
on: on && { type: 'origin', value: on[4] }
2925+
}
2926+
}
2927+
}
2928+
2929+
json_object_func_args
2930+
= head:json_object_func_arg tail:(__ COMMA __ json_object_func_arg)* {
2931+
return { type: 'expr_list', value: createList(head, tail) }
2932+
}
2933+
2934+
json_object_func_clause
2935+
= 'json_object'i __ LPAREN __ args:json_object_func_args __ RPAREN {
2936+
return {
2937+
type: 'function',
2938+
name: { name: [{ type: 'origin', value: 'json_object' }]},
2939+
args,
2940+
};
2941+
}
2942+
29172943
trim_position
29182944
= 'BOTH'i / 'LEADING'i / 'TRAILING'i
29192945

@@ -2994,6 +3020,7 @@ substring_func_clause
29943020

29953021
func_call
29963022
= position_func_clause
3023+
/ json_object_func_clause
29973024
/ trim_func_clause
29983025
/ substring_func_clause
29993026
/ overlay_func_clause

src/expr.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { assignToSQL } from './assign'
55
import { binaryToSQL } from './binary'
66
import { caseToSQL } from './case'
77
import { columnDefinitionToSQL, columnRefToSQL, fullTextSearchToSQL } from './column'
8-
import { anyValueFuncToSQL, castToSQL, extractFunToSQL, flattenFunToSQL, funcToSQL, lambdaToSQL, tablefuncFunToSQL } from './func'
8+
import { anyValueFuncToSQL, castToSQL, extractFunToSQL, flattenFunToSQL, funcToSQL, jsonObjectArgToSQL, lambdaToSQL, tablefuncFunToSQL } from './func'
99
import { intervalToSQL } from './interval'
1010
import { jsonExprToSQL, jsonVisitorExprToSQL } from './json'
1111
import { selectToSQL } from './select'
@@ -36,6 +36,7 @@ const exprToSQLConvertFn = {
3636
insert : unionToSQL,
3737
interval : intervalToSQL,
3838
json : jsonExprToSQL,
39+
json_object_arg : jsonObjectArgToSQL,
3940
json_visitor : jsonVisitorExprToSQL,
4041
show : showToSQL,
4142
struct : arrayStructExprToSQL,

src/func.js

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ function flattenArgToSQL(arg) {
5959
return result.filter(hasVal).join(' ')
6060
}
6161

62+
function jsonObjectArgToSQL(argExpr) {
63+
const { expr } = argExpr
64+
const { key, value, on } = expr
65+
const result = [exprToSQL(key), 'VALUE', exprToSQL(value)]
66+
if (on) result.push('ON', 'NULL', exprToSQL(on))
67+
return result.filter(hasVal).join(' ')
68+
}
69+
6270
function flattenFunToSQL(stmt) {
6371
const { args, type } = stmt
6472
const keys = ['input', 'path', 'outer', 'recursive', 'mode']
@@ -103,6 +111,7 @@ export {
103111
extractFunToSQL,
104112
flattenFunToSQL,
105113
funcToSQL,
114+
jsonObjectArgToSQL,
106115
lambdaToSQL,
107116
tablefuncFunToSQL,
108117
}

test/flink.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,24 @@ describe('Flink', () => {
394394
"INSERT INTO `sink_soc_10086_node_11` (pk, f1) SELECT CAST(MAP['uri', uri] AS VARCHAR), `uri` FROM `view_soc_10086_node_8`"
395395
]
396396
},
397+
{
398+
title: 'json_object',
399+
sql: [
400+
`select name, eventTime, eventDetail
401+
from (
402+
select
403+
concat('AK中文信息') as name,
404+
cast(event_time as varchar) as eventTime,
405+
json_object(
406+
'risk-tag' value risk_tag,
407+
'abc' VALUE (10 * 2),
408+
'user-agent' value JSON_OBJECT('city' VALUE 'New York', 'postalCode' VALUE '10001')
409+
) as eventDetail
410+
from check_risk
411+
);`,
412+
"SELECT `name`, `eventTime`, `eventDetail` FROM (SELECT concat('AK中文信息') AS `name`, CAST(`event_time` AS VARCHAR) AS `eventTime`, JSON_OBJECT('risk-tag' VALUE `risk_tag`, 'abc' VALUE (10 * 2), 'user-agent' VALUE JSON_OBJECT('city' VALUE 'New York', 'postalCode' VALUE '10001')) AS `eventDetail` FROM `check_risk`)"
413+
]
414+
},
397415
];
398416

399417
SQL_LIST.forEach(sqlInfo => {

0 commit comments

Comments
 (0)