Skip to content

Commit

Permalink
feat(python): Minor DB type inference updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed May 3, 2024
1 parent 5da378f commit 39efecf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion py-polars/polars/io/database/_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def _infer_dtype_from_database_typename(
else:
dtype = _integer_dtype_from_nbits(sz, unsigned=False, default=Int64)

# number types (note: 'number' alone is not that helpful and requires refinement)
elif "NUMBER" in value and "CARDINAL" in value:
dtype = UInt64

# decimal dtypes
elif (is_dec := ("DECIMAL" in value)) or ("NUMERIC" in value):
if "," in modifier:
Expand All @@ -152,7 +156,7 @@ def _infer_dtype_from_database_typename(
# string dtypes
elif (
any(tp in value for tp in ("VARCHAR", "STRING", "TEXT", "UNICODE"))
or value.startswith(("STR", "CHAR", "NCHAR", "UTF"))
or value.startswith(("STR", "CHAR", "BPCHAR", "NCHAR", "UTF"))
or value.endswith(("_UTF8", "_UTF16", "_UTF32"))
):
dtype = String
Expand Down
2 changes: 2 additions & 0 deletions py-polars/tests/unit/io/database/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# string types
("UTF16", pl.String),
("char(8)", pl.String),
("BPCHAR", pl.String),
("nchar[128]", pl.String),
("varchar", pl.String),
("CHARACTER VARYING(64)", pl.String),
Expand Down Expand Up @@ -50,6 +51,7 @@
("ROWID", pl.UInt64),
("mediumint", pl.Int32),
("unsigned mediumint", pl.UInt32),
("cardinal_number", pl.UInt64),
("smallserial", pl.Int16),
("serial", pl.Int32),
("bigserial", pl.Int64),
Expand Down

0 comments on commit 39efecf

Please sign in to comment.