Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(managed-warehouse): accept tuple columns and huge limits in trino builds (#123148)

Jovan Sakovice4d27b5e4113
diff --git a/posthog/hogql/printer/test/test_trino_printer.py b/posthog/hogql/printer/test/test_trino_printer.py
index 6b702f45af4d..75e067902bdb 100644
--- a/posthog/hogql/printer/test/test_trino_printer.py
+++ b/posthog/hogql/printer/test/test_trino_printer.py
@@ -1793,6 +1793,15 @@ def test_rejects_invalid_trino_cast_types(type_name: str) -> None:
"SELECT user_id FROM users ORDER BY user_id LIMIT 10 WITH TIES",
"FETCH FIRST 10 ROWS WITH TIES",
),
+ ("SELECT user_id FROM users ORDER BY user_id LIMIT 2147483647", "ASC LIMIT 2147483647"),
+ # Trino cannot sort with a LIMIT above 2147483647, and a LIMIT that large cannot cut a result.
+ ("SELECT user_id FROM users ORDER BY user_id LIMIT 1000000000000", '"users"."user_id" ASC'),
+ ("SELECT user_id FROM users ORDER BY user_id LIMIT 1000000000000 OFFSET 4", "ASC OFFSET 4 ROWS"),
+ ("SELECT user_id FROM users ORDER BY user_id LIMIT 1000000000000 WITH TIES", '"users"."user_id" ASC'),
+ ("SELECT user_id FROM users ORDER BY user_id LIMIT 2147483647 OFFSET 1", "ASC OFFSET 1 ROWS"),
+ # An unsorted LIMIT has no cap in Trino and can cut a nested result, so it stays.
+ ("SELECT user_id FROM users LIMIT 2147483648", "LIMIT 2147483648"),
+ ("SELECT count() FROM (SELECT user_id FROM users LIMIT 2147483648)", "LIMIT 2147483648)"),
],
)
def test_prints_trino_limit_syntax(query: str, expected_suffix: str) -> None:
@@ -1801,15 +1810,32 @@ def test_prints_trino_limit_syntax(query: str, expected_suffix: str) -> None:
assert sql.endswith(expected_suffix)
-def test_prints_trino_limit_syntax_after_set_operation() -> None:
+ "query",
+ [
+ "SELECT count() FROM (SELECT user_id FROM users ORDER BY user_id LIMIT 2147483648)",
+ "SELECT count() FROM (SELECT user_id FROM users ORDER BY user_id LIMIT 2147483647 OFFSET 1)",
+ ],
+)
+def test_rejects_nested_sorted_limit_above_trino_cap(query: str) -> None:
+ with pytest.raises(TrinoLoweringError) as error:
+ prepare_and_print_ast(parse_select(query), _context_with_trino_table(), "trino")
+
+ assert error.value.feature_code == "TRINO_SORTED_LIMIT_TOO_LARGE"
+
+
+ "limit, expected_suffix", [(10, "OFFSET 4 ROWS LIMIT 10"), (2**40, "OFFSET 4 ROWS LIMIT 1099511627776")]
+)
+def test_prints_trino_limit_syntax_after_set_operation(limit: int, expected_suffix: str) -> None:
query = parse_select("SELECT user_id FROM users UNION ALL SELECT user_id FROM users")
assert isinstance(query, ast.SelectSetQuery)
- query.limit = ast.Constant(value=10)
+ query.limit = ast.Constant(value=limit)
query.offset = ast.Constant(value=4)
sql, _ = prepare_and_print_ast(query, _context_with_trino_table(), "trino")
- assert sql.endswith("OFFSET 4 ROWS LIMIT 10")
+ assert sql.endswith(expected_suffix)
@pytest.mark.parametrize("pretty", [False, True])
diff --git a/posthog/hogql/printer/trino.py b/posthog/hogql/printer/trino.py
index cd0fd7c30d01..afc8d6c63e33 100644
--- a/posthog/hogql/printer/trino.py
+++ b/posthog/hogql/printer/trino.py
@@ -45,6 +45,10 @@ from posthog.exchange_rate_constants import EXCHANGE_RATE_DECIMAL_PRECISION
if TYPE_CHECKING:
from posthog.hogql.database.models import Table
+# Trino refuses a sorted LIMIT whose LIMIT + OFFSET is above this. On the outermost query a LIMIT that large
+# cannot cut a result, so the printer drops it. In a nested query it can, so the printer refuses it.
+TRINO_MAX_LIMIT = 2**31 - 1
+
_TRINO_SIMPLE_TYPES = {
"bigint": "BIGINT",
"bool": "BOOLEAN",
@@ -313,11 +317,20 @@ class TrinoPrinter(PostgresPrinter):
def _append_select_limit_and_offset(
self, clauses: list[str | None], node: ast.SelectQuery, limit: ast.Expr | None
) -> None:
+ offset = self._validate_row_count(node.offset, "OFFSET") if node.offset is not None else 0
if node.offset is not None:
- clauses.append(f"OFFSET {self._validate_row_count(node.offset, 'OFFSET')} ROWS")
+ clauses.append(f"OFFSET {offset} ROWS")
if limit is None:
return
count = self._validate_row_count(limit, "LIMIT")
+ if node.order_by and not node.limit_percent and count + offset > TRINO_MAX_LIMIT:
+ if len(self.stack) > 1:
+ self._unsupported(
+ "TRINO_SORTED_LIMIT_TOO_LARGE",
+ f"A sorted LIMIT plus OFFSET above {TRINO_MAX_LIMIT} is only supported on the outermost query in Trino mode.",
+ node,
+ )
+ return
if node.limit_percent:
self._unsupported("TRINO_LIMIT_PERCENT_UNSUPPORTED", "LIMIT PERCENT is not supported in Trino mode.")
if node.limit_with_ties:
diff --git a/products/managed_warehouse/backend/facade/contracts.py b/products/managed_warehouse/backend/facade/contracts.py
index 9ad6204465f0..466ff42fa65d 100644
--- a/products/managed_warehouse/backend/facade/contracts.py
+++ b/products/managed_warehouse/backend/facade/contracts.py
@@ -380,6 +380,11 @@ class TrinoCompiledQuery:
sql: str
values: dict[str, Any]
hogql: str | None = None
+ needs_row_type_probe: bool = True
+ """Whether an output column may hold a ROW with anonymous fields.
+
+ False only when every output column resolved to a type without a tuple or an unknown part.
+ """
@frozen
diff --git a/products/managed_warehouse/backend/tests/test_trino_compiler.py b/products/managed_warehouse/backend/tests/test_trino_compiler.py
index 207f12f6c177..696f93aa63cb 100644
--- a/products/managed_warehouse/backend/tests/test_trino_compiler.py
+++ b/products/managed_warehouse/backend/tests/test_trino_compiler.py
@@ -272,6 +272,44 @@ class TestCompileHogQLToTrinoSQL:
assert access_key not in compiled.sql
assert access_secret not in compiled.sql
+ @pytest.mark.django_db
+ @pytest.mark.parametrize(
+ "query, needs_probe",
+ [
+ ("SELECT 1 AS id, 'x' AS name", False),
+ ("SELECT [1, 2] AS ids, 'x' AS name", False),
+ ("SELECT tuple(1, 'x') AS tag", True),
+ ("SELECT 1 AS id, [tuple(1, 'x')] AS tags", True),
+ ("SELECT map('a', tuple(1)) AS pairs", True),
+ ],
+ )
+ def test_flags_output_columns_that_may_hold_anonymous_rows(self, query: str, needs_probe: bool) -> None:
+ organization = Organization.objects.create(name="trino-row-type-probe")
+ team = Team.objects.create(organization=organization)
+ membership = _membership(team_id=team.pk, organization_id=str(organization.pk))
+
+ with (
+ mock.patch("products.managed_warehouse.backend.cp_teams.list_org_teams", return_value=[]),
+ mock.patch(
+ "products.managed_warehouse.backend.trino_compiler.get_ready_trino_catalog_name",
+ return_value="org_catalog",
+ ),
+ mock.patch(
+ "products.managed_warehouse.backend.trino_compiler.get_org_team_membership",
+ return_value=membership,
+ ),
+ ):
+ compiled = compile_hogql_to_trino_sql(
+ team.pk,
+ HogQLQuery(query=query),
+ team=team,
+ expansion_mode=TrinoExpansionMode.DJANGO,
+ bypass_warehouse_access_control=True,
+ preserve_clickhouse_output_names=True,
+ )
+
+ assert compiled.needs_row_type_probe is needs_probe
+
@pytest.mark.parametrize(
"limit_clause, trino_limit_clause",
[
diff --git a/products/managed_warehouse/backend/tests/test_trino_materialization.py b/products/managed_warehouse/backend/tests/test_trino_materialization.py
index ee88d00e817f..81f3b43474ba 100644
--- a/products/managed_warehouse/backend/tests/test_trino_materialization.py
+++ b/products/managed_warehouse/backend/tests/test_trino_materialization.py
@@ -66,7 +66,7 @@ class TestTrinoShadowMaterialization(BaseTest):
@parameterized.expand([(False,), (True,)])
def test_compiles_and_executes_without_saved_translation(self, fail_write: bool) -> None:
if fail_write:
- self.cursor.fetchall.side_effect = [[], RuntimeError("Trino write failed")]
+ self.cursor.fetchall.side_effect = [[], [], RuntimeError("Trino write failed")]
with (
patch(
"products.managed_warehouse.backend.trino_materialization.compile_hogql_to_trino_sql",
@@ -103,7 +103,7 @@ class TestTrinoShadowMaterialization(BaseTest):
assert self.cursor.execute.call_args_list[0].args == (
f'CREATE SCHEMA IF NOT EXISTS "org_""catalog"."posthog_data_modeling_team_{self.team.pk}"',
)
- assert self.cursor.execute.call_args_list[1].args == (
+ assert self.cursor.execute.call_args_list[2].args == (
f'CREATE OR REPLACE TABLE "org_""catalog"."posthog_data_modeling_team_{self.team.pk}"."model_{self.saved_query_id.hex}" '
'AS SELECT name FROM "org_catalog"."imports"."orders" WHERE name = ?',
["example' OR 1=1 --"],
@@ -393,14 +393,16 @@ class _ScriptedCursor:
processed_bytes: int = 0,
cpu_milliseconds: int = 0,
unreachable_from: str | None = None,
+ output_types: list[tuple[str, ...]] | None = None,
) -> None:
+ self.output_types = output_types
self.table_exists = table_exists
self.staged = staged
self.duplicate = duplicate
self.max_key = max_key
self.statements: list[tuple[str, Any]] = []
self.rowcount = -1
- self.description: list[tuple[str]] | None = None
+ self.description: list[tuple[str, ...]] | None = None
self.processed_bytes = processed_bytes
self.cpu_milliseconds = cpu_milliseconds
self.unreachable_from = unreachable_from
@@ -420,7 +422,7 @@ class _ScriptedCursor:
elif "HAVING count(*) > 1" in statement:
self._rows = [(1,)] if self.duplicate else []
elif statement.endswith("LIMIT 0"):
- self.description = [("id",), ("ts",)]
+ self.description = list(self.output_types or [("id",), ("ts",)])
elif statement.startswith("MERGE"):
self.rowcount = self.staged
elif statement.startswith("SELECT max("):
@@ -489,6 +491,84 @@ class TestTrinoIncrementalMaterialization(BaseTest):
assert f'SELECT max("ts") FROM {stage}' in statements
assert statements[-1] == f"DROP TABLE IF EXISTS {stage}"
+ @parameterized.expand(
+ [
+ (
+ "nested_anonymous_rows",
+ "row(varchar(8), varchar(1), array(row(varchar)))",
+ 'row("1" varchar(8), "2" varchar(1), "3" array(row("1" varchar)))',
+ ),
+ (
+ "rows_inside_a_map",
+ "map(varchar, row(timestamp(3) with time zone, decimal(10, 2)))",
+ 'map(varchar, row("1" timestamp(3) with time zone, "2" decimal(10, 2)))',
+ ),
+ (
+ "named_fields_kept",
+ 'row("a" bigint, date varchar, row(bigint))',
+ 'row("a" bigint, "date" varchar, "3" row("1" bigint))',
+ ),
+ ("named_row", 'row("a" bigint)', None),
+ ("named_row_with_percent", 'row("discount %" bigint)', None),
+ (
+ "named_field_with_percent_kept",
+ 'row("discount %" bigint, bigint)',
+ 'row("discount %" bigint, "2" bigint)',
+ ),
+ (
+ "quoted_parenthesis_in_nested_row",
+ 'row(row("metric)" bigint), row("metric(" bigint), bigint)',
+ 'row("1" row("metric)" bigint), "2" row("metric(" bigint), "3" bigint)',
+ ),
+ (
+ "quoted_parenthesis_in_nested_array",
+ 'row(array(row("x)" bigint)), array(row("x(" bigint)))',
+ 'row("1" array(row("x)" bigint)), "2" array(row("x(" bigint)))',
+ ),
+ ("plain_column", "array(varchar)", None),
+ ]
+ )
+ def test_names_anonymous_row_fields_in_the_output(self, _name: str, output_type: str, named: str | None) -> None:
+ cursor = _ScriptedCursor(table_exists=False, staged=1, output_types=[("tag", output_type), ("id", "bigint")])
+
+ _, statements, _ = self.run_build(cursor, None)
+
+ table = f'"cat"."posthog_data_modeling_team_{self.team.pk}"."model_{self.saved_query_id.hex}"'
+ (write,) = [statement for statement in statements if statement.startswith("CREATE OR REPLACE TABLE")]
+ if named is None:
+ assert write == f"CREATE OR REPLACE TABLE {table} AS SELECT id, ts FROM orders"
+ else:
+ assert write == (
+ f'CREATE OR REPLACE TABLE {table} AS SELECT CAST("__ph_output"."tag" AS {named}) AS "tag", '
+ '"__ph_output"."id" AS "id" FROM (SELECT id, ts FROM orders) AS "__ph_output"'
+ )
+
+ def test_skips_the_zero_row_run_when_no_output_can_be_a_row(self) -> None:
+ cursor = _ScriptedCursor(table_exists=False, staged=1, output_types=[("tag", "row(bigint)")])
+ compiled = TrinoCompiledQuery(sql="SELECT id, ts FROM orders", values={}, needs_row_type_probe=False)
+ connection = MagicMock(catalog="cat")
+ connection.cursor.return_value = cursor
+ with (
+ patch(
+ "products.managed_warehouse.backend.trino_materialization.compile_hogql_to_trino_sql",
+ return_value=compiled,
+ ),
+ patch(
+ "products.managed_warehouse.backend.trino_materialization.connect_managed_warehouse_trino"
+ ) as connect,
+ ):
+ connect.return_value.__enter__.return_value = connection
+ execute_trino_model_statements(
+ organization_id=str(self.organization.pk),
+ team_id=self.team.pk,
+ saved_query_id=self.saved_query_id,
+ source_query=self.query,
+ )
+
+ statements = [statement for statement, _ in cursor.statements]
+ assert not any(statement.endswith("LIMIT 0") for statement in statements)
+ assert any(statement.startswith("CREATE OR REPLACE TABLE") for statement in statements)
+
@parameterized.expand(
[
("quiet_window", _ScriptedCursor(table_exists=True, staged=0), None),
@@ -524,10 +604,10 @@ class TestTrinoIncrementalMaterialization(BaseTest):
@parameterized.expand(
[
- # Schema, table check, stage build, duplicate check, column read, merge and watermark read.
- ("merge_succeeds", False, 7),
+ # Schema, table check, output type read, stage build, duplicate check, column read, merge and watermark read.
+ ("merge_succeeds", False, 8),
# The run stops at the duplicate check, and its statements still count.
- ("duplicate_keys_fail_the_merge", True, 4),
+ ("duplicate_keys_fail_the_merge", True, 5),
]
)
def test_reports_usage_by_every_statement(self, _name: str, duplicate: bool, statements: int) -> None:
@@ -553,4 +633,4 @@ class TestTrinoIncrementalMaterialization(BaseTest):
result, _, _ = self.run_build(cursor, self.since, TrinoQueryControl(on_processed_bytes=processed.append))
assert isinstance(result, ConnectionError)
- assert processed == [100] * 5
+ assert processed == [100] * 6
diff --git a/products/managed_warehouse/backend/trino_compiler.py b/products/managed_warehouse/backend/trino_compiler.py
index 26f2633c4ec2..9ca1e34d46dd 100644
--- a/products/managed_warehouse/backend/trino_compiler.py
+++ b/products/managed_warehouse/backend/trino_compiler.py
@@ -423,11 +423,21 @@ def compile_hogql_to_trino_sql(
]
return prepared
+ needs_row_type_probe = True
+
+ def prepare_output(prepared: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:
+ nonlocal needs_row_type_probe
+ output = prepared
+ while isinstance(output, ast.SelectSetQuery):
+ output = output.initial_select_query
+ needs_row_type_probe = any(_may_hold_anonymous_row(column, trino_context) for column in output.select)
+ return preserve_output_names(prepared) if preserve_clickhouse_output_names else prepared
+
trino_sql, _ = prepare_and_print_ast(
parsed,
trino_context,
dialect="trino",
- prepared_ast_transform=preserve_output_names if preserve_clickhouse_output_names else None,
+ prepared_ast_transform=prepare_output,
)
hogql_pretty: str | None = None
@@ -449,4 +459,32 @@ def compile_hogql_to_trino_sql(
resolved_hogql = resolve_types(hogql_ast, hogql_context, dialect="trino")
hogql_pretty = TrinoHogQLPrinter(context=hogql_context).visit(resolved_hogql)
- return TrinoCompiledQuery(sql=trino_sql, values=dict(trino_context.values), hogql=hogql_pretty)
+ return TrinoCompiledQuery(
+ sql=trino_sql,
+ values=dict(trino_context.values),
+ hogql=hogql_pretty,
+ needs_row_type_probe=needs_row_type_probe,
+ )
+
+
+def _may_hold_anonymous_row(column: Any, context: Any) -> bool:
+ """Whether the resolved type of an output column is, or contains, a tuple or an unknown type."""
+ from posthog.hogql import ast # noqa: PLC0415 -- keeps compiler imports off Django startup
+
+ def check(constant_type: ast.ConstantType) -> bool:
+ if isinstance(constant_type, ast.TupleType | ast.UnknownType):
+ return True
+ if isinstance(constant_type, ast.ArrayType):
+ return check(constant_type.item_type)
+ if isinstance(constant_type, ast.MapType):
+ return check(constant_type.key_type) or check(constant_type.value_type)
+ if isinstance(constant_type, ast.AggregateStateType):
+ return check(constant_type.wrapped_type)
+ return False
+
+ if column.type is None:
+ return True
+ try:
+ return check(column.type.resolve_constant_type(context))
+ except Exception: # noqa: BLE001 -- a type that cannot be resolved is probed, which is always safe
+ return True
diff --git a/products/managed_warehouse/backend/trino_materialization.py b/products/managed_warehouse/backend/trino_materialization.py
index bc4371748c21..7e461b789c09 100644
--- a/products/managed_warehouse/backend/trino_materialization.py
+++ b/products/managed_warehouse/backend/trino_materialization.py
@@ -1,5 +1,7 @@
from __future__ import annotations
+import re
+from collections.abc import Iterator
from typing import TYPE_CHECKING, Any
from uuid import UUID
@@ -10,6 +12,7 @@ from posthog.schema import HogQLQuery
from posthog.hogql.escape_sql import escape_trino_identifier
from posthog.hogql.trino_parameters import convert_pyformat_placeholders
+from posthog.dataclasses import frozen
from posthog.models import Team
from products.data_modeling.backend.facade.system_tables import DATA_MODELING_ALLOWED_SYSTEM_TABLES
@@ -75,6 +78,8 @@ def execute_trino_shadow_materialization(
merge = False
compiled = _compile(team, hogql_query, None)
sql, parameters = convert_pyformat_placeholders(compiled.sql, compiled.values)
+ if compiled.needs_row_type_probe:
+ sql = _name_anonymous_row_fields(cursor, control, sql, parameters)
if merge:
assert incremental is not None
stage = f"{schema}.{escape_trino_identifier(table_name + STAGE_SUFFIX)}"
@@ -163,6 +168,158 @@ def _merge(
return staged, _max_key(cursor, control, stage, incremental.incremental_key)
+_OUTPUT_ALIAS = "__ph_output"
+_ROW_TYPE_NAMES = frozenset(
+ {
+ "array",
+ "bigint",
+ "boolean",
+ "char",
+ "date",
+ "decimal",
+ "double",
+ "integer",
+ "interval",
+ "ipaddress",
+ "json",
+ "map",
+ "real",
+ "row",
+ "smallint",
+ "time",
+ "timestamp",
+ "tinyint",
+ "unknown",
+ "uuid",
+ "varbinary",
+ "varchar",
+ }
+)
+_ROW_TYPE_SUFFIXES = frozenset({"", "with time zone", "without time zone", "day to second", "year to month"})
+
+
+def _name_anonymous_row_fields(
+ cursor: Cursor, control: TrinoQueryControl | None, sql: str, parameters: list[object]
+) -> str:
+ """Cast every anonymous ROW field in the output to a named one.
+
+ DuckLake refuses anonymous row fields, and Trino gives no way to name them without spelling
+ out the type, so the output types are read from a zero-row run of the query. Fields are named
+ "1", "2", ... as ClickHouse names unnamed tuple elements, so both builds store the same struct.
+ """
+ _fetch(cursor, control, f"SELECT * FROM ({sql}) LIMIT 0", parameters)
+ projections: list[str] = []
+ renamed = False
+ for column in cursor.description or []:
+ name = escape_trino_identifier(column[0])
+ reference = f"{escape_trino_identifier(_OUTPUT_ALIAS)}.{name}"
+ # The DB-API stubs type this as an int code; the Trino client returns the type name.
+ type_code: object = column[1] if len(column) > 1 else None
+ type_text = type_code if isinstance(type_code, str) else None
+ named_type = _named_row_type(type_text) if type_text is not None else None
+ if named_type is None:
+ projections.append(f"{reference} AS {name}")
+ else:
+ renamed = True
+ projections.append(f"CAST({reference} AS {named_type}) AS {name}")
+ if not renamed:
+ return sql
+ return f"SELECT {', '.join(projections)} FROM ({sql}) AS {escape_trino_identifier(_OUTPUT_ALIAS)}"
+
+
+def _named_row_type(type_text: str) -> str | None:
+ """Return ``type_text`` with each anonymous row field named by position, or None when none is anonymous."""
+ text = type_text.strip()
+ lowered = text.lower()
+ for prefix in ("row(", "array(", "map("):
+ if not (lowered.startswith(prefix) and text.endswith(")")):
+ continue
+ parts = _split_type_arguments(text[len(prefix) : -1])
+ renamed = False
+ arguments: list[str] = []
+ for position, part in enumerate(parts, start=1):
+ field_name, field_type = _split_row_field(part) if prefix == "row(" else (None, part.strip())
+ named_type = _named_row_type(field_type)
+ renamed = renamed or named_type is not None or (prefix == "row(" and field_name is None)
+ argument = named_type or field_type
+ if prefix == "row(":
+ argument = f"{_quote_row_field(field_name or str(position))} {argument}"
+ arguments.append(argument)
+ return f"{prefix}{', '.join(arguments)})" if renamed else None
+ return None
+
+
+def _quote_row_field(name: str) -> str:
+ # Field names come from Trino's own type text, so they skip escape_trino_identifier's limits on user input.
+ return '"' + name.replace('"', '""') + '"'
+
+
+@frozen
+class _UnquotedCharacter:
+ index: int
+ character: str
+ depth: int # parenthesis depth after this character
+
+
+def _unquoted_characters(text: str) -> Iterator[_UnquotedCharacter]:
+ """Yield each character outside quoted identifiers with its parenthesis depth after it."""
+ depth = 0
+ quoted = False
+ for index, character in enumerate(text):
+ if character == '"':
+ quoted = not quoted
+ continue
+ if quoted:
+ continue
+ if character == "(":
+ depth += 1
+ elif character == ")":
+ depth -= 1
+ yield _UnquotedCharacter(index=index, character=character, depth=depth)
+
+
+def _split_type_arguments(text: str) -> list[str]:
+ parts: list[str] = []
+ start = 0
+ for scanned in _unquoted_characters(text):
+ if scanned.character == "," and scanned.depth == 0:
+ parts.append(text[start : scanned.index])
+ start = scanned.index + 1
+ parts.append(text[start:])
+ return parts
+
+
+def _split_row_field(text: str) -> tuple[str | None, str]:
+ text = text.strip()
+ if text.startswith('"'):
+ index = 1
+ while index < len(text):
+ if text[index] == '"':
+ if text[index + 1 : index + 2] == '"':
+ index += 2
+ continue
+ return text[1:index].replace('""', '"'), text[index + 1 :].strip()
+ index += 1
+ if _is_type(text):
+ return None, text
+ name, _, field_type = text.partition(" ")
+ return name, field_type.strip()
+
+
+def _is_type(text: str) -> bool:
+ # An unquoted field name looks like a type name, so `date varchar` is a field and `date` is a type.
+ match = re.match(r"[a-z_][a-z0-9_]*", text, re.IGNORECASE)
+ if match is None or match.group(0).lower() not in _ROW_TYPE_NAMES:
+ return False
+ rest = text[match.end() :]
+ if rest.startswith("("):
+ for scanned in _unquoted_characters(rest):
+ if scanned.character == ")" and scanned.depth == 0:
+ rest = rest[scanned.index + 1 :]
+ break
+ return rest.strip().lower() in _ROW_TYPE_SUFFIXES
+
+
def _drop_stage(cursor: Cursor, stage: str) -> None:
# A leftover stage costs storage only; the next run replaces it. Never mask the build's own error.
try: