Skip to content

impl(bq_driver): SQLStatistics Implementation - #1659

Open
KanchanShu wants to merge 3 commits into
googleapis:mainfrom
KanchanShu:imple_SQLStatistic
Open

KanchanShu wants to merge 3 commits into
googleapis:mainfrom
KanchanShu:imple_SQLStatistic

Conversation

@KanchanShu

Copy link
Copy Markdown
Contributor

No description provided.

@KanchanShu KanchanShu changed the title Imple sql statistic impl(bq_driver): SQLStatistics Implementation Sep 10, 2026
ASSERT_TRUE(status == SQL_SUCCESS || status == SQL_SUCCESS_WITH_INFO);
++row_count;
}
EXPECT_GE(row_count, 0);

@shivamd-gpartner shivamd-gpartner Sep 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use exact value to match here and elsewhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Added flags according to the drivers


// Returns a ResultSet containing table-level statistics for the given BigQuery
// table. BigQuery does not support traditional indexes, so only a
// SQL_TABLE_STAT row (TYPE = 0) is returned with the row count in CARDINALITY.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

where are we returning SQL_TABLE_STAT row?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

returning it as part of result set

// See:
// https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlstatistics-function
StatusRecordOr<ResultSet> FetchStatisticsResultSet(
StatementHandle& stmt_handle, std::string const& catalog_name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

catalog_name, schema_name, table_name and other parameters are not used in the code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated code


// Resolve catalog (project). Per the spec, catalog_name is not a search
// pattern. When null/empty, use the connection's current catalog.
std::string s_catalog_name;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

catalog_name, s_schema_name , s_table_name seems not to be used anywhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated code

@KanchanShu
KanchanShu force-pushed the imple_SQLStatistic branch 2 times, most recently from 16fab7c to f062554 Compare September 13, 2026 19:54

// 7: TYPE — SQL_TABLE_STAT (0) indicates this is a table statistics row
DSValue ds_type;
ArithmeticToDSValue<SQLSMALLINT>(static_cast<SQLSMALLINT>(SQL_TABLE_STAT),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

have the type as SQLBIGINT here, this column is declared kTypeSchema{0, BQDataType::kInt64} and we have kInt64 handling as SQLBIGINT everywhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@sachinpro
sachinpro marked this pull request as ready for review September 14, 2026 14:07
@sachinpro
sachinpro requested a review from a team as a code owner September 14, 2026 14:07
@sachinpro

Copy link
Copy Markdown
Collaborator

/gcbrun

#ifndef BQ_DRIVER_INTEGRATION_TESTS
EXPECT_EQ(0, row_count);
#else
EXPECT_EQ(1, row_count);

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.

Why are we deviating from the existing driver here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We are deviating here because our implementation strictly follows the official ODBC specification, whereas the existing driver took a shortcut.

According to the Microsoft ODBC specification for SQLStatistics, when a table exists, the driver is supposed to return a baseline row containing statistics for the table itself (TYPE = SQL_TABLE_STAT), followed by rows for any indexes.

Because BigQuery doesn't use traditional RDBMS indexes, the existing driver appears to have simply stubbed out this function to return an empty result set, completely skipping that baseline table-statistics row.

By taking the time to implement it, we are able to fetch BigQuery's table metadata (like numRows) and map it to the CARDINALITY column in the SQL_TABLE_STAT row. This provides ODBC clients with the actual table-level statistics (row count, size) that they expect to receive, making our driver more compliant and feature-complete than the existing one.

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.

That's fine. We should validate the relevant values then.

Also, have we tested this e2e with MS Access?

#ifndef BQ_DRIVER_INTEGRATION_TESTS
SQLSMALLINT const table_name_nullable = SQL_NULLABLE;
#else
SQLSMALLINT const table_name_nullable = SQL_NO_NULLS;

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.

Why are we deviating from the existing driver here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

According to the ODBC specification, the TABLE_NAME column in catalog functions cannot be null, so its metadata should report SQL_NO_NULLS. However, the existing driver incorrectly reports TABLE_NAME as SQL_NULLABLE specifically for SQLStatistics.

In our new driver, we fixed this so that it correctly reports SQL_NO_NULLS (which is enforced in ApplyMetadataIrdOverrides inside odbc_desc_attr.cc).

@sachinpro

sachinpro commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

@KanchanShu Please fix the CI checks and merge conflicts.

@sachinpro

Copy link
Copy Markdown
Collaborator

/gcbrun

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.

3 participants