Skip to content

Block taking jinja2.runtime.Undefined into DatabricksAdapter#98

Merged
ueshin merged 4 commits into
databricks:mainfrom
ueshin:undefined_proof
May 25, 2022
Merged

Block taking jinja2.runtime.Undefined into DatabricksAdapter#98
ueshin merged 4 commits into
databricks:mainfrom
ueshin:undefined_proof

Conversation

@ueshin
Copy link
Copy Markdown
Contributor

@ueshin ueshin commented May 23, 2022

Description

Blocks taking jinja2.runtime.Undefined into DatabricksAdapter.

When calling the adapter method from macros, jinja2.runtime.Undefined can be passed to DatabricksAdapter that could cause unexpected behavior.

For example,

{% if not adapter.check_schema_exists(model.database, model.schema) %}

The model.database is not defined and jinja2.runtime.Undefined will be used to call adapter.check_schema_exists and Cursor.schema() will fail:

[debug] [Thread-13 ]: Databricks adapter: Error while running:
GetSchemas(database=, schema=test16533281379208408401_test_basic)
[debug] [Thread-13 ]: Databricks adapter: <class 'databricks.sql.exc.RequestError'>: Error during request to server
[debug] [Thread-13 ]: finished collecting timing info
[debug] [Thread-13 ]: On snapshot.snapshot_strategy_timestamp.ts_snapshot: Close
[error] [Thread-13 ]: Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server
  Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server

@allisonwang-db
Copy link
Copy Markdown
Collaborator

If we call adapter.check_schema_exists(model.database, model.schema) with model.database = jinja2.runtime.Undefined, what will happen?

@ueshin
Copy link
Copy Markdown
Contributor Author

ueshin commented May 23, 2022

@allisonwang-db

If we call adapter.check_schema_exists(model.database, model.schema) with model.database = jinja2.runtime.Undefined, what will happen?

For example, cursor.schema() call will fail:

[debug] [Thread-13 ]: Databricks adapter: Error while running:
GetSchemas(database=, schema=test16533281379208408401_test_basic)
[debug] [Thread-13 ]: Databricks adapter: <class 'databricks.sql.exc.RequestError'>: Error during request to server
[debug] [Thread-13 ]: finished collecting timing info
[debug] [Thread-13 ]: On snapshot.snapshot_strategy_timestamp.ts_snapshot: Close
[error] [Thread-13 ]: Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server
  Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server

Also updated the description.

if "database" not in data["path"]:
data["path"]["database"] = None
else:
data["path"]["database"] = remove_undefined(data["path"]["database"])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am curious when will "database" here be Undefined? Is there a specific configuration that can trigger this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

While parsing profiles, models, etc. and building metadata that happen before setting up DatabricksRelation, it could contain Undefined.

Copy link
Copy Markdown
Contributor Author

@ueshin ueshin May 24, 2022

Choose a reason for hiding this comment

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

Also macros for snapshot seems to cause the issue after we change DatabricksRelation.include_policy.database=True.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you think it might be helpful to add a test case here (if we set DatabricksRelation.include_policy.database=True)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what kind of test in your mind?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For example, construct a test with the database field being jinja Undefined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a test. Thanks!

@ueshin ueshin requested a review from allisonwang-db May 25, 2022 17:37
Copy link
Copy Markdown
Collaborator

@allisonwang-db allisonwang-db left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for adding the test.

@ueshin
Copy link
Copy Markdown
Contributor Author

ueshin commented May 25, 2022

Thanks! merging.

@ueshin ueshin merged commit 6606aa6 into databricks:main May 25, 2022
@ueshin ueshin deleted the undefined_proof branch May 25, 2022 23:49
ueshin added a commit that referenced this pull request May 25, 2022
…101)

### Description

Backport of #98 

Blocks taking `jinja2.runtime.Undefined` into `DatabricksAdapter`.

When calling the adapter method from macros, `jinja2.runtime.Undefined` can be passed to `DatabricksAdapter` that could cause unexpected behavior.

For example,

https://github.com/databricks/dbt-databricks/blob/b8152b697b003e02fb52e86f59b2bde4b492cc0a/dbt/include/databricks/macros/materializations/snapshot.sql#L33

The `model.database` is not defined and `jinja2.runtime.Undefined` will be used to call `adapter.check_schema_exists` and `Cursor.schema()` will fail:

```
[debug] [Thread-13 ]: Databricks adapter: Error while running:
GetSchemas(database=, schema=test16533281379208408401_test_basic)
[debug] [Thread-13 ]: Databricks adapter: <class 'databricks.sql.exc.RequestError'>: Error during request to server
[debug] [Thread-13 ]: finished collecting timing info
[debug] [Thread-13 ]: On snapshot.snapshot_strategy_timestamp.ts_snapshot: Close
[error] [Thread-13 ]: Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server
  Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server
```
ueshin added a commit that referenced this pull request May 25, 2022
)

### Description

Backport of #98 

Blocks taking `jinja2.runtime.Undefined` into `DatabricksAdapter`.

When calling the adapter method from macros, `jinja2.runtime.Undefined` can be passed to `DatabricksAdapter` that could cause unexpected behavior.

For example,

https://github.com/databricks/dbt-databricks/blob/b8152b697b003e02fb52e86f59b2bde4b492cc0a/dbt/include/databricks/macros/materializations/snapshot.sql#L33

The `model.database` is not defined and `jinja2.runtime.Undefined` will be used to call `adapter.check_schema_exists` and `Cursor.schema()` will fail:

```
[debug] [Thread-13 ]: Databricks adapter: Error while running:
GetSchemas(database=, schema=test16533281379208408401_test_basic)
[debug] [Thread-13 ]: Databricks adapter: <class 'databricks.sql.exc.RequestError'>: Error during request to server
[debug] [Thread-13 ]: finished collecting timing info
[debug] [Thread-13 ]: On snapshot.snapshot_strategy_timestamp.ts_snapshot: Close
[error] [Thread-13 ]: Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server
  Unhandled error while executing snapshot.snapshot_strategy_timestamp.ts_snapshot
Error during request to server
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants