code: def test_group_by_ordinal(self):
      spark = self.spark
      df = spark.createDataFrame(
          [
              (1, 1),
              (1, 2),
              (2, 1),
              (2, 2),
              (3, 1),
              (3, 2),
          ],
          ["a", "b"],
      )
  
      with self.tempView("v"):
          df.createOrReplaceTempView("v")
  
          # basic case
          df1 = spark.sql("select a, sum(b) from v group by 1;")
          df2 = df.groupBy(1).agg(sf.sum("b"))
          assertSchemaEqual(df1.schema, df2.schema)
          assertDataFrameEqual(df1, df2)
  
          # constant case
          df1 = spark.sql("select 1, 2, sum(b) from v group by 1, 2;")
          df2 = (
              df.select(sf.lit(1), sf.lit(2), "b")
              .groupBy(1, 2)
              .agg(sf.sum("b"))
          )
          assertSchemaEqual(df1.schema, df2.schema)
          assertDataFrameEqual(df1, df2)
  
          # duplicate group by column
          df1 = spark.sql("select a, 1, sum(b) from v group by a, 1;")
          df2 = (
              df.select("a", sf.lit(1), "b")
              .groupBy("a", 2)
              .agg(sf.sum("b"))
          )
          assertSchemaEqual(df1.schema, df2.schema)
          assertDataFrameEqual(df1, df2)
  
          df1 = spark.sql("select a, 1, sum(b) from v group by 1, 2;")
          df2 = (
              df.select("a", sf.lit(1), "b")
              .groupBy(1, 2)
              .agg(sf.sum("b"))
          )
          assertSchemaEqual(df1.schema, df2.schema)
          assertDataFrameEqual(df1, df2)
  
          # group by a non-aggregate expression's ordinal
          df1 = spark.sql(
              "select a, b + 2, count(2) from v group by a, 2;"
          )
          df2 = (
              df.select("a", df.b + 2)
              .groupBy(1, 2)
              .agg(sf.count(sf.lit(2)))
          )
          assertSchemaEqual(df1.schema, df2.schema)
          assertDataFrameEqual(df1, df2)
  
          # negative cases: ordinal out of range
          with self.assertRaises(IndexError):
              df.groupBy(0).agg(sf.sum("b"))
  
          with self.assertRaises(IndexError):
              df.groupBy(-1).agg(sf.sum("b"))
  
          with self.assertRaises(IndexError):
              df.groupBy(3).agg(sf.sum("b"))
  
          with self.assertRaises(IndexError):
              df.groupBy(10).agg(sf.sum("b"))
  
file_path: spark/python/pyspark/sql/tests/test_group.py
imports: import unittest
  from pyspark.sql import Row
  from pyspark.sql import functions
  from pyspark.testing.sqlutils import ReusedSQLTestCase, have_pandas, have_pyarrow, pandas_requirement_message, pyarrow_requirement_message
  from pyspark.testing import assertDataFrameEqual, assertSchemaEqual
label_desc: Use image node_id 3 for calling the GroupTestsMixin obj's underlying member method code with example usage: obj.test_group_by_ordinal() without return types