summaryrefslogtreecommitdiffstats
path: root/tests/test_expressions.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_expressions.py')
-rw-r--r--tests/test_expressions.py70
1 files changed, 66 insertions, 4 deletions
diff --git a/tests/test_expressions.py b/tests/test_expressions.py
index 1fbe2d7..118b992 100644
--- a/tests/test_expressions.py
+++ b/tests/test_expressions.py
@@ -115,7 +115,26 @@ class TestExpressions(unittest.TestCase):
self.assertIsNone(column.find_ancestor(exp.Join))
def test_to_dot(self):
- column = parse_one('a.b.c."d".e.f').find(exp.Column)
+ orig = parse_one('a.b.c."d".e.f')
+ self.assertEqual(".".join(str(p) for p in orig.parts), 'a.b.c."d".e.f')
+
+ self.assertEqual(
+ ".".join(
+ str(p)
+ for p in exp.Dot.build(
+ [
+ exp.to_table("a.b.c"),
+ exp.to_identifier("d"),
+ exp.to_identifier("e"),
+ exp.to_identifier("f"),
+ ]
+ ).parts
+ ),
+ "a.b.c.d.e.f",
+ )
+
+ self.assertEqual(".".join(str(p) for p in orig.parts), 'a.b.c."d".e.f')
+ column = orig.find(exp.Column)
dot = column.to_dot()
self.assertEqual(dot.sql(), 'a.b.c."d".e.f')
@@ -198,17 +217,42 @@ class TestExpressions(unittest.TestCase):
"foo.`{bar,er}`",
)
+ self.assertEqual(exp.table_name(bq_dashed_table, identify=True), '"a-1"."b"."c"')
+
def test_table(self):
self.assertEqual(exp.table_("a", alias="b"), parse_one("select * from a b").find(exp.Table))
self.assertEqual(exp.table_("a", "").sql(), "a")
+ self.assertEqual(exp.Table(db=exp.to_identifier("a")).sql(), "a")
def test_replace_tables(self):
self.assertEqual(
exp.replace_tables(
- parse_one("select * from a AS a, b, c.a, d.a cross join e.a"),
- {"a": "a1", "b": "b.a", "c.a": "c.a2", "d.a": "d2"},
+ parse_one(
+ 'select * from a AS a, b, c.a, d.a cross join e.a cross join "f-F"."A" cross join G'
+ ),
+ {
+ "a": "a1",
+ "b": "b.a",
+ "c.a": "c.a2",
+ "d.a": "d2",
+ "`f-F`.`A`": '"F"',
+ "g": "g1.a",
+ },
+ dialect="bigquery",
).sql(),
- "SELECT * FROM a1 AS a, b.a, c.a2, d2 CROSS JOIN e.a",
+ 'SELECT * FROM a1 AS a /* a */, b.a /* b */, c.a2 /* c.a */, d2 /* d.a */ CROSS JOIN e.a CROSS JOIN "F" /* f-F.A */ CROSS JOIN g1.a /* g */',
+ )
+
+ def test_expand(self):
+ self.assertEqual(
+ exp.expand(
+ parse_one('select * from "a-b"."C" AS a'),
+ {
+ "`a-b`.`c`": parse_one("select 1"),
+ },
+ dialect="spark",
+ ).sql(),
+ "SELECT * FROM (SELECT 1) AS a /* source: a-b.c */",
)
def test_replace_placeholders(self):
@@ -267,9 +311,18 @@ class TestExpressions(unittest.TestCase):
self.assertEqual(exp.func("bla", 1, "foo").sql(), "BLA(1, foo)")
self.assertEqual(exp.func("COUNT", exp.Star()).sql(), "COUNT(*)")
self.assertEqual(exp.func("bloo").sql(), "BLOO()")
+ self.assertEqual(exp.func("concat", exp.convert("a")).sql("duckdb"), "CONCAT('a')")
self.assertEqual(
exp.func("locate", "'x'", "'xo'", dialect="hive").sql("hive"), "LOCATE('x', 'xo')"
)
+ self.assertEqual(
+ exp.func("log", exp.to_identifier("x"), 2, dialect="bigquery").sql("bigquery"),
+ "LOG(x, 2)",
+ )
+ self.assertEqual(
+ exp.func("log", dialect="bigquery", expression="x", this=2).sql("bigquery"),
+ "LOG(x, 2)",
+ )
self.assertIsInstance(exp.func("instr", "x", "b", dialect="mysql"), exp.StrPosition)
self.assertIsInstance(exp.func("bla", 1, "foo"), exp.Anonymous)
@@ -284,6 +337,15 @@ class TestExpressions(unittest.TestCase):
with self.assertRaises(ValueError):
exp.func("abs")
+ with self.assertRaises(ValueError) as cm:
+ exp.func("to_hex", dialect="bigquery", this=5)
+
+ self.assertEqual(
+ str(cm.exception),
+ "Unable to convert 'to_hex' into a Func. Either manually construct the Func "
+ "expression of interest or parse the function call.",
+ )
+
def test_named_selects(self):
expression = parse_one(
"SELECT a, b AS B, c + d AS e, *, 'zz', 'zz' AS z FROM foo as bar, baz"