Skip to content

fix(sqlite): implement PARSE_COLNAMES column name parsing #5923

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

Merged
merged 3 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_sqlite3/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,6 @@ def test_none(self):
val = self.cur.fetchone()[0]
self.assertEqual(val, None)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_col_name(self):
self.cur.execute("insert into test(x) values (?)", ("xxx",))
self.cur.execute('select x as "x y [bar]" from test')
Expand Down
36 changes: 30 additions & 6 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ mod _sqlite {

inner.row_cast_map = zelf.build_row_cast_map(&st, vm)?;

inner.description = st.columns_description(vm)?;
inner.description = st.columns_description(zelf.connection.detect_types, vm)?;

if ret == SQLITE_ROW {
drop(st);
Expand Down Expand Up @@ -1546,7 +1546,7 @@ mod _sqlite {
));
}

inner.description = st.columns_description(vm)?;
inner.description = st.columns_description(zelf.connection.detect_types, vm)?;

inner.rowcount = if stmt.is_dml { 0 } else { -1 };

Expand Down Expand Up @@ -2726,22 +2726,46 @@ mod _sqlite {
unsafe { sqlite3_column_name(self.st, pos) }
}

fn columns_name(self, vm: &VirtualMachine) -> PyResult<Vec<PyStrRef>> {
fn columns_name(self, detect_types: i32, vm: &VirtualMachine) -> PyResult<Vec<PyStrRef>> {

Choose a reason for hiding this comment

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

medium

The detect_types parameter is an integer, but it's being treated as a boolean flag using bitwise operations. It would improve readability to rename it to detect_colnames to clearly indicate its purpose.

fn columns_name(detect_colnames: i32, vm: &VirtualMachine) -> PyResult<Vec<PyStrRef>> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll leave the self requirement in columns_name for now, as it's part of the existing implementation.

let count = self.column_count();
(0..count)
.map(|i| {
let name = self.column_name(i);
ptr_to_str(name, vm).map(|x| vm.ctx.new_str(x))
let name_str = ptr_to_str(name, vm)?;

// If PARSE_COLNAMES is enabled, strip everything after the first '[' (and preceding space)
let processed_name = if detect_types & PARSE_COLNAMES != 0
&& let Some(bracket_pos) = name_str.find('[')
{
// Check if there's a single space before '[' and remove it (CPython compatibility)
let end_pos = if bracket_pos > 0
&& name_str.chars().nth(bracket_pos - 1) == Some(' ')
{
bracket_pos - 1
} else {
bracket_pos
};

&name_str[..end_pos]
} else {
name_str
};

Ok(vm.ctx.new_str(processed_name))
})
.collect()
}

fn columns_description(self, vm: &VirtualMachine) -> PyResult<Option<PyTupleRef>> {
fn columns_description(
self,
detect_types: i32,
vm: &VirtualMachine,
) -> PyResult<Option<PyTupleRef>> {
if self.column_count() == 0 {
return Ok(None);
}
let columns = self
.columns_name(vm)?
.columns_name(detect_types, vm)?
.into_iter()
.map(|s| {
vm.ctx
Expand Down
Loading