Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
vdimir committed Dec 28, 2023
1 parent 440b7bf commit 84893f5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
14 changes: 10 additions & 4 deletions programs/format/Format.cpp
Expand Up @@ -3,6 +3,7 @@
#include <string_view>
#include <boost/program_options.hpp>

#include <IO/copyData.h>
#include <IO/ReadBufferFromFileDescriptor.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromFileDescriptor.h>
Expand All @@ -13,6 +14,7 @@
#include <Parsers/obfuscateQueries.h>
#include <Parsers/parseQuery.h>
#include <Common/ErrorCodes.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/TerminalSize.h>

#include <Interpreters/Context.h>
Expand All @@ -28,9 +30,8 @@
#include <DataTypes/DataTypeFactory.h>
#include <Formats/FormatFactory.h>
#include <Formats/registerFormats.h>

#include <Processors/Transforms/getSourceFromASTInsertQuery.h>
#include <IO/copyData.h>


namespace DB::ErrorCodes
{
Expand Down Expand Up @@ -236,8 +237,10 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
if (auto * insert_query = res->as<ASTInsertQuery>(); insert_query && insert_query->data)
{
if ("Values" != insert_query->format)
throw Exception(DB::ErrorCodes::NOT_IMPLEMENTED,
"Can't format INSERT query with data format '{}'", insert_query->format);
throw Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "Can't format INSERT query with data format '{}'", insert_query->format);

/// Reset format to default to have `INSERT INTO table VALUES` instead of `INSERT INTO table VALUES FORMAT Values`
insert_query->format = {};

/// We assume that data ends with a newline character (same as client does)
const char * this_query_end = find_first_symbols<'\n'>(insert_query->data, end);
Expand All @@ -264,6 +267,9 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
String res_string = str_buf.str();
const char * s_pos = res_string.data();
const char * s_end = s_pos + res_string.size();
/// remove trailing spaces
while (s_end > s_pos && isWhitespaceASCIIOneLine(*(s_end - 1)))
--s_end;
WriteBufferFromOStream res_cout(std::cout, 4096);
/// For multiline queries we print ';' at new line,
/// but for single line queries we print ';' at the same line
Expand Down
@@ -1,5 +1,4 @@
SELECT 1
;
SELECT 1;

SELECT 1
UNION ALL
Expand All @@ -10,8 +9,7 @@ UNION ALL
)
;

SELECT 1
;
SELECT 1;

SELECT 1
UNION ALL
Expand All @@ -22,4 +20,6 @@ UNION ALL
)
;

INSERT INTO t VALUES (1);

OK
4 changes: 3 additions & 1 deletion tests/queries/0_stateless/01753_fix_clickhouse_format.sh
Expand Up @@ -8,4 +8,6 @@ echo "select 1; select 1 union all (select 1 union distinct select 1); " | $CL

echo "select 1; select 1 union all (select 1 union distinct select 1); -- comment " | $CLICKHOUSE_FORMAT -n;

echo "insert into t values (1); " | $CLICKHOUSE_FORMAT -n 2>&1 \ | grep -F -q "Code: 578" && echo 'OK' || echo 'FAIL'
echo "insert into t values (1); " | $CLICKHOUSE_FORMAT -n

echo 'insert into t format JSONEachRow {"a":1};' | $CLICKHOUSE_FORMAT -n 2>&1 \ | grep -F -q "NOT_IMPLEMENTED" && echo 'OK' || echo 'FAIL'
@@ -1,7 +1,7 @@
[multi] insert into foo settings max_threads=1
Syntax error (query): failed at position 40 (end of query):
[multi] insert into foo format tsv settings max_threads=1
Can't format ASTInsertQuery with data, since data will be lost.
NOT_IMPLEMENTED
[multi] insert into foo format tsv settings max_threads=1
INSERT INTO foo
SETTINGS max_threads = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/queries/0_stateless/02263_format_insert_settings.sh
Expand Up @@ -25,7 +25,7 @@ function run_format_both()
run_format 'insert into foo settings max_threads=1' |& grep --max-count 2 --only-matching -e "Syntax error (query): failed at position .* (end of query):" -e '^\[.*$'

# compatibility
run_format 'insert into foo format tsv settings max_threads=1' |& grep --max-count 2 --only-matching -e "Can't format ASTInsertQuery with data, since data will be lost." -e '^\[.*$'
run_format 'insert into foo format tsv settings max_threads=1' |& grep --max-count 2 --only-matching -e "NOT_IMPLEMENTED" -e '^\[.*$'
run_format_both 'insert into foo format tsv settings max_threads=1' --allow_settings_after_format_in_insert
run_format 'insert into foo settings max_threads=1 format tsv settings max_threads=1' --allow_settings_after_format_in_insert |& grep --max-count 2 --only-matching -e "You have SETTINGS before and after FORMAT" -e '^\[.*$'

Expand Down
12 changes: 7 additions & 5 deletions tests/queries/0_stateless/02946_format_values.reference
@@ -1,10 +1,10 @@
INSERT INTO table1 FORMAT Values (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td')
INSERT INTO table1 VALUES (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td')
======================================
SELECT a
FROM table1
;

INSERT INTO table1 FORMAT Values (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td');
INSERT INTO table1 VALUES (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td');

SELECT b
FROM table1
Expand All @@ -17,7 +17,7 @@ FROM table1
;

-- some insert query
INSERT INTO table1 FORMAT Values (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td');
INSERT INTO table1 VALUES (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td');

-- more comments
-- in a row
Expand Down Expand Up @@ -135,5 +135,7 @@ FROM
)
SELECT b, c, d, e, f FROM (SELECT b, c, d, e, f FROM table1)
======================================
BAD_ARGUMENTS
BAD_ARGUMENTS
Option 'max_line_length' must be less than 256.
2
Options 'oneline' and 'max_line_length' are mutually exclusive.
2
4 changes: 2 additions & 2 deletions tests/queries/0_stateless/02946_format_values.sh
Expand Up @@ -69,5 +69,5 @@ echo "select b, c, d, e, f from ( select b, c, d, e, f from table1 )" | ${CLICKH

echo "======================================"

echo "select 1" | ${CLICKHOUSE_FORMAT} --comments --max_line_length=260 2>&1 | grep -o 'BAD_ARGUMENTS' ||:
echo "select 1" | ${CLICKHOUSE_FORMAT} --comments --max_line_length=120 --oneline 2>&1 | grep -o 'BAD_ARGUMENTS' ||:
{ echo "select 1" | ${CLICKHOUSE_FORMAT} --comments --max_line_length=260 2>&1; echo $?; }
{ echo "select 1" | ${CLICKHOUSE_FORMAT} --comments --max_line_length=120 --oneline 2>&1; echo $?; }

0 comments on commit 84893f5

Please sign in to comment.