Skip to content

Commit f243fbf

Browse files
committed
fix(bigframes): escape backtick in ordering_clause identifier
1 parent 270c7fe commit f243fbf

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

packages/bigframes/bigframes/core/sql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def ordering_clause(
154154
# Probably shouldn't have constants in ordering definition, but best to ignore if somehow they end up here.
155155
continue
156156
assert isinstance(ordering_expr, bigframes.core.expression.DerefOp)
157-
part = f"`{ordering_expr.id.sql}` {asc_desc} {null_clause}"
157+
part = f"`{escape_chars(ordering_expr.id.sql)}` {asc_desc} {null_clause}"
158158
parts.append(part)
159159
return f"ORDER BY {' ,'.join(parts)}"
160160

packages/bigframes/tests/unit/core/test_sql.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,24 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import bigframes.core.expression as ex
16+
import bigframes.core.identifiers as ids
17+
import bigframes.core.ordering as order
1518
from bigframes.core import sql
1619

1720

21+
def test_ordering_clause_escapes_backtick_in_column_name():
22+
ordering = order.OrderingExpression(ex.DerefOp(ids.ColumnId("col`,(SELECT 1))--")))
23+
result = sql.ordering_clause([ordering])
24+
assert result == "ORDER BY `col\\`,(SELECT 1))--` ASC NULLS LAST"
25+
26+
27+
def test_ordering_clause_leaves_plain_column_name_unchanged():
28+
ordering = order.OrderingExpression(ex.DerefOp(ids.ColumnId("my_col")))
29+
result = sql.ordering_clause([ordering])
30+
assert result == "ORDER BY `my_col` ASC NULLS LAST"
31+
32+
1833
def test_create_vector_search_sql_simple():
1934
result_query = sql.create_vector_search_sql(
2035
sql_string="SELECT embedding FROM my_embeddings_table WHERE id = 1",

0 commit comments

Comments
 (0)