snuyanzin commented on code in PR #22063: URL: https://github.com/apache/flink/pull/22063#discussion_r1144929172
########## flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/parser/SqlClientSyntaxHighlighter.java: ########## @@ -0,0 +1,246 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.client.cli.parser; + +import org.apache.flink.table.api.SqlDialect; +import org.apache.flink.table.api.config.TableConfigOptions; +import org.apache.flink.table.client.gateway.Executor; + +import org.jline.reader.LineReader; +import org.jline.reader.impl.DefaultHighlighter; +import org.jline.utils.AttributedString; +import org.jline.utils.AttributedStringBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.Properties; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.apache.flink.table.client.cli.CliClient.COLOR_SCHEMA_VAR; + +/** Sql Client syntax highlighter. */ +public class SqlClientSyntaxHighlighter extends DefaultHighlighter { + private static final Set<String> FLINK_KEYWORD_SET; + private static final Set<String> HIVE_KEYWORD_SET; + + static { + try (InputStream is = + SqlClientSyntaxHighlighter.class.getResourceAsStream("/keywords.properties")) { + Properties props = new Properties(); + props.load(is); + FLINK_KEYWORD_SET = + Collections.unmodifiableSet( + Arrays.stream(props.get("default").toString().split(";")) + .collect(Collectors.toSet())); + HIVE_KEYWORD_SET = + Collections.unmodifiableSet( + Arrays.stream(props.get("hive").toString().split(";")) + .collect(Collectors.toSet())); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private final Executor executor; + + public SqlClientSyntaxHighlighter(Executor executor) { + this.executor = executor; + } + + @Override + public AttributedString highlight(LineReader reader, String buffer) { + + final Object colorSchemeOrdinal = reader.getVariable(COLOR_SCHEMA_VAR); + SyntaxHighlightStyle.BuiltInStyle style = + SyntaxHighlightStyle.BuiltInStyle.fromOrdinal( + colorSchemeOrdinal == null ? 0 : (Integer) colorSchemeOrdinal); + if (style == SyntaxHighlightStyle.BuiltInStyle.DEFAULT) { + return super.highlight(reader, buffer); + } + final SqlDialect dialect = + executor.getSessionConfig() + .get(TableConfigOptions.TABLE_SQL_DIALECT) + .equalsIgnoreCase(SqlDialect.HIVE.toString()) + ? SqlDialect.HIVE + : SqlDialect.DEFAULT; + return getHighlightedOutput(buffer, style.getHighlightStyle(), dialect); + } + + static AttributedString getHighlightedOutput( + String buffer, SyntaxHighlightStyle style, SqlDialect dialect) { + final Set<Character> stateStartSymbols = + Arrays.stream(State.values()) + .map(t -> t.getStart(dialect).charAt(0)) + .collect(Collectors.toSet()); + final AttributedStringBuilder highlightedOutput = new AttributedStringBuilder(); + State currentParseState = null; + int counter = 0; + StringBuilder word = new StringBuilder(); + for (int i = 0; i < buffer.length(); i++) { + final char c = buffer.charAt(i); + if (currentParseState == null) { + if (stateStartSymbols.contains(c)) { Review Comment: I know escaping with doubling of symbols e.g. `''`, `""` - it works in Flink. Although some engines support escaping with help of backslash `\` however it seems Flink does not support it (yet) e.g. query ```sql SELECT '\'; --returns \ as a varchar ``` ```sql SELECT 123 AS `\`; --returns 123 with a field named as \ ``` ```sql SELECT ''''; -- returns value with 1 quote ``` ```sql SELECT 1 AS ````; -- returns a value with field name containing a tick ``` in case there is no support for escaping with backslash it should be ok, since ```sql -- for the case of escaping quotes or ticks (depending on the dialect) select ''''; -- all 4 single quotes should be in quoted style select 'test '' 123 '; -- everything between first and last singe quote should be in a quoted style ``` Or did I miss anything? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
