Skip to content

feat: add database_dialect property#794

Merged
olavloite merged 2 commits intomainfrom
add-database-dialect-read-only-property
Apr 14, 2026
Merged

feat: add database_dialect property#794
olavloite merged 2 commits intomainfrom
add-database-dialect-read-only-property

Conversation

@olavloite
Copy link
Copy Markdown
Collaborator

Adds a database_dialect property that shows the dialect of the current database. This property is automatically determined at startup and cannot be manually modified. This is different from the 'dialect' connection property, which can be set manually, and is used to determine the dialect that should be used for new databases that are created from this connection. The latter is primarily used in combination with the auto_config_emulator property.

Fixes #784

@olavloite olavloite requested a review from a team as a code owner April 13, 2026 08:47
Copy link
Copy Markdown
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 introduces a new read-only connection property, database_dialect, which is automatically determined at startup, along with unit tests to verify its behavior for PostgreSQL and Google Standard SQL. A critical concurrency issue was identified in driver.go, where updating the initialPropertyValues map could lead to a race condition and potential panic because concurrent reads in other methods are not synchronized with the same lock.

@olavloite olavloite force-pushed the add-database-dialect-read-only-property branch from 191bf4f to 5d2b7c1 Compare April 13, 2026 08:55
Adds a database_dialect property that shows the dialect of the current database. This property
is automatically determined at startup and cannot be manually modified. This is different from
the 'dialect' connection property, which can be set manually, and is used to determine the
dialect that should be used for new databases that are created from this connection. The latter
is primarily used in combination with the auto_config_emulator property.

Fixes #784
@olavloite olavloite force-pushed the add-database-dialect-read-only-property branch from 5d2b7c1 to 0b72b6e Compare April 13, 2026 09:19
@olavloite olavloite requested a review from rahul2393 April 14, 2026 09:17
Copy link
Copy Markdown
Collaborator

@rahul2393 rahul2393 left a comment

Choose a reason for hiding this comment

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

LGTM with some nits, feel free to address them separately

c.connectorConfig.Instance,
c.connectorConfig.Database)
if value, ok := c.initialPropertyValues[propertyConnectTimeout.Key()]; ok {
c.clientMu.Lock()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is unrelated change, is it to fix some existing race condition?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is actually related. This change writes to c.initialPropertyValues (it writes the database_dialect value) after the connector has been created, and at the same time there are potentially concurrent reads from this map. The latter happens if the application tries to open multiple connections in parallel using the same connector. These connections all try to read the connect_timeout value from this map.

driver.go Outdated
}

if err := c.increaseConnCount(ctx, databaseName, opts); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: why extra line?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed

driver.go Outdated
connectionStateType = connectionstate.TypeNonTransactional
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: why extra line?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed

driver.go Outdated
execSingleDMLTransactional: execInNewRWTransaction,
execSingleDMLPartitioned: execAsPartitionedDML,
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: why extra line?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed

driver.go Outdated
// Initialize the session.
_ = connection.ResetSession(context.Background())
return connection, nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: why extra line?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed

verifyConnectionPropertyValue(t, conn, "database_dialect", "POSTGRESQL")
// Verify that 'dialect' property is NOT updated and remains empty/unspecified.
verifyConnectionPropertyValue(t, conn, "dialect", "")
verifySetFails(t, conn, "database_dialect", "'GOOGLE_STANDARD_SQL'")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: The property defaults to DATABASE_DIALECT_UNSPECIFIED. What happens if database_dialect is queried before the dialect is determined (e.g., if increaseConnCount fails). Can it be ignored?

The property's defaultValue is databasepb.DatabaseDialect_DATABASE_DIALECT_UNSPECIFIED (an enum/int), but the test asserts string values like "POSTGRESQL". This works because of the converter/display logic, but it's worth verifying that the SHOW VARIABLE path consistently formats the enum as its string name. If someone changes the formatting, these tests could silently pass with wrong values.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Regarding the first: It is impossible to read DATABASE_DIALECT_UNSPECIFIED from the connection, because the query that is used to determine the dialect of the underlying database must successfully succeed before a connection is returned. So there are two possibilities:

  1. An application requests a connection and the dialect of the database has not yet been determined for this connector: The query is executed, it successfully returns the dialect, and only then is a connection returned to the application.
  2. An application requests a connection and the dialect of the database has not yet been determined for this connector: The query is executed and it fails and returns an error. The connector does not return a connection to the application, but an error.

Regarding the latter: These tests verify exactly that; The verifyConnectionPropertyValue constructs a SHOW [VARIABLE] <variable name> statement and executes it, and then verifies that the returned value is the expected value. So if anyone changes the format function of those enums, then this test will fail. The show statement is 'hidden' in a separate helper method because the syntax is slightly different for GoogleSQL and PostgreSQL, so it is easier to use a helper method to generate it.

rahul2393

This comment was marked as duplicate.

@olavloite olavloite merged commit acb1f42 into main Apr 14, 2026
43 checks passed
@olavloite olavloite deleted the add-database-dialect-read-only-property branch April 14, 2026 13:00
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

Successfully merging this pull request may close these issues.

Sync determineDialect result to dialect connection property (or align SHOW DIALECT)

2 participants