Yibo Dong created SPARK-62285:
---------------------------------

             Summary: ALTER TABLE CLUSTER BY accepts partitioned tables and 
produces invalid table definitions
                 Key: SPARK-62285
                 URL: https://issues.apache.org/jira/browse/SPARK-62285
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
         Environment: Stock Apache Spark 5.0.0-SNAPSHOT at commit 
082cf160890a0a0a748f70ffb5a993144b0de079, Java 25.0.1, macOS 15.7.4 aarch64, 
local[2], in-memory session catalog, built-in Parquet data source (V1), default 
SQL configuration.
            Reporter: Yibo Dong


h2. What happened

Spark does not allow CLUSTER BY to be combined with PARTITIONED BY or CLUSTERED 
BY ... INTO ... BUCKETS when creating a table.

However, ALTER TABLE ... CLUSTER BY succeeds on an existing partitioned or 
bucketed Parquet table, leaving both specifications in the table metadata.

SHOW CREATE TABLE then generates a table definition containing the conflicting 
specifications. Executing that definition fails with INTERNAL_ERROR instead of 
a user-facing analysis error.

h2. How to reproduce

Run the following SQL in a fresh Spark SQL session:

{code:sql}
DROP TABLE IF EXISTS cluster_part;
DROP TABLE IF EXISTS cluster_part_copy;

CREATE TABLE cluster_part (a INT, p INT)
USING parquet PARTITIONED BY (p);

INSERT INTO cluster_part VALUES (1, 1), (2, 2);

ALTER TABLE cluster_part CLUSTER BY (a);

DESCRIBE TABLE EXTENDED cluster_part;
SHOW CREATE TABLE cluster_part;

-- Recreate the conflicting partitioning definition.
CREATE TABLE cluster_part_copy (a INT, p INT)
USING parquet
PARTITIONED BY (p, cluster_by(a));
{code}

Save as tmp.sql and execute:

{code:bash}
spark-sql --master 'local[2]' --conf spark.sql.catalogImplementation=in-memory 
-f tmp.sql
{code}

h2. Expected result

ALTER TABLE cluster_part CLUSTER BY (a) should reject the conflicting 
partitioning and clustering specifications, consistent with CREATE TABLE:

{noformat}
[SPECIFY_CLUSTER_BY_WITH_PARTITIONED_BY_IS_NOT_ALLOWED]
Cannot specify both CLUSTER BY and PARTITIONED BY.
SQLSTATE: 42908
{noformat}

The final CREATE TABLE should also reject conflicting specifications with a 
user-facing error rather than an internal assertion.

h2. Actual result

ALTER TABLE cluster_part CLUSTER BY (a) succeeds.

DESCRIBE TABLE EXTENDED reports both the partition column p and clustering 
column a.

SHOW CREATE TABLE generates:

{code:sql}
CREATE TABLE spark_catalog.default.cluster_part (
a INT,
p INT)
USING parquet
PARTITIONED BY (p, cluster_by(a))
TBLPROPERTIES (
'clusteringColumns' = '[["a"]]')
{code}

The final CREATE TABLE fails with:

{noformat}
org.apache.spark.SparkException:
[INTERNAL_ERROR] The Spark SQL phase analysis failed
with an internal error. SQLSTATE: XX000

Caused by: java.lang.AssertionError: assertion failed
at scala.Predef$.assert(Predef.scala:264)
at 
org.apache.spark.sql.connector.catalog.CatalogV2Implicits$TransformHelper.convertTransforms(CatalogV2Implicits.scala:95)
at 
org.apache.spark.sql.catalyst.analysis.CreateTableHelper$.buildCatalogTable(CreateTableHelper.scala:258)
{noformat}



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to