Skip to content

Commit 717a50d

Browse files
authored
Support FORMAT JSON for JSON_TABLE in Oracle (#7578)
1 parent 1a10806 commit 717a50d

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

sqlglot/expressions/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,7 @@ class JSONColumnDef(Expression):
19561956
"path": False,
19571957
"nested_schema": False,
19581958
"ordinality": False,
1959+
"format_json": False,
19591960
}
19601961

19611962

sqlglot/generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3609,9 +3609,10 @@ def jsoncolumndef_sql(self, expression: exp.JSONColumnDef) -> str:
36093609
this = self.sql(expression, "this")
36103610
kind = self.sql(expression, "kind")
36113611
kind = f" {kind}" if kind else ""
3612+
format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
36123613

36133614
ordinality = " FOR ORDINALITY" if expression.args.get("ordinality") else ""
3614-
return f"{this}{kind}{path}{ordinality}"
3615+
return f"{this}{kind}{format_json}{path}{ordinality}"
36153616

36163617
def jsonschema_sql(self, expression: exp.JSONSchema) -> str:
36173618
return self.func("COLUMNS", *expression.expressions)

sqlglot/parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7931,12 +7931,18 @@ def _parse_json_column_def(self) -> exp.JSONColumnDef:
79317931
kind = None
79327932
nested = True
79337933

7934+
format_json = self._match_text_seq("FORMAT", "JSON")
79347935
path = self._match_text_seq("PATH") and self._parse_string()
79357936
nested_schema = nested and self._parse_json_schema()
79367937

79377938
return self.expression(
79387939
exp.JSONColumnDef(
7939-
this=this, kind=kind, path=path, nested_schema=nested_schema, ordinality=ordinality
7940+
this=this,
7941+
kind=kind,
7942+
path=path,
7943+
nested_schema=nested_schema,
7944+
ordinality=ordinality,
7945+
format_json=format_json,
79407946
)
79417947
)
79427948

tests/dialects/test_oracle.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ def test_json_table(self):
580580
)) src""",
581581
pretty=True,
582582
)
583+
self.validate_identity(
584+
"SELECT * FROM JSON_TABLE(my_doc, '$.data[*]' COLUMNS(NAME VARCHAR2(200) PATH '$.name', DATA CLOB FORMAT JSON PATH '$.data')) j"
585+
)
583586
self.validate_identity("CONVERT('foo', 'dst')")
584587
self.validate_identity("CONVERT('foo', 'dst', 'src')")
585588

0 commit comments

Comments
 (0)