Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions sqlmesh/core/engine_adapter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from sqlglot import Dialect, exp
from sqlglot.errors import ErrorLevel
from sqlglot.helper import ensure_list, seq_get
from sqlglot.helper import ensure_list, find_new_name, seq_get
from sqlglot.optimizer.qualify_columns import quote_identifiers

from sqlmesh.core.dialect import (
Expand Down Expand Up @@ -2073,6 +2073,11 @@ def remove_managed_columns(
prefixed_col = exp.column(column).copy()
prefixed_col.this.set("this", f"t_{prefixed_col.name}")
prefixed_unmanaged_columns.append(prefixed_col)
target_exists_column = find_new_name(
{col.name.lower() for col in prefixed_columns_to_types}
| {col.lower() for col in unmanaged_columns_to_types},
"t__exists",
)
query = (
exp.Select() # type: ignore
.select(*table_columns)
Expand Down Expand Up @@ -2141,6 +2146,7 @@ def remove_managed_columns(
"joined",
exp.select(
exp.column("_exists", table="source").as_("_exists"),
exp.column("_exists", table="latest").as_(target_exists_column),
*(
exp.column(col, table="latest").as_(prefixed_columns_to_types[i].this)
for i, col in enumerate(target_columns_to_types)
Expand All @@ -2164,6 +2170,7 @@ def remove_managed_columns(
.union(
exp.select(
exp.column("_exists", table="source").as_("_exists"),
exp.column("_exists", table="latest").as_(target_exists_column),
*(
exp.column(col, table="latest").as_(
prefixed_columns_to_types[i].this
Expand Down Expand Up @@ -2195,11 +2202,15 @@ def remove_managed_columns(
"updated_rows",
exp.select(
*(
exp.func(
"COALESCE",
exp.Case()
.when(
exp.column(target_exists_column, table="joined")
.is_(exp.Null())
.not_(),
exp.column(prefixed_unmanaged_columns[i].this, table="joined"),
exp.column(col, table="joined"),
).as_(col)
)
.else_(exp.column(col, table="joined"))
.as_(col)
for i, col in enumerate(unmanaged_columns_to_types)
),
valid_from_case_stmt,
Expand Down
Loading