Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: Enable qualified sql references to shadow regular variables (#6168)

Dylan Madisetti20c1327ef616
diff --git a/marimo/_ast/sql_visitor.py b/marimo/_ast/sql_visitor.py
index 9af1fff185..9084cfc0ae 100644
--- a/marimo/_ast/sql_visitor.py
+++ b/marimo/_ast/sql_visitor.py
@@ -5,7 +5,7 @@ import ast
import re
from dataclasses import dataclass, field
from textwrap import dedent
-from typing import Any, Optional
+from typing import Any, Literal, Optional, Union
from marimo import _loggers
from marimo._dependencies.dependencies import DependencyManager
@@ -22,6 +22,10 @@ COMMON_FILE_EXTENSIONS = (
".xlsx",
)
+SQLKind = Literal["table", "view", "schema", "catalog"]
+
+SQLTypes = Union[SQLKind, Literal["any"]]
+
class SQLVisitor(ast.NodeVisitor):
"""
@@ -142,8 +146,8 @@ class TokenExtractor:
@dataclass
class SQLDefs:
- tables: list[str] = field(default_factory=list)
- views: list[str] = field(default_factory=list)
+ tables: list[SQLRef] = field(default_factory=list)
+ views: list[SQLRef] = field(default_factory=list)
schemas: list[str] = field(default_factory=list)
catalogs: list[str] = field(default_factory=list)
@@ -177,8 +181,8 @@ def find_sql_defs(sql_statement: str) -> SQLDefs:
token_extractor = TokenExtractor(
sql_statement=sql_statement, tokens=tokens
)
- created_tables: list[str] = []
- created_views: list[str] = []
+ created_tables: list[SQLRef] = []
+ created_views: list[SQLRef] = []
created_schemas: list[str] = []
created_catalogs: list[str] = []
@@ -250,7 +254,7 @@ def find_sql_defs(sql_statement: str) -> SQLDefs:
if is_table:
# only add the table name
- created_tables.append(parts[-1])
+ created_tables.append(SQLRef.from_parts(parts))
# add the catalog and schema if exist
if len(parts) == 3:
reffed_catalogs.append(parts[0])
@@ -259,7 +263,7 @@ def find_sql_defs(sql_statement: str) -> SQLDefs:
reffed_catalogs.append(parts[0])
elif is_view:
# only add the table name
- created_views.append(parts[-1])
+ created_views.append(SQLRef.from_parts(parts))
# add the catalog and schema if exist
if len(parts) == 3:
reffed_catalogs.append(parts[0])
@@ -329,6 +333,25 @@ class SQLRef:
schema: Optional[str] = None
catalog: Optional[str] = None
+ @classmethod
+ def from_parts(
+ cls,
+ parts: list[str],
+ ) -> SQLRef:
+ catalog = None
+ schema = None
+ table = ""
+ if len(parts) == 3:
+ catalog, schema, table = parts
+ catalog = catalog.lower()
+ schema = schema.lower()
+ elif len(parts) == 2:
+ schema, table = parts
+ schema = schema.lower()
+ elif len(parts) == 1:
+ table = parts[0]
+ return cls(table=table.lower(), schema=schema, catalog=catalog)
+
@property
def qualified_name(self) -> str:
"""Convert a SQLRef to a fully qualified name to be used as a reference in the visitor"""
@@ -341,47 +364,101 @@ class SQLRef:
# Table is always required
parts.append(self.table)
name = ".".join(parts)
- return name
+ return name.lower()
- def matches_hierarchical_ref(self, name: str, ref: str) -> bool:
+ def matches_hierarchical_ref(
+ self, name: str, ref: str, kind: SQLTypes = "any"
+ ) -> bool:
"""
Determine if a hierarchical reference string matches a SQLRef.
Args:
name: The name to match against (could be catalog, schema, or table).
ref: The fully qualified reference string (e.g., "schema.table", "catalog.schema.table").
+ kind: The kind of reference ("table", "view", "schema", "catalog").
Returns:
True if the reference matches the SQLRef's structure and values, False otherwise.
"""
+ ref = ref.lower()
+ name = name.lower()
parts = ref.split(".")
num_parts = len(parts)
+ if num_parts == 0:
+ return False
+
+ if kind == "catalog":
+ if self.catalog is not None:
+ return name == self.catalog == parts[0]
+ # Fallback to schema if catalog is None
+ kind = "schema"
+
+ if kind == "schema":
+ if num_parts < 3:
+ return name == self.schema == parts[0]
+ return name == self.schema == parts[1]
+
+ # Otherwise, kind is "table" or "view", and we should check the ordering
+ # and return accordingly
if num_parts == 1:
# Only table name provided
- return name == self.table == parts[0]
+ return name == self.table == parts[0] and kind in (
+ "table",
+ "view",
+ "any",
+ )
if num_parts == 2:
# Format: schema.table or catalog.table
# sqlglot cannot differentiate between schema and catalog
# so we check if the qualifier matches either
qualifier, table = parts
- if table != self.table:
- return False
# Try matching as schema or catalog
+ if (self.schema, self.catalog) == (None, None):
+ return name == self.table == table and kind in (
+ "table",
+ "view",
+ "any",
+ )
if qualifier not in (self.schema, self.catalog):
return False
- return qualifier == name
+
+ return name in (
+ self.catalog,
+ self.schema,
+ self.table,
+ ) and kind in (
+ "table",
+ "view",
+ "catalog",
+ "schema",
+ "any",
+ )
if num_parts == 3:
# Format: catalog.schema.table
catalog, schema, table = parts
- if table != self.table:
- return False
- if catalog == self.catalog:
- return name == self.catalog
- if schema == self.schema:
- return name == self.schema
+ if self.catalog:
+ if catalog != self.catalog:
+ return False
+ if schema != self.schema:
+ return name == self.catalog and kind in ("catalog", "any")
+ elif self.schema:
+ if schema != self.schema:
+ return False
+ return name == self.schema and kind in ("schema", "any")
+ return name in (
+ self.catalog,
+ self.schema,
+ self.table,
+ ) and kind in (
+ "table",
+ "view",
+ "catalog",
+ "schema",
+ "any",
+ )
return False
diff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.py
index 31e9217581..3e6a200d08 100644
--- a/marimo/_ast/visitor.py
+++ b/marimo/_ast/visitor.py
@@ -14,6 +14,7 @@ from marimo import _loggers
from marimo._ast.errors import ImportStarError
from marimo._ast.sql_visitor import (
SQLDefs,
+ SQLKind,
SQLRef,
find_sql_defs,
find_sql_refs,
@@ -57,15 +58,14 @@ class AnnotationData:
@dataclass
class VariableData:
# "table", "view", "schema", and "catalog" are SQL variables, not Python.
- kind: Literal[
- "function",
- "class",
- "import",
- "variable",
- "table",
- "view",
- "schema",
- "catalog",
+ kind: Union[
+ Literal[
+ "function",
+ "class",
+ "import",
+ "variable",
+ ],
+ SQLKind,
] = "variable"
# If kind == function or class, it may be dependent on externally defined
@@ -89,6 +89,9 @@ class VariableData:
# For kind == import
import_data: Optional[ImportData] = None
+ # In the sql case, the name may be qualified
+ qualified_name: Optional[str] = None
+
@property
def language(self) -> Language:
return (
@@ -675,9 +678,21 @@ class ScopedVisitor(ast.NodeVisitor):
sql_defs = SQLDefs()
for _table in sql_defs.tables:
- self._define(None, _table, VariableData("table"))
+ self._define(
+ None,
+ _table.table,
+ VariableData(
+ "table", qualified_name=_table.qualified_name
+ ),
+ )
for _view in sql_defs.views:
- self._define(None, _view, VariableData("view"))
+ self._define(
+ None,
+ _view.table,
+ VariableData(
+ "view", qualified_name=_view.qualified_name
+ ),
+ )
for _schema in sql_defs.schemas:
self._define(None, _schema, VariableData("schema"))
for _catalog in sql_defs.catalogs:
diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.py
index 6c968c9aad..c4abd044fd 100644
--- a/marimo/_runtime/dataflow.py
+++ b/marimo/_runtime/dataflow.py
@@ -4,14 +4,14 @@ from __future__ import annotations
import threading
from collections import deque
from dataclasses import dataclass, field
-from typing import TYPE_CHECKING, Any, Callable, Literal, Optional
+from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, cast
from marimo import _loggers
from marimo._ast.cell import (
CellImpl,
)
from marimo._ast.compiler import code_key
-from marimo._ast.sql_visitor import SQLRef
+from marimo._ast.sql_visitor import SQLRef, SQLTypes
from marimo._ast.variables import is_mangled_local
from marimo._ast.visitor import ImportData, Name, VariableData
from marimo._runtime.executor import (
@@ -60,6 +60,10 @@ class DirectedGraph:
# A mapping from defs to the cells that define them
definitions: dict[Name, set[CellId_t]] = field(default_factory=dict)
+ typed_definitions: dict[tuple[Name, str], set[CellId_t]] = field(
+ default_factory=dict
+ )
+ definition_types: dict[Name, set[str]] = field(default_factory=dict)
# The set of cycles in the graph
cycles: set[tuple[Edge, ...]] = field(default_factory=set)
@@ -110,8 +114,15 @@ class DirectedGraph:
sql_ref = cell.sql_refs.get(ref)
+ kind: SQLTypes = "any"
+ if name in cell.variable_data:
+ variable_data = cell.variable_data[name][-1]
+ kind = cast(SQLTypes, variable_data.kind)
+
# Hierarchical reference match
- if sql_ref and sql_ref.matches_hierarchical_ref(name, ref):
+ if sql_ref and sql_ref.matches_hierarchical_ref(
+ name, ref, kind
+ ):
cells.add(cid)
break
@@ -147,20 +158,9 @@ class DirectedGraph:
variable_name: Name = sql_ref.qualified_name
matching_cell_ids = set()
- for def_name, cell_ids in self.definitions.items():
- # NB. Only the last definition matters.
- # Technically more nuanced with branching statements, but this is
- # the best we can do with static analysis.
- var_data = [
- v[-1]
- for cell_id in cell_ids
- if (v := self.cells[cell_id].variable_data.get(def_name, []))
- ]
- # Only consider definitions with a single variable data entry
- # since multiple defining cells is undefined behavior
- var, *_ = var_data
+ for (def_name, kind), cell_ids in self.typed_definitions.items():
# Match table/view definitions
- if sql_ref.contains_hierarchical_ref(def_name, var.kind):
+ if sql_ref.contains_hierarchical_ref(def_name, kind):
variable_name = def_name
matching_cell_ids.update(cell_ids)
@@ -217,7 +217,31 @@ class DirectedGraph:
# any cell that defines a variable defined by this cell becomes
# a sibling.
for name, variable_data in cell.variable_data.items():
- self.definitions.setdefault(name, set()).add(cell_id)
+ # NB. Only the last definition matters.
+ # Technically more nuanced with branching statements, but this is
+ # the best we can do with static analysis.
+ variable = variable_data[-1]
+ typed_def = (name, variable.kind)
+
+ if (
+ name in self.definitions
+ and typed_def not in self.typed_definitions
+ ):
+ # Duplicate if the qualified name is no different
+ if (
+ variable.qualified_name == name
+ or variable.language != "sql"
+ ):
+ self.definitions[name].add(cell_id)
+ else:
+ self.definitions.setdefault(name, set()).add(cell_id)
+ self.typed_definitions.setdefault(typed_def, set()).add(
+ cell_id
+ )
+ self.definition_types.setdefault(name, set()).add(
+ variable.kind
+ )
+
for sibling in self.definitions[name]:
# TODO(akshayka): Distinguish between Python/SQL?
if sibling != cell_id:
@@ -257,25 +281,32 @@ class DirectedGraph:
) - set((cell_id,))
variable_name: Name = name
- if not other_ids_defining_name:
- # Handle SQL matching for hierarchical references
- sql_ref = cell.sql_refs.get(name)
- if sql_ref:
- other_ids_defining_name, variable_name = (
- self._find_sql_hierarchical_matches(sql_ref)
- )
+ # Handle SQL matching for hierarchical references
+ sql_ref = cell.sql_refs.get(name)
+ if sql_ref:
+ _other_ids_defining_name, variable_name = (
+ self._find_sql_hierarchical_matches(sql_ref)
+ )
+ other_ids_defining_name.update(_other_ids_defining_name)
# If other_ids_defining_name is empty, the user will get a
# NameError at runtime (unless the symbol is a builtin).
for other_id in other_ids_defining_name:
- language = (
- self.cells[other_id]
- .variable_data[variable_name][-1]
- .language
- )
+ other_variable_data = self.cells[other_id].variable_data[
+ variable_name
+ ][-1]
+ language = other_variable_data.language
if language == "sql" and cell.language == "python":
# SQL table/db def -> Python ref is not an edge
continue
+ if language == "sql" and cell.language == "sql":
+ # Edges between SQL cells need to respect hierarchy.
+ if sql_ref and not sql_ref.matches_hierarchical_ref(
+ variable_name,
+ other_variable_data.qualified_name or name,
+ kind=cast(SQLTypes, other_variable_data.kind),
+ ):
+ continue
parents.add(other_id)
# we are adding an edge (other_id, cell_id). If there
# is a path from cell_id to other_id, then the new
@@ -407,6 +438,10 @@ class DirectedGraph:
# No more cells define this name, so we remove it from the
# graph
del self.definitions[name]
+ # Clean up all typed definitions
+ for typed_def in self.definition_types[name]:
+ del self.typed_definitions[(name, typed_def)]
+ del self.definition_types[name]
# Remove cycles that are broken from removing this cell.
edges = [(cell_id, child) for child in self.children[cell_id]] + [
diff --git a/tests/_ast/test_compiler.py b/tests/_ast/test_compiler.py
index eced330db8..38279df5fe 100644
--- a/tests/_ast/test_compiler.py
+++ b/tests/_ast/test_compiler.py
@@ -268,7 +268,9 @@ class TestParseSQLCell:
assert cell.defs == set(["t1"])
assert cell.refs == set(["mo"])
assert cell.language == "sql"
- assert cell.variable_data == {"t1": [VariableData("table")]}
+ assert cell.variable_data == {
+ "t1": [VariableData("table", qualified_name="t1")]
+ }
@staticmethod
def test_table_reference() -> None:
@@ -296,7 +298,9 @@ class TestParseSQLCell:
assert cell.defs == set(["t1"])
assert cell.refs == set(["duckdb"])
assert cell.language == "sql"
- assert cell.variable_data == {"t1": [VariableData("table")]}
+ assert cell.variable_data == {
+ "t1": [VariableData("table", qualified_name="t1")]
+ }
@staticmethod
@pytest.mark.parametrize(
diff --git a/tests/_ast/test_sql_visitor.py b/tests/_ast/test_sql_visitor.py
index e08f10a1f0..30eed88aa6 100644
--- a/tests/_ast/test_sql_visitor.py
+++ b/tests/_ast/test_sql_visitor.py
@@ -99,13 +99,11 @@ class TestFindSQLDefs:
def test_find_sql_defs_simple() -> None:
sql = "CREATE TABLE test_table (id INT, name VARCHAR(255));"
assert find_sql_defs(sql) == SQLDefs(
- tables=["test_table"],
+ tables=[SQLRef(table="test_table")]
)
sql = "CREATE VIEW test_view (id INT, name VARCHAR(255));"
- assert find_sql_defs(sql) == SQLDefs(
- views=["test_view"],
- )
+ assert find_sql_defs(sql) == SQLDefs(views=[SQLRef(table="test_view")])
@staticmethod
def test_find_sql_defs_multiple() -> None:
@@ -115,8 +113,8 @@ class TestFindSQLDefs:
"""
assert find_sql_defs(sql) == SQLDefs(
tables=[
- "table1",
- "table2",
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
],
)
@@ -126,8 +124,8 @@ class TestFindSQLDefs:
"""
assert find_sql_defs(sql) == SQLDefs(
views=[
- "table1",
- "table2",
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
],
)
@@ -144,8 +142,8 @@ class TestFindSQLDefs:
"""
assert find_sql_defs(sql) == SQLDefs(
tables=[
- "table1",
- "table2",
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
],
)
@@ -160,8 +158,8 @@ class TestFindSQLDefs:
"""
assert find_sql_defs(sql) == SQLDefs(
views=[
- "table1",
- "table2",
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
],
)
@@ -169,37 +167,33 @@ class TestFindSQLDefs:
def test_find_sql_defs_with_or_replace() -> None:
sql = "CREATE OR REPLACE TABLE test_table (id INT);"
assert find_sql_defs(sql) == SQLDefs(
- tables=["test_table"],
+ tables=[SQLRef(table="test_table")]
)
sql = "CREATE OR REPLACE VIEW test_view (id INT);"
- assert find_sql_defs(sql) == SQLDefs(
- views=["test_view"],
- )
+ assert find_sql_defs(sql) == SQLDefs(views=[SQLRef(table="test_view")])
@staticmethod
def test_find_sql_defs_temporary() -> None:
sql = "CREATE TEMPORARY TABLE temp_table (id INT);"
assert find_sql_defs(sql) == SQLDefs(
- tables=["temp_table"],
+ tables=[SQLRef(table="temp_table")],
)
sql = "CREATE TEMPORARY VIEW temp_table (id INT);"
assert find_sql_defs(sql) == SQLDefs(
- views=["temp_table"],
+ views=[SQLRef(table="temp_table")],
)
@staticmethod
def test_find_sql_defs_if_not_exists() -> None:
sql = "CREATE TABLE IF NOT EXISTS new_table (id INT);"
assert find_sql_defs(sql) == SQLDefs(
- tables=["new_table"],
+ tables=[SQLRef(table="new_table")],
)
sql = "CREATE VIEW IF NOT EXISTS new_table (id INT);"
- assert find_sql_defs(sql) == SQLDefs(
- views=["new_table"],
- )
+ assert find_sql_defs(sql) == SQLDefs(views=[SQLRef(table="new_table")])
@staticmethod
def test_find_sql_defs_complex() -> None:
@@ -210,9 +204,9 @@ class TestFindSQLDefs:
""" # noqa: E501
assert find_sql_defs(sql) == SQLDefs(
tables=[
- "table1",
- "table2",
- "table3",
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
+ SQLRef(table="table3"),
],
)
@@ -223,9 +217,9 @@ class TestFindSQLDefs:
""" # noqa: E501
assert find_sql_defs(sql) == SQLDefs(
views=[
- "table1",
- "table2",
- "table3",
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
+ SQLRef(table="table3"),
],
)
@@ -238,12 +232,12 @@ class TestFindSQLDefs:
def test_find_sql_defs_case_insensitive() -> None:
sql = "create TABLE Test_Table (id INT);"
assert find_sql_defs(sql) == SQLDefs(
- tables=["Test_Table"],
+ tables=[SQLRef(table="test_table")],
)
sql = "create VIEW Test_Table (id INT);"
assert find_sql_defs(sql) == SQLDefs(
- views=["Test_Table"],
+ views=[SQLRef(table="test_table")]
)
@staticmethod
@@ -305,7 +299,7 @@ class TestFindSQLDefs:
query: str,
) -> None:
assert find_sql_defs(query) == SQLDefs(
- tables=["my_table"],
+ tables=[SQLRef(table="my_table")],
)
@staticmethod
@@ -333,12 +327,12 @@ class TestFindSQLDefs:
"""
assert find_sql_defs(sql) == SQLDefs(
tables=[
- "my--table",
- "my_table_with_select",
- "my/*weird*/table",
- "with a space",
- "single-quotes",
- r"escaped\ntable",
+ SQLRef(table="my--table"),
+ SQLRef(table="my_table_with_select"),
+ SQLRef(table="my/*weird*/table"),
+ SQLRef(table="with a space"),
+ SQLRef(table="single-quotes"),
+ SQLRef(table=r"escaped\ntable"),
],
)
@@ -381,7 +375,7 @@ class TestFindSQLDefs:
CREATE TABLE my_catalog.my_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[SQLRef(table="my_table", schema="my_catalog")],
reffed_catalogs=["my_catalog"],
)
@@ -391,7 +385,7 @@ class TestFindSQLDefs:
CREATE OR REPLACE TABLE my_db.my_table as (SELECT 42);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[SQLRef(table="my_table", schema="my_db")],
reffed_catalogs=["my_db"],
)
@@ -401,7 +395,11 @@ class TestFindSQLDefs:
CREATE TABLE my_catalog.my_schema.my_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[
+ SQLRef(
+ table="my_table", catalog="my_catalog", schema="my_schema"
+ )
+ ],
reffed_catalogs=["my_catalog"],
reffed_schemas=["my_schema"],
)
@@ -412,7 +410,9 @@ class TestFindSQLDefs:
CREATE TABLE my_catalog.main.my_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[
+ SQLRef(table="my_table", catalog="my_catalog", schema="main")
+ ],
reffed_catalogs=["my_catalog"],
reffed_schemas=[], # main not included, since that is the default
)
@@ -433,7 +433,7 @@ class TestFindSQLDefs:
CREATE TABLE memory.main.my_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[SQLRef(table="my_table", catalog="memory", schema="main")],
)
@staticmethod
@@ -442,7 +442,7 @@ class TestFindSQLDefs:
CREATE TABLE memory.my_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[SQLRef(table="my_table", schema="memory")],
)
@staticmethod
@@ -451,7 +451,7 @@ class TestFindSQLDefs:
CREATE TEMP TABLE my_temp_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_temp_table"],
+ tables=[SQLRef(table="my_temp_table")],
)
@staticmethod
@@ -460,7 +460,7 @@ class TestFindSQLDefs:
CREATE TABLE IF NOT EXISTS my_table (id INT);
"""
assert find_sql_defs(sql) == SQLDefs(
- tables=["my_table"],
+ tables=[SQLRef(table="my_table")],
)
diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.py
index 8379cce2c8..c9ef8b912c 100644
--- a/tests/_ast/test_visitor.py
+++ b/tests/_ast/test_visitor.py
@@ -1285,7 +1285,9 @@ def test_sql_statement_with_url() -> None:
mod = ast.parse(code)
v.visit(mod)
assert v.defs == set(["cars"])
- assert v.variable_data == {"cars": [VariableData("table")]}
+ assert v.variable_data == {
+ "cars": [VariableData("table", qualified_name="cars")]
+ }
assert v.refs == set(["mo"])
diff --git a/tests/_runtime/test_dataflow.py b/tests/_runtime/test_dataflow.py
index 4834f090c2..66e9c9832d 100644
--- a/tests/_runtime/test_dataflow.py
+++ b/tests/_runtime/test_dataflow.py
@@ -564,7 +564,6 @@ class TestSQL:
"3": set(),
}
- @pytest.mark.xfail(reason="TODO: A bug in finding multiply defined names")
def test_redefine_sql_table_diff_schema(self):
graph = dataflow.DirectedGraph()
code = "t1 = 123"
@@ -603,6 +602,25 @@ class TestSQL:
# Without the qualification, t1 is considered multiply defined
assert multiply_defined == ["t1"]
+ def test_sql_table_schema_to_python_ref_rev(self):
+ graph = dataflow.DirectedGraph()
+ code = "t1 = 123"
+ first_cell = parse_cell(code)
+ graph.register_cell("0", first_cell)
+
+ code = 'df = mo.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")'
+ second_cell = parse_cell(code)
+ graph.register_cell("1", second_cell)
+
+ assert graph.cells == {
+ "0": first_cell,
+ "1": second_cell,
+ }
+
+ multiply_defined = graph.get_multiply_defined()
+ # Without the qualification, t1 is considered multiply defined
+ assert multiply_defined == ["t1"]
+
def test_no_sql_table_to_python_ref(self):
graph = dataflow.DirectedGraph()
code = 'df = mo.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")'
diff --git a/tests/_runtime/test_dataflow_cases.py b/tests/_runtime/test_dataflow_cases.py
index fd6428bfd3..b9ba3095b4 100644
--- a/tests/_runtime/test_dataflow_cases.py
+++ b/tests/_runtime/test_dataflow_cases.py
@@ -206,8 +206,6 @@ SQL_CASES = [
expected_parents={"0": [], "1": []},
expected_children={"0": [], "1": []},
expected_refs={"0": ["pd"], "1": ["mo", "my_schema.df"]},
- # This is correct
- # expected_refs={"0": ["pd"], "1": ["df.my_schema", "mo"]},
expected_defs={"0": ["df"], "1": ["result"]},
),
GraphTestCase(
@@ -220,8 +218,6 @@ SQL_CASES = [
expected_parents={"0": [], "1": []},
expected_children={"0": [], "1": []},
expected_refs={"0": [], "1": ["mo", "my_schema.df"]},
- # This is correct
- # expected_refs={"0": ["pd"], "1": ["my_schema.df", "mo"]},
expected_defs={"0": ["my_schema"], "1": []},
),
GraphTestCase(
@@ -234,8 +230,6 @@ SQL_CASES = [
expected_parents={"0": [], "1": []},
expected_children={"0": [], "1": []},
expected_refs={"0": [], "1": ["mo", "my_catalog.my_schema.df"]},
- # This is correct
- # expected_refs={"0": ["pd"], "1": ["my_catalog.my_schema.df", "mo"]},
expected_defs={"0": ["my_catalog"], "1": []},
),
GraphTestCase(
@@ -304,11 +298,7 @@ SQL_CASES = [
"1": ["mo"],
"2": ["mo", "schema_one.my_table"],
},
- # TODO: What should the defs be?
expected_defs={"0": ["my_table"], "1": ["my_table"], "2": []},
- # currently:
- # {'2': {'1', '0'}}
- xfail=True,
),
GraphTestCase(
name="sql definitions with same name as qualified schema and table",
@@ -427,10 +417,6 @@ SQL_CASES = [
"3": ["mo", "catalog_one.schema_two.my_table"],
},
expected_defs={"0": ["my_table"], "1": [], "2": [], "3": []},
- # Currently:
- # '2': {'0'},
- # '3': {'0'},
- xfail=True,
),
GraphTestCase(
name="sql table substring doesn't cause false positive",
@@ -464,6 +450,50 @@ SQL_CASES = [
},
expected_defs={"0": ["my_table"], "1": []},
),
+ GraphTestCase(
+ name="sql table ordering doesn't cause false positives",
+ enabled=HAS_DUCKDB,
+ code={
+ "0": "_ = mo.sql(f'CREATE TABLE catalog_one.schema_one.my_table (name STRING)')",
+ "1": "_ = mo.sql(f'CREATE TABLE catalog_one.my_table.schema_one (name STRING)')",
+ "2": "_ = mo.sql(f'CREATE TABLE schema_one.my_table.catalog_one (name STRING)')",
+ "3": "_ = mo.sql(f'CREATE TABLE schema_one.catalog_one.my_table (name STRING)')",
+ "4": "_ = mo.sql(f'CREATE TABLE my_table.catalog_one.schema_one (name STRING)')",
+ "5": "_ = mo.sql(f'CREATE TABLE my_table.schema_one.catalog_one (name STRING)')",
+ },
+ expected_parents={
+ "0": [],
+ "1": [],
+ "2": [],
+ "3": [],
+ "4": [],
+ "5": [],
+ },
+ expected_children={
+ "0": [],
+ "1": [],
+ "2": [],
+ "3": [],
+ "4": [],
+ "5": [],
+ },
+ expected_refs={
+ "0": ["mo"],
+ "1": ["mo"],
+ "2": ["mo"],
+ "3": ["mo"],
+ "4": ["mo"],
+ "5": ["mo"],
+ },
+ expected_defs={
+ "0": ["my_table"],
+ "1": ["schema_one"],
+ "2": ["catalog_one"],
+ "3": ["my_table"],
+ "4": ["schema_one"],
+ "5": ["catalog_one"],
+ },
+ ),
]
CASES = PYTHON_CASES + SQL_CASES