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

impl Display for Statement #3646

Closed
tisonkun opened this issue Apr 7, 2024 · 5 comments · Fixed by #3744
Closed

impl Display for Statement #3646

tisonkun opened this issue Apr 7, 2024 · 5 comments · Fixed by #3744
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@tisonkun
Copy link
Collaborator

tisonkun commented Apr 7, 2024

What type of enhancement is this?

API improvement

What does the enhancement do?

sqlparser's Statement implements Display, we need to implement Display for our Statement, named sql::statement::Statement also.

See

match self.query_statement(stmt, query_ctx.clone()).await {
    Ok(output) => {
        let output_result =
            query_interceptor.post_execute(output, query_ctx.clone());
        results.push(output_result);
    }
    Err(e) => {
        let redacted = sql::util::redact_sql_secrets(query.as_ref());
        error!(e; "Failed to execute query: {redacted}");

        results.push(Err(e));
        break;
    }
}

We currently print all the query if any of the statement failed to execute. We should write instead:

match self.query_statement(stmt, query_ctx.clone()).await {
    Ok(output) => {
        let output_result =
            query_interceptor.post_execute(output, query_ctx.clone());
        results.push(output_result);
    }
    Err(e) => {
        error!(e; "Failed to execute query: {stmt}");

        results.push(Err(e));
        break;
    }
}

with stmt probably redacted. And formatting structural stmt would be more reliable to redact fields, instead of depending on brittle regexps.

Implementation challenges

No response

@tisonkun tisonkun added good first issue Good for newcomers help wanted Extra attention is needed labels Apr 7, 2024
@irenjj
Copy link
Collaborator

irenjj commented Apr 7, 2024

I'd like to give it a try.

@tisonkun
Copy link
Collaborator Author

tisonkun commented Apr 7, 2024

@irenjj Assigned and please go ahead!

You can implement Display for Statement first and we later integrate it with the current logging on error logic. Or you can implmenet both at once if you'd like to. But anyway, please submit a PR and ping me as a reviewer when the basic functionality is ready.

@tisonkun
Copy link
Collaborator Author

@irenjj a reminder to confirm that you still work on this issue - do you find any trouble here?

@irenjj
Copy link
Collaborator

irenjj commented Apr 16, 2024

hi, @tisonkun, Sorry, I've been a bit busy lately. I have only completed part of it.

impl Display for Statement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Statement::Query(s) => s.inner.fmt(f),
            Statement::Insert(s) => s.inner.fmt(f),
            Statement::Delete(s) => s.inner.fmt(f),
            Statement::CreateTable(s) => s.fmt(f),
            Statement::CreateExternalTable(s) => todo!(),
            Statement::CreateTableLike(s) => todo!(),
            Statement::DropTable(s) => s.fmt(f),
            Statement::DropDatabase(s) => s.fmt(f),
            Statement::CreateDatabase(s) => s.fmt(f),
            Statement::Alter(s) => todo!(),
            Statement::ShowDatabases(s) => s.fmt(f),
            Statement::ShowTables(s) => s.fmt(f),
            Statement::ShowColumns(s) => s.fmt(f),
            Statement::ShowIndex(s) => s.fmt(f),
            Statement::ShowCreateTable(s) => s.fmt(f),
            Statement::DescribeTable(s) => s.fmt(f),
            Statement::Explain(s) => s.inner.fmt(f),
            Statement::Copy(s) => todo!(),
            Statement::Tql(s) => todo!(),
            Statement::TruncateTable(s) => s.fmt(f),
            Statement::SetVariables(s) => s.fmt(f),
            Statement::ShowVariables(s) => s.fmt(f),
        }
    }
}

I'll complete the remaining logic in the next few days and submit a draft.

@tisonkun
Copy link
Collaborator Author

@irenjj Thanks for your update! That's great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants