Skip to content
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

BREAKING CHANGE: Relative queries #416

Merged
merged 1 commit into from
May 14, 2024

Conversation

andresgutgon
Copy link
Contributor

Describe your changes

Until now we were requiring users to specify the queries in the ref function from the root of the source folder. Example:

MY_QUERIES
  - my-source
    - source.yml
    - query.sql
    - another_query.sql

Given that folder structure in query.sql

Before 🚫

-- MY_QUERIES/my-source/query.sql
SELECT * FROM {ref('my-source/another_query')}

Now βœ…

-- MY_QUERIES/my-source/query.sql
SELECT * FROM {ref('./another_query')}

Or βœ…

-- MY_QUERIES/my-source/query.sql
SELECT * FROM {ref('another_query')}

Or βœ…

-- MY_QUERIES/my-source/query.sql
SELECT * FROM {ref('/my-source/another_query')}

Issue ticket number and link

#369

Dependency

https://github.com/latitude-dev/latitude/pull/414/files

TODO

  • Pass SQL content from query file from source to connector
  • Include in the context request that SQL text
  • Fix runQuery πŸ›. I introduced the bug here
  • Use in ref method the SQL
  • Fix tests
  • QA is working the changes
  • Do Changeset

Copy link

changeset-bot bot commented May 14, 2024

πŸ¦‹ Changeset detected

Latest commit: 8a6b394

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@latitude-data/source-manager Major
@latitude-data/server Major
@latitude-data/sql-compiler Patch
@latitude-data/cli Patch
@latitude-data/athena-connector Patch
@latitude-data/bigquery-connector Patch
@latitude-data/clickhouse-connector Patch
@latitude-data/databricks-connector Patch
@latitude-data/duckdb-connector Patch
@latitude-data/mssql-connector Patch
@latitude-data/mysql-connector Patch
@latitude-data/postgresql-connector Patch
@latitude-data/snowflake-connector Patch
@latitude-data/sqlite-connector Patch
@latitude-data/test-connector Patch
@latitude-data/trino-connector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Base automatically changed from feature/make-source-available-to-connectors to canary May 14, 2024 09:34
@andresgutgon andresgutgon force-pushed the feature/find-query-files-with-relative-paths branch from 439d804 to 23e5c64 Compare May 14, 2024 09:44
async function buildConnector(queryPath: string) {
const source = await sourceManager.loadFromQuery(queryPath)

if (!source['_connector']) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what's this check about?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

connector is lazy loaded when a source compiles a query. Until that moment connector is not initialized

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done to avoid injecting source

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't get it sorry could you add a comment with explanations as to why we need this defense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Copy link
Contributor Author

@andresgutgon andresgutgon May 14, 2024

Choose a reason for hiding this comment

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

We want to have expectations on the underlying connector. But the connector is not accessible from the outside. On top of that _connector is null until you load the good query because it's lazily evaluated.

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm ok, the comment is good enough for now πŸ‘ŒπŸΌ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GPT4o did for me. Is so nice

query,
queryParams,
force,
}: {
source: Source
Copy link
Contributor

Choose a reason for hiding this comment

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

much better πŸ‘ŒπŸΌ

-- queries/users/orders/query.sql
SELECT * FROM {ref('../query.sql')} -- This will reference queries/users/query.sql
```

Copy link
Contributor

Choose a reason for hiding this comment

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

review the rest of this documentation because i think there are some things wrong now

Copy link
Contributor

Choose a reason for hiding this comment

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

also we should explain the difference between /query_path query_path and ./query_path

Copy link
Contributor Author

@andresgutgon andresgutgon May 14, 2024

Choose a reason for hiding this comment

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

Review the docs and changed docs about queries path resolution
image

@@ -96,6 +81,9 @@ export default async function createConnectorFactory({
connectorOptions: ConnectorOptions<ConnectorAttributes>
}): Promise<BaseConnector> {
const packageName = getConnectorPackage(type)
const ConnectorClass = await importConnector(packageName)
const ConnectorClass = !packageName
? TestConnector // If no package is found, use the test connector
Copy link
Contributor

Choose a reason for hiding this comment

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

a bit worried about all the code changes caused by the tests. Not blocking but I'd love if we could find ways to test code without having to adapt it to tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The factory is super magic. It expects an npm package to exist with that name and to be installed in the source_manager. This is super annoying for testing. The way it is done now is that the test connector is a local file in this package.

request: CompileQueryRequest
// Parameters used in the query
accessedParams: QueryParams
// Parameters resolved by the connector, in order of appearance
Copy link
Contributor

Choose a reason for hiding this comment

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

I still get confused by the resolved name. What does it mean to resolve a parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it means computing their value. cc @csansoon

Copy link
Contributor

@geclos geclos left a comment

Choose a reason for hiding this comment

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

Awesome work! This is super delicate though – We need to manually test query references in multiple scenarios:

/query
    source.yml
    query1.sql
    /folder1/query2.sql -- references query1
    /folder1/source2.yml
    /folder1/query3.sql -- uses source2.yml
    /folder1/folder2/query4.sql -- references query3

Running query1, query2, query3 and query4 should work ☝🏼

@andresgutgon andresgutgon force-pushed the feature/find-query-files-with-relative-paths branch 2 times, most recently from f7a3f23 to 2d36079 Compare May 14, 2024 10:38
We want to move the responsability of fining the query in the file
system from the connector to the source. After the last refactor now
this is easier to todo because we have access to the source and the
source manager in the connector.
With this change is easier to pass the the full path of the query and
look for relative paths and paths that lives outside the source of the
connector. We want to do both things in the upcoming changes
@andresgutgon andresgutgon force-pushed the feature/find-query-files-with-relative-paths branch from 2d36079 to 8a6b394 Compare May 14, 2024 11:01
@andresgutgon andresgutgon merged commit a8d4658 into canary May 14, 2024
4 checks passed
@andresgutgon andresgutgon deleted the feature/find-query-files-with-relative-paths branch May 14, 2024 11:05
@github-actions github-actions bot locked and limited conversation to collaborators May 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants