Skip to content
Open
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
16 changes: 12 additions & 4 deletions sql/api/src/main/scala/org/apache/spark/sql/Column.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql

import scala.jdk.CollectionConverters._

import org.apache.spark.annotation.Stable
import org.apache.spark.annotation.{DeveloperApi, Stable}
import org.apache.spark.internal.Logging
import org.apache.spark.internal.LogKeys.{LEFT_EXPR, RIGHT_EXPR}
import org.apache.spark.sql.catalyst.parser.DataTypeParser
Expand All @@ -30,11 +30,19 @@ import org.apache.spark.sql.internal.{ColumnNode, TableValuedFunctionArgument}
import org.apache.spark.sql.types._
import org.apache.spark.util.ArrayImplicits._

private[spark] object Column {
/**
* The companion object is public so that the `Column` type can be referenced as a value. This
* allows an implementation to add a `Column(expression)` factory through an extension method. All
* of its members are internal to Spark.
*
* @since 4.4.0
*/
@DeveloperApi
object Column {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we change the visibility, we need annotations like we did for class Column.

@Stable
class Column(val node: ColumnNode) extends Logging with TableValuedFunctionArgument {

In this case, which one do you propose in this PR? Stable or DeveloperApi, @peter-toth ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks. I went with @DeveloperApi (plus @since 4.4.0) in f4b2d23.

Reasoning: everything that makes Column(expression) work is already @DeveloperApiClassicConversions and ColumnConstructorExt — so the path is only as stable as those, and @Stable on the companion would over-promise. The object also has no public members; it is public purely so the type can be named as a value.

The counter-argument, in case you'd rather have @Stable: @DeveloperApi does let us make the object private again in a minor release, which would re-break the use case this PR fixes. Happy to switch if you prefer that guarantee.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1


def apply(colName: String): Column = new Column(colName)
private[spark] def apply(colName: String): Column = new Column(colName)

def apply(node: => ColumnNode): Column = withOrigin(new Column(node))
private[spark] def apply(node: => ColumnNode): Column = withOrigin(new Column(node))

/**
* Invoke a function with an options map as its last argument. If there are no options, its
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@ trait ClassicConversions {
}

@DeveloperApi
object ClassicConversions extends ClassicConversions
object ClassicConversions extends ClassicConversions {
/**
* Convert an [[Expression]] into a [[Column]]. This is the counterpart of
* [[ColumnConversions.expression]], for callers that would rather name the conversion than rely
* on the implicit [[ClassicConversions.ColumnConstructorExt]].
*
* This is intentionally defined on the object rather than on the [[ClassicConversions]] trait:
* on the trait it would shadow `functions.column(colName: String)` for everyone who mixes the
* trait in, which is the trait's documented use case.
*
* @since 4.4.0
*/
@DeveloperApi
def column(e: Expression): Column = ExpressionUtils.column(e)
}

/**
* Conversions from a [[Column]] to an [[Expression]].
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 test.org.apache.spark.sql

import org.apache.spark.sql.{Column, QueryTest, Row}
import org.apache.spark.sql.catalyst.expressions.{Expression, Literal}
import org.apache.spark.sql.classic.{ClassicConversions, ColumnConversions}
import org.apache.spark.sql.classic.ClassicConversions._
import org.apache.spark.sql.test.SharedSparkSession

/**
* Tests the public Expression <-> Column conversions from a package outside of
* `org.apache.spark`, which is the only way to catch that a step of the path is package-private.
* Compiling this suite is as much a part of the test as running it.
*/
class ExpressionToColumnSuite extends QueryTest with SharedSparkSession {

test("SPARK-49828: build a Column from an Expression via the Column companion") {
val e: Expression = Literal(1)
val c: Column = Column(e)
assert(ColumnConversions.expression(c) == e)
}

test("SPARK-49828: build a Column from an Expression via ClassicConversions.column") {
val e: Expression = Literal(1)
val c: Column = ClassicConversions.column(e)
assert(ColumnConversions.expression(c) == e)
}

test("SPARK-49828: a Column built from an Expression is usable in a query") {
val df = spark.range(2).select(Column(Literal(1)).as("one"))
checkAnswer(df, Seq(Row(1), Row(1)))
}
}