diff --git a/sqlglot/dialects/duckdb.py b/sqlglot/dialects/duckdb.py index 05761e4fd6..f61cd2c4d5 100644 --- a/sqlglot/dialects/duckdb.py +++ b/sqlglot/dialects/duckdb.py @@ -1166,18 +1166,26 @@ def length_sql(self, expression: exp.Length) -> str: return self.sql(case) def lower_sql(self, expression: exp.Lower) -> str: + return self._case_conversion_sql(expression, "LOWER") + + def upper_sql(self, expression: exp.Upper) -> str: + return self._case_conversion_sql(expression, "UPPER") + + def _case_conversion_sql( + self, expression: t.Union[exp.Lower, exp.Upper], func_name: str + ) -> str: arg = expression.this if arg.type and not arg.is_type(exp.DataType.Type.VARCHAR, exp.DataType.Type.UNKNOWN): expression.this.replace(exp.cast(expression.this, exp.DataType.Type.VARCHAR)) - lower_sql = self.func("LOWER", expression.this) + result_sql = self.func(func_name, expression.this) is_binary = expression.is_type(exp.DataType.Type.BINARY) if is_binary: blob = exp.DataType.build("BLOB", dialect="duckdb") - lower_sql = self.sql(exp.Cast(this=lower_sql, to=blob)) + result_sql = self.sql(exp.Cast(this=result_sql, to=blob)) - return lower_sql + return result_sql def objectinsert_sql(self, expression: exp.ObjectInsert) -> str: this = expression.this diff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py index 00a720b39d..8249894f88 100644 --- a/tests/dialects/test_bigquery.py +++ b/tests/dialects/test_bigquery.py @@ -900,6 +900,20 @@ def test_bigquery(self): "trino": "LOWER(TO_HEX(x))", }, ) + + sql = "UPPER(CAST('hello' AS BYTES))" + expr = self.parse_one(sql) + qualified = qualify(expr, dialect="bigquery") + annotated = annotate_types(qualified, dialect="bigquery") + self.assertEqual( + annotated.sql("duckdb"), "CAST(UPPER(CAST(CAST('hello' AS BLOB) AS TEXT)) AS BLOB)" + ) + + sql = "UPPER('hello')" + expr = self.parse_one(sql) + annotated = annotate_types(expr, dialect="bigquery") + self.assertEqual(annotated.sql("duckdb"), "UPPER('hello')") + self.validate_all( "UPPER(TO_HEX(x))", read={ diff --git a/tests/dialects/test_duckdb.py b/tests/dialects/test_duckdb.py index f59c680d67..e6520e50bd 100644 --- a/tests/dialects/test_duckdb.py +++ b/tests/dialects/test_duckdb.py @@ -1166,6 +1166,9 @@ def test_duckdb(self): self.assertEqual( annotate_types(self.parse_one("LOWER('HELLO')")).sql("duckdb"), "LOWER('HELLO')" ) + self.assertEqual( + annotate_types(self.parse_one("UPPER('hello')")).sql("duckdb"), "UPPER('hello')" + ) def test_array_index(self): with self.assertLogs(helper_logger) as cm: