ydb-sqlglot-plugin 0.2.5__tar.gz → 0.2.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ydb-sqlglot-plugin
3
- Version: 0.2.5
3
+ Version: 0.2.6
4
4
  Summary: YDB dialect plugin for sqlglot
5
5
  Author: YDB Team
6
6
  License: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ydb-sqlglot-plugin"
7
- version = "0.2.5" # AUTOVERSION
7
+ version = "0.2.6" # AUTOVERSION
8
8
  description = "YDB dialect plugin for sqlglot"
9
9
  readme = "README.md"
10
10
  license = {text = "Apache-2.0"}
@@ -0,0 +1 @@
1
+ VERSION = "0.2.6"
@@ -1532,6 +1532,31 @@ class YDB(Dialect):
1532
1532
  return None
1533
1533
  return super()._parse_table_alias(alias_tokens=alias_tokens)
1534
1534
 
1535
+ def _normalize_ydb_table_path(self, table: exp.Expression) -> exp.Expression:
1536
+ if not isinstance(table, exp.Table):
1537
+ return table
1538
+
1539
+ identifier = table.this
1540
+ if not isinstance(identifier, exp.Identifier) or table.catalog:
1541
+ return table
1542
+
1543
+ path = identifier.this
1544
+ if "/" not in path:
1545
+ if table.db:
1546
+ table.set("catalog", table.args.get("db"))
1547
+ table.set("db", None)
1548
+ return table
1549
+
1550
+ schema, name = path.rsplit("/", 1)
1551
+ if not schema or not name:
1552
+ return table
1553
+
1554
+ if table.db:
1555
+ table.set("catalog", table.args.get("db"))
1556
+ table.set("db", exp.to_identifier(schema, quoted=True))
1557
+ table.set("this", exp.to_identifier(name, quoted=True))
1558
+ return table
1559
+
1535
1560
  def _parse_table_hints(self) -> t.Optional[t.List[exp.Expression]]:
1536
1561
  if not (self._curr and self._curr.token_type == TokenType.WITH):
1537
1562
  return super()._parse_table_hints()
@@ -1845,9 +1870,9 @@ class YDB(Dialect):
1845
1870
  parts.append(self._curr.text)
1846
1871
  self._advance()
1847
1872
  self._match(TokenType.R_BRACKET)
1848
- table = self.expression(exp.Table(this=exp.to_identifier("".join(parts))))
1873
+ table = self.expression(exp.Table(this=exp.to_identifier("".join(parts), quoted=True)))
1849
1874
  table.set("alias", self._parse_table_alias())
1850
- return table
1875
+ return self._normalize_ydb_table_path(table)
1851
1876
 
1852
1877
  table = super()._parse_table(*args, **kwargs)
1853
1878
  if (
@@ -1861,6 +1886,8 @@ class YDB(Dialect):
1861
1886
  param = self._parse_parameter()
1862
1887
  table = self.expression(exp.Table(this=param))
1863
1888
  table.set("alias", self._parse_table_alias())
1889
+ if table:
1890
+ table = self._normalize_ydb_table_path(table)
1864
1891
  if table and self._match(TokenType.VIEW):
1865
1892
  table.set("ydb_index_view", self._parse_id_var(any_token=True))
1866
1893
  explicit_alias = bool(self._curr and self._curr.token_type == TokenType.ALIAS)
@@ -2412,8 +2439,12 @@ class YDB(Dialect):
2412
2439
  if expression.alias:
2413
2440
  sql += f" AS {expression.alias}"
2414
2441
  return _with_table_joins(sql)
2415
- prefix = f"{expression.db}/" if expression.db else ""
2416
- sql = f"`{prefix}{expression.name}`"
2442
+ path = f"{expression.db}/{expression.name}" if expression.db else expression.name
2443
+ if expression.catalog:
2444
+ table_identifier = f"`{path}`" if expression.db else self.sql(expression, "this")
2445
+ sql = f"{expression.catalog}.{table_identifier}"
2446
+ else:
2447
+ sql = f"`{path}`"
2417
2448
 
2418
2449
  ydb_index_view = self.sql(expression, "ydb_index_view")
2419
2450
  if ydb_index_view:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ydb-sqlglot-plugin
3
- Version: 0.2.5
3
+ Version: 0.2.6
4
4
  Summary: YDB dialect plugin for sqlglot
5
5
  Author: YDB Team
6
6
  License: Apache-2.0
@@ -1 +0,0 @@
1
- VERSION = "0.2.5"