Skip to content

Commit

Permalink
[sql] add pretty_print() function
Browse files Browse the repository at this point in the history
  • Loading branch information
tstack committed Jun 6, 2024
1 parent edf1eae commit 223b6af
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 217 deletions.
121 changes: 75 additions & 46 deletions src/internals/sql-ref.rst

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions src/string-extension-functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "libbase64.h"
#include "mapbox/variant.hpp"
#include "pcrepp/pcre2pp.hh"
#include "pretty_printer.hh"
#include "safe/safe.h"
#include "scn/scn.h"
#include "spookyhash/SpookyV2.h"
Expand Down Expand Up @@ -890,6 +891,18 @@ sql_humanize_id(string_fragment id)
id);
}

static std::string
sql_pretty_print(string_fragment in)
{
data_scanner ds(in);
pretty_printer pp(&ds, {});
attr_line_t retval;

pp.append_to(retval);

return std::move(retval.get_string());
}

int
string_extension_functions(struct FuncDef** basic_funcs,
struct FuncDefAgg** agg_funcs)
Expand Down Expand Up @@ -1246,6 +1259,21 @@ string_extension_functions(struct FuncDef** basic_funcs,
"\"example.com\"}')",
})),

sqlite_func_adapter<decltype(&sql_pretty_print), sql_pretty_print>::
builder(
help_text("pretty_print", "Pretty-print the given string")
.sql_function()
.with_prql_path({"text", "pretty"})
.with_parameter(help_text("str", "The string to format"))
.with_tags({"string"})
.with_example({
"To pretty-print the string "
"'{\"scheme\": \"https\", \"host\": \"example.com\"}'",
"SELECT "
"pretty_print('{\"scheme\": \"https\", \"host\": "
"\"example.com\"}')",
})),

{nullptr},
};

Expand Down
2 changes: 2 additions & 0 deletions test/expected/expected.am
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,8 @@ EXPECTED_FILES = \
$(srcdir)/%reldir%/test_sql_str_func.sh_e68167bf5edc7a7b1defd06bdfb694ffa8b00df2.out \
$(srcdir)/%reldir%/test_sql_str_func.sh_ec939e82da809965c61f1c00f68d7afaa4a88382.err \
$(srcdir)/%reldir%/test_sql_str_func.sh_ec939e82da809965c61f1c00f68d7afaa4a88382.out \
$(srcdir)/%reldir%/test_sql_str_func.sh_fa059d4738b2b5bd668360b1287f0245e452b9ef.err \
$(srcdir)/%reldir%/test_sql_str_func.sh_fa059d4738b2b5bd668360b1287f0245e452b9ef.out \
$(srcdir)/%reldir%/test_sql_time_func.sh_028e99419eb1ac80b03b36148ef1d4ae1c38c44c.err \
$(srcdir)/%reldir%/test_sql_time_func.sh_028e99419eb1ac80b03b36148ef1d4ae1c38c44c.out \
$(srcdir)/%reldir%/test_sql_time_func.sh_123c85ff1178743f5cb78efeaf98b637bcbe55ff.err \
Expand Down
373 changes: 202 additions & 171 deletions test/expected/test_cmds.sh_b6a3bb78e9d60e5e1f5ce5b18e40d2f1662707ab.out

Large diffs are not rendered by default.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Row 0:
Column pretty_print('{a: 1, b:2}'): {
a: 1,
b:2
}
2 changes: 2 additions & 0 deletions test/test_sql_str_func.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ run_cap_test env TEST_COMMENT=unparse_url11 ./drive_sql <<'EOF'
SELECT unparse_url('{}')
EOF

run_cap_test ./drive_sql "SELECT pretty_print('{a: 1, b:2}')"

run_cap_test ${lnav_test} -n \
-c ';SELECT log_body, extract(log_body) from vmw_log' \
-c ':write-json-to -' \
Expand Down

0 comments on commit 223b6af

Please sign in to comment.