0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.sql.connector.catalog;
0019
0020 import java.util.Arrays;
0021 import java.util.Objects;
0022 import javax.annotation.Nullable;
0023
0024 import org.apache.spark.annotation.Evolving;
0025 import org.apache.spark.sql.types.DataType;
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 @Evolving
0043 public interface TableChange {
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 static TableChange setProperty(String property, String value) {
0055 return new SetProperty(property, value);
0056 }
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 static TableChange removeProperty(String property) {
0067 return new RemoveProperty(property);
0068 }
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 static TableChange addColumn(String[] fieldNames, DataType dataType) {
0082 return new AddColumn(fieldNames, dataType, true, null, null);
0083 }
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 static TableChange addColumn(String[] fieldNames, DataType dataType, boolean isNullable) {
0098 return new AddColumn(fieldNames, dataType, isNullable, null, null);
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 static TableChange addColumn(
0115 String[] fieldNames,
0116 DataType dataType,
0117 boolean isNullable,
0118 String comment) {
0119 return new AddColumn(fieldNames, dataType, isNullable, comment, null);
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 static TableChange addColumn(
0137 String[] fieldNames,
0138 DataType dataType,
0139 boolean isNullable,
0140 String comment,
0141 ColumnPosition position) {
0142 return new AddColumn(fieldNames, dataType, isNullable, comment, position);
0143 }
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157 static TableChange renameColumn(String[] fieldNames, String newName) {
0158 return new RenameColumn(fieldNames, newName);
0159 }
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 static TableChange updateColumnType(String[] fieldNames, DataType newDataType) {
0173 return new UpdateColumnType(fieldNames, newDataType);
0174 }
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 static TableChange updateColumnNullability(String[] fieldNames, boolean nullable) {
0188 return new UpdateColumnNullability(fieldNames, nullable);
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 static TableChange updateColumnComment(String[] fieldNames, String newComment) {
0203 return new UpdateColumnComment(fieldNames, newComment);
0204 }
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 static TableChange updateColumnPosition(String[] fieldNames, ColumnPosition newPosition) {
0218 return new UpdateColumnPosition(fieldNames, newPosition);
0219 }
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 static TableChange deleteColumn(String[] fieldNames) {
0230 return new DeleteColumn(fieldNames);
0231 }
0232
0233
0234
0235
0236
0237
0238 final class SetProperty implements TableChange {
0239 private final String property;
0240 private final String value;
0241
0242 private SetProperty(String property, String value) {
0243 this.property = property;
0244 this.value = value;
0245 }
0246
0247 public String property() {
0248 return property;
0249 }
0250
0251 public String value() {
0252 return value;
0253 }
0254
0255 @Override
0256 public boolean equals(Object o) {
0257 if (this == o) return true;
0258 if (o == null || getClass() != o.getClass()) return false;
0259 SetProperty that = (SetProperty) o;
0260 return property.equals(that.property) &&
0261 value.equals(that.value);
0262 }
0263
0264 @Override
0265 public int hashCode() {
0266 return Objects.hash(property, value);
0267 }
0268 }
0269
0270
0271
0272
0273
0274
0275 final class RemoveProperty implements TableChange {
0276 private final String property;
0277
0278 private RemoveProperty(String property) {
0279 this.property = property;
0280 }
0281
0282 public String property() {
0283 return property;
0284 }
0285
0286 @Override
0287 public boolean equals(Object o) {
0288 if (this == o) return true;
0289 if (o == null || getClass() != o.getClass()) return false;
0290 RemoveProperty that = (RemoveProperty) o;
0291 return property.equals(that.property);
0292 }
0293
0294 @Override
0295 public int hashCode() {
0296 return Objects.hash(property);
0297 }
0298 }
0299
0300 interface ColumnPosition {
0301
0302 static ColumnPosition first() {
0303 return First.INSTANCE;
0304 }
0305
0306 static ColumnPosition after(String column) {
0307 return new After(column);
0308 }
0309 }
0310
0311
0312
0313
0314
0315
0316 final class First implements ColumnPosition {
0317 private static final First INSTANCE = new First();
0318
0319 private First() {}
0320
0321 @Override
0322 public String toString() {
0323 return "FIRST";
0324 }
0325 }
0326
0327
0328
0329
0330
0331
0332 final class After implements ColumnPosition {
0333 private final String column;
0334
0335 private After(String column) {
0336 assert column != null;
0337 this.column = column;
0338 }
0339
0340 public String column() {
0341 return column;
0342 }
0343
0344 @Override
0345 public String toString() {
0346 return "AFTER " + column;
0347 }
0348
0349 @Override
0350 public boolean equals(Object o) {
0351 if (this == o) return true;
0352 if (o == null || getClass() != o.getClass()) return false;
0353 After after = (After) o;
0354 return column.equals(after.column);
0355 }
0356
0357 @Override
0358 public int hashCode() {
0359 return Objects.hash(column);
0360 }
0361 }
0362
0363 interface ColumnChange extends TableChange {
0364 String[] fieldNames();
0365 }
0366
0367
0368
0369
0370
0371
0372
0373
0374 final class AddColumn implements ColumnChange {
0375 private final String[] fieldNames;
0376 private final DataType dataType;
0377 private final boolean isNullable;
0378 private final String comment;
0379 private final ColumnPosition position;
0380
0381 private AddColumn(
0382 String[] fieldNames,
0383 DataType dataType,
0384 boolean isNullable,
0385 String comment,
0386 ColumnPosition position) {
0387 this.fieldNames = fieldNames;
0388 this.dataType = dataType;
0389 this.isNullable = isNullable;
0390 this.comment = comment;
0391 this.position = position;
0392 }
0393
0394 @Override
0395 public String[] fieldNames() {
0396 return fieldNames;
0397 }
0398
0399 public DataType dataType() {
0400 return dataType;
0401 }
0402
0403 public boolean isNullable() {
0404 return isNullable;
0405 }
0406
0407 @Nullable
0408 public String comment() {
0409 return comment;
0410 }
0411
0412 @Nullable
0413 public ColumnPosition position() {
0414 return position;
0415 }
0416
0417 @Override
0418 public boolean equals(Object o) {
0419 if (this == o) return true;
0420 if (o == null || getClass() != o.getClass()) return false;
0421 AddColumn addColumn = (AddColumn) o;
0422 return isNullable == addColumn.isNullable &&
0423 Arrays.equals(fieldNames, addColumn.fieldNames) &&
0424 dataType.equals(addColumn.dataType) &&
0425 Objects.equals(comment, addColumn.comment) &&
0426 Objects.equals(position, addColumn.position);
0427 }
0428
0429 @Override
0430 public int hashCode() {
0431 int result = Objects.hash(dataType, isNullable, comment, position);
0432 result = 31 * result + Arrays.hashCode(fieldNames);
0433 return result;
0434 }
0435 }
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445 final class RenameColumn implements ColumnChange {
0446 private final String[] fieldNames;
0447 private final String newName;
0448
0449 private RenameColumn(String[] fieldNames, String newName) {
0450 this.fieldNames = fieldNames;
0451 this.newName = newName;
0452 }
0453
0454 @Override
0455 public String[] fieldNames() {
0456 return fieldNames;
0457 }
0458
0459 public String newName() {
0460 return newName;
0461 }
0462
0463 @Override
0464 public boolean equals(Object o) {
0465 if (this == o) return true;
0466 if (o == null || getClass() != o.getClass()) return false;
0467 RenameColumn that = (RenameColumn) o;
0468 return Arrays.equals(fieldNames, that.fieldNames) &&
0469 newName.equals(that.newName);
0470 }
0471
0472 @Override
0473 public int hashCode() {
0474 int result = Objects.hash(newName);
0475 result = 31 * result + Arrays.hashCode(fieldNames);
0476 return result;
0477 }
0478 }
0479
0480
0481
0482
0483
0484
0485
0486
0487 final class UpdateColumnType implements ColumnChange {
0488 private final String[] fieldNames;
0489 private final DataType newDataType;
0490
0491 private UpdateColumnType(String[] fieldNames, DataType newDataType) {
0492 this.fieldNames = fieldNames;
0493 this.newDataType = newDataType;
0494 }
0495
0496 @Override
0497 public String[] fieldNames() {
0498 return fieldNames;
0499 }
0500
0501 public DataType newDataType() {
0502 return newDataType;
0503 }
0504
0505 @Override
0506 public boolean equals(Object o) {
0507 if (this == o) return true;
0508 if (o == null || getClass() != o.getClass()) return false;
0509 UpdateColumnType that = (UpdateColumnType) o;
0510 return Arrays.equals(fieldNames, that.fieldNames) &&
0511 newDataType.equals(that.newDataType);
0512 }
0513
0514 @Override
0515 public int hashCode() {
0516 int result = Objects.hash(newDataType);
0517 result = 31 * result + Arrays.hashCode(fieldNames);
0518 return result;
0519 }
0520 }
0521
0522
0523
0524
0525
0526
0527
0528
0529 final class UpdateColumnNullability implements ColumnChange {
0530 private final String[] fieldNames;
0531 private final boolean nullable;
0532
0533 private UpdateColumnNullability(String[] fieldNames, boolean nullable) {
0534 this.fieldNames = fieldNames;
0535 this.nullable = nullable;
0536 }
0537
0538 public String[] fieldNames() {
0539 return fieldNames;
0540 }
0541
0542 public boolean nullable() {
0543 return nullable;
0544 }
0545
0546 @Override
0547 public boolean equals(Object o) {
0548 if (this == o) return true;
0549 if (o == null || getClass() != o.getClass()) return false;
0550 UpdateColumnNullability that = (UpdateColumnNullability) o;
0551 return nullable == that.nullable &&
0552 Arrays.equals(fieldNames, that.fieldNames);
0553 }
0554
0555 @Override
0556 public int hashCode() {
0557 int result = Objects.hash(nullable);
0558 result = 31 * result + Arrays.hashCode(fieldNames);
0559 return result;
0560 }
0561 }
0562
0563
0564
0565
0566
0567
0568
0569
0570 final class UpdateColumnComment implements ColumnChange {
0571 private final String[] fieldNames;
0572 private final String newComment;
0573
0574 private UpdateColumnComment(String[] fieldNames, String newComment) {
0575 this.fieldNames = fieldNames;
0576 this.newComment = newComment;
0577 }
0578
0579 @Override
0580 public String[] fieldNames() {
0581 return fieldNames;
0582 }
0583
0584 public String newComment() {
0585 return newComment;
0586 }
0587
0588 @Override
0589 public boolean equals(Object o) {
0590 if (this == o) return true;
0591 if (o == null || getClass() != o.getClass()) return false;
0592 UpdateColumnComment that = (UpdateColumnComment) o;
0593 return Arrays.equals(fieldNames, that.fieldNames) &&
0594 newComment.equals(that.newComment);
0595 }
0596
0597 @Override
0598 public int hashCode() {
0599 int result = Objects.hash(newComment);
0600 result = 31 * result + Arrays.hashCode(fieldNames);
0601 return result;
0602 }
0603 }
0604
0605
0606
0607
0608
0609
0610
0611
0612 final class UpdateColumnPosition implements ColumnChange {
0613 private final String[] fieldNames;
0614 private final ColumnPosition position;
0615
0616 private UpdateColumnPosition(String[] fieldNames, ColumnPosition position) {
0617 this.fieldNames = fieldNames;
0618 this.position = position;
0619 }
0620
0621 @Override
0622 public String[] fieldNames() {
0623 return fieldNames;
0624 }
0625
0626 public ColumnPosition position() {
0627 return position;
0628 }
0629
0630 @Override
0631 public boolean equals(Object o) {
0632 if (this == o) return true;
0633 if (o == null || getClass() != o.getClass()) return false;
0634 UpdateColumnPosition that = (UpdateColumnPosition) o;
0635 return Arrays.equals(fieldNames, that.fieldNames) &&
0636 position.equals(that.position);
0637 }
0638
0639 @Override
0640 public int hashCode() {
0641 int result = Objects.hash(position);
0642 result = 31 * result + Arrays.hashCode(fieldNames);
0643 return result;
0644 }
0645 }
0646
0647
0648
0649
0650
0651
0652 final class DeleteColumn implements ColumnChange {
0653 private final String[] fieldNames;
0654
0655 private DeleteColumn(String[] fieldNames) {
0656 this.fieldNames = fieldNames;
0657 }
0658
0659 @Override
0660 public String[] fieldNames() {
0661 return fieldNames;
0662 }
0663
0664 @Override
0665 public boolean equals(Object o) {
0666 if (this == o) return true;
0667 if (o == null || getClass() != o.getClass()) return false;
0668 DeleteColumn that = (DeleteColumn) o;
0669 return Arrays.equals(fieldNames, that.fieldNames);
0670 }
0671
0672 @Override
0673 public int hashCode() {
0674 return Arrays.hashCode(fieldNames);
0675 }
0676 }
0677
0678 }