void VisitAlterTableNode()

in backend/schema/parser/ddl_parser.cc [2406:2513]


void VisitAlterTableNode(const SimpleNode* node, absl::string_view ddl_text,
                         DDLStatement* statement,
                         std::vector<std::string>* errors) {
  CheckNode(node, JJTALTER_TABLE_STATEMENT);
  const SimpleNode* child = GetChildNode(node, 1);
  std::string table_name =
      GetQualifiedIdentifier(GetFirstChildNode(node, JJTTABLE_NAME));
  if (child->getId() == JJTALTER_COLUMN) {
    // Depending on the ALTER COLUMN variant, we may generate either
    // an AlterTable or SetColumnOptions statement.
    std::string column_name = GetChildNode(node, 2, JJTNAME)->image();
    VisitColumnNodeAlter(table_name, column_name,
                         GetChildNode(node, 3, JJTCOLUMN_DEF_ALTER), ddl_text,
                         statement, errors);
  } else {
    // These will generate an AlterTable statement.
    AlterTable* alter_table = statement->mutable_alter_table();
    alter_table->set_table_name(table_name);
    switch (child->getId()) {
      case JJTRENAME_TO: {
        alter_table->mutable_rename_to()->set_name(
            GetQualifiedIdentifier(child));
        if (node->jjtGetNumChildren() > 2) {
          SimpleNode* synonym = GetChildNode(node, 2, JJTSYNONYM);
          alter_table->mutable_rename_to()->set_synonym(
              GetQualifiedIdentifier(synonym));
        }
        break;
      }
      case JJTADD_COLUMN: {
        int offset = 2;
        if (GetChildNode(node, offset)->getId() == JJTIF_NOT_EXISTS) {
          offset++;
          alter_table->mutable_add_column()->set_existence_modifier(
              IF_NOT_EXISTS);
        }
        VisitColumnNode(GetChildNode(node, offset, JJTCOLUMN_DEF), ddl_text,
                        alter_table->mutable_add_column()->mutable_column(),
                        /*containing_table=*/nullptr, errors);
      } break;
      case JJTDROP_COLUMN: {
        SimpleNode* column = GetChildNode(node, 2, JJTCOLUMN_NAME);
        alter_table->set_drop_column(column->image());
        break;
      }
      case JJTSET_ON_DELETE: {
        SimpleNode* on_delete_node = GetChildNode(node, 2, JJTON_DELETE_CLAUSE);
        InterleaveClause::Action on_delete_action;
        VisitOnDeleteClause(on_delete_node, &on_delete_action);
        alter_table->mutable_set_on_delete()->set_action(on_delete_action);
        break;
      }
      case JJTFOREIGN_KEY:
        VisitForeignKeyNode(
            child,
            alter_table->mutable_add_foreign_key()->mutable_foreign_key(),
            errors);
        break;
      case JJTCHECK_CONSTRAINT:
        VisitCheckConstraintNode(child, ddl_text,
                                 alter_table->mutable_add_check_constraint()
                                     ->mutable_check_constraint(),
                                 errors);
        break;
      case JJTDROP_CONSTRAINT: {
        SimpleNode* constraint_name = GetChildNode(node, 2, JJTCONSTRAINT_NAME);
        alter_table->mutable_drop_constraint()->set_name(
            constraint_name->image());
        break;
      }
      case JJTADD_ROW_DELETION_POLICY: {
        VisitTableRowDeletionPolicyNode(
            GetChildNode(child, 0, JJTROW_DELETION_POLICY_CLAUSE),
            alter_table->mutable_add_row_deletion_policy(), errors);
        break;
      }
      case JJTREPLACE_ROW_DELETION_POLICY: {
        VisitTableRowDeletionPolicyNode(
            GetChildNode(child, 0, JJTROW_DELETION_POLICY_CLAUSE),
            alter_table->mutable_alter_row_deletion_policy(), errors);
        break;
      }
      case JJTDROP_ROW_DELETION_POLICY: {
        alter_table->mutable_drop_row_deletion_policy();
        break;
      }
      case JJTADD_SYNONYM: {
        alter_table->mutable_add_synonym()->set_synonym(
            GetQualifiedIdentifier(child));
        break;
      }
      case JJTDROP_SYNONYM: {
        alter_table->mutable_drop_synonym()->set_synonym(
            GetQualifiedIdentifier(child));
        break;
      }
      case JJTOPTIONS_CLAUSE: {
        VisitTableOptionListNode(
            child, 0 /* option_list_offset */,
            alter_table->mutable_set_options()->mutable_options(), errors);
        break;
      }
      default:
        ABSL_LOG(FATAL) << "Unexpected alter table type: "
                   << GetChildNode(node, 1)->toString();
    }
  }
}