Spaces:
Sleeping
Sleeping
Fix: backtick-quote multi-word column names in SQL output
Browse files
app.py
CHANGED
|
@@ -59,10 +59,14 @@ def parse_schema(schema_str):
|
|
| 59 |
return table_name, columns
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def structured_to_sql(sel, agg, conds, columns, table_name="table"):
|
| 63 |
if sel is None or agg is None:
|
| 64 |
return None
|
| 65 |
-
col_name = columns[sel] if sel < len(columns) else f"col{sel}"
|
| 66 |
if agg == 0:
|
| 67 |
sql = f"SELECT {col_name} FROM {table_name}"
|
| 68 |
else:
|
|
@@ -71,7 +75,7 @@ def structured_to_sql(sel, agg, conds, columns, table_name="table"):
|
|
| 71 |
if conds:
|
| 72 |
where_parts = []
|
| 73 |
for c_idx, c_op, c_val in conds:
|
| 74 |
-
c_name = columns[c_idx] if c_idx < len(columns) else f"col{c_idx}"
|
| 75 |
op_str = OPS[c_op] if c_op < len(OPS) else "="
|
| 76 |
try:
|
| 77 |
float(c_val)
|
|
|
|
| 59 |
return table_name, columns
|
| 60 |
|
| 61 |
|
| 62 |
+
def quote_col(name):
|
| 63 |
+
return f"`{name}`" if " " in name else name
|
| 64 |
+
|
| 65 |
+
|
| 66 |
def structured_to_sql(sel, agg, conds, columns, table_name="table"):
|
| 67 |
if sel is None or agg is None:
|
| 68 |
return None
|
| 69 |
+
col_name = quote_col(columns[sel] if sel < len(columns) else f"col{sel}")
|
| 70 |
if agg == 0:
|
| 71 |
sql = f"SELECT {col_name} FROM {table_name}"
|
| 72 |
else:
|
|
|
|
| 75 |
if conds:
|
| 76 |
where_parts = []
|
| 77 |
for c_idx, c_op, c_val in conds:
|
| 78 |
+
c_name = quote_col(columns[c_idx] if c_idx < len(columns) else f"col{c_idx}")
|
| 79 |
op_str = OPS[c_op] if c_op < len(OPS) else "="
|
| 80 |
try:
|
| 81 |
float(c_val)
|