Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.distributions;

import org.apache.spark.annotation.Experimental;
import org.apache.spark.sql.connector.expressions.Expression;

/**
* A distribution where tuples that share the same values for clustering expressions are co-located
* in the same partition.
*
* @since 3.2.0
*/
@Experimental
public interface ClusteredDistribution extends Distribution {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have org.apache.spark.sql.connector.read.partitioning.ClusteredDistribution, shall we remove that and always use this new one? The new one is better as it's more flexible (using Expression rather than String).

It's better if we can avoid breaking existing APIs, but I can't figure out a good way. The old ClusteredDistribution is only used in a mixin trait and marked as Evolving. Seems OK to break it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have started this discussion in comments to the original PR a bit. Some background here and here.

Overall, there are two options: break the API now and use new interfaces instead of the old ones in other places or evolve these two separately for a while and replace the read side once we have a clear plan for bucketed joins.

My first idea was to break the API and migrate to the new interfaces like suggested by @cloud-fan as that seems inevitable at some point. However, there were a couple of good points mentioned by reviewers on the original PR and I am now inclined to evolve these separately for a while until we know what changes will be required to support bucketed tables. I would like to avoid breaking the read side twice if possible.

That said, I don't feel strongly here and each option has its own benefits and drawbacks. I'll be fine with either one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also cc @viirya @HeartSaVioR @rdblue @dongjoon-hyun @sunchao who reviewed the change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK to break it later, just don't leave the old one there forever. Shall we create a blocker ticket for Spark 3.2 to remove the old ClusteredDistribution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a blocker SPARK-33807 to track this.

Thanks, everyone!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @aokolnychyi , do you have time to clean it up now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Partitioning.satisfy is still referring to the old interface I'm wondering whether we should defer this to 3.3.0 timeframe. For my storage partitioned join work I found that I need to change the Partitioning interface again in a backward incompatible way so that it can work for joins. It may be better to break it once instead of two times. That's my 2 cents.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's retarget it to 3.3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my late reply. I also assumed we don't have any clarity on how the Partitioning interface should look like so I think delaying the update and breaking that API once is better.

/**
* Returns clustering expressions.
*/
Expression[] clustering();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.distributions;

import org.apache.spark.annotation.Experimental;

/**
* An interface that defines how data is distributed across partitions.
*
* @since 3.2.0
*/
@Experimental
public interface Distribution {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.distributions;

import org.apache.spark.annotation.Experimental;
import org.apache.spark.sql.connector.expressions.Expression;
import org.apache.spark.sql.connector.expressions.SortOrder;

/**
* Helper methods to create distributions to pass into Spark.
*
* @since 3.2.0
*/
@Experimental
public class Distributions {
private Distributions() {
}

/**
* Creates a distribution where no promises are made about co-location of data.
*/
public static UnspecifiedDistribution unspecified() {
return LogicalDistributions.unspecified();
}

/**
* Creates a distribution where tuples that share the same values for clustering expressions are
* co-located in the same partition.
*/
public static ClusteredDistribution clustered(Expression[] clustering) {
return LogicalDistributions.clustered(clustering);
}

/**
* Creates a distribution where tuples have been ordered across partitions according
* to ordering expressions, but not necessarily within a given partition.
*/
public static OrderedDistribution ordered(SortOrder[] ordering) {
return LogicalDistributions.ordered(ordering);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.distributions;

import org.apache.spark.annotation.Experimental;
import org.apache.spark.sql.connector.expressions.SortOrder;

/**
* A distribution where tuples have been ordered across partitions according
* to ordering expressions, but not necessarily within a given partition.
*
* @since 3.2.0
*/
@Experimental
public interface OrderedDistribution extends Distribution {
/**
* Returns ordering expressions.
*/
SortOrder[] ordering();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.distributions;

import org.apache.spark.annotation.Experimental;

/**
* A distribution where no promises are made about co-location of data.
*
* @since 3.2.0
*/
@Experimental
public interface UnspecifiedDistribution extends Distribution {}
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,15 @@ public static Transform hours(String column) {
return LogicalExpressions.hours(Expressions.column(column));
}

/**
* Create a sort expression.
*
* @param expr an expression to produce values to sort
* @param direction direction of the sort
* @param nullOrder null order of the sort
* @return a SortOrder
*/
public static SortOrder sort(Expression expr, SortDirection direction, NullOrdering nullOrder) {
return LogicalExpressions.sort(expr, direction, nullOrder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.expressions;

import org.apache.spark.annotation.Experimental;

/**
* A null order used in sorting expressions.
*
* @since 3.2.0
*/
@Experimental
public enum NullOrdering {
NULLS_FIRST, NULLS_LAST;

@Override
public String toString() {
switch (this) {
case NULLS_FIRST:
return "NULLS FIRST";
case NULLS_LAST:
return "NULLS LAST";
default:
throw new IllegalArgumentException("Unexpected null order: " + this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.expressions;

import org.apache.spark.annotation.Experimental;

/**
* A sort direction used in sorting expressions.
*
* @since 3.2.0
*/
@Experimental
public enum SortDirection {
ASCENDING, DESCENDING;

@Override
public String toString() {
switch (this) {
case ASCENDING:
return "ASC";
case DESCENDING:
return "DESC";
default:
throw new IllegalArgumentException("Unexpected sort direction: " + this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.expressions;

import org.apache.spark.annotation.Experimental;

/**
* Represents a sort order in the public expression API.
*
* @since 3.2.0
*/
@Experimental
public interface SortOrder extends Expression {
/**
* Returns the sort expression.
*/
Expression expression();

/**
* Returns the sort direction.
*/
SortDirection direction();

/**
* Returns the null ordering.
*/
NullOrdering nullOrdering();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.connector.write;

import org.apache.spark.annotation.Experimental;
import org.apache.spark.sql.connector.distributions.Distribution;
import org.apache.spark.sql.connector.distributions.UnspecifiedDistribution;
import org.apache.spark.sql.connector.expressions.SortOrder;

/**
* A write that requires a specific distribution and ordering of data.
*
* @since 3.2.0
*/
@Experimental
public interface RequiresDistributionAndOrdering extends Write {
/**
* Returns the distribution required by this write.
* <p>
* Spark will distribute incoming records across partitions to satisfy the required distribution
* before passing the records to the data source table on write.
* <p>
* Implementations may return {@link UnspecifiedDistribution} if they don't require any specific
* distribution of data on write.
*
* @return the required distribution
*/
Distribution requiredDistribution();

/**
* Returns the ordering required by this write.
* <p>
* Spark will order incoming records within partitions to satisfy the required ordering
* before passing those records to the data source table on write.
* <p>
* Implementations may return an empty array if they don't require any specific ordering of data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon for the late comment.

The return value is an array.
It would be better to illustrate when more than one SortOrder would be returned (and how they are used).

* on write.
*
* @return the required ordering
*/
SortOrder[] requiredOrdering();
}
Loading