dianfu commented on code in PR #29088:
URL: https://github.com/apache/flink/pull/29088#discussion_r3954623215


##########
flink-python/pyflink/dataframe/context.py:
##########
@@ -95,9 +105,14 @@ def get_or_create_table_environment() -> TableEnvironment:
     global _global_table_environment
 
     if _global_table_environment is None:
+        from pyflink.dataframe._config import config
         from pyflink.datastream import StreamExecutionEnvironment
 
-        stream_environment = 
StreamExecutionEnvironment.get_execution_environment()
-        _global_table_environment = 
StreamTableEnvironment.create(stream_environment)
+        stream_environment = 
StreamExecutionEnvironment.get_execution_environment(
+            config._to_configuration()
+        )
+        t_env = StreamTableEnvironment.create(stream_environment)

Review Comment:
   Could we pass the buffered configuration to EnvironmentSettings as well and 
use the two-argument StreamTableEnvironment.create() as following?
   ```
   configuration = config._to_configuration()
   t_env = StreamExecutionEnvironment.get_execution_environment(configuration)
   
   settings = (
       EnvironmentSettings.new_instance()
       .with_configuration(configuration)
       .build()
   )
   
   t_env = StreamTableEnvironment.create(
       t_env,
       environment_settings=settings,
   )
   ```
   
   The current overload only derives the runtime mode from the 
StreamExecutionEnvironment. Other TableEnvironment creation-time options are 
applied too late by _apply_to(). For example:
   
   ```
       pf.config.set("table.builtin-catalog-name", "my_catalog")
       t_env = pf.get_or_create_table_environment()
   
       assert pf.config.get("table.builtin-catalog-name") == "my_catalog"
       assert t_env.get_current_catalog() == "default_catalog"  # not applied
   ```
   
   PS: in this case, `config._apply_to(t_env, overwrite=True)` is not required 
any more?



##########
flink-python/pyflink/dataframe/context.py:
##########
@@ -50,6 +54,10 @@ def set_table_environment(t_env: Optional[TableEnvironment]) 
-> None:
     global _global_table_environment
     if t_env is not None and not isinstance(t_env, TableEnvironment):
         raise TypeError("t_env must be a TableEnvironment or None")
+    if t_env is not None:

Review Comment:
   Some Flink options are consumed while the StreamExecutionEnvironment or 
TableEnvironment is being created. Applying buffered values later through 
`t_env.get_config().set(...)` only updates TableConfig and cannot reconfigure 
the existing environment.
   
   Do you think it makes sense to separate the two initialization paths:
   
   1. `pf.config` configures only environments created by 
`get_or_create_table_environment()`.
   2. An environment passed to `set_table_environment()` is already constructed 
and should be treated as authoritative. So `set_table_environment()` should not 
call `config._apply_to()`. I guess we could check if config is empty to ensure 
users use it correctly.



##########
flink-python/pyflink/dataframe/_config.py:
##########
@@ -0,0 +1,151 @@
+################################################################################
+#  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.
+################################################################################
+
+from typing import Dict, Optional
+
+from pyflink.common import Configuration
+from pyflink.table import TableEnvironment
+from pyflink.util.api_stability_decorators import PublicEvolving
+
+__all__ = [
+    "DataFrameConfig",

Review Comment:
   `DataFrameConfig` is exported as a `@PublicEvolving` class, so users can 
reasonably instantiate it. However, independently created instances are not 
consumed by `get_or_create_table_environment()`, which always reads the 
module-level `pf.config` singleton.
   
   For example:
   
       config = pf.DataFrameConfig()
       config.set("execution.runtime-mode", "batch")
       t_env = pf.get_or_create_table_environment()
   
   The call to `set()` succeeds, but the environment is created from 
`pf.config`, so the value stored in `config` is silently ignored.
   
   Since this API is designed around a single global configuration, so I think 
we need make the implementation class private and export only the `pf.config` 
singleton. 



##########
flink-python/pyflink/dataframe/_config.py:
##########
@@ -0,0 +1,151 @@
+################################################################################
+#  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.
+################################################################################
+
+from typing import Dict, Optional
+
+from pyflink.common import Configuration
+from pyflink.table import TableEnvironment
+from pyflink.util.api_stability_decorators import PublicEvolving
+
+__all__ = [
+    "DataFrameConfig",
+    "config",
+]
+
+
+@PublicEvolving()
+class DataFrameConfig:
+    """
+    A unified entry point for Flink configuration in the DataFrame API.
+
+    Accepts any Flink configuration key and buffers the value, so 
configuration can be set
+    at any time -- even before an environment exists. Buffered values are used 
when
+    :func:`get_or_create_table_environment` creates the underlying
+    :class:`~pyflink.table.TableEnvironment`, so options that can only be 
chosen at creation
+    time, such as ``execution.runtime-mode``, take effect. An environment 
injected via
+    :func:`set_table_environment` receives the buffered values for every key 
it does not
+    already set explicitly. While an environment is active, values are also 
written through
+    to its configuration immediately.
+
+    Use the module-level singleton :data:`config` instead of instantiating 
this class.
+
+    Example::
+
+        >>> import pyflink.dataframe as pf
+        >>> _ = pf.config.set("parallelism.default", "4")
+        >>> pf.config.get("parallelism.default")
+        '4'
+
+    .. versionadded:: 2.4.0
+    """
+
+    def __init__(self: "DataFrameConfig"):
+        self._buffered: Dict[str, str] = {}
+
+    def set(self, key: str, value: str) -> "DataFrameConfig":
+        """
+        Sets a string-based value for the given string-based key.
+
+        The value is buffered and applied to the underlying environment once 
it is created
+        or injected; when an environment is already active, the value is 
applied to its
+        configuration immediately as well. A value the active environment 
rejects is not
+        buffered.
+
+        :param key: The configuration key.
+        :param value: The configuration value. It will be parsed by the 
framework on access.
+        :return: This object, to allow chaining of calls.
+        :raises TypeError: If ``key`` or ``value`` is not a string.
+
+        Example::
+
+            >>> import pyflink.dataframe as pf
+            >>> _ = pf.config.set("parallelism.default", "4") \\
+            ...              .set("execution.runtime-mode", "batch")
+
+        .. versionadded:: 2.4.0
+        """
+        if not isinstance(key, str):
+            raise TypeError("key must be a string")
+        if not isinstance(value, str):
+            raise TypeError("value must be a string")
+
+        from pyflink.dataframe.context import get_table_environment
+
+        t_env = get_table_environment()
+        if t_env is not None:

Review Comment:
   Some Flink options are consumed when the StreamExecutionEnvironment or 
TableEnvironment is created. For those options, forwarding a later update to
   
       _global_table_environment.get_config().set(key, value)
   
   only changes the TableConfig value; it does not reconfigure the 
already-created environment. For example, changing `execution.runtime-mode` 
here cannot change how the existing environment was initialized.
   
   Do you think it make sense to let DataFrameConfig.set() fail fast whenever a 
global TableEnvironment already exists, and require all DataFrame configuration 
to be set before the environment is created?



-- 
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]

Reply via email to