This is an automated email from the ASF dual-hosted git repository. snuyanzin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/master by this push: new af25884103e [FLINK-24911][table] Enable line numbers in SQL Client af25884103e is described below commit af25884103ee842b9c7bf8c47198b058f85d69f9 Author: Sergey Nuyanzin <snuyan...@gmail.com> AuthorDate: Wed Jun 28 21:58:44 2023 +0200 [FLINK-24911][table] Enable line numbers in SQL Client --- .../generated/sql_client_configuration.html | 6 +++ .../apache/flink/table/client/cli/CliClient.java | 21 ++++++-- .../table/client/config/SqlClientOptions.java | 8 +++ .../flink/table/client/cli/CliClientITCase.java | 63 ++++++++++++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) diff --git a/docs/layouts/shortcodes/generated/sql_client_configuration.html b/docs/layouts/shortcodes/generated/sql_client_configuration.html index d53c4307abf..ae132665e45 100644 --- a/docs/layouts/shortcodes/generated/sql_client_configuration.html +++ b/docs/layouts/shortcodes/generated/sql_client_configuration.html @@ -20,6 +20,12 @@ <td>Integer</td> <td>Deprecated, please use table.display.max-column-width instead. When printing the query results, this parameter determines the number of characters shown on screen before truncating. This only applies to columns with variable-length types (e.g. STRING) in streaming mode. Fixed-length types and all types in batch mode are printed using a deterministic column width.</td> </tr> + <tr> + <td><h5>sql-client.display.show-line-numbers</h5><br> <span class="label label-primary">Batch</span> <span class="label label-primary">Streaming</span></td> + <td style="word-wrap: break-word;">false</td> + <td>Boolean</td> + <td>Determines whether there should be shown line numbers in multiline SQL or not.</td> + </tr> <tr> <td><h5>sql-client.execution.max-table-result.rows</h5><br> <span class="label label-primary">Batch</span> <span class="label label-primary">Streaming</span></td> <td style="word-wrap: break-word;">1000000</td> diff --git a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/CliClient.java b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/CliClient.java index c1a0cf00041..0acc4d5afd5 100644 --- a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/CliClient.java +++ b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/CliClient.java @@ -32,6 +32,7 @@ import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; import org.jline.reader.MaskingCallback; import org.jline.reader.UserInterruptException; +import org.jline.reader.impl.LineReaderImpl; import org.jline.terminal.Terminal; import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; @@ -63,6 +64,7 @@ public class CliClient implements AutoCloseable { .style(AttributedStyle.DEFAULT) .append("> ") .toAnsi(); + private static final String SHOW_LINE_NUMBERS_PATTERN = "%N%M> "; private final Executor executor; @@ -109,9 +111,14 @@ public class CliClient implements AutoCloseable { /** Opens the interactive CLI shell. */ public void executeInInteractiveMode() { + executeInInteractiveMode(null); + } + + @VisibleForTesting + void executeInInteractiveMode(LineReader lineReader) { try { terminal = terminalFactory.get(); - executeInteractive(); + executeInteractive(lineReader); } finally { closeTerminal(); } @@ -157,7 +164,7 @@ public class CliClient implements AutoCloseable { * Execute statement from the user input and prints status information and/or errors on the * terminal. */ - private void executeInteractive() { + private void executeInteractive(LineReader inputLineReader) { // make space from previous output and test the writer terminal.writer().println(); terminal.writer().flush(); @@ -165,7 +172,10 @@ public class CliClient implements AutoCloseable { // print welcome terminal.writer().append(CliStrings.MESSAGE_WELCOME); - LineReader lineReader = createLineReader(terminal, ExecutionMode.INTERACTIVE_EXECUTION); + LineReader lineReader = + inputLineReader == null + ? createLineReader(terminal, ExecutionMode.INTERACTIVE_EXECUTION) + : inputLineReader; getAndExecuteStatements(lineReader, false); } @@ -182,6 +192,11 @@ public class CliClient implements AutoCloseable { try { // read a statement from terminal and parse it line = lineReader.readLine(NEWLINE_PROMPT, null, inputTransformer, null); + lineReader.setVariable( + LineReader.SECONDARY_PROMPT_PATTERN, + (executor.getSessionConfig().get(SqlClientOptions.DISPLAY_SHOW_LINE_NUMBERS) + ? SHOW_LINE_NUMBERS_PATTERN + : LineReaderImpl.DEFAULT_SECONDARY_PROMPT_PATTERN)); if (line.trim().isEmpty()) { continue; } diff --git a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/SqlClientOptions.java b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/SqlClientOptions.java index caabc391ede..7b0cea498b4 100644 --- a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/SqlClientOptions.java +++ b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/SqlClientOptions.java @@ -77,4 +77,12 @@ public class SqlClientOptions { .defaultValue(SyntaxHighlightStyle.BuiltInStyle.DEFAULT.name()) .withDescription( "SQL highlight color schema to be used at SQL client. Possible values: 'default', 'dark', 'light', 'chester', 'vs2010', 'solarized', 'obsidian', 'geshi'"); + + @Documentation.TableOption(execMode = Documentation.ExecMode.BATCH_STREAMING) + public static final ConfigOption<Boolean> DISPLAY_SHOW_LINE_NUMBERS = + ConfigOptions.key("sql-client.display.show-line-numbers") + .booleanType() + .defaultValue(Boolean.FALSE) + .withDescription( + "Determines whether there should be shown line numbers in multiline SQL or not."); } diff --git a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientITCase.java b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientITCase.java index bdb308292e6..533c8c9fe94 100644 --- a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientITCase.java +++ b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientITCase.java @@ -21,6 +21,8 @@ package org.apache.flink.table.client.cli; import org.apache.flink.configuration.Configuration; import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration; import org.apache.flink.table.api.config.ExecutionConfigOptions; +import org.apache.flink.table.client.cli.parser.SqlCommandParserImpl; +import org.apache.flink.table.client.cli.parser.SqlMultiLineParser; import org.apache.flink.table.client.cli.utils.SqlScriptReader; import org.apache.flink.table.client.cli.utils.TestSqlStatement; import org.apache.flink.table.client.gateway.Executor; @@ -42,6 +44,8 @@ import org.apache.flink.shaded.guava30.com.google.common.io.PatternFilenameFilte import org.apache.calcite.util.Util; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.jline.reader.LineReader; +import org.jline.reader.LineReaderBuilder; import org.jline.reader.MaskingCallback; import org.jline.terminal.Terminal; import org.jline.terminal.impl.DumbTerminal; @@ -51,6 +55,7 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.io.BufferedReader; @@ -192,6 +197,64 @@ class CliClientITCase { assertThat(out).isEqualTo(in); } + @ParameterizedTest + @MethodSource("tableOptionToJlineVarProvider") + void testPropagationOfTableOptionToVars( + String setTableOptionCommand, + String jlineVarName, + String expectedValue, + @InjectClusterClientConfiguration Configuration configuration) + throws IOException { + + DefaultContext defaultContext = + new DefaultContext( + new Configuration(configuration) + // Make sure we use the new cast behaviour + .set( + ExecutionConfigOptions.TABLE_EXEC_LEGACY_CAST_BEHAVIOUR, + ExecutionConfigOptions.LegacyCastBehaviour.DISABLED), + Collections.emptyList()); + + // Since DumbTerminal exits automatically with the last line need to add \n to have command + // processed before the exit + InputStream inputStream = + new ByteArrayInputStream((setTableOptionCommand + "\n").getBytes()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256); + + try (final Executor executor = + Executor.create( + defaultContext, + InetSocketAddress.createUnresolved( + SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetAddress(), + SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetPort()), + "test-session"); + Terminal terminal = new DumbTerminal(inputStream, outputStream); + CliClient client = + new CliClient( + () -> terminal, executor, historyPath, HideSqlStatement.INSTANCE)) { + + LineReader dummyLineReader = + LineReaderBuilder.builder() + .terminal(terminal) + .parser( + new SqlMultiLineParser( + new SqlCommandParserImpl(), + executor, + CliClient.ExecutionMode.INTERACTIVE_EXECUTION)) + .build(); + client.executeInInteractiveMode(dummyLineReader); + assertThat(dummyLineReader.getVariable(jlineVarName)).isEqualTo(expectedValue); + } + } + + static Stream<Arguments> tableOptionToJlineVarProvider() { + return Stream.of( + Arguments.of( + "SET 'sql-client.display.show-line-numbers' = 'true';", + LineReader.SECONDARY_PROMPT_PATTERN, + "%N%M> ")); + } + /** * Returns printed results for each ran SQL statements. *