Skip to content
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
4 changes: 4 additions & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,10 @@ def tablesample_sql(
expression: exp.TableSample,
tablesample_keyword: str | None = None,
) -> str:
if expression.expressions:
self.unsupported("Multiple sample sets are not supported")
return ""

method = self.sql(expression, "method")
method = f"{method} " if method and self.TABLESAMPLE_WITH_METHOD else ""
numerator = self.sql(expression, "bucket_numerator")
Expand Down
12 changes: 11 additions & 1 deletion sqlglot/generators/teradata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import typing as t
from decimal import Decimal

from sqlglot import exp, generator, transforms
from sqlglot.dialects.dialect import (
Expand Down Expand Up @@ -116,7 +117,16 @@ def tablesample_sql(
expression: exp.TableSample,
tablesample_keyword: str | None = None,
) -> str:
return f"{self.sql(expression, 'this')} SAMPLE {self.expressions(expression)}"
sample = expression.args.get("size")
percent = expression.args.get("percent")
if percent and percent.is_number:
fraction = Decimal(percent.to_py()).scaleb(-2)
# 100 percent keeps its decimal point, otherwise it'd be read back as a row count
sample = exp.Literal.number(
format(fraction.normalize() if fraction < 1 else fraction, "f")
)

return f" SAMPLE {self.sql(sample) or self.expressions(expression)}"

def window_sql(self, expression: exp.Window) -> str:
spec = expression.args.get("spec")
Expand Down
15 changes: 15 additions & 0 deletions sqlglot/parsers/teradata.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ def _parse_update(self) -> exp.Update:
)
)

def _parse_table_sample(self, as_modifier: bool = False) -> exp.TableSample | None:
sample = super()._parse_table_sample(as_modifier=as_modifier)

# A single integer sample is a row count, while a fraction is a proportion of rows
# https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Data-Manipulation-Language/SELECT-Statements/SAMPLE-Clause/SAMPLE-Clause-Syntax
if sample and len(sample.expressions) == 1 and sample.expressions[0].is_number:
num = sample.expressions[0].pop()
if num.is_int:
sample.set("size", num)
else:
percent = format(num.to_py().scaleb(2).normalize(), "f")
sample.set("percent", exp.Literal.number(percent))

return sample

def _parse_rangen(self):
this = self._parse_id_var()
self._match(TokenType.BETWEEN)
Expand Down
30 changes: 29 additions & 1 deletion tests/dialects/test_teradata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlglot import exp
from sqlglot import ErrorLevel, UnsupportedError, exp
from tests.dialects.test_dialect import Validator


Expand All @@ -20,6 +20,34 @@ def test_teradata(self):
"SELECT * FROM tbl SAMPLE 0.33, .25, .1",
"SELECT * FROM tbl SAMPLE 0.33, 0.25, 0.1",
)
self.validate_all(
"SELECT * FROM t SAMPLE 100",
write={
"teradata": "SELECT * FROM t SAMPLE 100",
"snowflake": "SELECT * FROM t TABLESAMPLE (100 ROWS)",
},
)
self.validate_all(
"SELECT * FROM t SAMPLE 0.25",
read={
"snowflake": "SELECT * FROM t TABLESAMPLE (25)",
},
write={
"teradata": "SELECT * FROM t SAMPLE 0.25",
"snowflake": "SELECT * FROM t TABLESAMPLE (25)",
},
)
self.validate_all(
"SELECT * FROM t SAMPLE 0.6, 0.25",
write={
"teradata": "SELECT * FROM t SAMPLE 0.6, 0.25",
"snowflake": "SELECT * FROM t",
},
)
with self.assertRaises(UnsupportedError):
self.parse_one("SELECT * FROM t SAMPLE 10, 20").sql(
"snowflake", unsupported_level=ErrorLevel.RAISE
)

self.validate_all(
"DATABASE tduser",
Expand Down
Loading