diff --git a/python/pyspark/sql/tests/test_dataframe.py b/python/pyspark/sql/tests/test_dataframe.py index 446ef0a10e43c..3119d398b9971 100644 --- a/python/pyspark/sql/tests/test_dataframe.py +++ b/python/pyspark/sql/tests/test_dataframe.py @@ -705,6 +705,12 @@ def test_to_local_iterator_not_fully_consumed(self): break self.assertEqual(df.take(8), result) + def test_different_col_order(self): + row = self.spark.createDataFrame( + self.sc.parallelize([Row(A="1", B="2")]), "B string, A string").first() + self.assertEqual(row['A'], "1") + self.assertEqual(row['B'], "2") + class QueryExecutionListenerTests(unittest.TestCase, SQLTestUtils): # These tests are separate because it uses 'spark.sql.queryExecutionListeners' which is diff --git a/python/pyspark/sql/types.py b/python/pyspark/sql/types.py index f9b12f15117db..36d9258d6cc43 100644 --- a/python/pyspark/sql/types.py +++ b/python/pyspark/sql/types.py @@ -613,6 +613,8 @@ def toInternal(self, obj): return tuple(obj.get(n) for n in self.names) elif isinstance(obj, Row) and getattr(obj, "__from_dict__", False): return tuple(obj[n] for n in self.names) + elif isinstance(obj, Row) and hasattr(obj, "__fields__"): + return tuple(obj[n] for n in self.names) elif isinstance(obj, (list, tuple)): return tuple(obj) elif hasattr(obj, "__dict__"):