Skip to content

Conversation

@anicoll
Copy link
Contributor

@anicoll anicoll commented Oct 30, 2025

Summary

This is a very simple PR to improve the readability of SQL written out by this package.

Highlights

  • improve the SQL output for all types from a single long line to a more human readable format
  • Add test coverage for all fields updated

Please note that this is related to another of my PR's as I really need both to progress please.
Related: #13266

@anicoll anicoll requested review from a team as code owners October 30, 2025 04:33
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Oct 30, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @anicoll, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the readability of generated SQL for CreateView statements within the spansql package. The primary goal is to transform single-line SQL outputs into a more structured, multi-line format with appropriate indentation, making complex view definitions easier to understand and debug. This change is complemented by an extensive suite of new test cases to ensure the accuracy and reliability of the enhanced formatting across various SQL clauses and scenarios.

Highlights

  • Improved SQL Formatting for CreateView: The CreateView.SQL() method has been significantly refactored to generate more human-readable SQL. Instead of a single long line, the output now includes newlines and indentation for SELECT expressions, FROM, WHERE, GROUP BY, ORDER BY, LIMIT, and OFFSET clauses, enhancing clarity for complex view definitions.
  • Enhanced Test Coverage for CreateView: Comprehensive new test cases have been added for CreateView to validate the improved formatting logic across a wide range of SQL constructs, including views with WHERE, GROUP BY, ORDER BY, LIMIT, OFFSET, and complex nested JOIN operations, ensuring robustness and correctness.
  • Minor SQL Formatting for Joins: A small adjustment was made to SelectFromJoin.SQL() to introduce a newline before the JOIN keyword, further contributing to the overall readability of generated SQL statements.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the readability of the generated SQL for CREATE VIEW statements by formatting them across multiple lines. The addition of extensive test cases to cover various clauses like WHERE, GROUP BY, LIMIT, and complex joins is a great enhancement. My review includes a couple of suggestions to improve the efficiency and robustness of the string building logic.

@anicoll anicoll force-pushed the feat/view_sql_formatting branch from 8a0aaff to b6d3799 Compare October 30, 2025 04:40
@anicoll anicoll force-pushed the feat/view_sql_formatting branch from b6d3799 to 0634223 Compare October 30, 2025 04:41
@anicoll
Copy link
Contributor Author

anicoll commented Oct 30, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the SQL formatting for CREATE VIEW statements, making the output much more readable by adding newlines and indentation. The changes are supported by an excellent and comprehensive set of new test cases that cover a wide range of SQL clauses. My primary feedback concerns the implementation in CreateView.SQL(), which currently duplicates a substantial amount of query formatting logic. I have provided a suggestion to refactor this to enhance code maintainability.

@anicoll anicoll changed the title spansql:[feat] improve the SQL formatting when printing out CreateViews feat(spansql): improve the SQL formatting when printing out CreateViews Oct 30, 2025
@anicoll
Copy link
Contributor Author

anicoll commented Oct 30, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the readability of SQL generated for CREATE VIEW statements by formatting them across multiple lines. The use of strings.Builder is a good improvement for performance, and the new writeFormattedQuery function effectively encapsulates the formatting logic. The test coverage added is excellent and covers a wide range of SQL clauses. I have one suggestion to further improve the maintainability of the new code by reducing some duplication.

@anicoll
Copy link
Contributor Author

anicoll commented Oct 30, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the readability of SQL generated for CREATE VIEW statements by formatting them across multiple lines. The use of strings.Builder is a good choice for performance, and the addition of extensive test cases is excellent. I've identified a couple of omissions in the new formatting logic, specifically the lack of support for DISTINCT and HAVING clauses, which could lead to incorrect SQL generation. I've also included some suggestions to further improve consistency and code reuse. Overall, this is a valuable improvement.

}

func writeFormattedQuery(sb *strings.Builder, q *Query) {
sb.WriteString("SELECT\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The new writeFormattedQuery function doesn't handle SELECT DISTINCT. The q.Select.Distinct field is ignored, which will lead to incorrect SQL generation for views that use DISTINCT. This should be added after the SELECT keyword.

sb.WriteString("SELECT")
	if q.Select.Distinct {
		sb.WriteString(" DISTINCT")
	}
	sb.WriteString("\n")

Comment on lines 229 to 233
// GROUP BY clause
if len(q.Select.GroupBy) > 0 {
sb.WriteString("\nGROUP BY ")
writeSQLList(sb, q.Select.GroupBy)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The writeFormattedQuery function is missing support for the HAVING clause. It should be added after the GROUP BY clause to ensure correctly formatted SQL for queries that use it.

	// GROUP BY clause
	if len(q.Select.GroupBy) > 0 {
		sb.WriteString("\nGROUP BY ")
		writeSQLList(sb, q.Select.GroupBy)
	}

	// HAVING clause
	if q.Select.Having != nil {
		sb.WriteString("\nHAVING ")
		sb.WriteString(q.Select.Having.SQL())
	}

}
}

func writeFormattedQuery(sb *strings.Builder, q *Query) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This is a great helper function for formatting queries! Once the missing DISTINCT and HAVING clauses are added, you could consider using this function to implement Query.SQL() to improve its formatting and address the // TODO: improve this. comment. This would make the SQL output for standalone queries consistent with the formatting inside CREATE VIEW.

It would look something like this:

func (q Query) SQL() string {
	var sb strings.Builder
	writeFormattedQuery(&sb, &q)
	return sb.String()
}

Note that this would change the output for existing query tests, so they would need to be updated.

func (sfj SelectFromJoin) SQL() string {
// TODO: The grammar permits arbitrary nesting. Does this need to add parens?
str := sfj.LHS.SQL() + " " + joinTypes[sfj.Type] + " JOIN "
str := sfj.LHS.SQL() + "\n" + joinTypes[sfj.Type] + " JOIN "
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While adding the newline here improves readability, this function and the lines that follow still use string concatenation (+ and +=). For consistency with the other changes in this PR (like in CreateView.SQL) that adopt strings.Builder, it would be beneficial to refactor this entire function to use strings.Builder as well. This would improve performance and maintainability.

@rahul2393 rahul2393 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 30, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 30, 2025
@anicoll
Copy link
Contributor Author

anicoll commented Oct 30, 2025

@rahul2393 apologies. I pushed another commit to apply the formatting in a more general manner.

@rahul2393 rahul2393 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 30, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Oct 30, 2025
@anicoll anicoll changed the title feat(spansql): improve the SQL formatting when printing out CreateViews feat(spansql): improve the SQL formatting when printing out SQL Oct 30, 2025
@rahul2393 rahul2393 merged commit af0806f into googleapis:main Oct 31, 2025
9 of 10 checks passed
@anicoll anicoll deleted the feat/view_sql_formatting branch November 1, 2025 08:26
rahul2393 added a commit that referenced this pull request Dec 15, 2025
PR created by the Librarian CLI to initialize a release. Merging this PR
will auto trigger a release.

Librarian Version: v1.0.0
Language Image:
us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/librarian-go@sha256:718167d5c23ed389b41f617b3a00ac839bdd938a6bd2d48ae0c2f1fa51ab1c3d
<details><summary>spanner: 1.87.0</summary>

##
[1.87.0](spanner/v1.86.1...spanner/v1.87.0)
(2025-12-10)

### Features

* Add Send and Ack mutations for Queues (PiperOrigin-RevId: 832425466)
([185951b](185951b3))

* Exposing AutoscalingConfig in InstancePartition (PiperOrigin-RevId:
825184314)
([185951b](185951b3))

* Add Spanner location API (PiperOrigin-RevId: 833474957)
([185951b](185951b3))

* Add QueryAdvisorResult for query plan (PiperOrigin-RevId: 832425466)
([185951b](185951b3))

* improve the SQL formatting when printing out SQL (#13267)
([af0806f](af0806f4))

* Add grpc.xds.resource_type label to xDS client metrics (#13358)
([b9196cf](b9196cf6))

* support subquery in View Join (#13266)
([d19f797](d19f797b))

### Bug Fixes

* add env var to allow disabling directpath bound token (#13265)
([029bc79](029bc795))

* fix createMultiplexedSession goroutine leak (#13396)
([1805e89](1805e895))

* decoding spanner rows using SelectAll should map values in correct
annotations (#13301)
([315f65b](315f65b5))

* error instead of panic for iterator after tx end (#13366)
([a27c19a](a27c19ae))

* transaction_tag should be set on BeginTransactionRequest (#13463)
([a429aea](a429aea4))

* Configure keepAlive time for gRPC TCP connections (#13216)
([ca8f64e](ca8f64e0))

* avoid double decrement in session counting (#13395)
([e036421](e0364214))

### Documentation

* minor update for Spanner Location API (PiperOrigin-RevId: 834841888)
([185951b](185951b3))

* Update description for the BatchCreateSessionsRequest and Session
(PiperOrigin-RevId: 832425466)
([185951b](185951b3))

* Update description for the IsolationLevel (PiperOrigin-RevId:
832425466)
([185951b](185951b3))

</details>
cpriti-os pushed a commit to cpriti-os/google-cloud-go that referenced this pull request Dec 16, 2025
…3464)

PR created by the Librarian CLI to initialize a release. Merging this PR
will auto trigger a release.

Librarian Version: v1.0.0
Language Image:
us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/librarian-go@sha256:718167d5c23ed389b41f617b3a00ac839bdd938a6bd2d48ae0c2f1fa51ab1c3d
<details><summary>spanner: 1.87.0</summary>

##
[1.87.0](googleapis/google-cloud-go@spanner/v1.86.1...spanner/v1.87.0)
(2025-12-10)

### Features

* Add Send and Ack mutations for Queues (PiperOrigin-RevId: 832425466)
([185951b](googleapis@185951b3))

* Exposing AutoscalingConfig in InstancePartition (PiperOrigin-RevId:
825184314)
([185951b](googleapis@185951b3))

* Add Spanner location API (PiperOrigin-RevId: 833474957)
([185951b](googleapis@185951b3))

* Add QueryAdvisorResult for query plan (PiperOrigin-RevId: 832425466)
([185951b](googleapis@185951b3))

* improve the SQL formatting when printing out SQL (googleapis#13267)
([af0806f](googleapis@af0806f4))

* Add grpc.xds.resource_type label to xDS client metrics (googleapis#13358)
([b9196cf](googleapis@b9196cf6))

* support subquery in View Join (googleapis#13266)
([d19f797](googleapis@d19f797b))

### Bug Fixes

* add env var to allow disabling directpath bound token (googleapis#13265)
([029bc79](googleapis@029bc795))

* fix createMultiplexedSession goroutine leak (googleapis#13396)
([1805e89](googleapis@1805e895))

* decoding spanner rows using SelectAll should map values in correct
annotations (googleapis#13301)
([315f65b](googleapis@315f65b5))

* error instead of panic for iterator after tx end (googleapis#13366)
([a27c19a](googleapis@a27c19ae))

* transaction_tag should be set on BeginTransactionRequest (googleapis#13463)
([a429aea](googleapis@a429aea4))

* Configure keepAlive time for gRPC TCP connections (googleapis#13216)
([ca8f64e](googleapis@ca8f64e0))

* avoid double decrement in session counting (googleapis#13395)
([e036421](googleapis@e0364214))

### Documentation

* minor update for Spanner Location API (PiperOrigin-RevId: 834841888)
([185951b](googleapis@185951b3))

* Update description for the BatchCreateSessionsRequest and Session
(PiperOrigin-RevId: 832425466)
([185951b](googleapis@185951b3))

* Update description for the IsolationLevel (PiperOrigin-RevId:
832425466)
([185951b](googleapis@185951b3))

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants