Skip to content

Conversation

Tishj
Copy link
Contributor

@Tishj Tishj commented Feb 14, 2023

This PR makes a couple changes to the DuckDBPyRelation

(This feature was suggested by @mauropagano)
Add the sql method, it prints the relation in query form.
Example:

import duckdb

rel = duckdb.table_function('range', [10000]).limit(10)
print(rel.sql())
print(rel.fetchall())
SELECT * FROM range(10000) LIMIT 10
[(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]

Change the explain() method to no longer print the type of internal relation, but instead create an ExplainRelation around the relation and execute it.
Example:

import duckdb

rel = duckdb.table_function('range', [10000]).limit(10)
print(rel.explain())
explain_key     explain_value
VARCHAR VARCHAR
[ Rows: 1]
physical_plan   ┌───────────────────────────┐
│      STREAMING_LIMIT      │
└─────────────┬─────────────┘                             
┌─────────────┴─────────────┐
│           RANGE           │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│           EC: 0           │
└───────────────────────────┘  

@Mytherin
Copy link
Collaborator

Thanks! Looks good. Two minor comments:

  • Can we call it .sql_query() to make the distinction with the other .sql method more clear?
  • For the explain - could we extract the plan itself and only print that, instead of the entire result?

@Tishj
Copy link
Contributor Author

Tishj commented Feb 16, 2023

@Mytherin

duckdb git:(general_python_fixes) ✗ python3 tmp/relation_explain.py                              
┌───────────────────────────┐
│      STREAMING_LIMIT      │
└─────────────┬─────────────┘                             
┌─────────────┴─────────────┐
│           RANGE           │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─   │
│           EC: 0           │
└───────────────────────────┘ 

👍

string DuckDBPyRelation::Explain() {
	AssertRelation();
	auto context = rel->context.GetContext();
	auto relation_statement = unique_ptr_cast<RelationStatement, SQLStatement>(make_unique<RelationStatement>(rel));
	auto prepared_statement = context->Prepare(std::move(relation_statement));
	return prepared_statement->data->plan->ToString();
}

this feels very dirty, but it gets the job done

@Mytherin
Copy link
Collaborator

Thanks for the improvements! One more minor comment, otherwise LGTM

@Tishj Tishj requested a review from Mytherin February 16, 2023 16:51
@Mytherin Mytherin merged commit 73ea444 into duckdb:master Feb 17, 2023
@Mytherin
Copy link
Collaborator

Thanks!

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