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

Implement table name extraction. #1598

Merged
merged 12 commits into from
Nov 29, 2016
Merged

Implement table name extraction. #1598

merged 12 commits into from
Nov 29, 2016

Conversation

bkyryliuk
Copy link
Member

@bkyryliuk bkyryliuk commented Nov 14, 2016

This function parses SQL statement and extracts table names using sqlparse.

Resolves: #1607

Reviewers:

If you have tests in mind - please add SQL statements in the comments, happy to extend test coverage.


def test_join(self):
query = "SELECT t1.*, t2.* FROM t1 JOIN t2 ON t1.a = t2.a;"
self.assertEquals(["t1", "t2"], sql_parse.extract_tables(query))
Copy link
Member

Choose a reason for hiding this comment

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

should probably use sets instead of lists as ordering shouldn't get in the way

Copy link
Member

@mistercrunch mistercrunch left a comment

Choose a reason for hiding this comment

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

I'd add more nesting, say 3 levels deep, or a UNION ALL within a subquery, a subquery whithin a union all.

also a select in an expression

SELECT f1, (SELECT count(1) FROM t2) FROM t1


RESULT_OPERATIONS = {'UNION', 'INTERSECT', 'EXCEPT'}
PRECEDES_TABLE_NAME = {'FROM', 'JOIN', 'DESC', 'DESCRIBE', 'WITH'}

Copy link
Member

Choose a reason for hiding this comment

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

could be nice to have a SqlStatement and/or SqlSegment class(es)

Copy link
Member Author

Choose a reason for hiding this comment

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

I want to keep it as simple as I can for now. Happy to redesign it once we have other use cases.

@bkyryliuk bkyryliuk changed the title WIP. Implement table name extraction tests. Implement table name extraction tests. Nov 15, 2016
@bkyryliuk bkyryliuk changed the title Implement table name extraction tests. Implement table name extraction. Nov 16, 2016
@bkyryliuk
Copy link
Member Author

@mistercrunch - build is green, could you take another look ?

@john-bodley
Copy link
Member

@bkyryliuk have you thought about testing columns named the same as reserved keywords which need escaping.

@john-bodley
Copy link
Member

You also may want to look at other join types: (LEFT, RIGHT) INNER, (LEFT, RIGHT) OUTER, FULL OUTER, LEFT SEMI.

def process_identifier(identifier, table_names, aliases):
# exclude subselects
if '(' not in '{}'.format(identifier):
table_names.append(get_full_name(identifier))
Copy link
Member

Choose a reason for hiding this comment

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

I didn't realize this was going on when I first read the code but it's not a good idea to have a function mutate their input as a way to "return", that's just a no-no. This is a sign that you need an object, because an object's method is expected to mutate its properties.

Alternatively, if you want to go "functional programming" on this one and want to return multiple things, you can return a tuple (preferably namedtuple) or a dict.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, it's more c++ approach, I'll try to rewrite it using yield - it will be more pythonic way

def extract_tables(sql):
table_names = []
aliases = []
extract_from_token(sqlparse.parse(sql)[0], table_names, aliases)
Copy link
Member

Choose a reason for hiding this comment

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

I'd advise to raise if there are multiple statements as it may be misleading to only return for the first statement. The caller should make sure they have a single statement before calling.

table_names.append(get_full_name(identifier))
else:
# store aliases
if hasattr(identifier, 'get_alias'):
Copy link
Member

@mistercrunch mistercrunch Nov 22, 2016

Choose a reason for hiding this comment

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

oh interesting, can the identifier object be of different types? What are these types? Should the condition be using isinstance ?

@mistercrunch
Copy link
Member

I still feel like an object there would be useful, even some of the logic in sql_lab.py could be moved to this object, it allows to abstract SqlParse, tokenize only once and run multiple tests against it all in one place.



# TODO: some sql_lab logic here.
class SupersetQuery:
Copy link
Member

Choose a reason for hiding this comment

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

always derive object when supporting py2

.filter_by(datasource_name=datasource_name)
.all()
)
return None
Copy link
Member

Choose a reason for hiding this comment

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

return None is always implicit in Python

Copy link
Member Author

Choose a reason for hiding this comment

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

true, I prefer to return in cases when the function value will be used.
The post on the stackoverflow explains better:
http://stackoverflow.com/questions/15300550/python-return-return-none-and-no-return-at-all

Using return None:
This tells that the function is indeed meant to return a value for later use, and in this case it returns None.

class SupersetQuery(object):
def __init__(self, sql_statement):
self._tokens = []
self._sql = sql_statement
Copy link
Member

Choose a reason for hiding this comment

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

public

@mistercrunch
Copy link
Member

LGTM

@bkyryliuk bkyryliuk merged commit dc98c67 into apache:master Nov 29, 2016
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.14.0 labels Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants