Skip to content

Implement full refresh preview option #1671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
47 changes: 32 additions & 15 deletions dbt_core_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def __init__(
favor_state: Optional[bool] = False,
# dict in 1.5.x onwards, json string before.
vars: Optional[Union[Dict[str, Any], str]] = {} if DBT_MAJOR_VER >= 1 and DBT_MINOR_VER >= 5 else "{}",
full_refresh: Optional[bool] = False,
):
self.threads = threads
self.target = target if target else os.environ.get("DBT_TARGET")
Expand All @@ -291,6 +292,7 @@ def __init__(
self.defer = defer
self.state = state
self.favor_state = favor_state
self.full_refresh = full_refresh
# dict in 1.5.x onwards, json string before.
if DBT_MAJOR_VER >= 1 and DBT_MINOR_VER >= 5:
self.vars = vars if vars else json.loads(os.environ.get("DBT_VARS", "{}"))
Expand Down Expand Up @@ -358,6 +360,7 @@ def __init__(
manifest_path: Optional[str] = None,
favor_state: bool = False,
vars: Optional[Dict[str, Any]] = {},
full_refresh: bool = False,
):
self.args = ConfigInterface(
threads=threads,
Expand All @@ -370,6 +373,7 @@ def __init__(
state=manifest_path,
favor_state=favor_state,
vars=vars,
full_refresh=full_refresh,
)

# Utilities
Expand Down Expand Up @@ -674,21 +678,28 @@ def execute_macro(
"""Wraps adapter execute_macro. Execute a macro like a function."""
return self.get_macro_function(macro, compiled_code)(kwargs=kwargs)

def execute_sql(self, raw_sql: str, original_node: Optional[Union["ManifestNode", str]] = None) -> DbtAdapterExecutionResult:
def execute_sql(self, raw_sql: str, original_node: Optional[Union["ManifestNode", str]] = None, full_refresh: bool = False) -> DbtAdapterExecutionResult:
"""Execute dbt SQL statement against database"""
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execute_sql method's docstring doesn't mention the new full_refresh parameter; update the docstring to explain how this flag alters execution behavior.

Suggested change
"""Execute dbt SQL statement against database"""
"""Execute dbt SQL statement against the database.
Args:
raw_sql (str): The raw SQL statement to execute.
original_node (Optional[Union["ManifestNode", str]]): The original node associated with the SQL, if any.
full_refresh (bool): If True, forces a full refresh of incremental models by setting the `full_refresh`
flag in the execution context. This temporarily overrides the `self.args.full_refresh` attribute.
Returns:
DbtAdapterExecutionResult: The result of the SQL execution, including raw and compiled SQL.
"""

Copilot uses AI. Check for mistakes.

with self.adapter.connection_named("master"):
# if no jinja chars then these are synonymous
compiled_sql = raw_sql
if has_jinja(raw_sql):
# jinja found, compile it
compilation_result = self._compile_sql(raw_sql, original_node)
compiled_sql = compilation_result.compiled_sql

return DbtAdapterExecutionResult(
*self.adapter_execute(compiled_sql, fetch=True),
raw_sql,
compiled_sql,
)
prev = getattr(self.args, "full_refresh", False)
self.args.full_refresh = full_refresh
set_from_args(self.args, None)
try:
with self.adapter.connection_named("master"):
# if no jinja chars then these are synonymous
compiled_sql = raw_sql
if has_jinja(raw_sql):
# jinja found, compile it
compilation_result = self._compile_sql(raw_sql, original_node)
compiled_sql = compilation_result.compiled_sql

return DbtAdapterExecutionResult(
*self.adapter_execute(compiled_sql, fetch=True),
raw_sql,
compiled_sql,
)
finally:
self.args.full_refresh = prev
set_from_args(self.args, None)

def execute_node(self, node: "ManifestNode") -> DbtAdapterExecutionResult:
"""Execute dbt SQL statement against database from a"ManifestNode"""
Expand All @@ -707,12 +718,18 @@ def execute_node(self, node: "ManifestNode") -> DbtAdapterExecutionResult:
except Exception as e:
raise Exception(str(e))

def compile_sql(self, raw_sql: str, original_node: Optional["ManifestNode"] = None) -> DbtAdapterCompilationResult:
def compile_sql(self, raw_sql: str, original_node: Optional["ManifestNode"] = None, full_refresh: bool = False) -> DbtAdapterCompilationResult:
try:
prev = getattr(self.args, "full_refresh", False)
self.args.full_refresh = full_refresh
set_from_args(self.args, None)
with self.adapter.connection_named("master"):
return self._compile_sql(raw_sql, original_node)
except Exception as e:
raise Exception(str(e))
finally:
self.args.full_refresh = prev
set_from_args(self.args, None)

def compile_node(
self, node: "ManifestNode"
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@
"default": 500,
"minimum": 1
},
"dbt.previewFullRefresh": {
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Document the new dbt.previewFullRefresh setting in the project README or changelog so users know about and can configure the full-refresh preview mode.

Copilot uses AI. Check for mistakes.

"type": "boolean",
"description": "When enabled, compiled SQL previews use full refresh mode so `is_incremental()` evaluates to false.",
"default": false
},
"dbt.perspectiveTheme": {
"type": "string",
"description": "Theme for perspective viewer in query results panel",
Expand Down
13 changes: 12 additions & 1 deletion src/dbt_client/dbtCloudIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export class DBTCloudProjectIntegration
query: string,
limit: number,
modelName: string,
fullRefresh = false,
): Promise<QueryExecution> {
this.throwIfNotAuthenticated();
this.throwBridgeErrorIfAvailable();
Expand All @@ -267,6 +268,9 @@ export class DBTCloudProjectIntegration
"json",
]),
);
if (fullRefresh) {
showCommand.addArgument("--full-refresh");
}
const cancellationTokenSource = new CancellationTokenSource();
showCommand.setToken(cancellationTokenSource.token);
return new QueryExecution(
Expand Down Expand Up @@ -657,7 +661,11 @@ export class DBTCloudProjectIntegration
return compiledLine[0].data.compiled;
}

async unsafeCompileQuery(query: string): Promise<string> {
async unsafeCompileQuery(
query: string,
_originalModelName: string | undefined = undefined,
fullRefresh = false,
): Promise<string> {
this.throwIfNotAuthenticated();
this.throwBridgeErrorIfAvailable();
const compileQueryCommand = this.dbtCloudCommand(
Expand All @@ -671,6 +679,9 @@ export class DBTCloudProjectIntegration
"json",
]),
);
if (fullRefresh) {
compileQueryCommand.addArgument("--full-refresh");
}
const { stdout, stderr } = await compileQueryCommand.execute();
const compiledLine = stdout
.trim()
Expand Down
13 changes: 12 additions & 1 deletion src/dbt_client/dbtCoreCommandIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class DBTCoreCommandProjectIntegration extends DBTCoreProjectIntegration
query: string,
limit: number,
modelName: string,
fullRefresh = false,
): Promise<QueryExecution> {
this.throwBridgeErrorIfAvailable();
const showCommand = this.dbtCoreCommand(
Expand All @@ -50,6 +51,9 @@ export class DBTCoreCommandProjectIntegration extends DBTCoreProjectIntegration
"json",
]),
);
if (fullRefresh) {
showCommand.addArgument("--full-refresh");
}
const cancellationTokenSource = new CancellationTokenSource();
showCommand.setToken(cancellationTokenSource.token);
return new QueryExecution(
Expand Down Expand Up @@ -151,7 +155,11 @@ export class DBTCoreCommandProjectIntegration extends DBTCoreProjectIntegration
return compiledLine[0].data.compiled;
}

async unsafeCompileQuery(query: string): Promise<string> {
async unsafeCompileQuery(
query: string,
_originalModelName: string | undefined = undefined,
fullRefresh = false,
): Promise<string> {
this.throwBridgeErrorIfAvailable();
const compileQueryCommand = this.dbtCoreCommand(
new DBTCommand("Compiling sql...", [
Expand All @@ -164,6 +172,9 @@ export class DBTCoreCommandProjectIntegration extends DBTCoreProjectIntegration
"json",
]),
);
if (fullRefresh) {
compileQueryCommand.addArgument("--full-refresh");
}
const { stdout, stderr } = await compileQueryCommand.execute();
const compiledLine = stdout
.trim()
Expand Down
8 changes: 6 additions & 2 deletions src/dbt_client/dbtCoreIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ export class DBTCoreProjectIntegration
query: string,
limit: number,
modelName: string,
fullRefresh = false,
): Promise<QueryExecution> {
this.throwBridgeErrorIfAvailable();
const { limitQuery } = await this.getQuery(query, limit);
Expand All @@ -432,11 +433,13 @@ export class DBTCoreProjectIntegration
const compiledQuery = await this.unsafeCompileQuery(
limitQuery,
modelName,
fullRefresh,
);
try {
// execute query
result = await queryThread!.lock<ExecuteSQLResult>(
(python) => python`to_dict(project.execute_sql(${compiledQuery}))`,
(python) =>
python`to_dict(project.execute_sql(${compiledQuery}, ${fullRefresh}))`,
);
const { manifestPathType } =
this.deferToProdService.getDeferConfigByProjectRoot(
Expand Down Expand Up @@ -881,11 +884,12 @@ export class DBTCoreProjectIntegration
async unsafeCompileQuery(
query: string,
originalModelName: string | undefined = undefined,
fullRefresh = false,
): Promise<string> {
this.throwBridgeErrorIfAvailable();
const output = await this.python?.lock<CompilationResult>(
(python) =>
python!`to_dict(project.compile_sql(${query}, ${originalModelName}))`,
python!`to_dict(project.compile_sql(${query}, ${originalModelName}, ${fullRefresh}))`,
);
return output.compiled_sql;
}
Expand Down
2 changes: 2 additions & 0 deletions src/dbt_client/dbtIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export interface DBTProjectIntegration extends Disposable {
query: string,
limit: number,
modelName: string,
fullRefresh?: boolean,
): Promise<QueryExecution>;
// dbt commands
runModel(command: DBTCommand): Promise<CommandProcessResult | undefined>;
Expand All @@ -362,6 +363,7 @@ export interface DBTProjectIntegration extends Disposable {
unsafeCompileQuery(
query: string,
originalModelName: string | undefined,
fullRefresh?: boolean,
): Promise<string>;
validateSql(
query: string,
Expand Down
18 changes: 17 additions & 1 deletion src/manifest/dbtProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,14 @@ export class DBTProject implements Disposable {
originalModelName: string | undefined = undefined,
): Promise<string | undefined> {
this.telemetry.sendTelemetryEvent("compileQuery");
const fullRefresh = workspace
.getConfiguration("dbt")
.get<boolean>("previewFullRefresh", false);
try {
return await this.dbtProjectIntegration.unsafeCompileQuery(
query,
originalModelName,
fullRefresh,
);
} catch (exc: any) {
if (exc instanceof PythonException) {
Expand Down Expand Up @@ -899,10 +903,12 @@ export class DBTProject implements Disposable {
async unsafeCompileQuery(
query: string,
originalModelName: string | undefined = undefined,
fullRefresh = false,
) {
return this.dbtProjectIntegration.unsafeCompileQuery(
query,
originalModelName,
fullRefresh,
);
}

Expand Down Expand Up @@ -1203,11 +1209,16 @@ export class DBTProject implements Disposable {
limit: limit.toString(),
});

const fullRefresh = workspace
.getConfiguration("dbt")
.get<boolean>("previewFullRefresh", false);

if (returnImmediately) {
const execution = await this.dbtProjectIntegration.executeSQL(
query,
limit,
modelName,
fullRefresh,
);
const result = await execution.executeQuery();
if (returnRawResults) {
Expand All @@ -1234,7 +1245,12 @@ export class DBTProject implements Disposable {
command: "executeQuery",
payload: {
query,
fn: this.dbtProjectIntegration.executeSQL(query, limit, modelName),
fn: this.dbtProjectIntegration.executeSQL(
query,
limit,
modelName,
fullRefresh,
),
projectName: this.getProjectName(),
},
});
Expand Down
Loading