pavansai1997 commented on code in PR #76230:
URL: https://github.com/apache/airflow/pull/76230#discussion_r4438891280
##########
providers/common/ai/tests/unit/common/ai/operators/test_llm_sql.py:
##########
@@ -759,6 +759,103 @@ def _make_context(ti_id=None):
@pytest.mark.skipif(
not AIRFLOW_V_3_1_PLUS, reason="Human in the loop is only compatible with
Airflow >= 3.1.0"
)
+class TestLLMSQLQueryOperatorAllowedTables:
+ @staticmethod
+ def _execute(mock_hook_cls, make_mock_run_result, sql, allowed_tables,
**kwargs):
+ mock_hook_cls.get_hook.return_value.create_agent.return_value =
_make_mock_agent(
+ sql, make_mock_run_result
+ )
+ op = LLMSQLQueryOperator(
+ task_id="t",
+ prompt="p",
+ llm_conn_id="c",
+ schema_context="Table: orders",
+ allowed_tables=allowed_tables,
+ **kwargs,
+ )
+ return op.execute(context=MagicMock())
+
+ @pytest.mark.parametrize(
+ ("sql", "allowed_tables"),
+ [
+ pytest.param("SELECT customer_id, SUM(amount) FROM orders GROUP BY
1", ["orders"], id="allowed"),
+ pytest.param("SELECT * FROM ORDERS", ["orders"],
id="case-insensitive"),
+ pytest.param("SELECT * FROM sales.orders", ["sales.orders"],
id="schema-qualified"),
+ pytest.param(
+ "WITH salaries AS (SELECT * FROM orders) SELECT * FROM
salaries",
+ ["orders"],
+ id="cte-named-like-another-table",
+ ),
+ ],
+ )
+ @patch("airflow.providers.common.ai.operators.llm.PydanticAIHook",
autospec=True)
+ def test_sql_within_allowed_tables_is_returned(
+ self, mock_hook_cls, make_mock_run_result, sql, allowed_tables
+ ):
+ assert self._execute(mock_hook_cls, make_mock_run_result, sql,
allowed_tables) == sql
+
+ @pytest.mark.parametrize(
+ ("sql", "disallowed"),
+ [
+ pytest.param("SELECT name, salary FROM salaries", "salaries",
id="other-table"),
+ pytest.param(
+ "SELECT * FROM orders WHERE customer_id IN (SELECT id FROM
customers)",
+ "customers",
+ id="subquery",
+ ),
+ pytest.param(
+ "SELECT * FROM orders o JOIN salaries s ON o.customer_id =
s.id", "salaries", id="join"
+ ),
+ pytest.param("SELECT * FROM otherdb.public.orders",
"otherdb.public.orders", id="catalog"),
Review Comment:
Added `SELECT * FROM sales.orders` against `["orders"]` to the rejected
cases.
--
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]