Skip to content

Commit 43398af

Browse files
committed
address comments
1 parent 5da54d2 commit 43398af

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ intervalUnit
855855
;
856856

857857
colPosition
858-
: position=FIRST | position=AFTER multipartIdentifier
858+
: position=FIRST | position=AFTER afterCol=errorCapturingIdentifier
859859
;
860860

861861
dataType

sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/TableChange.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,15 @@ public int hashCode() {
301301
interface ColumnPosition {
302302
First FIRST = new First();
303303

304-
static ColumnPosition After(String[] column) {
304+
static ColumnPosition createAfter(String column) {
305305
return new After(column);
306306
}
307307
}
308308

309309
/**
310310
* Column position FIRST means the specified column should be the first column.
311+
* Note that, the specified column may be a nested field, and then FIRST means this field should
312+
* be the first one within the struct.
311313
*/
312314
final class First implements ColumnPosition {
313315
private First() {}
@@ -320,35 +322,37 @@ public String toString() {
320322

321323
/**
322324
* Column position AFTER means the specified column should be put after the given `column`.
325+
* Note that, the specified column may be a nested field, and then the given `column` refers to
326+
* a nested field in the same struct.
323327
*/
324328
final class After implements ColumnPosition {
325-
private final String[] column;
329+
private final String column;
326330

327-
private After(String[] column) {
331+
private After(String column) {
328332
assert column != null;
329333
this.column = column;
330334
}
331335

332-
public String[] getColumn() {
336+
public String getColumn() {
333337
return column;
334338
}
335339

336340
@Override
337341
public String toString() {
338-
return "AFTER " + CatalogV2Implicits.quoteNameParts(column);
342+
return "AFTER " + column;
339343
}
340344

341345
@Override
342346
public boolean equals(Object o) {
343347
if (this == o) return true;
344348
if (o == null || getClass() != o.getClass()) return false;
345349
After after = (After) o;
346-
return Arrays.equals(column, after.column);
350+
return column.equals(after.column);
347351
}
348352

349353
@Override
350354
public int hashCode() {
351-
return Arrays.hashCode(column);
355+
return Objects.hash(column);
352356
}
353357
}
354358

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
4545
createAlterTable(nameParts, catalog, tbl, changes)
4646

4747
case AlterTableAlterColumnStatement(
48-
nameParts @ NonSessionCatalogAndTable(catalog, tbl), colName, dataType, comment, pos) =>
48+
nameParts @ NonSessionCatalogAndTable(catalog, tbl), colName, dataType, comment, pos) =>
4949
val colNameArray = colName.toArray
5050
val typeChange = dataType.map { newDataType =>
5151
TableChange.updateColumnType(colNameArray, newDataType, true)

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,8 +2807,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
28072807
override def visitColPosition(ctx: ColPositionContext): ColumnPosition = {
28082808
ctx.position.getType match {
28092809
case SqlBaseParser.FIRST => ColumnPosition.FIRST
2810-
case SqlBaseParser.AFTER =>
2811-
ColumnPosition.After(typedVisit[Seq[String]](ctx.multipartIdentifier).toArray)
2810+
case SqlBaseParser.AFTER => ColumnPosition.createAfter(ctx.afterCol.getText)
28122811
}
28132812
}
28142813

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DDLParserSuite.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, GlobalTempView, Loc
2424
import org.apache.spark.sql.catalyst.catalog.BucketSpec
2525
import org.apache.spark.sql.catalyst.expressions.{EqualTo, Literal}
2626
import org.apache.spark.sql.catalyst.plans.logical._
27-
import org.apache.spark.sql.connector.catalog.TableChange.ColumnPosition.After
28-
import org.apache.spark.sql.connector.catalog.TableChange.ColumnPosition.FIRST
27+
import org.apache.spark.sql.connector.catalog.TableChange.ColumnPosition.{createAfter, FIRST}
2928
import org.apache.spark.sql.connector.expressions.{ApplyTransform, BucketTransform, DaysTransform, FieldReference, HoursTransform, IdentityTransform, LiteralValue, MonthsTransform, Transform, YearsTransform}
3029
import org.apache.spark.sql.internal.SQLConf
3130
import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType, TimestampType}
@@ -549,7 +548,7 @@ class DDLParserSuite extends AnalysisTest {
549548
comparePlans(
550549
parsePlan("ALTER TABLE table_name ADD COLUMN x int AFTER y"),
551550
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
552-
QualifiedColType(Seq("x"), IntegerType, None, Some(After(Array("y"))))
551+
QualifiedColType(Seq("x"), IntegerType, None, Some(createAfter("y")))
553552
)))
554553
}
555554

@@ -639,13 +638,13 @@ class DDLParserSuite extends AnalysisTest {
639638
test("alter table: update column type, comment and position") {
640639
comparePlans(
641640
parsePlan("ALTER TABLE table_name CHANGE COLUMN a.b.c " +
642-
"TYPE bigint COMMENT 'new comment' AFTER x.y"),
641+
"TYPE bigint COMMENT 'new comment' AFTER d"),
643642
AlterTableAlterColumnStatement(
644643
Seq("table_name"),
645644
Seq("a", "b", "c"),
646645
Some(LongType),
647646
Some("new comment"),
648-
Some(After(Array("x", "y")))))
647+
Some(createAfter("d"))))
649648
}
650649

651650
test("alter table: drop column") {

0 commit comments

Comments
 (0)