Skip to content

Commit

Permalink
Update test case and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
takegue committed Nov 4, 2023
1 parent fa3895a commit 482fd6b
Showing 1 changed file with 76 additions and 13 deletions.
89 changes: 76 additions & 13 deletions bigquery/@default/v0/@routines/pp_sql/ddl.sql
Original file line number Diff line number Diff line change
@@ -1,24 +1,65 @@
create or replace function `v0.pp_sql`(
table_like_object any type
)
returns string
options(
description="""Pretty print a table-like object as a SQL format representation
Arguments:
===
- table_like_object: array<any type>
- options: JSON
* max_cue_rows: max number of rows to infer the type. default: 3
* unknown_value: filling value if type inference is not working. default: 'UNKNOWN'
"""
Returns:
===
valid SQL string to generate table values
Examples:
===
```sql
with cte as (
select *
from unnest(
array<struct<
int int64
, str string
, float float64
, boolean bool
, ts timestamp
, dt datetime
, d date
, json json
, arr array<int64>
, record struct<int int64>>
>[
(1, 'hoge', 3.21234556, null, null, null, null, null, null, null),
(null, string(null), float64(null), bool(null), '2023-01-01', '2023-01-01', '2023-01-01', to_json(null), [1, 2], struct(1))
]
)
)
select
v0.pp_table(array_agg(cte)),
from cte
--> /*
# AUTO-GENERATED
select *
from unnest(array<struct<int INT64, str STRING, float FLOAT64, boolean INT64, ts TIMESTAMP, dt DATETIME, d DATE, json JSON, arr ARRAY<INT64>, record STRUCT<INT64>>>[
(1, "hoge", 3.21234556, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(NULL, NULL, NULL, NULL, TIMESTAMP "2023-01-01 00:00:00+00", DATETIME "2023-01-01 00:00:00", DATE "2023-01-01", JSON "null", [1, 2], STRUCT(1))
])
*/
```
"""
)
as ((
select as value
header
from unnest([struct(
'int64' as unknown_value
-- 'INT64' as unknown_value
'INT64' as unknown_value
, array(
select as struct
`v0.ztypeof_columns`(format('%T', _sample), to_json_string(_sample)) as value
Expand All @@ -28,11 +69,11 @@ as ((
) as _signatures
)])
left join unnest([struct(
format("""-- AUTO-GENERATED SQL
select *
from unnest(array<struct<%t>>[
%s
])
format("""#AUTO-GENERATED
select *
from unnest(array<struct<%t>>[
%s
])
"""
, array_to_string(
array(
Expand Down Expand Up @@ -64,10 +105,10 @@ from unnest(array<struct<%t>>[
)])
))
;

begin
execute immediate (
with cte as (
with cte as (
select *
from unnest([
(null, string(null), float64(null), bool(null), '2023-01-01', '2023-01-01', '2023-01-01', to_json(null), [1, 2], struct(1)),
Expand All @@ -77,4 +118,26 @@ begin

select as value `v0.pp_sql`(array_agg(cte)) from cte
);

execute immediate (
with cte as (
select *
from unnest(array<struct<
int int64
, str string
, float float64
, boolean bool
, ts timestamp
, dt datetime
, d date
, json json
, arr array<int64>
, record struct<int int64>>
>[
(1, 'hoge', 3.21234556, null, null, null, null, null, null, null),
(null, string(null), float64(null), bool(null), '2023-01-01', '2023-01-01', '2023-01-01', to_json(null), [1, 2], struct(1))
])
)
select as value `v0.pp_sql`(array_agg(cte)) from cte
);
end;

0 comments on commit 482fd6b

Please sign in to comment.