github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/changefeedccl/changefeedbase/validate.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Licensed as a CockroachDB Enterprise file under the Cockroach Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
     8  
     9  package changefeedbase
    10  
    11  import (
    12  	"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
    13  	"github.com/cockroachdb/cockroach/pkg/keys"
    14  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    15  	"github.com/cockroachdb/errors"
    16  )
    17  
    18  // ValidateTable validates that a table descriptor can be watched by a CHANGEFEED.
    19  func ValidateTable(targets jobspb.ChangefeedTargets, tableDesc *sqlbase.TableDescriptor) error {
    20  	t, ok := targets[tableDesc.ID]
    21  	if !ok {
    22  		return errors.Errorf(`unwatched table: %s`, tableDesc.Name)
    23  	}
    24  
    25  	// Technically, the only non-user table known not to work is system.jobs
    26  	// (which creates a cycle since the resolved timestamp high-water mark is
    27  	// saved in it), but there are subtle differences in the way many of them
    28  	// work and this will be under-tested, so disallow them all until demand
    29  	// dictates.
    30  	if tableDesc.ID < keys.MinUserDescID {
    31  		return errors.Errorf(`CHANGEFEEDs are not supported on system tables`)
    32  	}
    33  	if tableDesc.IsView() {
    34  		return errors.Errorf(`CHANGEFEED cannot target views: %s`, tableDesc.Name)
    35  	}
    36  	if tableDesc.IsVirtualTable() {
    37  		return errors.Errorf(`CHANGEFEED cannot target virtual tables: %s`, tableDesc.Name)
    38  	}
    39  	if tableDesc.IsSequence() {
    40  		return errors.Errorf(`CHANGEFEED cannot target sequences: %s`, tableDesc.Name)
    41  	}
    42  	if len(tableDesc.Families) != 1 {
    43  		return errors.Errorf(
    44  			`CHANGEFEEDs are currently supported on tables with exactly 1 column family: %s has %d`,
    45  			tableDesc.Name, len(tableDesc.Families))
    46  	}
    47  
    48  	if tableDesc.State == sqlbase.TableDescriptor_DROP {
    49  		return errors.Errorf(`"%s" was dropped or truncated`, t.StatementTimeName)
    50  	}
    51  	if tableDesc.Name != t.StatementTimeName {
    52  		return errors.Errorf(`"%s" was renamed to "%s"`, t.StatementTimeName, tableDesc.Name)
    53  	}
    54  
    55  	// TODO(mrtracy): re-enable this when allow-backfill option is added.
    56  	// if tableDesc.HasColumnBackfillMutation() {
    57  	// 	return errors.Errorf(`CHANGEFEEDs cannot operate on tables being backfilled`)
    58  	// }
    59  
    60  	return nil
    61  }