import json
import robin_sparkless as rs
def test_select_when_otherwise_alias() -> None:
spark = rs.SparkSession.builder().app_name("test").get_or_create()
df = spark._create_dataframe_from_rows(
[{"x": 1}, {"x": 2}],
[("x", "bigint")],
)
result = df.select(
rs.when(rs.col("x") > 1)
.then(rs.lit("yes"))
.otherwise(rs.lit("no"))
.alias("result")
)
rows = result.collect()
assert len(rows) == 2
assert rows[0]["result"] == "no"
assert rows[1]["result"] == "yes"
def test_select_window_rank_alias() -> None:
spark = rs.SparkSession.builder().app_name("test").get_or_create()
df = spark._create_dataframe_from_rows(
[{"x": 10}, {"x": 20}, {"x": 20}],
[("x", "bigint")],
)
result = df.select(rs.col("x").rank(False).over(["x"]).alias("rank"))
rows = result.collect()
assert len(rows) == 3
assert all("rank" in r for r in rows)
assert all(isinstance(r["rank"], int) for r in rows)
def test_select_expression_alias_via_execute_plan() -> None:
data = [{"a": 1}, {"a": 2}]
schema = [("a", "bigint")]
plan = [
{
"op": "select",
"payload": [
{
"name": "result",
"expr": {
"op": "gt",
"left": {"col": "a"},
"right": {"lit": 1},
},
}
],
}
]
plan_json = json.dumps(plan)
df = rs._execute_plan(data, schema, plan_json)
rows = df.collect()
assert len(rows) == 2
assert rows[0]["result"] is False assert rows[1]["result"] is True