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

Add Tinybird query runner #5616

Merged
merged 1 commit into from
Sep 6, 2023

Conversation

pandomic
Copy link
Contributor

@pandomic pandomic commented Oct 9, 2021

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • New Query Runner (Data Source)
  • New Alert Destination
  • Other

Description

tinybird

Tinybird is a fast-growing service providing real-time analytics for large amounts of data. Integrating it with powerful Redash tools will open new opportunities for real-time data analysis and visualizations, especially when mixed with other data sources like Python.

Under the hood, Tinybird uses ClickHouse, which makes it possible to re-use existing query runner with minimal adjustments.

Implementation Details

Connection Testing

Tinybird has few system data sources that should become queriable once the connection is properly configured. We use one of those (tinybird.pipe_stats) to test whether it can be executed or not.

Data Querying

There are different ways how you may run SQL queries against Tinybird. The most flexible one is of course the /sql endpoint which does not enforce you to use a single data source or pipe.

Schema Fetching

Schema for pipes and data sources can not be easily observed through SQL queries. Thus you may notice a little trick here: we are fetching lists of pipes and data sources and then running a simple query on each of them. Result schema is then returned as meta attribute. There is one limitation though, for pipes schema can only be observed if it is exported as an endpoint (because you are restricted to run queries only against shared pipes).

Related Tickets & Documents

Proactive initiative

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

The following Loom recording shows the Tinybird query runner at work
https://www.loom.com/share/8d96d361e2534de09710216df528af44

@guidopetri
Copy link
Contributor

@pandomic , thanks for the PR! We've updated a lot of things now that we're Community-driven so - if you're still interested in getting this merged - would you mind rebasing off master to re-run the CI?

We're trying to clean up our PR todo list, so if you're not interested, that's fine - we'll close the PR in about a week if we don't hear back. If you're interested in reopening the PR afterwards, we would also very much welcome that.

@pandomic
Copy link
Contributor Author

Hi @guidopetri, thanks for the update, I will try to align the PR in the next following days 🙌

@pandomic pandomic force-pushed the feat/tinybird-query-runner branch from 0be05c9 to 7d8a80b Compare August 28, 2023 15:55
@pandomic
Copy link
Contributor Author

Hey @guidopetri, I just rebased the PR. Hope it looks good. Looking forward to getting Tinybird support 🙌

@justinclift
Copy link
Member

justinclift commented Aug 29, 2023

@pandomic Looks like it's about 99.95% done, but still not quite finished. It's failing the formatting check lint test:

Run black --check .
would reformat /home/runner/work/redash/redash/redash/query_runner/tinybird.py

We have instructions on how to get that fixed if that's useful?

https://github.com/getredash/redash/wiki/Local-development-setup

It's the make format command there. 😄

@guidopetri guidopetri self-assigned this Aug 29, 2023
@pandomic pandomic force-pushed the feat/tinybird-query-runner branch from 7d8a80b to 5f8aaac Compare August 29, 2023 14:47
@codecov
Copy link

codecov bot commented Aug 29, 2023

Codecov Report

Merging #5616 (82c2998) into master (0258dca) will increase coverage by 0.20%.
Report is 19 commits behind head on master.
The diff coverage is 72.53%.

❗ Current head 82c2998 differs from pull request most recent head 4e46071. Consider uploading reports for the commit 4e46071 to get more accurate results

@@            Coverage Diff             @@
##           master    #5616      +/-   ##
==========================================
+ Coverage   60.77%   60.97%   +0.20%     
==========================================
  Files         154      156       +2     
  Lines       12656    12780     +124     
  Branches     1721     1735      +14     
==========================================
+ Hits         7692     7793     +101     
- Misses       4739     4753      +14     
- Partials      225      234       +9     
Files Changed Coverage Δ
redash/handlers/queries.py 86.17% <0.00%> (ø)
redash/settings/__init__.py 98.65% <ø> (ø)
redash/query_runner/google_spreadsheets.py 67.64% <65.85%> (+0.50%) ⬆️
redash/query_runner/tinybird.py 70.96% <70.96%> (ø)
redash/destinations/asana.py 81.48% <81.48%> (ø)
redash/models/__init__.py 92.25% <83.33%> (+0.29%) ⬆️
redash/__init__.py 88.57% <100.00%> (ø)
redash/query_runner/elasticsearch2.py 59.89% <100.00%> (ø)
redash/query_runner/snowflake.py 29.06% <100.00%> (+0.83%) ⬆️

... and 2 files with indirect coverage changes

@justinclift justinclift added the Team Review Meets PR criteria, ready for full review label Aug 29, 2023
@guidopetri
Copy link
Contributor

@pandomic thanks for your patience and for updating the PR! Any chance you could add some tests to make the Codecov bot happy?

@pandomic pandomic force-pushed the feat/tinybird-query-runner branch from 3de96c4 to 512ed91 Compare September 4, 2023 11:31
Copy link
Contributor

@guidopetri guidopetri left a comment

Choose a reason for hiding this comment

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

@pandomic thanks for adding these tests. I left a few comments but this looks good to me. I'll approve and merge tomorrow unless you want to change anything in response to the feedback.

Comment on lines 50 to 55
def test_connection(self):
try:
self._send_query("SELECT count() FROM tinybird.pipe_stats LIMIT 1 FORMAT JSON")
return True
except Exception:
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we set Tinybird.noop_query this should work automatically and we don't have to override the base class implementation.

Comment on lines 42 to 48
@classmethod
def name(cls):
return "Tinybird"

@classmethod
def type(cls):
return "tinybird"
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, I don't think we have to override this.

Copy link
Contributor Author

@pandomic pandomic Sep 6, 2023

Choose a reason for hiding this comment

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

Makes sense 👍 Slightly adjusted the ClickHouse class to re-use the underlying type() method

Comment on lines 102 to 103
url = endpoint % self.configuration.get("url", self.DEFAULT_URL)
authorization = "Bearer %s" % self.configuration.get("token")
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, I think we should lean on f-strings or .format() - I believe it's safer than using string interpolation with %s

redash/query_runner/tinybird.py Show resolved Hide resolved

class TestTinybird(TestCase):
@patch("requests.get")
def test_get_schema(self, get_request):
Copy link
Contributor

Choose a reason for hiding this comment

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

Both the test cases here are testing downstream functionality (i.e. get_schema and run_query, that are not directly implemented in the Tinybird runner). One potential improvement would be to test the Tinybird query runner directly, since it uses the base implementation for these methods.

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 adjusted the test name to reflect what is happening in get_schema (it collects the schema from available pipes and datasources).

I am not sure what else to test when it comes to query execution though, as it's essentially just a tiny GET request which is already being tested for proper formatting and with mocked response handling 🤔

Co-authored-by: Thomas Rausch <[email protected]>
Co-authored-by: Vlad Gramuzov <[email protected]>"
@pandomic pandomic force-pushed the feat/tinybird-query-runner branch from 82c2998 to 4e46071 Compare September 6, 2023 09:51
@justinclift
Copy link
Member

Looks like it's passing all of the tests. Weirdly though, the Code coverage tests don't seem to have automatically-rerun. Not sure why. 🤔

Copy link
Contributor

@guidopetri guidopetri left a comment

Choose a reason for hiding this comment

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

Thanks again for your contribution :) I retriggered the CI but it should be passing.

@guidopetri guidopetri enabled auto-merge (squash) September 6, 2023 12:23
@guidopetri guidopetri merged commit cb4af6d into getredash:master Sep 6, 2023
14 checks passed
@pandomic pandomic deleted the feat/tinybird-query-runner branch September 6, 2023 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Meets PR criteria, ready for full review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants