You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by lz...@apache.org on 2020/04/23 05:08:11 UTC

[flink] branch master updated: [FLINK-17209][table] Allow users to specify dialect in sql-client yaml

This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 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 aa48926  [FLINK-17209][table] Allow users to specify dialect in sql-client yaml
aa48926 is described below

commit aa489269a1429f25136765af94b05d10ef5b7fd3
Author: Rui Li <li...@apache.org>
AuthorDate: Thu Apr 23 13:07:50 2020 +0800

    [FLINK-17209][table] Allow users to specify dialect in sql-client yaml
    
    
    This close #11788
---
 .../generated/table_config_configuration.html      |  6 ++++
 .../client/config/entries/ExecutionEntry.java      | 15 ++++++++++
 .../client/gateway/local/ExecutionContext.java     |  1 +
 .../client/gateway/local/ExecutionContextTest.java | 17 +++++++++++
 .../test/resources/test-sql-client-dialect.yaml    | 34 ++++++++++++++++++++++
 .../org/apache/flink/table/api/TableConfig.java    | 11 ++-----
 .../flink/table/api/config/TableConfigOptions.java |  9 ++++++
 7 files changed, 85 insertions(+), 8 deletions(-)

diff --git a/docs/_includes/generated/table_config_configuration.html b/docs/_includes/generated/table_config_configuration.html
index b7c1a49..c1bdb67 100644
--- a/docs/_includes/generated/table_config_configuration.html
+++ b/docs/_includes/generated/table_config_configuration.html
@@ -14,5 +14,11 @@
             <td>Boolean</td>
             <td>Enable or disable the OPTIONS hint used to specify table optionsdynamically, if disabled, an exception would be thrown if any OPTIONS hint is specified</td>
         </tr>
+        <tr>
+            <td><h5>table.sql-dialect</h5><br> <span class="label label-primary">Batch</span> <span class="label label-primary">Streaming</span></td>
+            <td style="word-wrap: break-word;">"default"</td>
+            <td>String</td>
+            <td>The SQL dialect defines how to parse a SQL query. A different SQL dialect may support different SQL grammar. Currently supported dialects are: default and hive</td>
+        </tr>
     </tbody>
 </table>
diff --git a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/entries/ExecutionEntry.java b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/entries/ExecutionEntry.java
index 6e60c95..04d81a2 100644
--- a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/entries/ExecutionEntry.java
+++ b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/config/entries/ExecutionEntry.java
@@ -22,6 +22,7 @@ import org.apache.flink.api.common.restartstrategy.RestartStrategies;
 import org.apache.flink.api.common.time.Time;
 import org.apache.flink.streaming.api.TimeCharacteristic;
 import org.apache.flink.table.api.EnvironmentSettings;
+import org.apache.flink.table.api.SqlDialect;
 import org.apache.flink.table.client.config.ConfigUtil;
 import org.apache.flink.table.client.config.Environment;
 import org.apache.flink.table.descriptors.DescriptorProperties;
@@ -110,6 +111,8 @@ public class ExecutionEntry extends ConfigEntry {
 
 	public static final String EXECUTION_CURRENT_DATABASE = "current-database";
 
+	public static final String EXECUTION_SQL_DIALECT = "dialect";
+
 	private ExecutionEntry(DescriptorProperties properties) {
 		super(properties);
 	}
@@ -154,6 +157,12 @@ public class ExecutionEntry extends ConfigEntry {
 		properties.validateInt(EXECUTION_RESTART_STRATEGY_MAX_FAILURES_PER_INTERVAL, true, 1);
 		properties.validateString(EXECUTION_CURRENT_CATALOG, true, 1);
 		properties.validateString(EXECUTION_CURRENT_DATABASE, true, 1);
+		properties.validateEnumValues(EXECUTION_SQL_DIALECT,
+				true,
+				Arrays.asList(
+						SqlDialect.DEFAULT.name().toLowerCase(),
+						SqlDialect.HIVE.name().toLowerCase()
+				));
 	}
 
 	public EnvironmentSettings getEnvironmentSettings() {
@@ -330,6 +339,12 @@ public class ExecutionEntry extends ConfigEntry {
 				.orElse(false);
 	}
 
+	public SqlDialect getSqlDialect() {
+		return properties.getOptionalString(EXECUTION_SQL_DIALECT)
+				.map(name -> SqlDialect.valueOf(name.toUpperCase()))
+				.orElse(SqlDialect.DEFAULT);
+	}
+
 	public Map<String, String> asTopLevelMap() {
 		return properties.asPrefixedMap(EXECUTION_ENTRY + '.');
 	}
diff --git a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/gateway/local/ExecutionContext.java b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/gateway/local/ExecutionContext.java
index dc77a7b..21efff7 100644
--- a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/gateway/local/ExecutionContext.java
+++ b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/gateway/local/ExecutionContext.java
@@ -444,6 +444,7 @@ public class ExecutionContext<ClusterID> {
 		final TableConfig config = new TableConfig();
 		environment.getConfiguration().asMap().forEach((k, v) ->
 				config.getConfiguration().setString(k, v));
+		config.setSqlDialect(environment.getExecution().getSqlDialect());
 
 		if (noInheritedState) {
 			//--------------------------------------------------------------------------------------------------------------
diff --git a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/gateway/local/ExecutionContextTest.java b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/gateway/local/ExecutionContextTest.java
index 205113f..b47b6c4 100644
--- a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/gateway/local/ExecutionContextTest.java
+++ b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/gateway/local/ExecutionContextTest.java
@@ -24,6 +24,7 @@ import org.apache.flink.client.cli.DefaultCLI;
 import org.apache.flink.client.deployment.DefaultClusterClientServiceLoader;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders;
+import org.apache.flink.table.api.SqlDialect;
 import org.apache.flink.table.api.TableEnvironment;
 import org.apache.flink.table.api.TableSchema;
 import org.apache.flink.table.api.config.ExecutionConfigOptions;
@@ -71,6 +72,7 @@ public class ExecutionContextTest {
 	public static final String CATALOGS_ENVIRONMENT_FILE = "test-sql-client-catalogs.yaml";
 	private static final String STREAMING_ENVIRONMENT_FILE = "test-sql-client-streaming.yaml";
 	private static final String CONFIGURATION_ENVIRONMENT_FILE = "test-sql-client-configuration.yaml";
+	private static final String DIALECT_ENVIRONMENT_FILE = "test-sql-client-dialect.yaml";
 
 	@Test
 	public void testExecutionConfig() throws Exception {
@@ -286,6 +288,21 @@ public class ExecutionContextTest {
 				Collections.singletonList(new DefaultCLI(flinkConfig))).build();
 	}
 
+	@Test
+	public void testSQLDialect() throws Exception {
+		ExecutionContext<?> context = createDefaultExecutionContext();
+		assertEquals(SqlDialect.DEFAULT, context.getTableEnvironment().getConfig().getSqlDialect());
+
+		Map<String, String> replaceVars = new HashMap<>();
+		replaceVars.put("$VAR_DIALECT", "default");
+		context = createExecutionContext(DIALECT_ENVIRONMENT_FILE, replaceVars);
+		assertEquals(SqlDialect.DEFAULT, context.getTableEnvironment().getConfig().getSqlDialect());
+
+		replaceVars.put("$VAR_DIALECT", "hive");
+		context = createExecutionContext(DIALECT_ENVIRONMENT_FILE, replaceVars);
+		assertEquals(SqlDialect.HIVE, context.getTableEnvironment().getConfig().getSqlDialect());
+	}
+
 	@SuppressWarnings("unchecked")
 	private <T> ExecutionContext<T> createExecutionContext(String file, Map<String, String> replaceVars) throws Exception {
 		final Environment env = EnvironmentFileUtil.parseModified(
diff --git a/flink-table/flink-sql-client/src/test/resources/test-sql-client-dialect.yaml b/flink-table/flink-sql-client/src/test/resources/test-sql-client-dialect.yaml
new file mode 100644
index 0000000..d4fa4bf
--- /dev/null
+++ b/flink-table/flink-sql-client/src/test/resources/test-sql-client-dialect.yaml
@@ -0,0 +1,34 @@
+################################################################################
+#  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.
+################################################################################
+
+#==============================================================================
+# TEST ENVIRONMENT FILE
+# This test file is to check whether the configuration can be successfully set.
+#==============================================================================
+
+execution:
+  planner: blink
+  type: batch
+  result-mode: table
+  dialect: "$VAR_DIALECT"
+
+configuration:
+  table.exec.sort.default-limit: 100
+  table.exec.spill-compression.enabled: true
+  table.exec.spill-compression.block-size: 128kb
+  table.optimizer.join-reorder-enabled: true
diff --git a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/TableConfig.java b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/TableConfig.java
index 38a5f43..a21e519 100644
--- a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/TableConfig.java
+++ b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/TableConfig.java
@@ -25,6 +25,7 @@ import org.apache.flink.configuration.Configuration;
 import org.apache.flink.configuration.PipelineOptions;
 import org.apache.flink.table.api.config.ExecutionConfigOptions;
 import org.apache.flink.table.api.config.OptimizerConfigOptions;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.util.Preconditions;
 
 import java.math.MathContext;
@@ -107,12 +108,6 @@ public class TableConfig {
 	private Configuration configuration = new Configuration();
 
 	/**
-	 * The SQL dialect defines how to parse a SQL query. A different SQL dialect may support different
-	 * SQL grammar.
-	 */
-	private SqlDialect sqlDialect = SqlDialect.DEFAULT;
-
-	/**
 	 * Gives direct access to the underlying key-value map for advanced configuration.
 	 */
 	public Configuration getConfiguration() {
@@ -134,14 +129,14 @@ public class TableConfig {
 	 * Returns the current SQL dialect.
 	 */
 	public SqlDialect getSqlDialect() {
-		return this.sqlDialect;
+		return SqlDialect.valueOf(getConfiguration().getString(TableConfigOptions.TABLE_SQL_DIALECT).toUpperCase());
 	}
 
 	/**
 	 * Sets the current SQL dialect to parse a SQL query. Flink's SQL behavior by default.
 	 */
 	public void setSqlDialect(SqlDialect sqlDialect) {
-		this.sqlDialect = sqlDialect;
+		getConfiguration().setString(TableConfigOptions.TABLE_SQL_DIALECT, sqlDialect.name().toLowerCase());
 	}
 
 	/**
diff --git a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/TableConfigOptions.java b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/TableConfigOptions.java
index 8692f67..66348a4 100644
--- a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/TableConfigOptions.java
+++ b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/TableConfigOptions.java
@@ -20,6 +20,7 @@ package org.apache.flink.table.api.config;
 
 import org.apache.flink.annotation.docs.Documentation;
 import org.apache.flink.configuration.ConfigOption;
+import org.apache.flink.table.api.SqlDialect;
 
 import static org.apache.flink.configuration.ConfigOptions.key;
 
@@ -40,4 +41,12 @@ public class TableConfigOptions {
 					.withDescription("Enable or disable the OPTIONS hint used to specify table options" +
 							"dynamically, if disabled, an exception would be thrown " +
 							"if any OPTIONS hint is specified");
+
+	@Documentation.TableOption(execMode = Documentation.ExecMode.BATCH_STREAMING)
+	public static final ConfigOption<String> TABLE_SQL_DIALECT = key("table.sql-dialect")
+			.stringType()
+			.defaultValue(SqlDialect.DEFAULT.name().toLowerCase())
+			.withDescription("The SQL dialect defines how to parse a SQL query. " +
+					"A different SQL dialect may support different SQL grammar. " +
+					"Currently supported dialects are: default and hive");
 }