Merge pull request #61797 from ClickHouse/output-format-in-client

Support for `--output-format` in client
This commit is contained in:
Alexey Milovidov 2024-03-23 06:47:32 +03:00 committed by GitHub
commit b84772a6a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 9 deletions

View File

@ -1,4 +1,3 @@
#include <boost/algorithm/string/join.hpp>
#include <cstdlib>
#include <fcntl.h>
#include <map>
@ -7,7 +6,6 @@
#include <memory>
#include <optional>
#include <Common/ThreadStatus.h>
#include <Common/scope_guard_safe.h>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <filesystem>
@ -45,8 +43,6 @@
#include <Processors/Transforms/getSourceFromASTInsertQuery.h>
#include <Interpreters/InterpreterSetQuery.h>
#include <Functions/registerFunctions.h>
#include <AggregateFunctions/registerAggregateFunctions.h>
#include <Formats/registerFormats.h>
@ -1180,7 +1176,7 @@ void Client::processConfig()
pager = config().getString("pager", "");
is_default_format = !config().has("vertical") && !config().has("format");
is_default_format = !config().has("vertical") && !config().has("output-format") && !config().has("format");
if (is_default_format && checkIfStdoutIsRegularFile())
{
is_default_format = false;
@ -1189,9 +1185,13 @@ void Client::processConfig()
format = format_from_file_name ? *format_from_file_name : "TabSeparated";
}
else if (config().has("vertical"))
format = config().getString("format", "Vertical");
{
format = config().getString("output-format", config().getString("format", "Vertical"));
}
else
format = config().getString("format", is_interactive ? "PrettyCompact" : "TabSeparated");
{
format = config().getString("output-format", config().getString("format", is_interactive ? "PrettyCompact" : "TabSeparated"));
}
format_max_block_size = config().getUInt64("format_max_block_size",
global_context->getSettingsRef().max_block_size);

View File

@ -819,7 +819,6 @@ void LocalServer::addOptions(OptionsDescription & options_description)
("file,F", po::value<std::string>(), "path to file with data of the initial table (stdin if not specified)")
("input-format", po::value<std::string>(), "input format of the initial table data")
("output-format", po::value<std::string>(), "default output format")
("logger.console", po::value<bool>()->implicit_value(true), "Log to console")
("logger.log", po::value<std::string>(), "Log file name")

View File

@ -2892,7 +2892,9 @@ void ClientBase::init(int argc, char ** argv)
("suggestion_limit", po::value<int>()->default_value(10000), "Suggestion limit for how many databases, tables and columns to fetch.")
("format,f", po::value<std::string>(), "default output format")
("format,f", po::value<std::string>(), "default output format (and input format for clickhouse-local)")
("output-format", po::value<std::string>(), "default output format (this option has preference over --format)")
("vertical,E", "vertical output format, same as --format=Vertical or FORMAT Vertical or \\G at end of command")
("highlight", po::value<bool>()->default_value(true), "enable or disable basic syntax highlight in interactive command line")
@ -2975,6 +2977,8 @@ void ClientBase::init(int argc, char ** argv)
config().setBool("ignore-error", true);
if (options.count("format"))
config().setString("format", options["format"].as<std::string>());
if (options.count("output-format"))
config().setString("output-format", options["output-format"].as<std::string>());
if (options.count("vertical"))
config().setBool("vertical", true);
if (options.count("stacktrace"))

View File

@ -0,0 +1,12 @@
| x |
|:-|
| Hello, world |
| x |
|:-|
| Hello, world |
| x |
|:-|
| Hello, world |
| x |
|:-|
| Hello, world |

View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
# clickhouse-local has --input-format and --output-format parameters,
# and also the --format parameter which is the default for both input and output formats, but has less preference.
# clickhouse-client does not have the --input-format parameter.
# However, it accepts both --format and --output-format for convenience.
${CLICKHOUSE_LOCAL} --output-format Markdown --query "SELECT 'Hello, world' AS x"
${CLICKHOUSE_CLIENT} --output-format Markdown --query "SELECT 'Hello, world' AS x"
${CLICKHOUSE_LOCAL} --format Markdown --query "SELECT 'Hello, world' AS x"
${CLICKHOUSE_CLIENT} --format Markdown --query "SELECT 'Hello, world' AS x"