-
Notifications
You must be signed in to change notification settings - Fork 6
impl(bq_driver): SQLStatistics Implementation #1659
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
Open
KanchanShu
wants to merge
4
commits into
googleapis:main
Choose a base branch
from
KanchanShu:imple_SQLStatistic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
google/cloud/odbc/bq_driver/internal/odbc_sql_statistics.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "google/cloud/odbc/bq_driver/internal/odbc_sql_statistics.h" | ||
| #include "google/cloud/odbc/bq_client_interface/utils.h" | ||
| #include "google/cloud/odbc/bq_driver/internal/odbc_sql_columns.h" | ||
| #include "google/cloud/odbc/bq_driver/internal/trace_utils.h" | ||
| #include "google/cloud/odbc/bq_driver/internal/utils.h" | ||
|
|
||
| namespace google::cloud::odbc_bq_driver_internal { | ||
|
|
||
| using ::google::cloud::Options; | ||
| using ::google::cloud::bigquery_v2_minimal_internal::TableMetadataView; | ||
| using ::google::cloud::odbc_bigquery_client_interface::MaxRetriesOption; | ||
| using ::google::cloud::odbc_bigquery_client_interface::TableFilter; | ||
| using ::google::cloud::odbc_internal::SQLStates; | ||
| using ::google::cloud::odbc_internal::StatusRecord; | ||
| using ::google::cloud::odbc_internal::StatusRecordOr; | ||
|
|
||
| // 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. | ||
| // All index-specific columns are set to NULL. | ||
| // | ||
| // Per the ODBC spec: | ||
| // - If the table does not exist or catalog/schema arguments do not identify a | ||
| // table, the function returns SQL_SUCCESS with an empty result set. | ||
| // - CARDINALITY is the number of rows in the table (num_rows from BQ | ||
| // metadata). | ||
| // - The unique argument is ignored since BigQuery has no traditional indexes. | ||
| // - When reserved = SQL_QUICK, CARDINALITY and PAGES may be NULL; we always | ||
| // return num_rows from BQ metadata since it is cheap to retrieve. | ||
| // | ||
| // See: | ||
| // https://learn.microsoft.com/en-us/sql/odbc/reference/syntax/sqlstatistics-function | ||
| StatusRecordOr<ResultSet> FetchStatisticsResultSet( | ||
| StatementHandle& stmt_handle, std::string const& catalog_name, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated code |
||
| std::string const& schema_name, std::string const& table_name, | ||
| SQLUSMALLINT unique, SQLUSMALLINT reserved) { | ||
| if (unique != SQL_INDEX_UNIQUE && unique != SQL_INDEX_ALL) { | ||
| LOG(ERROR) << "FetchStatisticsResultSet:: Invalid fUnique option: " | ||
| << unique; | ||
| return StatusRecord{SQLStates::k_HY100(), "Invalid fUnique option."}; | ||
| } | ||
|
|
||
| if (reserved != SQL_ENSURE && reserved != SQL_QUICK) { | ||
| LOG(ERROR) << "FetchStatisticsResultSet:: Invalid fAccuracy option: " | ||
| << reserved; | ||
| return StatusRecord{SQLStates::k_HY101(), "Invalid fAccuracy option."}; | ||
| } | ||
|
|
||
| if (!stmt_handle.GetConnectionHandle()) { | ||
| LOG(ERROR) << "FetchStatisticsResultSet:: Connection handle is null."; | ||
| return StatusRecord{SQLStates::k_HY013(), | ||
| "Internal connection handle is null."}; | ||
| } | ||
|
|
||
| // Initialize the result set schema. | ||
| // Per the spec, SQLStatistics always returns the 13-column schema. | ||
| ResultSet result_set; | ||
| result_set.row_schema.resize(kStatisticsMap.size()); | ||
| for (auto const& [_, schema] : kStatisticsMap) { | ||
| result_set.row_schema[schema.col_index] = schema; | ||
| } | ||
|
|
||
| // Per the ODBC spec, table_name is a required identifier (not a search | ||
| // pattern). If it is empty or contains wildcard characters, return an empty | ||
| // result set. | ||
| if (table_name.empty() || absl::StrContains(table_name, "%") || | ||
| absl::StrContains(table_name, "\\")) { | ||
| return result_set; | ||
| } | ||
|
|
||
| // Fetch table metadata from BigQuery to populate the SQL_TABLE_STAT row. | ||
| auto bq_client = stmt_handle.GetConnectionHandle()->GetClient(); | ||
| if (!bq_client) { | ||
| LOG(ERROR) << "FetchStatisticsResultSet:: Invalid or null BQ Client."; | ||
| return StatusRecord{SQLStates::k_HY000(), "Invalid or null BQ Client."}; | ||
| } | ||
|
|
||
| Options options; | ||
| options.set<MaxRetriesOption>( | ||
| stmt_handle.GetConnectionHandle()->GetDsn().max_retries); | ||
|
|
||
| TableFilter filter{{}, TableMetadataView::Full()}; | ||
|
|
||
| auto table_status = bq_client->GetTable(catalog_name, schema_name, table_name, | ||
| filter, options); | ||
| if (!table_status) { | ||
| // Per the ODBC spec: if the table is not found, return SQL_SUCCESS with | ||
| // an empty result set. | ||
| if (table_status.GetStatusRecord().native_error_code == 404) { | ||
| LOG(INFO) << "FetchStatisticsResultSet:: Table not found, returning " | ||
| "empty result set."; | ||
| return result_set; | ||
| } | ||
| LOG(ERROR) << "FetchStatisticsResultSet::GetTable:: " | ||
| << table_status.GetStatusRecord().message; | ||
| return table_status.GetStatusRecord(); | ||
| } | ||
|
|
||
| auto const& table = *table_status; | ||
|
|
||
| // Build the single SQL_TABLE_STAT row per the ODBC spec. | ||
| // Columns: TABLE_CAT, TABLE_SCHEM, TABLE_NAME, NON_UNIQUE, INDEX_QUALIFIER, | ||
| // INDEX_NAME, TYPE, ORDINAL_POSITION, COLUMN_NAME, ASC_OR_DESC, | ||
| // CARDINALITY, PAGES, FILTER_CONDITION | ||
| DSRow ds_row; | ||
|
|
||
| // 1: TABLE_CAT | ||
| DSValue ds_table_cat = kNullValue; | ||
| if (!catalog_name.empty()) StringToDSValue(catalog_name, ds_table_cat); | ||
| ds_row.push_back(ds_table_cat); | ||
|
|
||
| // 2: TABLE_SCHEM | ||
| DSValue ds_table_schema = kNullValue; | ||
| if (!schema_name.empty()) StringToDSValue(schema_name, ds_table_schema); | ||
| ds_row.push_back(ds_table_schema); | ||
|
|
||
| // 3: TABLE_NAME | ||
| DSValue ds_table_name = kNullValue; | ||
| if (!table_name.empty()) StringToDSValue(table_name, ds_table_name); | ||
| ds_row.push_back(ds_table_name); | ||
|
|
||
| // 4: NON_UNIQUE — NULL for SQL_TABLE_STAT rows | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 5: INDEX_QUALIFIER — NULL for SQL_TABLE_STAT rows | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 6: INDEX_NAME — NULL for SQL_TABLE_STAT rows | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 7: TYPE — SQL_TABLE_STAT (0) indicates this is a table statistics row | ||
| DSValue ds_type; | ||
| ArithmeticToDSValue<SQLBIGINT>(static_cast<SQLBIGINT>(SQL_TABLE_STAT), | ||
| ds_type); | ||
| ds_row.push_back(ds_type); | ||
|
|
||
| // 8: ORDINAL_POSITION — NULL for SQL_TABLE_STAT rows | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 9: COLUMN_NAME — NULL for SQL_TABLE_STAT rows | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 10: ASC_OR_DESC — NULL for SQL_TABLE_STAT rows | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 11: CARDINALITY — number of rows in the table | ||
| DSValue ds_cardinality; | ||
| ArithmeticToDSValue<SQLBIGINT>(static_cast<SQLBIGINT>(table.num_rows), | ||
| ds_cardinality); | ||
| ds_row.push_back(ds_cardinality); | ||
|
|
||
| // 12: PAGES — NULL (BigQuery has no concept of pages) | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| // 13: FILTER_CONDITION — NULL (only for filtered indexes) | ||
| ds_row.push_back(kNullValue); | ||
|
|
||
| result_set.rows.push_back(std::move(ds_row)); | ||
| return result_set; | ||
| } | ||
|
|
||
| } // namespace google::cloud::odbc_bq_driver_internal | ||
51 changes: 51 additions & 0 deletions
51
google/cloud/odbc/bq_driver/internal/odbc_sql_statistics.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef CPP_BIGQUERY_ODBC_GOOGLE_CLOUD_ODBC_BQ_DRIVER_INTERNAL_ODBC_SQL_STATISTICS_H | ||
| #define CPP_BIGQUERY_ODBC_GOOGLE_CLOUD_ODBC_BQ_DRIVER_INTERNAL_ODBC_SQL_STATISTICS_H | ||
|
|
||
| #include "google/cloud/odbc/bq_client_interface/odbc_bq_client.h" | ||
| #include "google/cloud/odbc/bq_driver/internal/odbc_conn_handle.h" | ||
| #include "google/cloud/odbc/bq_driver/internal/odbc_internal_commons.h" | ||
| #include "google/cloud/odbc/bq_driver/internal/odbc_stmt_handle.h" | ||
| #include "google/cloud/odbc/internal/odbc_includes.h" | ||
| #include <map> | ||
| #include <string> | ||
|
|
||
| namespace google::cloud::odbc_bq_driver_internal { | ||
|
|
||
| static std::map<std::string, ColumnSchema> const kStatisticsMap = { | ||
| {kTableCatColName, WithIndex(0, kTableCatSchema)}, | ||
| {kTableSchemaColName, WithIndex(1, kTableSchemaSchema)}, | ||
| {kTableNameColName, WithIndex(2, kTableNameSchema)}, | ||
| {kNonUniqueColName, WithIndex(3, kNonUniqueSchema)}, | ||
| {kIndexQualifierColName, WithIndex(4, kIndexQualifierSchema)}, | ||
| {kIndexNameColName, WithIndex(5, kIndexNameSchema)}, | ||
| {kTypeColName, WithIndex(6, kTypeSchema)}, | ||
| {kOrdinalPositionColName, WithIndex(7, kOrdinalPositionSchema)}, | ||
| {kColumnNameColName, WithIndex(8, kColumnNameSchema)}, | ||
| {kAscOrDescColName, WithIndex(9, kAscOrDescSchema)}, | ||
| {kCardinalityColName, WithIndex(10, kCardinalitySchema)}, | ||
| {kPagesColName, WithIndex(11, kPagesSchema)}, | ||
| {kFilterConditionColName, WithIndex(12, kFilterConditionSchema)}, | ||
| }; | ||
|
|
||
| StatusRecordOr<ResultSet> FetchStatisticsResultSet( | ||
| StatementHandle& stmt_handle, std::string const& catalog_name, | ||
| std::string const& schema_name, std::string const& table_name, | ||
| SQLUSMALLINT unique, SQLUSMALLINT reserved); | ||
|
|
||
| } // namespace google::cloud::odbc_bq_driver_internal | ||
|
|
||
| #endif // CPP_BIGQUERY_ODBC_GOOGLE_CLOUD_ODBC_BQ_DRIVER_INTERNAL_ODBC_SQL_STATISTICS_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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