void InformationSchemaCatalog::FillConstraintTableUsageTable()

in backend/query/information_schema_catalog.cc [1796:1952]


void InformationSchemaCatalog::FillConstraintTableUsageTable() {
  auto constraint_table_usage =
      tables_by_name_.at(GetNameForDialect(kConstraintTableUsage)).get();

  std::vector<std::vector<zetasql::Value>> rows;

  // Add the user table constraints.
  for (const auto* table : default_schema_->tables()) {
    const auto& [table_schema_part, table_name_part] =
        GetSchemaAndNameForInformationSchema(table->Name());
    // Add the primary key.
    rows.push_back({
        // table_catalog
        DialectTableCatalog(),
        // table_schema
        String(table_schema_part),
        // table_name
        String(table_name_part),
        // constraint_catalog
        DialectTableCatalog(),
        // constraint_schema
        String(table_schema_part),
        // constraint_name
        String(PrimaryKeyName(table)),
    });

    // Production doesn't add check constraints to the information schema for
    // this table so we also don't add it in the emulator.
    if (dialect_ != DatabaseDialect::POSTGRESQL) {
      // Add the NOT NULL check constraints.
      for (const auto* column : table->columns()) {
        if (column->is_nullable()) {
          continue;
        }
        rows.push_back({
            // table_catalog
            DialectTableCatalog(),
            // table_schema
            String(table_schema_part),
            // table_name
            String(table_name_part),
            // constraint_catalog
            DialectTableCatalog(),
            // constraint_schema
            String(table_schema_part),
            // constraint_name
            String(CheckNotNullName(table, column)),
        });
      }

      // Add the check constraints defined by the ZETASQL_VLOG keyword.
      for (const auto* check_constraint : table->check_constraints()) {
        rows.push_back({
            // table_catalog
            DialectTableCatalog(),
            // table_schema
            String(table_schema_part),
            // table_name
            String(table_name_part),
            // constraint_catalog
            DialectTableCatalog(),
            // constraint_schema
            String(table_schema_part),
            // constraint_name
            String(SDLObjectName::GetInSchemaName(check_constraint->Name())),
        });
      }
    }

    // Add the foreign keys.
    for (const auto* foreign_key : table->foreign_keys()) {
      const auto& [referenced_table_schema_part, referenced_table_name_part] =
          GetSchemaAndNameForInformationSchema(
              foreign_key->referenced_table()->Name());
      rows.push_back({
          // table_catalog
          DialectTableCatalog(),
          // table_schema
          String(referenced_table_schema_part),
          // table_name
          String(referenced_table_name_part),
          // constraint_catalog
          DialectTableCatalog(),
          // constraint_schema
          String(table_schema_part),
          // constraint_name
          String(SDLObjectName::GetInSchemaName(foreign_key->Name())),
      });

      // Add the foreign key's unique backing index as a unique constraint.
      if (foreign_key->referenced_index()) {
        rows.push_back({
            // table_catalog
            DialectTableCatalog(),
            // table_schema
            String(referenced_table_schema_part),
            // table_name
            String(referenced_table_name_part),
            // constraint_catalog
            DialectTableCatalog(),
            // constraint_schema
            String(table_schema_part),
            // constraint_name
            String(foreign_key->referenced_index()->Name()),
        });
      }
    }
  }

  // Production doesn't add check constraints for the information schema so we
  // also don't add it in the emulator.
  if (dialect_ != DatabaseDialect::POSTGRESQL) {
    // Add the information schema constraints.
    for (const auto* table : this->tables()) {
      // Add the primary key.
      rows.push_back({
          // table_catalog
          DialectTableCatalog(),
          // table_schema
          String(kInformationSchema),
          // table_name
          String(table->Name()),
          // constraint_catalog
          DialectTableCatalog(),
          // constraint_schema
          String(kInformationSchema),
          // constraint_name
          String(PrimaryKeyName(table)),
      });

      // Add the NOT NULL check constraints.
      for (int i = 0; i < table->NumColumns(); ++i) {
        const auto* column = table->GetColumn(i);
        const auto& metadata = GetColumnMetadata(dialect_, table, column);
        if (IsNullable(metadata)) {
          continue;
        }
        rows.push_back({
            // table_catalog
            DialectTableCatalog(),
            // table_schema
            String(kInformationSchema),
            // table_name
            String(table->Name()),
            // constraint_catalog
            DialectTableCatalog(),
            // constraint_schema
            String(kInformationSchema),
            // constraint_name
            String(CheckNotNullName(table, column)),
        });
      }
    }
  }

  constraint_table_usage->SetContents(rows);
}