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
2 changes: 1 addition & 1 deletion sqlglot-integration-tests
29 changes: 29 additions & 0 deletions sqlglot/generators/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3422,6 +3422,35 @@ def right_sql(self, expression: exp.Right) -> str:
def rtrimmedlength_sql(self, expression: exp.RtrimmedLength) -> str:
return self.func("LENGTH", exp.Trim(this=expression.this, position="TRAILING"))

def stuff_sql(self, expression: exp.Stuff) -> str:
Comment thread
georgesittas marked this conversation as resolved.
base = expression.this
start = expression.args["start"]
length = expression.args["length"]
insertion = expression.expression
is_binary = _is_binary(base)

if is_binary:
# DuckDB's SUBSTRING doesn't accept BLOB; operate on the HEX string instead
# (each byte = 2 hex chars), then UNHEX back to BLOB
base = exp.Hex(this=base)
insertion = exp.Hex(this=insertion)
start = (start - exp.Literal.number(1)) * exp.Literal.number(2) + exp.Literal.number(1)

left = exp.Substring(
this=base.copy(),
start=exp.Literal.number(1),
length=start.copy() - exp.Literal.number(1),
)
right = exp.Substring(this=base.copy(), start=start.copy() + length.copy())
result: exp.Expr = exp.DPipe(
this=exp.DPipe(this=left, expression=insertion), expression=right
)

if is_binary:
result = exp.Unhex(this=result)

return self.sql(result)

def rand_sql(self, expression: exp.Rand) -> str:
seed = expression.this
if seed is not None:
Expand Down
Loading