Skip to content

Commit 75553d9

Browse files
authored
Fix!: Remove SQL dialect config for dbt projects (#1189)
1 parent 6046a49 commit 75553d9

File tree

15 files changed

+65
-98
lines changed

15 files changed

+65
-98
lines changed

docs/reference/cli.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Commands:
2323
format Format all models in a given directory.
2424
ide Start a browser-based SQLMesh IDE.
2525
info Print info.
26-
init Create a new SQLMesh repository with a default SQL dialect.
26+
init Create a new SQLMesh repository.
2727
migrate Migrate SQLMesh to the current running version.
2828
plan Plan a migration of the current context's...
2929
prompt Uses LLM to generate a SQL query from a prompt.
@@ -149,9 +149,10 @@ Options:
149149

150150
## init
151151
```
152-
Usage: sqlmesh init [OPTIONS] SQL_DIALECT
152+
Usage: sqlmesh init [SQL_DIALECT] [OPTIONS]
153153
154-
Create a new SQLMesh repository with a default SQL dialect.
154+
Create a new SQLMesh repository. Argument SQL_DIALECT is required unless the dbt
155+
template option is specified.
155156
156157
Options:
157158
-t, --template TEXT Project template. Support values: airflow, dbt,

docs/reference/notebook.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ positional arguments:
5555

5656
## init
5757
```
58-
%init [--template TEMPLATE] path sql_dialect
58+
%init path sql_dialect [--template TEMPLATE]
5959
60-
Creates a SQLMesh project scaffold with a default SQL dialect.
60+
Creates a SQLMesh project scaffold. Argument `sql_dialect` is required unless the dbt
61+
template option is specified.
6162
6263
positional arguments:
6364
path The path where the new SQLMesh project should be

examples/sushi_dbt/config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sqlmesh.core.config import AirflowSchedulerConfig
44
from sqlmesh.dbt.loader import sqlmesh_config
55

6-
config = sqlmesh_config(Path(__file__).parent, default_sql_dialect="duckdb")
6+
config = sqlmesh_config(Path(__file__).parent)
77

88

99
test_config = config
@@ -12,5 +12,4 @@
1212
airflow_config = sqlmesh_config(
1313
Path(__file__).parent,
1414
default_scheduler=AirflowSchedulerConfig(),
15-
default_sql_dialect="duckdb",
1615
)

sqlmesh/cli/example_project.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ProjectTemplate(Enum):
1111
DEFAULT = "default"
1212

1313

14-
def _gen_config(dialect: str, template: ProjectTemplate) -> str:
14+
def _gen_config(dialect: t.Optional[str], template: ProjectTemplate) -> str:
1515

1616
default_configs = {
1717
ProjectTemplate.DEFAULT: f"""gateways:
@@ -46,7 +46,7 @@ def _gen_config(dialect: str, template: ProjectTemplate) -> str:
4646
4747
from sqlmesh.dbt.loader import sqlmesh_config
4848
49-
config = sqlmesh_config(Path(__file__).parent, default_sql_dialect='{dialect}'))
49+
config = sqlmesh_config(Path(__file__).parent)
5050
""",
5151
}
5252

@@ -153,7 +153,9 @@ def _gen_config(dialect: str, template: ProjectTemplate) -> str:
153153

154154

155155
def init_example_project(
156-
path: t.Union[str, Path], dialect: str, template: ProjectTemplate = ProjectTemplate.DEFAULT
156+
path: t.Union[str, Path],
157+
dialect: t.Optional[str],
158+
template: ProjectTemplate = ProjectTemplate.DEFAULT,
157159
) -> None:
158160
root_path = Path(path)
159161
config_extension = "py" if template == ProjectTemplate.DBT else "yaml"
@@ -167,6 +169,11 @@ def init_example_project(
167169
if config_path.exists():
168170
raise click.ClickException(f"Found an existing config in '{config_path}'")
169171

172+
if not dialect and template != ProjectTemplate.DBT:
173+
raise click.ClickException(
174+
"Default SQL dialect is a required argument for SQLMesh projects"
175+
)
176+
170177
_create_config(config_path, dialect, template)
171178
if template == ProjectTemplate.DBT:
172179
return
@@ -185,7 +192,7 @@ def _create_folders(target_folders: t.Sequence[Path]) -> None:
185192
(folder_path / ".gitkeep").touch()
186193

187194

188-
def _create_config(config_path: Path, dialect: str, template: ProjectTemplate) -> None:
195+
def _create_config(config_path: Path, dialect: t.Optional[str], template: ProjectTemplate) -> None:
189196
project_config = _gen_config(dialect, template)
190197

191198
_write_file(

sqlmesh/cli/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def cli(
9696

9797

9898
@cli.command("init")
99-
@click.argument("sql_dialect")
99+
@click.argument("sql_dialect", required=False)
100100
@click.option(
101101
"-t",
102102
"--template",
@@ -105,8 +105,10 @@ def cli(
105105
)
106106
@click.pass_context
107107
@error_handler
108-
def init(ctx: click.Context, sql_dialect: str, template: t.Optional[str] = None) -> None:
109-
"""Create a new SQLMesh repository with a default SQL dialect."""
108+
def init(
109+
ctx: click.Context, sql_dialect: t.Optional[str] = None, template: t.Optional[str] = None
110+
) -> None:
111+
"""Create a new SQLMesh repository."""
110112
try:
111113
project_template = ProjectTemplate(template.lower() if template else "default")
112114
except ValueError:

sqlmesh/dbt/context.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from dataclasses import dataclass, field, replace
55
from pathlib import Path
66

7-
from sqlmesh.core.config import ModelDefaultsConfig
8-
from sqlmesh.core.engine_adapter import EngineAdapter
97
from sqlmesh.dbt.manifest import ManifestHelper
108
from sqlmesh.dbt.target import TargetConfig
119
from sqlmesh.utils import AttributeDict
@@ -26,7 +24,6 @@ class DbtContext:
2624
"""Context for DBT environment"""
2725

2826
project_root: Path = Path()
29-
model_defaults: t.Optional[ModelDefaultsConfig] = None
3027
target_name: t.Optional[str] = None
3128
profile_name: t.Optional[str] = None
3229
project_schema: t.Optional[str] = None
@@ -36,8 +33,6 @@ class DbtContext:
3633
)
3734
)
3835

39-
engine_adapter: t.Optional[EngineAdapter] = None
40-
4136
_project_name: t.Optional[str] = None
4237
_variables: t.Dict[str, t.Any] = field(default_factory=dict)
4338
_models: t.Dict[str, ModelConfig] = field(default_factory=dict)
@@ -52,8 +47,10 @@ class DbtContext:
5247
_manifest: t.Optional[ManifestHelper] = None
5348

5449
@property
55-
def dialect(self) -> t.Optional[str]:
56-
return self.model_defaults.dialect if self.model_defaults else None
50+
def dialect(self) -> str:
51+
if not self.target:
52+
raise SQLMeshError("Target must be configured before calling the dialect property.")
53+
return self.target.type
5754

5855
@property
5956
def project_name(self) -> t.Optional[str]:
@@ -180,7 +177,6 @@ def target(self, value: TargetConfig) -> None:
180177
raise ConfigError("Project name must be set in the context in order to use a target.")
181178

182179
self._target = value
183-
self.engine_adapter = self._target.to_sqlmesh().create_engine_adapter()
184180
self._jinja_environment = None
185181

186182
def render(self, source: str, **kwargs: t.Any) -> str:

sqlmesh/dbt/loader.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from sqlmesh.dbt.project import Project
2222
from sqlmesh.dbt.target import TargetConfig
2323
from sqlmesh.utils import UniqueKeyDict
24-
from sqlmesh.utils.errors import ConfigError
2524
from sqlmesh.utils.jinja import JinjaMacroRegistry
2625

2726
logger = logging.getLogger(__name__)
@@ -33,29 +32,19 @@
3332
def sqlmesh_config(
3433
project_root: t.Optional[Path] = None,
3534
state_connection: t.Optional[ConnectionConfig] = None,
36-
default_sql_dialect: t.Optional[str] = None,
3735
**kwargs: t.Any,
3836
) -> Config:
39-
if default_sql_dialect is None:
40-
raise ConfigError(
41-
"""Default model SQL dialect is a required configuration parameter. In the project's config.py file, set it in the `sqlmesh_config()` argument `default_sql_dialect`.
42-
43-
For example:
44-
`config = sqlmesh_config(Path(__file__).parent, default_sql_dialect=SQL_DIALECT_IN_QUOTES]))`
45-
"""
46-
)
47-
4837
project_root = project_root or Path()
49-
context = DbtContext(
50-
project_root=project_root, model_defaults=ModelDefaultsConfig(dialect=default_sql_dialect)
51-
)
38+
context = DbtContext(project_root=project_root)
5239
profile = Profile.load(context)
40+
model_defaults = kwargs.get("model_defaults", ModelDefaultsConfig())
41+
model_defaults.dialect = profile.target.type
5342

5443
return Config(
5544
default_gateway=profile.target_name,
5645
gateways={profile.target_name: GatewayConfig(connection=profile.target.to_sqlmesh(), state_connection=state_connection)}, # type: ignore
5746
loader=DbtLoader,
58-
model_defaults=ModelDefaultsConfig(dialect=default_sql_dialect),
47+
model_defaults=model_defaults,
5948
**kwargs,
6049
)
6150

@@ -130,11 +119,7 @@ def _load_project(self) -> Project:
130119
return self._project
131120

132121
self._project = Project.load(
133-
DbtContext(
134-
project_root=self._context.path,
135-
target_name=self._context.gateway,
136-
model_defaults=self._context.config.model_defaults,
137-
)
122+
DbtContext(project_root=self._context.path, target_name=self._context.gateway)
138123
)
139124
for path in self._project.project_files:
140125
self._track_file(path)

sqlmesh/dbt/model.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class ModelConfig(BaseModelConfig):
5151
time_column: The name of the time column
5252
cron: A cron string specifying how often the model should be refreshed, leveraging the
5353
[croniter](https://github.com/kiorky/croniter) library.
54-
dialect: The SQL dialect that the model's query is written in. By default,
55-
this is assumed to be the dialect of the context.
5654
batch_size: The maximum number of incremental intervals that can be run per backfill job. If this is None,
5755
then backfilling this model will do all of history in one job. If this is set, a model's backfill
5856
will be chunked such that each individual job will only contain jobs with max `batch_size` intervals.
@@ -69,7 +67,6 @@ class ModelConfig(BaseModelConfig):
6967
sql: SqlStr = SqlStr("")
7068
time_column: t.Optional[str] = None
7169
cron: t.Optional[str] = None
72-
dialect: t.Optional[str] = None
7370
batch_size: t.Optional[int] = None
7471
lookback: t.Optional[int] = None
7572

@@ -120,10 +117,6 @@ def _validate_partition_by(cls, v: t.Any) -> t.Union[t.List[str], t.Dict[str, t.
120117
},
121118
}
122119

123-
@property
124-
def model_dialect(self) -> t.Optional[str]:
125-
return self.dialect or self.meta.get("dialect", None)
126-
127120
@property
128121
def model_materialization(self) -> Materialization:
129122
return Materialization(self.materialized.lower())
@@ -250,7 +243,7 @@ def _big_query_partition_by_expr(self) -> exp.Expression:
250243

251244
def to_sqlmesh(self, context: DbtContext) -> Model:
252245
"""Converts the dbt model into a SQLMesh model."""
253-
dialect = self.model_dialect or context.dialect
246+
dialect = context.dialect
254247
query = d.jinja_query(self.sql_no_config)
255248

256249
optional_kwargs: t.Dict[str, t.Any] = {}

sqlmesh/dbt/test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class TestConfig(GeneralConfig):
3434
owner: The name of the model under test.
3535
column_name: The name of the column under test.
3636
dependencies: The macros, refs, and sources the test depends upon.
37-
dialect: The sql dialect of the test.
3837
package_name: Name of the package that defines the test.
3938
alias: The alias for the materialized table where failures are stored (Not supported).
4039
schema: The schema for the materialized table where the failures are stored (Not supported).
@@ -56,7 +55,6 @@ class TestConfig(GeneralConfig):
5655
owner: str
5756
column_name: t.Optional[str] = None
5857
dependencies: Dependencies = Dependencies()
59-
dialect: str = ""
6058

6159
# dbt fields
6260
package_name: str = ""
@@ -110,7 +108,7 @@ def to_sqlmesh(self, context: DbtContext) -> Audit:
110108

111109
audit = Audit(
112110
name=self.name,
113-
dialect=self.dialect,
111+
dialect=context.dialect,
114112
skip=skip,
115113
blocking=blocking,
116114
query=query,

sqlmesh/magics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def context(self, line: str) -> None:
9494
@argument(
9595
"sql_dialect",
9696
type=str,
97+
nargs="?",
98+
default=None,
9799
help=f"Default model SQL dialect. Supported values: {sqlglot_dialects()}.",
98100
)
99101
@argument(

0 commit comments

Comments
 (0)