Skip to content

Commit

Permalink
Feat: add hint builder (#1758)
Browse files Browse the repository at this point in the history
* Feat: add hint builder

* Comment fixup

* Comment fixup

* Comment fixup
  • Loading branch information
georgesittas authored Jun 12, 2023
1 parent 146e66a commit a4934cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 25 additions & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3077,12 +3077,36 @@ def lock(self, update: bool = True, copy: bool = True) -> Select:
Returns:
The modified expression.
"""

inst = _maybe_copy(self, copy)
inst.set("locks", [Lock(update=update)])

return inst

def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
"""
Set hints for this expression.
Examples:
>>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
'SELECT /*+ BROADCAST(y) */ x FROM tbl'
Args:
hints: The SQL code strings to parse as the hints.
If an `Expression` instance is passed, it will be used as-is.
dialect: The dialect used to parse the hints.
copy: If `False`, modify this expression instance in-place.
Returns:
The modified expression.
"""
inst = _maybe_copy(self, copy)
inst.set(
"hint",
Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints if h]),
)

return inst

@property
def named_selects(self) -> t.List[str]:
return [e.output_name for e in self.expressions if e.alias_or_name]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def test_build(self):
"SELECT x FROM tbl WHERE x > 0 FOR SHARE",
"postgres",
),
(
lambda: select("x").from_("tbl").hint("repartition(100)"),
"SELECT /*+ REPARTITION(100) */ x FROM tbl",
"spark",
),
(
lambda: select("x").from_("tbl").hint("coalesce(3)", "broadcast(x)"),
"SELECT /*+ COALESCE(3), BROADCAST(x) */ x FROM tbl",
"spark",
),
(
lambda: select("x", "y").from_("tbl").group_by("x"),
"SELECT x, y FROM tbl GROUP BY x",
Expand Down

0 comments on commit a4934cb

Please sign in to comment.