Skip to content
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

Disambiguate duplicate column names during read #253

Merged
merged 1 commit into from
Jan 29, 2024
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
9 changes: 8 additions & 1 deletion src/pantab/libpantab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,16 @@ auto read_from_hyper_query(const std::string &path, const std::string &query)
}

const auto column_count = resultSchema.getColumnCount();
std::unordered_map<std::string, size_t> name_counter;
for (size_t i = 0; i < column_count; i++) {
const auto column = resultSchema.getColumn(i);
const auto name = column.getName().getUnescaped();
auto name = column.getName().getUnescaped();
const auto& [elem, did_insert] = name_counter.emplace(name, 0);
if (!did_insert) {
name = name + "_" + std::to_string(elem->second);
}
elem->second += 1;

if (ArrowSchemaSetName(schema->children[i], name.c_str())) {
throw std::runtime_error("ArrowSchemaSetName failed");
}
Expand Down
31 changes: 31 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,34 @@ def test_read_varchar(tmp_hyper):

result = pantab.frame_from_hyper(tmp_hyper, table=table_name)
tm.assert_frame_equal(result, expected)


def test_reader_handles_duplicate_columns(tmp_hyper):
column_name = "does_not_matter"
table_name = tab_api.TableName("public", "table")
table = tab_api.TableDefinition(
table_name=table_name,
columns=[
tab_api.TableDefinition.Column(
name=column_name,
type=tab_api.SqlType.varchar(42),
nullability=tab_api.NOT_NULLABLE,
)
],
)
with tab_api.HyperProcess(
telemetry=tab_api.Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU
) as hyper:
with tab_api.Connection(
endpoint=hyper.endpoint,
database=tmp_hyper,
create_mode=tab_api.CreateMode.CREATE_AND_REPLACE,
) as connection:
connection.catalog.create_table(table_definition=table)

with tab_api.Inserter(connection, table) as inserter:
inserter.add_rows([["foo"], ["bar"]])
inserter.execute()

df = pantab.frame_from_hyper_query(tmp_hyper, "SELECT 1 as col, 2 AS col")
assert df.columns.tolist() == ["col", "col_1"]
Loading