koalas icon indicating copy to clipboard operation
koalas copied to clipboard

Does spark DataFrame.limit guarantees any order?

Open guyao opened this issue 7 years ago • 7 comments

This is just a question, not an issue.
Out of curiosity, does spark.DataFrame.limit guarantees the order ?

That method is used to implement many methods like .head, .iloc
In pandas the data is small and the order could be guaranteed. But in koalas, the data is distributed. Is it guaranteed those methods would keep order?

guyao avatar Nov 22 '19 02:11 guyao

Yeah, this is a pretty important issue in Koalas.

Currently, it does not guarantees the natural order in general. Natural order can be kept when it only performs map operation (without shuffle) but if shuffle happens, the order cannot be kept.

So, if users want the order, they should sort by, for example, index.

HyukjinKwon avatar Nov 22 '19 02:11 HyukjinKwon

We should still think about how we'll handle this in general.

HyukjinKwon avatar Nov 22 '19 02:11 HyukjinKwon

Thank you!

Follow up of my last question, do you mean spark DataFrame.limit(n) will return first n result ( in natural order, if no shuffle happens)

For example, if read a file as DataFrame, the file would be split into partitions P1, P2 ... , where P1 contains first part of file, ... If I tried to call df.limit(n), it will return first n of P1 (if n < # P1)

Is that correct? (sorry it more like a spark question but I asked here)

guyao avatar Nov 22 '19 02:11 guyao

Yes, that's correct :-).

HyukjinKwon avatar Nov 22 '19 02:11 HyukjinKwon

I was thinking about what pandas users expected when using iloc[slice(start, end, step)] Do people really know which start index, end index he need when he is using an uncountable dataframe.

( but I think user still expected order if the DF is small )

guyao avatar Nov 22 '19 02:11 guyao

Yes, it's a bit questionable at this moment. Maybe we should do one of these: ...

  1. We just disallow all such cases
  2. Document the case when the order can be deterministic
  3. Document that the index always should be sorted before such operations for order guarantee
  4. Internally always sort on the index before such operations in such cases

HyukjinKwon avatar Nov 22 '19 02:11 HyukjinKwon

I think we can close this, since now we have __natural_order__ column internally. https://github.com/databricks/koalas/pull/1146.

itholic avatar Aug 09 '21 05:08 itholic