Skip to content

GH-47721: [C++][FlightRPC] Return ODBC Column Attribute from result set - #48050

Merged
lidavidm merged 2 commits into
apache:mainfrom
Bit-Quill:gh-47721-sql-col-attribute
Dec 30, 2025
Merged

lidavidm merged 2 commits into
apache:mainfrom
Bit-Quill:gh-47721-sql-col-attribute

Conversation

@alinaliBQ

@alinaliBQ alinaliBQ commented Nov 4, 2025 •

Copy link
Copy Markdown
Collaborator

Rationale for this change

Implement ODBC to return column attribute values from the result set

What changes are included in this PR?

  • SQLColAttribute & Tests

Are these changes tested?

Tested on local MSVC

Are there any user-facing changes?

N/A

@github-actions

github-actions Bot commented Nov 4, 2025

Copy link
Copy Markdown

⚠️ GitHub issue #47721 has been automatically assigned in GitHub to PR creator.

@alinaliBQ
alinaliBQ force-pushed the gh-47721-sql-col-attribute branch from 44deffe to f3e267e Compare November 10, 2025 18:52
@alinaliBQ alinaliBQ changed the title GH-47721: [C++][FlightRPC] WIP: Return ODBC Column Attribute from result set GH-47721: [C++][FlightRPC] Return ODBC Column Attribute from result set Nov 10, 2025
@alinaliBQ

Copy link
Copy Markdown
Collaborator Author

@lidavidm Draft PR is ready for review 🙂

@lidavidm

Copy link
Copy Markdown
Member

@alinaliBQ sorry but do you mind just undrafting PRs when they are ready? That way it pings me automatically and if I approve, I can merge directly

return true;
default:
return false;
return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't this give a bad result on non-numeric types? (Maybe the right answer is to throw?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In any case, there's is_unsigned_integer in type_traits.h...I don't see why listing out the signed types explicitly here makes any difference if you aren't going to throw for non-numeric types

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.

Now using arrow::is_unsigned_integer() instead.
We don't want to throw an exception for non-numeric types because we expect all the columns of a result set to hit this logic regardless of type. Returning false for non-numerics is the desired behavior.

}

std::string FlightSqlResultSetMetadata::GetTypeName(int column_position) {
std::string FlightSqlResultSetMetadata::GetTypeName(int column_position, int data_type) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a more precise type we can use than int?

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.

Now using int16_t.

Comment on lines +154 to +160
void CheckSQLColAttributes(SQLHSTMT stmt, SQLUSMALLINT idx,
const std::wstring& expected_column_name,
SQLLEN expected_data_type, SQLLEN expected_display_size,
SQLLEN expected_prec_scale, SQLLEN expected_length,
SQLLEN expected_column_size, SQLLEN expected_column_scale,
SQLLEN expected_column_nullability, SQLLEN expected_searchable,
SQLLEN expected_unsigned_column) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the difference just which attributes are checked? Is there any better way to name this? Otherwise I think they should just be overloads of the same function

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.

No this is testing a different ODBC API. The earlier function was testing SQLColAttribute (singular attribute) while this one is testing SQLColAttributes (plural attributes). Understandably this difference is very easy to miss.

@alinaliBQ

alinaliBQ commented Nov 12, 2025 •

Copy link
Copy Markdown
Collaborator Author

@alinaliBQ sorry but do you mind just undrafting PRs when they are ready? That way it pings me automatically and if I approve, I can merge directly

Hi @lidavidm I was out of office yesterday, will reply to your comment on #46099 (comment). Please see my reply in #46099 (comment)

@justing-bq

Copy link
Copy Markdown
Contributor

@lidavidm Please take another look.

@justing-bq
justing-bq force-pushed the gh-47721-sql-col-attribute branch from d46ee0c to e2a3362 Compare November 17, 2025 23:18
@alinaliBQ
alinaliBQ marked this pull request as ready for review November 18, 2025 17:45
@alinaliBQ
alinaliBQ marked this pull request as draft November 28, 2025 22:52
@alinaliBQ

Copy link
Copy Markdown
Collaborator Author

Turning PR back to draft state since there is a dependency on SQLExecDirect API implementation. PR can still be reviewed

@alinaliBQ
alinaliBQ force-pushed the gh-47721-sql-col-attribute branch from e2a3362 to 630a544 Compare November 28, 2025 22:52
@justing-bq
justing-bq force-pushed the gh-47721-sql-col-attribute branch from 630a544 to 02ac357 Compare December 3, 2025 18:34
@alinaliBQ
alinaliBQ force-pushed the gh-47721-sql-col-attribute branch from 02ac357 to d92a1a8 Compare December 5, 2025 23:29
@alinaliBQ
alinaliBQ marked this pull request as ready for review December 5, 2025 23:29
@alinaliBQ
alinaliBQ requested a review from lidavidm December 5, 2025 23:29
@alinaliBQ
alinaliBQ force-pushed the gh-47721-sql-col-attribute branch from d92a1a8 to 93b300d Compare December 8, 2025 22:29
alinaliBQ and others added 2 commits December 15, 2025 14:37
Use nullptr explicitly instead of 0

Co-Authored-By: alinalibq <alina.li@improving.com>
Co-Authored-By: justing-bq <justin.gossett@improving.com>
`metadata_map_` would be nullptr if `metadata_map` is null. Downstream might assume `metadata_map_` is always non-null, so it is safer to create an empty map if  `metadata_map` is null.
@alinaliBQ
alinaliBQ force-pushed the gh-47721-sql-col-attribute branch from 93b300d to 05e08e6 Compare December 15, 2025 23:21
Comment on lines 60 to +64
ColumnMetadata::ColumnMetadata(
std::shared_ptr<const arrow::KeyValueMetadata> metadata_map)
: metadata_map_(std::move(metadata_map)) {}
std::shared_ptr<const arrow::KeyValueMetadata> metadata_map) {
metadata_map_ = std::move(metadata_map ? metadata_map
: std::make_shared<arrow::KeyValueMetadata>());
}

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.

In the original implementation, metadata_map_ would be nullptr if metadata_map is null. Downstream might assume metadata_map_ is always non-null, so it is safer to create an empty map if metadata_map is null.

@HyukjinKwon

Copy link
Copy Markdown
Member

Hi! seems like the build is broken from this change https://github.com/apache/arrow/actions/runs/20586860759/job/59124745454?pr=48683.

std::shared_ptr<const arrow::KeyValueMetadata> metadata_map)
: metadata_map_(std::move(metadata_map)) {}
std::shared_ptr<const arrow::KeyValueMetadata> metadata_map) {
metadata_map_ = std::move(metadata_map ? metadata_map

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ack, I was questioning that line and should've checked the CI...let's revert for now

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

oh actually I was creating a followup. do you mind holding on a sec?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lidavidm added a commit to lidavidm/arrow that referenced this pull request Dec 30, 2025
@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit 16de220.

There weren't enough matching historic benchmark results to make a call on whether there were regressions.

The full Conbench report has more details.

Mottl pushed a commit to Mottl/arrow that referenced this pull request May 26, 2026
…sult set (apache#48050)

### Rationale for this change
Implement ODBC to return column attribute values from the result set

### What changes are included in this PR?
- SQLColAttribute & Tests
### Are these changes tested?
Tested on local MSVC
### Are there any user-facing changes?
N/A
* GitHub Issue: apache#47721

Authored-by: Alina (Xi) Li <alina.li@improving.com>
Signed-off-by: David Li <li.davidm96@gmail.com>
lidavidm pushed a commit that referenced this pull request Oct 16, 2026
…ring attributes with the wide decoder (#50853)

### Rationale for this change

Two string attributes in the ODBC driver are decoded with the byte-wise
decoder even when the value arrives through a wide (Unicode / `*W`) entry point,
so the wide buffer is misread and corrupted.

`ODBCConnection::SetConnectAttr(SQL_ATTR_CURRENT_CATALOG)` had its two decode
branches swapped:

```cpp
if (is_unicode) {
  SetAttributeUTF8(value, string_length, catalog);      // byte-wise
} else {
  SetAttributeSQLWCHAR(value, string_length, catalog);  // wide
}
```

`is_unicode` selects the buffer **width**: a unicode (`*W`) call passes a wide
`SQLWCHAR` buffer that must be decoded with `SetAttributeSQLWCHAR`; a non-unicode
call passes a byte string decoded with `SetAttributeUTF8`. With the branches
swapped, a wide catalog name (e.g. UTF-16 `"my_catalog"`) is reinterpreted
byte-wise and stored with the wide encoding's embedded NULs
(`"m\0y\0_\0c\0a\0t\0a\0l\0o\0g\0"`), so catalog scoping operates on a corrupt
name. This is the inverse of the mapping the getter already uses:
`GetStringAttribute` maps `is_unicode -> GetAttributeSQLWCHAR`.

`ODBCDescriptor::SetField(SQL_DESC_NAME)` is the same class of bug: it
unconditionally used the byte-wise `SetAttributeUTF8`, even though the matching
getter (`GetField` / `SQL_DESC_NAME`) reads the field back with
`GetAttributeSQLWCHAR`.

**How this happened (history):**
- The `SQL_ATTR_CURRENT_CATALOG` branches were swapped from the start, in the
  original driver import (GH-46522, #40939). Note the getter side was written
  against the shared `GetStringAttribute` helper (which decides width in one
  place and got it right), while the setter open-coded the branch inline and
  inverted the polarity — there is no matching `SetStringAttribute` helper.
- The descriptor `SQL_DESC_NAME` case was originally *consistent* (getter and
  setter both byte-wise). GH-47721 (#48050) later migrated the descriptor
  string getters to the wide `GetAttributeSQLWCHAR` for correct Unicode column
  attributes, but left the `SQL_DESC_NAME` setter on the byte-wise decoder,
  creating the asymmetry.

### What changes are included in this PR?

- `odbc_connection.cc`: swap the `SQL_ATTR_CURRENT_CATALOG` decode branches so a
  unicode call uses `SetAttributeSQLWCHAR` and a non-unicode call uses
  `SetAttributeUTF8`, matching `GetStringAttribute`.
- `odbc_descriptor.cc`: decode `SQL_DESC_NAME` with `SetAttributeSQLWCHAR` to
  match its getter.
- `connection_attr_test.cc`: add `TestSQLSetGetConnectAttrCurrentCatalogWide`, a
  `TYPED_TEST` (mock + remote fixtures) that sets a multi-character catalog
  through the wide entry point and asserts the full name round-trips back.
- `odbc_descriptor_test.cc` (new): add `ODBCDescriptorTest.SetGetNameWideRoundTrips`,
  a `SetField`/`GetField` round-trip on `SQL_DESC_NAME` with a multi-character
  wide name. There is no `SQLSetDescField` entry point in this tree, so this
  covers the descriptor change with a direct unit test on `ODBCDescriptor`.

### Are these changes tested?

Yes. Each change has a round-trip test that distinguishes the fixed and unfixed
code:

| check | result |
|---|---|
| catalog test, **with** fix (mock fixture) | PASS — `out_catalog == "my_catalog"` |
| catalog test, **without** fix (mock fixture) | FAIL — `out_catalog == "m\0y\0_\0c\0a\0t\0a\0l\0o\0g\0"` |
| descriptor test, **with** fix | PASS — name reads back as `"my_column"` |
| descriptor test, **without** fix | FAIL — byte-wise decode corrupts the stored UTF-16 |
| `arrow-odbc-spi-impl-test` (unit suite) | 85/85 pass |
| `ConnectionAttributeTest/0.*` (mock suite) | 25/25 pass |
| clang-format | clean on all changed files |

The connection catalog test uses the mock fixture (`FlightSQLODBCMockTestBase`,
an in-process SQLite Flight SQL server); its remote variant runs when
`ARROW_FLIGHT_SQL_ODBC_CONN` is set. The descriptor test is a standalone unit
test. Verified locally against the mock fixture and the unit suite.

### Are there any user-facing changes?

Yes. A catalog name (and descriptor `SQL_DESC_NAME`) set through the wide entry
point is now decoded correctly instead of being corrupted. Applications that set
a multi-character catalog through `SQLSetConnectAttrW` now scope to the intended
catalog.

### AI usage

Per the [AI-generated code guidance](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code):
the diagnosis, the fix, and the test were produced with the assistance of an AI
coding agent and reviewed and verified by me. Correctness was checked by
(1) confirming the getter path (`GetStringAttribute`) maps `is_unicode` to the
wide decoder, establishing the intended mapping the setters violated, and
(2) running the new round-trip test against both the fixed and unfixed driver to
confirm it distinguishes them (clean name vs. embedded-NUL corruption).

---
_🤖 Drafted by Claude Code (an AI agent) and reviewed & verified by vikrantpuppala._

* GitHub Issue: #50852

Lead-authored-by: Vikrant Puppala <vikrant.puppala@databricks.com>
Co-authored-by: Isaac
Signed-off-by: David Li <li.davidm96@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants