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: align SQL interpolation error offsets (#11159)

Shahmir Varqhac15169deec9e
diff --git a/marimo/_sql/parse.py b/marimo/_sql/parse.py
index 04d7db1be0..5e22c5b4a7 100644
--- a/marimo/_sql/parse.py
+++ b/marimo/_sql/parse.py
@@ -151,20 +151,15 @@ def _parse_sql_duckdb(
line_prefix = subquery[last_newline_idx + 1 :]
column_number = len(line_prefix.encode("utf-16-le")) // 2
- # Adjust column_number to account for any added quotes from bracket replacements
- # SELECT {id} FRO users
- # ^ error position should be here
- # SELECT {id} FRO users
- # ^ user sees this
- # So in this case, we subtract the offset from the column position.
- # If the error is before the brackets, we don't need to add the offset because it just increases the string length
- # Column position will be the same as the user sees.
+ # Bracket indices refer to the original SQL, but the error position refers
+ # to SQL with added quotes. Compare both in the latter coordinate system.
error_line_start = last_newline_idx + 1
cumulative_offset = 0
+ preceding_offset = 0
for idx, offset in idx_to_offset_dict.items():
- # Only count replacements that are on the same line as the error
- # and come before the error position
- if error_line_start <= idx < position:
+ sanitized_idx = idx + preceding_offset
+ preceding_offset += offset
+ if error_line_start <= sanitized_idx < position:
cumulative_offset += offset
column_number -= cumulative_offset
diff --git a/tests/_sql/test_sql_parse.py b/tests/_sql/test_sql_parse.py
index 2e94a06be6..48ed163c66 100644
--- a/tests/_sql/test_sql_parse.py
+++ b/tests/_sql/test_sql_parse.py
@@ -436,9 +436,6 @@ FRM users"""
result, error = parse_sql(query_with_brackets, "duckdb")
self.assert_line_column(result, error, expected_line, expected_column)
- @pytest.mark.xfail(
- reason="There is an incorrect calculation for column position"
- )
def test_brackets_on_error_line(self):
# Brackets on error line
query_error_line = """SELECT name,
@@ -455,6 +452,25 @@ id FRM users"""
result, error, expected_line, expected_column + 2
)
+ @pytest.mark.parametrize(
+ "query",
+ [
+ "SELECT {a}, {b}, {c}, {d}, {e};\nSELECT {value} FRM users",
+ "SELECT 'é😀', {a},\n{id} FRM users",
+ "SELECT {a}, {b}, {c}, {d}, {e} FRM users, {other}",
+ ],
+ )
+ def test_bracket_positions_after_replacements(self, query: str):
+ result, error = parse_sql(query, "duckdb")
+ prefix = query[: query.index("users")]
+ line_prefix = prefix.rsplit("\n", 1)[-1]
+ self.assert_line_column(
+ result,
+ error,
+ prefix.count("\n") + 1,
+ len(line_prefix.encode("utf-16-le")) // 2,
+ )
+
def test_multiline_brackets_after_error(self):
query = """SELECT *
FRM users WHERE name = name"""