github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/alter_table.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree
    12  
    13  import (
    14  	"strings"
    15  
    16  	"github.com/cockroachdb/cockroachdb-parser/pkg/sql/lex"
    17  )
    18  
    19  // AlterTable represents an ALTER TABLE statement.
    20  type AlterTable struct {
    21  	IfExists bool
    22  	Table    *UnresolvedObjectName
    23  	Cmds     AlterTableCmds
    24  }
    25  
    26  // Format implements the NodeFormatter interface.
    27  func (node *AlterTable) Format(ctx *FmtCtx) {
    28  	ctx.WriteString("ALTER TABLE ")
    29  	if node.IfExists {
    30  		ctx.WriteString("IF EXISTS ")
    31  	}
    32  	ctx.FormatNode(node.Table)
    33  	ctx.FormatNode(&node.Cmds)
    34  }
    35  
    36  // AlterTableCmds represents a list of table alterations.
    37  type AlterTableCmds []AlterTableCmd
    38  
    39  // Format implements the NodeFormatter interface.
    40  func (node *AlterTableCmds) Format(ctx *FmtCtx) {
    41  	for i, n := range *node {
    42  		if i > 0 {
    43  			ctx.WriteString(",")
    44  		}
    45  		ctx.FormatNode(n)
    46  	}
    47  }
    48  
    49  // AlterTableCmd represents a table modification operation.
    50  type AlterTableCmd interface {
    51  	NodeFormatter
    52  	// TelemetryName returns the counter name to use for telemetry purposes.
    53  	TelemetryName() string
    54  	// Placeholder function to ensure that only desired types
    55  	// (AlterTable*) conform to the AlterTableCmd interface.
    56  	alterTableCmd()
    57  }
    58  
    59  func (*AlterTableAddColumn) alterTableCmd()          {}
    60  func (*AlterTableAddConstraint) alterTableCmd()      {}
    61  func (*AlterTableAlterColumnType) alterTableCmd()    {}
    62  func (*AlterTableAlterPrimaryKey) alterTableCmd()    {}
    63  func (*AlterTableDropColumn) alterTableCmd()         {}
    64  func (*AlterTableDropConstraint) alterTableCmd()     {}
    65  func (*AlterTableDropNotNull) alterTableCmd()        {}
    66  func (*AlterTableDropStored) alterTableCmd()         {}
    67  func (*AlterTableSetNotNull) alterTableCmd()         {}
    68  func (*AlterTableRenameColumn) alterTableCmd()       {}
    69  func (*AlterTableRenameConstraint) alterTableCmd()   {}
    70  func (*AlterTableSetAudit) alterTableCmd()           {}
    71  func (*AlterTableSetDefault) alterTableCmd()         {}
    72  func (*AlterTableSetOnUpdate) alterTableCmd()        {}
    73  func (*AlterTableSetVisible) alterTableCmd()         {}
    74  func (*AlterTableValidateConstraint) alterTableCmd() {}
    75  func (*AlterTablePartitionByTable) alterTableCmd()   {}
    76  func (*AlterTableInjectStats) alterTableCmd()        {}
    77  func (*AlterTableSetStorageParams) alterTableCmd()   {}
    78  func (*AlterTableResetStorageParams) alterTableCmd() {}
    79  
    80  var _ AlterTableCmd = &AlterTableAddColumn{}
    81  var _ AlterTableCmd = &AlterTableAddConstraint{}
    82  var _ AlterTableCmd = &AlterTableAlterColumnType{}
    83  var _ AlterTableCmd = &AlterTableDropColumn{}
    84  var _ AlterTableCmd = &AlterTableDropConstraint{}
    85  var _ AlterTableCmd = &AlterTableDropNotNull{}
    86  var _ AlterTableCmd = &AlterTableDropStored{}
    87  var _ AlterTableCmd = &AlterTableSetNotNull{}
    88  var _ AlterTableCmd = &AlterTableRenameColumn{}
    89  var _ AlterTableCmd = &AlterTableRenameConstraint{}
    90  var _ AlterTableCmd = &AlterTableSetAudit{}
    91  var _ AlterTableCmd = &AlterTableSetDefault{}
    92  var _ AlterTableCmd = &AlterTableSetOnUpdate{}
    93  var _ AlterTableCmd = &AlterTableSetVisible{}
    94  var _ AlterTableCmd = &AlterTableValidateConstraint{}
    95  var _ AlterTableCmd = &AlterTablePartitionByTable{}
    96  var _ AlterTableCmd = &AlterTableInjectStats{}
    97  var _ AlterTableCmd = &AlterTableSetStorageParams{}
    98  var _ AlterTableCmd = &AlterTableResetStorageParams{}
    99  
   100  // ColumnMutationCmd is the subset of AlterTableCmds that modify an
   101  // existing column.
   102  type ColumnMutationCmd interface {
   103  	AlterTableCmd
   104  	GetColumn() Name
   105  }
   106  
   107  // AlterTableAddColumn represents an ADD COLUMN command.
   108  type AlterTableAddColumn struct {
   109  	IfNotExists bool
   110  	ColumnDef   *ColumnTableDef
   111  }
   112  
   113  // TelemetryName implements the AlterTableCmd interface.
   114  func (node *AlterTableAddColumn) TelemetryName() string {
   115  	return "add_column"
   116  }
   117  
   118  // Format implements the NodeFormatter interface.
   119  func (node *AlterTableAddColumn) Format(ctx *FmtCtx) {
   120  	ctx.WriteString(" ADD COLUMN ")
   121  	if node.IfNotExists {
   122  		ctx.WriteString("IF NOT EXISTS ")
   123  	}
   124  	ctx.FormatNode(node.ColumnDef)
   125  }
   126  
   127  // HoistAddColumnConstraints converts column constraints in ADD COLUMN commands,
   128  // stored in node.Cmds, into top-level commands to add those constraints.
   129  // Currently, this only applies to checks. For example, the ADD COLUMN in
   130  //
   131  //	ALTER TABLE t ADD COLUMN a INT CHECK (a < 1)
   132  //
   133  // is transformed into two commands, as in
   134  //
   135  //	ALTER TABLE t ADD COLUMN a INT, ADD CONSTRAINT check_a CHECK (a < 1)
   136  //
   137  // (with an auto-generated name).
   138  //
   139  // Note that some SQL databases require that a constraint attached to a column
   140  // to refer only to the column it is attached to. We follow Postgres' behavior,
   141  // however, in omitting this restriction by blindly hoisting all column
   142  // constraints. For example, the following statement is accepted in
   143  // CockroachDB and Postgres, but not necessarily other SQL databases:
   144  //
   145  //	ALTER TABLE t ADD COLUMN a INT CHECK (a < b)
   146  func (node *AlterTable) HoistAddColumnConstraints(onHoistedFKConstraint func()) {
   147  	var normalizedCmds AlterTableCmds
   148  
   149  	for _, cmd := range node.Cmds {
   150  		normalizedCmds = append(normalizedCmds, cmd)
   151  
   152  		if t, ok := cmd.(*AlterTableAddColumn); ok {
   153  			d := t.ColumnDef
   154  			for _, checkExpr := range d.CheckExprs {
   155  				normalizedCmds = append(normalizedCmds,
   156  					&AlterTableAddConstraint{
   157  						ConstraintDef: &CheckConstraintTableDef{
   158  							Expr: checkExpr.Expr,
   159  							Name: checkExpr.ConstraintName,
   160  						},
   161  						ValidationBehavior: ValidationDefault,
   162  					},
   163  				)
   164  			}
   165  			d.CheckExprs = nil
   166  			if d.HasFKConstraint() {
   167  				var targetCol NameList
   168  				if d.References.Col != "" {
   169  					targetCol = append(targetCol, d.References.Col)
   170  				}
   171  				fk := &ForeignKeyConstraintTableDef{
   172  					Table:    *d.References.Table,
   173  					FromCols: NameList{d.Name},
   174  					ToCols:   targetCol,
   175  					Name:     d.References.ConstraintName,
   176  					Actions:  d.References.Actions,
   177  					Match:    d.References.Match,
   178  				}
   179  				constraint := &AlterTableAddConstraint{
   180  					ConstraintDef:      fk,
   181  					ValidationBehavior: ValidationDefault,
   182  				}
   183  				normalizedCmds = append(normalizedCmds, constraint)
   184  				d.References.Table = nil
   185  				onHoistedFKConstraint()
   186  			}
   187  		}
   188  	}
   189  	node.Cmds = normalizedCmds
   190  }
   191  
   192  // ValidationBehavior specifies whether or not a constraint is validated.
   193  type ValidationBehavior int
   194  
   195  const (
   196  	// ValidationDefault is the default validation behavior (immediate).
   197  	ValidationDefault ValidationBehavior = iota
   198  	// ValidationSkip skips validation of any existing data.
   199  	ValidationSkip
   200  )
   201  
   202  // AlterTableAddConstraint represents an ADD CONSTRAINT command.
   203  type AlterTableAddConstraint struct {
   204  	ConstraintDef      ConstraintTableDef
   205  	ValidationBehavior ValidationBehavior
   206  }
   207  
   208  // TelemetryName implements the AlterTableCmd interface.
   209  func (node *AlterTableAddConstraint) TelemetryName() string {
   210  	return "add_constraint"
   211  }
   212  
   213  // Format implements the NodeFormatter interface.
   214  func (node *AlterTableAddConstraint) Format(ctx *FmtCtx) {
   215  	ctx.WriteString(" ADD ")
   216  	ctx.FormatNode(node.ConstraintDef)
   217  	if node.ValidationBehavior == ValidationSkip {
   218  		ctx.WriteString(" NOT VALID")
   219  	}
   220  }
   221  
   222  // AlterTableAlterColumnType represents an ALTER TABLE ALTER COLUMN TYPE command.
   223  type AlterTableAlterColumnType struct {
   224  	Collation string
   225  	Column    Name
   226  	ToType    ResolvableTypeReference
   227  	Using     Expr
   228  }
   229  
   230  // TelemetryName implements the AlterTableCmd interface.
   231  func (node *AlterTableAlterColumnType) TelemetryName() string {
   232  	return "alter_column_type"
   233  }
   234  
   235  // Format implements the NodeFormatter interface.
   236  func (node *AlterTableAlterColumnType) Format(ctx *FmtCtx) {
   237  	ctx.WriteString(" ALTER COLUMN ")
   238  	ctx.FormatNode(&node.Column)
   239  	ctx.WriteString(" SET DATA TYPE ")
   240  	ctx.FormatTypeReference(node.ToType)
   241  	if len(node.Collation) > 0 {
   242  		ctx.WriteString(" COLLATE ")
   243  		lex.EncodeLocaleName(&ctx.Buffer, node.Collation)
   244  	}
   245  	if node.Using != nil {
   246  		ctx.WriteString(" USING ")
   247  		ctx.FormatNode(node.Using)
   248  	}
   249  }
   250  
   251  // GetColumn implements the ColumnMutationCmd interface.
   252  func (node *AlterTableAlterColumnType) GetColumn() Name {
   253  	return node.Column
   254  }
   255  
   256  // AlterTableAlterPrimaryKey represents an ALTER TABLE ALTER PRIMARY KEY command.
   257  type AlterTableAlterPrimaryKey struct {
   258  	Columns       IndexElemList
   259  	Sharded       *ShardedIndexDef
   260  	Name          Name
   261  	StorageParams StorageParams
   262  }
   263  
   264  // TelemetryName implements the AlterTableCmd interface.
   265  func (node *AlterTableAlterPrimaryKey) TelemetryName() string {
   266  	return "alter_primary_key"
   267  }
   268  
   269  // Format implements the NodeFormatter interface.
   270  func (node *AlterTableAlterPrimaryKey) Format(ctx *FmtCtx) {
   271  	ctx.WriteString(" ALTER PRIMARY KEY USING COLUMNS (")
   272  	ctx.FormatNode(&node.Columns)
   273  	ctx.WriteString(")")
   274  	if node.Sharded != nil {
   275  		ctx.FormatNode(node.Sharded)
   276  	}
   277  	if node.StorageParams != nil {
   278  		ctx.WriteString(" WITH (")
   279  		ctx.FormatNode(&node.StorageParams)
   280  		ctx.WriteString(")")
   281  	}
   282  }
   283  
   284  // AlterTableDropColumn represents a DROP COLUMN command.
   285  type AlterTableDropColumn struct {
   286  	IfExists     bool
   287  	Column       Name
   288  	DropBehavior DropBehavior
   289  }
   290  
   291  // TelemetryName implements the AlterTableCmd interface.
   292  func (node *AlterTableDropColumn) TelemetryName() string {
   293  	return "drop_column"
   294  }
   295  
   296  // Format implements the NodeFormatter interface.
   297  func (node *AlterTableDropColumn) Format(ctx *FmtCtx) {
   298  	ctx.WriteString(" DROP COLUMN ")
   299  	if node.IfExists {
   300  		ctx.WriteString("IF EXISTS ")
   301  	}
   302  	ctx.FormatNode(&node.Column)
   303  	if node.DropBehavior != DropDefault {
   304  		ctx.Printf(" %s", node.DropBehavior)
   305  	}
   306  }
   307  
   308  // AlterTableDropConstraint represents a DROP CONSTRAINT command.
   309  type AlterTableDropConstraint struct {
   310  	IfExists     bool
   311  	Constraint   Name
   312  	DropBehavior DropBehavior
   313  }
   314  
   315  // TelemetryName implements the AlterTableCmd interface.
   316  func (node *AlterTableDropConstraint) TelemetryName() string {
   317  	return "drop_constraint"
   318  }
   319  
   320  // Format implements the NodeFormatter interface.
   321  func (node *AlterTableDropConstraint) Format(ctx *FmtCtx) {
   322  	ctx.WriteString(" DROP CONSTRAINT ")
   323  	if node.IfExists {
   324  		ctx.WriteString("IF EXISTS ")
   325  	}
   326  	ctx.FormatNode(&node.Constraint)
   327  	if node.DropBehavior != DropDefault {
   328  		ctx.Printf(" %s", node.DropBehavior)
   329  	}
   330  }
   331  
   332  // AlterTableValidateConstraint represents a VALIDATE CONSTRAINT command.
   333  type AlterTableValidateConstraint struct {
   334  	Constraint Name
   335  }
   336  
   337  // TelemetryName implements the AlterTableCmd interface.
   338  func (node *AlterTableValidateConstraint) TelemetryName() string {
   339  	return "validate_constraint"
   340  }
   341  
   342  // Format implements the NodeFormatter interface.
   343  func (node *AlterTableValidateConstraint) Format(ctx *FmtCtx) {
   344  	ctx.WriteString(" VALIDATE CONSTRAINT ")
   345  	ctx.FormatNode(&node.Constraint)
   346  }
   347  
   348  // AlterTableRenameColumn represents an ALTER TABLE RENAME [COLUMN] command.
   349  type AlterTableRenameColumn struct {
   350  	Column  Name
   351  	NewName Name
   352  }
   353  
   354  // TelemetryName implements the AlterTableCmd interface.
   355  func (node *AlterTableRenameColumn) TelemetryName() string {
   356  	return "rename_column"
   357  }
   358  
   359  // Format implements the NodeFormatter interface.
   360  func (node *AlterTableRenameColumn) Format(ctx *FmtCtx) {
   361  	ctx.WriteString(" RENAME COLUMN ")
   362  	ctx.FormatNode(&node.Column)
   363  	ctx.WriteString(" TO ")
   364  	ctx.FormatNode(&node.NewName)
   365  }
   366  
   367  // AlterTableRenameConstraint represents an ALTER TABLE RENAME CONSTRAINT command.
   368  type AlterTableRenameConstraint struct {
   369  	Constraint Name
   370  	NewName    Name
   371  }
   372  
   373  // TelemetryName implements the AlterTableCmd interface.
   374  func (node *AlterTableRenameConstraint) TelemetryName() string {
   375  	return "rename_constraint"
   376  }
   377  
   378  // Format implements the NodeFormatter interface.
   379  func (node *AlterTableRenameConstraint) Format(ctx *FmtCtx) {
   380  	ctx.WriteString(" RENAME CONSTRAINT ")
   381  	ctx.FormatNode(&node.Constraint)
   382  	ctx.WriteString(" TO ")
   383  	ctx.FormatNode(&node.NewName)
   384  }
   385  
   386  // AlterTableSetDefault represents an ALTER COLUMN SET DEFAULT
   387  // or DROP DEFAULT command.
   388  type AlterTableSetDefault struct {
   389  	Column  Name
   390  	Default Expr
   391  }
   392  
   393  // GetColumn implements the ColumnMutationCmd interface.
   394  func (node *AlterTableSetDefault) GetColumn() Name {
   395  	return node.Column
   396  }
   397  
   398  // TelemetryName implements the AlterTableCmd interface.
   399  func (node *AlterTableSetDefault) TelemetryName() string {
   400  	return "set_default"
   401  }
   402  
   403  // Format implements the NodeFormatter interface.
   404  func (node *AlterTableSetDefault) Format(ctx *FmtCtx) {
   405  	ctx.WriteString(" ALTER COLUMN ")
   406  	ctx.FormatNode(&node.Column)
   407  	if node.Default == nil {
   408  		ctx.WriteString(" DROP DEFAULT")
   409  	} else {
   410  		ctx.WriteString(" SET DEFAULT ")
   411  		ctx.FormatNode(node.Default)
   412  	}
   413  }
   414  
   415  // AlterTableSetOnUpdate represents an ALTER COLUMN ON UPDATE SET
   416  // or DROP ON UPDATE command.
   417  type AlterTableSetOnUpdate struct {
   418  	Column Name
   419  	Expr   Expr
   420  }
   421  
   422  // GetColumn implements the ColumnMutationCmd interface.
   423  func (node *AlterTableSetOnUpdate) GetColumn() Name {
   424  	return node.Column
   425  }
   426  
   427  // TelemetryName implements the AlterTableCmd interface.
   428  func (node *AlterTableSetOnUpdate) TelemetryName() string {
   429  	return "set_on_update"
   430  }
   431  
   432  // Format implements the NodeFormatter interface.
   433  func (node *AlterTableSetOnUpdate) Format(ctx *FmtCtx) {
   434  	ctx.WriteString(" ALTER COLUMN ")
   435  	ctx.FormatNode(&node.Column)
   436  	if node.Expr == nil {
   437  		ctx.WriteString(" DROP ON UPDATE")
   438  	} else {
   439  		ctx.WriteString(" SET ON UPDATE ")
   440  		ctx.FormatNode(node.Expr)
   441  	}
   442  }
   443  
   444  // AlterTableSetVisible represents an ALTER COLUMN SET VISIBLE or NOT VISIBLE command.
   445  type AlterTableSetVisible struct {
   446  	Column  Name
   447  	Visible bool
   448  }
   449  
   450  // GetColumn implements the ColumnMutationCmd interface.
   451  func (node *AlterTableSetVisible) GetColumn() Name {
   452  	return node.Column
   453  }
   454  
   455  // TelemetryName implements the AlterTableCmd interface.
   456  func (node *AlterTableSetVisible) TelemetryName() string {
   457  	return "set_visible"
   458  }
   459  
   460  // Format implements the NodeFormatter interface.
   461  func (node *AlterTableSetVisible) Format(ctx *FmtCtx) {
   462  	ctx.WriteString(" ALTER COLUMN ")
   463  	ctx.FormatNode(&node.Column)
   464  	ctx.WriteString(" SET ")
   465  	if !node.Visible {
   466  		ctx.WriteString("NOT ")
   467  	}
   468  	ctx.WriteString("VISIBLE")
   469  }
   470  
   471  // AlterTableSetNotNull represents an ALTER COLUMN SET NOT NULL
   472  // command.
   473  type AlterTableSetNotNull struct {
   474  	Column Name
   475  }
   476  
   477  // GetColumn implements the ColumnMutationCmd interface.
   478  func (node *AlterTableSetNotNull) GetColumn() Name {
   479  	return node.Column
   480  }
   481  
   482  // TelemetryName implements the AlterTableCmd interface.
   483  func (node *AlterTableSetNotNull) TelemetryName() string {
   484  	return "set_not_null"
   485  }
   486  
   487  // Format implements the NodeFormatter interface.
   488  func (node *AlterTableSetNotNull) Format(ctx *FmtCtx) {
   489  	ctx.WriteString(" ALTER COLUMN ")
   490  	ctx.FormatNode(&node.Column)
   491  	ctx.WriteString(" SET NOT NULL")
   492  }
   493  
   494  // AlterTableDropNotNull represents an ALTER COLUMN DROP NOT NULL
   495  // command.
   496  type AlterTableDropNotNull struct {
   497  	Column Name
   498  }
   499  
   500  // GetColumn implements the ColumnMutationCmd interface.
   501  func (node *AlterTableDropNotNull) GetColumn() Name {
   502  	return node.Column
   503  }
   504  
   505  // TelemetryName implements the AlterTableCmd interface.
   506  func (node *AlterTableDropNotNull) TelemetryName() string {
   507  	return "drop_not_null"
   508  }
   509  
   510  // Format implements the NodeFormatter interface.
   511  func (node *AlterTableDropNotNull) Format(ctx *FmtCtx) {
   512  	ctx.WriteString(" ALTER COLUMN ")
   513  	ctx.FormatNode(&node.Column)
   514  	ctx.WriteString(" DROP NOT NULL")
   515  }
   516  
   517  // AlterTableDropStored represents an ALTER COLUMN DROP STORED command
   518  // to remove the computed-ness from a column.
   519  type AlterTableDropStored struct {
   520  	Column Name
   521  }
   522  
   523  // GetColumn implemnets the ColumnMutationCmd interface.
   524  func (node *AlterTableDropStored) GetColumn() Name {
   525  	return node.Column
   526  }
   527  
   528  // TelemetryName implements the AlterTableCmd interface.
   529  func (node *AlterTableDropStored) TelemetryName() string {
   530  	return "drop_stored"
   531  }
   532  
   533  // Format implements the NodeFormatter interface.
   534  func (node *AlterTableDropStored) Format(ctx *FmtCtx) {
   535  	ctx.WriteString(" ALTER COLUMN ")
   536  	ctx.FormatNode(&node.Column)
   537  	ctx.WriteString(" DROP STORED")
   538  }
   539  
   540  // AlterTablePartitionByTable represents an ALTER TABLE PARTITION [ALL]
   541  // BY command.
   542  type AlterTablePartitionByTable struct {
   543  	*PartitionByTable
   544  }
   545  
   546  // TelemetryName implements the AlterTableCmd interface.
   547  func (node *AlterTablePartitionByTable) TelemetryName() string {
   548  	return "partition_by"
   549  }
   550  
   551  // Format implements the NodeFormatter interface.
   552  func (node *AlterTablePartitionByTable) Format(ctx *FmtCtx) {
   553  	ctx.FormatNode(node.PartitionByTable)
   554  }
   555  
   556  // AuditMode represents a table audit mode
   557  type AuditMode int
   558  
   559  const (
   560  	// AuditModeDisable is the default mode - no audit.
   561  	AuditModeDisable AuditMode = iota
   562  	// AuditModeReadWrite enables audit on read or write statements.
   563  	AuditModeReadWrite
   564  )
   565  
   566  var auditModeName = [...]string{
   567  	AuditModeDisable:   "OFF",
   568  	AuditModeReadWrite: "READ WRITE",
   569  }
   570  
   571  func (m AuditMode) String() string {
   572  	return auditModeName[m]
   573  }
   574  
   575  // TelemetryName returns a friendly string for use in telemetry that represents
   576  // the AuditMode.
   577  func (m AuditMode) TelemetryName() string {
   578  	return strings.ReplaceAll(strings.ToLower(m.String()), " ", "_")
   579  }
   580  
   581  // AlterTableSetAudit represents an ALTER TABLE AUDIT SET statement.
   582  type AlterTableSetAudit struct {
   583  	Mode AuditMode
   584  }
   585  
   586  // TelemetryName implements the AlterTableCmd interface.
   587  func (node *AlterTableSetAudit) TelemetryName() string {
   588  	return "set_audit"
   589  }
   590  
   591  // Format implements the NodeFormatter interface.
   592  func (node *AlterTableSetAudit) Format(ctx *FmtCtx) {
   593  	ctx.WriteString(" EXPERIMENTAL_AUDIT SET ")
   594  	ctx.WriteString(node.Mode.String())
   595  }
   596  
   597  // AlterTableInjectStats represents an ALTER TABLE INJECT STATISTICS statement.
   598  type AlterTableInjectStats struct {
   599  	Stats Expr
   600  }
   601  
   602  // TelemetryName implements the AlterTableCmd interface.
   603  func (node *AlterTableInjectStats) TelemetryName() string {
   604  	return "inject_stats"
   605  }
   606  
   607  // Format implements the NodeFormatter interface.
   608  func (node *AlterTableInjectStats) Format(ctx *FmtCtx) {
   609  	ctx.WriteString(" INJECT STATISTICS ")
   610  	ctx.FormatNode(node.Stats)
   611  }
   612  
   613  // AlterTableSetStorageParams represents a ALTER TABLE SET command.
   614  type AlterTableSetStorageParams struct {
   615  	StorageParams StorageParams
   616  }
   617  
   618  // TelemetryName implements the AlterTableCmd interface.
   619  func (node *AlterTableSetStorageParams) TelemetryName() string {
   620  	return "set_storage_param"
   621  }
   622  
   623  // Format implements the NodeFormatter interface.
   624  func (node *AlterTableSetStorageParams) Format(ctx *FmtCtx) {
   625  	ctx.WriteString(" SET (")
   626  	ctx.FormatNode(&node.StorageParams)
   627  	ctx.WriteString(")")
   628  }
   629  
   630  // AlterTableResetStorageParams represents a ALTER TABLE RESET command.
   631  type AlterTableResetStorageParams struct {
   632  	Params NameList
   633  }
   634  
   635  // TelemetryName implements the AlterTableCmd interface.
   636  func (node *AlterTableResetStorageParams) TelemetryName() string {
   637  	return "set_storage_param"
   638  }
   639  
   640  // Format implements the NodeFormatter interface.
   641  func (node *AlterTableResetStorageParams) Format(ctx *FmtCtx) {
   642  	ctx.WriteString(" RESET (")
   643  	ctx.FormatNode(&node.Params)
   644  	ctx.WriteString(")")
   645  }
   646  
   647  // AlterTableLocality represents an ALTER TABLE LOCALITY command.
   648  type AlterTableLocality struct {
   649  	Name     *UnresolvedObjectName
   650  	IfExists bool
   651  	Locality *Locality
   652  }
   653  
   654  var _ Statement = &AlterTableLocality{}
   655  
   656  // Format implements the NodeFormatter interface.
   657  func (node *AlterTableLocality) Format(ctx *FmtCtx) {
   658  	ctx.WriteString("ALTER TABLE ")
   659  	if node.IfExists {
   660  		ctx.WriteString("IF EXISTS ")
   661  	}
   662  	ctx.FormatNode(node.Name)
   663  	ctx.WriteString(" SET ")
   664  	ctx.FormatNode(node.Locality)
   665  }
   666  
   667  // AlterTableSetSchema represents an ALTER TABLE SET SCHEMA command.
   668  type AlterTableSetSchema struct {
   669  	Name           *UnresolvedObjectName
   670  	Schema         Name
   671  	IfExists       bool
   672  	IsView         bool
   673  	IsMaterialized bool
   674  	IsSequence     bool
   675  }
   676  
   677  // Format implements the NodeFormatter interface.
   678  func (node *AlterTableSetSchema) Format(ctx *FmtCtx) {
   679  	ctx.WriteString("ALTER")
   680  	if node.IsView {
   681  		if node.IsMaterialized {
   682  			ctx.WriteString(" MATERIALIZED")
   683  		}
   684  		ctx.WriteString(" VIEW ")
   685  	} else if node.IsSequence {
   686  		ctx.WriteString(" SEQUENCE ")
   687  	} else {
   688  		ctx.WriteString(" TABLE ")
   689  	}
   690  	if node.IfExists {
   691  		ctx.WriteString("IF EXISTS ")
   692  	}
   693  	ctx.FormatNode(node.Name)
   694  	ctx.WriteString(" SET SCHEMA ")
   695  	ctx.FormatNode(&node.Schema)
   696  }
   697  
   698  // TelemetryName returns the telemetry counter to increment
   699  // when this command is used.
   700  func (node *AlterTableSetSchema) TelemetryName() string {
   701  	return "set_schema"
   702  }
   703  
   704  // AlterTableOwner represents an ALTER TABLE OWNER TO command.
   705  type AlterTableOwner struct {
   706  	Name           *UnresolvedObjectName
   707  	Owner          RoleSpec
   708  	IfExists       bool
   709  	IsView         bool
   710  	IsMaterialized bool
   711  	IsSequence     bool
   712  }
   713  
   714  // TelemetryName returns the telemetry counter to increment
   715  // when this command is used.
   716  func (node *AlterTableOwner) TelemetryName() string {
   717  	return "owner_to"
   718  }
   719  
   720  // Format implements the NodeFormatter interface.
   721  func (node *AlterTableOwner) Format(ctx *FmtCtx) {
   722  	ctx.WriteString("ALTER")
   723  	if node.IsView {
   724  		if node.IsMaterialized {
   725  			ctx.WriteString(" MATERIALIZED")
   726  		}
   727  		ctx.WriteString(" VIEW ")
   728  	} else if node.IsSequence {
   729  		ctx.WriteString(" SEQUENCE ")
   730  	} else {
   731  		ctx.WriteString(" TABLE ")
   732  	}
   733  	if node.IfExists {
   734  		ctx.WriteString("IF EXISTS ")
   735  	}
   736  	ctx.FormatNode(node.Name)
   737  	ctx.WriteString(" OWNER TO ")
   738  	ctx.FormatNode(&node.Owner)
   739  }
   740  
   741  // GetTableType returns a string representing the type of table the command
   742  // is operating on.
   743  // It is assumed if the table is not a sequence or a view, then it is a
   744  // regular table.
   745  func GetTableType(isSequence bool, isView bool, isMaterialized bool) string {
   746  	tableType := "table"
   747  	if isSequence {
   748  		tableType = "sequence"
   749  	} else if isView {
   750  		if isMaterialized {
   751  			tableType = "materialized_view"
   752  		} else {
   753  			tableType = "view"
   754  		}
   755  	}
   756  
   757  	return tableType
   758  }