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

How to get the original SQL query from the "sql"/"query" field which is in json format #102

Open
lxw0109 opened this issue May 23, 2023 · 3 comments

Comments

@lxw0109
Copy link

lxw0109 commented May 23, 2023

How can I convert the following code in "sql" field into the original SQL query

{"phase": 1, "table_id": "1-1000181-1", "question": "Tell me what the notes are for South Australia ", "sql": {"sel": 5, "conds": [[3, 0, "SOUTH AUSTRALIA"]], "agg": 0}}

I tried using lib.query.Query.from_dict method but get SELECT col5 FROM table WHERE col3 = SOUTH AUSTRALIA
and tried using lib.dbengine.DBEngine.execute_query method but get SELECT col5 AS result FROM table_1_1000181_1 WHERE col3 = :col3.
None of the above two methods get the correct SQL query, so how can I get it? Anybody help?

@magic-YuanTian
Copy link

same question

1 similar comment
@skyrise-l
Copy link

same question

@3cham
Copy link

3cham commented Jun 23, 2023

You could add following code into the Query class and provide the types to get the correct query. E.g:

class Query
    ...
    def to_query(self, types):
        if self.agg_ops[self.agg_index]:
            rep = 'SELECT {agg} ({sel}) FROM table'.format(
                agg=self.agg_ops[self.agg_index],
                sel='col{}'.format(self.sel_index),
            )
        else:
            rep = f'SELECT col{self.sel_index} FROM table'
        if self.conditions:
            cond_strings = []
            for i, o, v in self.conditions:
                if types[i] == "text":
                    cond_strings.append(f"col{i} {self.cond_ops[o]} '{v}'")
                else:
                    cond_strings.append(f"col{i} {self.cond_ops[o]} {v}")
            rep +=  ' WHERE ' + ' AND '.join(cond_strings)
        return rep
    ...

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

No branches or pull requests

4 participants