Skip to content

Commit

Permalink
refactor: change jsonb to binary_expr in all dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Aug 4, 2024
1 parent 99cd4ae commit b29a452
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 469 deletions.
51 changes: 12 additions & 39 deletions pegjs/db2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,6 @@ comparison_op_right
/ between_op_right
/ is_op_right
/ like_op_right
/ jsonb_or_json_op_right

arithmetic_op_right
= l:(__ arithmetic_comparison_operator __ additive_expr)+ {
Expand Down Expand Up @@ -1782,32 +1781,6 @@ in_op_right
return { op: op, right: e };
}

jsonb_or_json_op_right
= s: ('@>' / '<@') __ e:column_list_item {
// => { type: 'jsonb'; op: string; right: column_list_item }
return {
type: 'jsonb',
op: s,
right: e
}
}
/ s: ('?|' / '?&' / '?' / '#-') __ e:literal {
// => { type: 'jsonb'; op: string; right: literal }
return {
type: 'jsonb',
op: s,
right: e
}
}
/ s: ('#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ e:literal {
// => { type: 'json'; op: string; right: literal }
return {
type: 'json',
op: s,
right: e
}
}

additive_expr
= head:multiplicative_expr
tail:(__ additive_operator __ multiplicative_expr)* {
Expand Down Expand Up @@ -1843,7 +1816,7 @@ primary
/ var_decl

unary_expr_or_primary
= primary
= jsonb_expr
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
Expand All @@ -1852,18 +1825,18 @@ unary_expr_or_primary
unary_operator
= '!' / '-' / '+' / '~'

column_ref
= tbl:(ident __ DOT __)? col:column __ jo:jsonb_or_json_op_right+ {
const tableName = tbl && tbl[0] || null
columnList.add(`select::${tableName}::${col}`);
return {
type: 'column_ref',
table: tableName,
column: col,
jsonb: jo,
};
jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ tbl:ident __ DOT __ col:column_without_kw {

column_ref
= tbl:ident __ DOT __ col:column_without_kw {
columnList.add(`select::${tbl}::${col}`);
return {
type: 'column_ref',
Expand Down
50 changes: 11 additions & 39 deletions pegjs/flinksql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,6 @@ comparison_op_right
/ is_op_right
/ like_op_right
/ similar_to_op_right
/ jsonb_or_json_op_right

arithmetic_op_right
= l:(__ arithmetic_comparison_operator __ additive_expr)+ {
Expand Down Expand Up @@ -2576,32 +2575,6 @@ exists_op_right
return { op: op, right: l };
}

jsonb_or_json_op_right
= s: ('@>' / '<@') __ e:column_list_item {
// => { type: 'jsonb'; op: string; right: column_list_item }
return {
type: 'jsonb',
op: s,
right: e
}
}
/ s: ('?|' / '?&' / '?' / '#-') __ e:literal {
// => { type: 'jsonb'; op: string; right: literal }
return {
type: 'jsonb',
op: s,
right: e
}
}
/ s: ('#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ e:literal {
// => { type: 'json'; op: string; right: literal }
return {
type: 'json',
op: s,
right: e
}
}

additive_expr
= head:multiplicative_expr
tail:(__ additive_operator __ multiplicative_expr)* {
Expand Down Expand Up @@ -2646,7 +2619,7 @@ primary
}

unary_expr_or_primary
= primary
= jsonb_expr
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
Expand All @@ -2655,6 +2628,16 @@ unary_expr_or_primary
unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}

map_expr_item
= k:literal_string __ COMMA __ v:ident_without_kw_type {
columnList.add(`select::null::${v.value}`);
Expand Down Expand Up @@ -2698,17 +2681,6 @@ column_ref
column: '*'
}
}
/ tbl:(ident __ DOT)? __ col:column __ jo:jsonb_or_json_op_right+ {
// => IGNORE
const tableName = tbl && tbl[0] || null
columnList.add(`select::${tableName}::${col}`);
return {
type: 'column_ref',
table: tableName,
column: col,
jsonb: jo,
};
}
/ tbl:ident __ DOT __ col:column {
/* => {
type: 'column_ref';
Expand Down
19 changes: 3 additions & 16 deletions pegjs/mariadb.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3106,27 +3106,14 @@ concat_separator
}

count_arg
= e:star_expr { return { expr: e }; }
/ d:KW_DISTINCT? __ LPAREN __ c:expr __ RPAREN tail:(__ (KW_AND / KW_OR) __ expr)* __ or:order_by_clause? __ s:concat_separator? {
const len = tail.length
let result = c
result.parentheses = true
for (let i = 0; i < len; ++i) {
result = createBinaryExpr(tail[i][1], result, tail[i][3])
}
return {
distinct: d,
expr: result,
orderby: or,
separator: s
};
}
= e:star_expr { return { expr: e, ...getLocationObject() }; }
/ d:KW_DISTINCT? __ c:or_and_where_expr __ or:order_by_clause? __ s:concat_separator? {
return {
distinct: d,
expr: c,
orderby: or,
separator: s
separator: s,
...getLocationObject()
};
}

Expand Down
70 changes: 16 additions & 54 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -3034,29 +3034,6 @@ in_op_right
return { op: op, right: e };
}

jsonb_or_json_op_right
= s: ('@>' / '<@') __ e:column_list_item {
return {
type: 'jsonb',
op: s,
right: e
}
}
/ s: ('?|' / '?&' / '?' / '#-') __ e:literal {
return {
type: 'jsonb',
op: s,
right: e
}
}
/ s: ('#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ e:literal {
return {
type: 'json',
op: s,
right: e
}
}

additive_expr
= head: multiplicative_expr
tail:(__ additive_operator __ multiplicative_expr)* {
Expand All @@ -3081,7 +3058,7 @@ multiplicative_operator
/ '&' / '>>' / '<<' / '^' / '|'

unary_expr_or_primary
= primary
= jsonb_expr
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
// if (op === '!') op = 'NOT'
return createUnaryExpr(op, tail[1])
Expand All @@ -3090,6 +3067,16 @@ unary_expr_or_primary
unary_operator
= '!' / '-' / '+' / '~'

jsonb_expr
= head:primary tail:(__ ('?|' / '?&' / '?' / '#-' / '#>>' / '#>' / DOUBLE_ARROW / SINGLE_ARROW) __ literal)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}
/ head:primary tail:(__ ('@>' / '<@') __ column_list_item)* {
if (!tail || tail.length === 0) return head
return createBinaryExprChain(head, tail)
}

primary
= aggr_func
/ fulltext_search
Expand All @@ -3113,19 +3100,8 @@ primary
}

column_ref
= tbl:(ident __ DOT __)? col:column __ jo:jsonb_or_json_op_right+ {
const tableName = tbl && tbl[0] || null
columnList.add(`select::${tableName}::${col}`);
return {
type: 'column_ref',
table: tableName,
column: col,
jsonb: jo,
...getLocationObject(),
};
}
/ db:(ident_name / backticks_quoted_ident) __ DOT __ tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
columnList.add(`select::${db}::${tbl}::${col}`);
= db:(ident_name / backticks_quoted_ident) __ DOT __ tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
columnList.add(`select::${typeof db === 'object' ? db.value : db}::${typeof tbl === 'object' ? tbl.value : tbl}::${col}`);
return {
type: 'column_ref',
db: db,
Expand All @@ -3135,7 +3111,7 @@ column_ref
};
}
/ tbl:(ident_name / backticks_quoted_ident) __ DOT __ col:column_without_kw {
columnList.add(`select::${tbl}::${col}`);
columnList.add(`select::${typeof tbl === 'object' ? tbl.value : tbl}::${col}`);
return {
type: 'column_ref',
table: tbl,
Expand Down Expand Up @@ -3412,27 +3388,13 @@ concat_separator

count_arg
= e:star_expr { return { expr: e, ...getLocationObject() }; }
/ d:KW_DISTINCT? __ LPAREN __ c:expr __ RPAREN tail:(__ (KW_AND / KW_OR) __ expr)* __ or:order_by_clause? __ s:concat_separator? {
const len = tail.length
let result = c
result.parentheses = true
for (let i = 0; i < len; ++i) {
result = createBinaryExpr(tail[i][1], result, tail[i][3])
}
return {
distinct: d,
expr: result,
orderby: or,
separator: s,
...getLocationObject()
};
}
/ d:KW_DISTINCT? __ c:or_and_where_expr __ or:order_by_clause? __ s:concat_separator? {
return {
distinct: d,
expr: c,
orderby: or,
separator: s
separator: s,
...getLocationObject()
};
}

Expand Down
Loading

0 comments on commit b29a452

Please sign in to comment.