|
5 | 5 | #include "textplot_qr.hpp" |
6 | 6 | #include "duckdb.hpp" |
7 | 7 | #include "duckdb/function/scalar_function.hpp" |
| 8 | +#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp" |
8 | 9 | #include "query_farm_telemetry.hpp" |
9 | 10 |
|
10 | 11 | namespace duckdb { |
11 | 12 |
|
12 | 13 | static void LoadInternal(ExtensionLoader &loader) { |
13 | | - auto bar_function = ScalarFunction("tp_bar", {LogicalType::DOUBLE}, LogicalType::VARCHAR, TextplotBar, |
14 | | - TextplotBarBind, nullptr, nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
15 | | - loader.RegisterFunction(bar_function); |
16 | | - |
17 | | - auto qr_function = ScalarFunction("tp_qr", {LogicalType::VARCHAR}, LogicalType::VARCHAR, TextplotQR, TextplotQRBind, |
18 | | - nullptr, nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
19 | | - loader.RegisterFunction(qr_function); |
20 | | - |
21 | | - // Lets register a density function. |
22 | | - auto density_function = |
23 | | - ScalarFunction("tp_density", {LogicalType::LIST(LogicalType::DOUBLE)}, LogicalType::VARCHAR, TextplotDensity, |
24 | | - TextplotDensityBind, nullptr, nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
25 | | - loader.RegisterFunction(density_function); |
26 | | - |
27 | | - auto sparkline_function = ScalarFunction("tp_sparkline", {LogicalType::LIST(LogicalType::DOUBLE)}, |
28 | | - LogicalType::VARCHAR, TextplotSparkline, TextplotSparklineBind, nullptr, |
29 | | - nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
30 | | - loader.RegisterFunction(sparkline_function); |
| 14 | + // tp_bar: Horizontal bar charts with thresholds and colors |
| 15 | + { |
| 16 | + auto bar_function = ScalarFunction("tp_bar", {LogicalType::DOUBLE}, LogicalType::VARCHAR, TextplotBar, |
| 17 | + TextplotBarBind, nullptr, nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
| 18 | + CreateScalarFunctionInfo info(std::move(bar_function)); |
| 19 | + |
| 20 | + FunctionDescription desc; |
| 21 | + desc.description = "Creates a horizontal bar chart visualization from a numeric value. " |
| 22 | + "Supports customizable width, colors, shapes (square/circle/heart), and color thresholds."; |
| 23 | + desc.parameter_names = {"value", "min", "max", "width", "on", "off", "on_color", "off_color", "shape", |
| 24 | + "filled", "thresholds"}; |
| 25 | + desc.examples = {"tp_bar(0.75)", |
| 26 | + "tp_bar(score, min := 0, max := 100, width := 20)", |
| 27 | + "tp_bar(value, on := '#', off := '-', width := 10)", |
| 28 | + "tp_bar(pct, shape := 'heart', on_color := 'red')", |
| 29 | + "tp_bar(temp, thresholds := [{'threshold': 80, 'color': 'red'}, " |
| 30 | + "{'threshold': 50, 'color': 'yellow'}])"}; |
| 31 | + info.descriptions.push_back(std::move(desc)); |
| 32 | + |
| 33 | + info.on_conflict = OnCreateConflict::ALTER_ON_CONFLICT; |
| 34 | + loader.RegisterFunction(std::move(info)); |
| 35 | + } |
| 36 | + |
| 37 | + // tp_qr: QR code generation |
| 38 | + { |
| 39 | + auto qr_function = ScalarFunction("tp_qr", {LogicalType::VARCHAR}, LogicalType::VARCHAR, TextplotQR, |
| 40 | + TextplotQRBind, nullptr, nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
| 41 | + CreateScalarFunctionInfo info(std::move(qr_function)); |
| 42 | + |
| 43 | + FunctionDescription desc; |
| 44 | + desc.description = "Generates a text-based QR code from a string or blob. " |
| 45 | + "Supports configurable error correction levels and custom on/off characters."; |
| 46 | + desc.parameter_names = {"data", "ecc", "on", "off"}; |
| 47 | + desc.examples = {"tp_qr('https://duckdb.org')", |
| 48 | + "tp_qr(url, ecc := 'high')", |
| 49 | + "tp_qr(message, on := '##', off := ' ')"}; |
| 50 | + info.descriptions.push_back(std::move(desc)); |
| 51 | + |
| 52 | + info.on_conflict = OnCreateConflict::ALTER_ON_CONFLICT; |
| 53 | + loader.RegisterFunction(std::move(info)); |
| 54 | + } |
| 55 | + |
| 56 | + // tp_density: Density plots/histograms from arrays |
| 57 | + { |
| 58 | + auto density_function = |
| 59 | + ScalarFunction("tp_density", {LogicalType::LIST(LogicalType::DOUBLE)}, LogicalType::VARCHAR, TextplotDensity, |
| 60 | + TextplotDensityBind, nullptr, nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
| 61 | + CreateScalarFunctionInfo info(std::move(density_function)); |
| 62 | + |
| 63 | + FunctionDescription desc; |
| 64 | + desc.description = "Creates a density plot (histogram) visualization from an array of numeric values. " |
| 65 | + "Supports multiple styles: shaded, dots, ascii, height, circles, safety, rainbow_circle, " |
| 66 | + "rainbow_square, moon, sparse, and white."; |
| 67 | + desc.parameter_names = {"values", "width", "style", "marker", "graph_chars"}; |
| 68 | + desc.examples = {"tp_density(list(value))", |
| 69 | + "tp_density(array_agg(score), width := 40)", |
| 70 | + "tp_density(data, style := 'height')", |
| 71 | + "tp_density(temps, style := 'rainbow_square', width := 30)"}; |
| 72 | + info.descriptions.push_back(std::move(desc)); |
| 73 | + |
| 74 | + info.on_conflict = OnCreateConflict::ALTER_ON_CONFLICT; |
| 75 | + loader.RegisterFunction(std::move(info)); |
| 76 | + } |
| 77 | + |
| 78 | + // tp_sparkline: Compact trend lines with multiple modes |
| 79 | + { |
| 80 | + auto sparkline_function = ScalarFunction("tp_sparkline", {LogicalType::LIST(LogicalType::DOUBLE)}, |
| 81 | + LogicalType::VARCHAR, TextplotSparkline, TextplotSparklineBind, nullptr, |
| 82 | + nullptr, nullptr, LogicalType(LogicalTypeId::ANY)); |
| 83 | + CreateScalarFunctionInfo info(std::move(sparkline_function)); |
| 84 | + |
| 85 | + FunctionDescription desc; |
| 86 | + desc.description = "Creates a sparkline visualization from an array of numeric values. " |
| 87 | + "Supports three modes: 'absolute' (height-based), 'delta' (up/down/same direction), " |
| 88 | + "and 'trend' (direction with magnitude). Multiple themes available per mode."; |
| 89 | + desc.parameter_names = {"values", "width", "mode", "theme"}; |
| 90 | + desc.examples = {"tp_sparkline(list(value))", |
| 91 | + "tp_sparkline(array_agg(price), width := 20)", |
| 92 | + "tp_sparkline(data, mode := 'delta', theme := 'arrows')", |
| 93 | + "tp_sparkline(temps, mode := 'absolute', theme := 'utf8_blocks')", |
| 94 | + "tp_sparkline(stocks, mode := 'trend', theme := 'faces')"}; |
| 95 | + info.descriptions.push_back(std::move(desc)); |
| 96 | + |
| 97 | + info.on_conflict = OnCreateConflict::ALTER_ON_CONFLICT; |
| 98 | + loader.RegisterFunction(std::move(info)); |
| 99 | + } |
31 | 100 |
|
32 | 101 | QueryFarmSendTelemetry(loader, "textplot", TextplotExtension().Version()); |
33 | 102 | } |
|
0 commit comments