MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / NewColumnTableDef

Function NewColumnTableDef

pkg/sql/sem/tree/create.go:569–725  ·  view source on GitHub ↗

NewColumnTableDef constructs a column definition for a CreateTable statement.

(
	name Name,
	typRef ResolvableTypeReference,
	isSerial bool,
	qualifications []NamedColumnQualification,
)

Source from the content-addressed store, hash-verified

567
568// NewColumnTableDef constructs a column definition for a CreateTable statement.
569func NewColumnTableDef(
570 name Name,
571 typRef ResolvableTypeReference,
572 isSerial bool,
573 qualifications []NamedColumnQualification,
574) (*ColumnTableDef, error) {
575 d := &ColumnTableDef{
576 Name: name,
577 Type: typRef,
578 IsSerial: isSerial,
579 }
580 d.Nullable.Nullability = SilentNull
581 for _, c := range qualifications {
582 switch t := c.Qualification.(type) {
583 case ColumnCollation:
584 locale := string(t)
585 // In postgres, all strings have collations defaulting to "default".
586 // In CRDB, collated strings are treated separately to string family types.
587 // To most behave like postgres, set the CollatedString type if a non-"default"
588 // collation is used.
589 if locale != collatedstring.DefaultCollationTag {
590 _, err := language.Parse(locale)
591 if err != nil {
592 return nil, pgerror.Wrapf(err, pgcode.Syntax, "invalid locale %s", locale)
593 }
594 collatedTyp, err := processCollationOnType(name, d.Type, t)
595 if err != nil {
596 return nil, err
597 }
598 d.Type = collatedTyp
599 }
600 case *ColumnDefault:
601 if d.HasDefaultExpr() || d.GeneratedIdentity.IsGeneratedAsIdentity {
602 return nil, pgerror.Newf(pgcode.Syntax,
603 "multiple default values specified for column %q", name)
604 }
605 d.DefaultExpr.Expr = t.Expr
606 d.DefaultExpr.ConstraintName = c.Name
607 case *ColumnOnUpdate:
608 if d.HasOnUpdateExpr() {
609 return nil, pgerror.Newf(pgcode.Syntax,
610 "multiple ON UPDATE values specified for column %q", name)
611 }
612 if d.GeneratedIdentity.IsGeneratedAsIdentity {
613 return nil, pgerror.Newf(pgcode.Syntax,
614 "both generated identity and on update expression specified for column %q",
615 name)
616 }
617 d.OnUpdateExpr.Expr = t.Expr
618 d.OnUpdateExpr.ConstraintName = c.Name
619 case *GeneratedAlwaysAsIdentity, *GeneratedByDefAsIdentity:
620 if typ, ok := typRef.(*types.T); !ok || typ.InternalType.Family != types.IntFamily {
621 return nil, pgerror.Newf(
622 pgcode.InvalidParameterValue,
623 "identity column type must be an INT",
624 )
625 }
626 if d.GeneratedIdentity.IsGeneratedAsIdentity {

Callers

nothing calls this directly

Calls 8

HasDefaultExprMethod · 0.95
HasOnUpdateExprMethod · 0.95
HasFKConstraintMethod · 0.95
HasColumnFamilyMethod · 0.95
WrapfFunction · 0.92
NewfFunction · 0.92
processCollationOnTypeFunction · 0.85
ParseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…