Skip to content

Obtaining the db information from a table expression #379

Closed Answered by barakalon
mikeonghub asked this question in Q&A
Discussion options

You must be logged in to vote

First, I think there is some confusion about fundamental Python behavior.

From the python docs for print:

All non-keyword arguments are converted to strings like str() does and written to the stream

So when you do print(obj), str(obj) gets called.

However, when converting a dict object to a str, Python will call repr on it's entries.

So when you do print({"a": obj}), repr(obj) gets called.

So a good way to dig into this behavior is to check the __str__ and __repr__ methods of the object.

For sqlglot, __repr__ calls the to_s method, and __str__ calls the sql method:

import sqlglot
from sqlglot import expressions as exp

table = sqlglot.parse_one("SELECT a FROM db.x").find(exp.Table)

print(

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mikeonghub
Comment options

@barakalon
Comment options

Answer selected by tobymao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants