Skip to content

Commit

Permalink
Fix: Hive UnixToTime regression, README stale results (#3055)
Browse files Browse the repository at this point in the history
* fix: Fix Hive UnixToTime regression, README stale examples

* Changing parse error example

* Nit: Adding whitespace
  • Loading branch information
VaggelisD committed Feb 29, 2024
1 parent ad21b6b commit 08249af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sqlglot.transpile("SELECT EPOCH_MS(1618088028295)", read="duckdb", write="hive")
```

```sql
'SELECT FROM_UNIXTIME(1618088028295 / 1000)'
'SELECT FROM_UNIXTIME(1618088028295 / POW(10, 3))'
```

SQLGlot can even translate custom time formats:
Expand Down Expand Up @@ -202,21 +202,21 @@ When the parser detects an error in the syntax, it raises a ParseError:

```python
import sqlglot
sqlglot.transpile("SELECT foo( FROM bar")
sqlglot.transpile("SELECT foo FROM (SELECT baz FROM t")
```

```
sqlglot.errors.ParseError: Expecting ). Line 1, Col: 13.
select foo( FROM bar
~~~~
sqlglot.errors.ParseError: Expecting ). Line 1, Col: 33.
SELECT foo FROM (SELECT baz FROM t
~
```

Structured syntax errors are accessible for programmatic use:

```python
import sqlglot
try:
sqlglot.transpile("SELECT foo( FROM bar")
sqlglot.transpile("SELECT foo FROM (SELECT baz FROM t")
except sqlglot.errors.ParseError as e:
print(e.errors)
```
Expand All @@ -225,11 +225,11 @@ except sqlglot.errors.ParseError as e:
[{
'description': 'Expecting )',
'line': 1,
'col': 16,
'start_context': 'SELECT foo( ',
'highlight': 'FROM',
'end_context': ' bar',
'into_expression': None,
'col': 33,
'start_context': 'SELECT foo FROM (SELECT baz FROM ',
'highlight': 't',
'end_context': '',
'into_expression': None
}]
```

Expand Down
11 changes: 10 additions & 1 deletion sqlglot/dialects/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ def _str_to_unix_sql(self: Hive.Generator, expression: exp.StrToUnix) -> str:
return self.func("UNIX_TIMESTAMP", expression.this, time_format("hive")(self, expression))


def _unix_to_time_sql(self: Hive.Generator, expression: exp.UnixToTime) -> str:
timestamp = self.sql(expression, "this")
scale = expression.args.get("scale")
if scale in (None, exp.UnixToTime.SECONDS):
return rename_func("FROM_UNIXTIME")(self, expression)

return f"FROM_UNIXTIME({timestamp} / POW(10, {scale}))"


def _str_to_date_sql(self: Hive.Generator, expression: exp.StrToDate) -> str:
this = self.sql(expression, "this")
time_format = self.format_time(expression)
Expand Down Expand Up @@ -536,7 +545,7 @@ class Generator(generator.Generator):
exp.UnixToStr: lambda self, e: self.func(
"FROM_UNIXTIME", e.this, time_format("hive")(self, e)
),
exp.UnixToTime: rename_func("FROM_UNIXTIME"),
exp.UnixToTime: _unix_to_time_sql,
exp.UnixToTimeStr: rename_func("FROM_UNIXTIME"),
exp.PartitionedByProperty: lambda self, e: f"PARTITIONED BY {self.sql(e, 'this')}",
exp.SerdeProperties: lambda self, e: self.properties(e, prefix="WITH SERDEPROPERTIES"),
Expand Down

0 comments on commit 08249af

Please sign in to comment.