Skip to content

Commit

Permalink
fix(bigquery): disallow column names longer than 300 characters (#9916)
Browse files Browse the repository at this point in the history
Closes #8931.
  • Loading branch information
cpcloud authored Aug 26, 2024
1 parent 69a44c4 commit ea97794
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ibis/backends/bigquery/tests/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,12 @@ def test_approx_quantiles(alltypes, quantiles, snapshot):
query = alltypes.double_col.approx_quantile(quantiles).name("qs")
result = ibis.to_sql(query, dialect="bigquery")
snapshot.assert_match(result, "out.sql")


def test_unreasonably_long_name():
expr = ibis.literal("hello, world!").name("a" * 301)
with pytest.raises(
com.IbisError,
match="BigQuery does not allow column names longer than 300 characters",
):
ibis.to_sql(expr, dialect="bigquery")
20 changes: 19 additions & 1 deletion ibis/backends/sql/compilers/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,25 @@ def visit_HashBytes(self, op, *, arg, how):

@staticmethod
def _gen_valid_name(name: str) -> str:
return "_".join(map(str.strip, _NAME_REGEX.findall(name))) or "tmp"
candidate = "_".join(map(str.strip, _NAME_REGEX.findall(name))) or "tmp"
# column names cannot be longer than 300 characters
#
# https://cloud.google.com/bigquery/docs/schemas#column_names
#
# it's easy to rename columns, so raise an exception telling the user
# to do so
#
# we could potentially relax this and support arbitrary-length columns
# by compressing the information using hashing, but there's no reason
# to solve that problem until someone encounters this error and cannot
# rename their columns
limit = 300
if len(candidate) > limit:
raise com.IbisError(
f"BigQuery does not allow column names longer than {limit:d} characters. "
"Please rename your columns to have fewer characters."
)
return candidate

def visit_CountStar(self, op, *, arg, where):
if where is not None:
Expand Down

0 comments on commit ea97794

Please sign in to comment.