ryan-syed commented on code in PR #1593:
URL: https://github.com/apache/arrow-adbc/pull/1593#discussion_r1516993213


##########
go/adbc/driver/snowflake/connection.go:
##########
@@ -557,13 +587,204 @@ func (c *cnxn) populateMetadata(ctx context.Context, 
depth adbc.ObjectDepth, cat
        return metadataRecords, nil
 }
 
+func (c *cnxn) populateConstraintSchema(ctx context.Context, depth 
adbc.ObjectDepth, metadataRecords []internal.Metadata) 
(map[internal.CatalogSchemaTable][]internal.ConstraintSchema, error) {
+       constraintLookup := 
make(map[internal.CatalogSchemaTable][]internal.ConstraintSchema)
+       tableConstraintsData, err := c.getConstraintsData(ctx, depth, 
metadataRecords)
+       if err != nil {
+               return nil, err
+       }
+
+       fullyQualifiedConstraintSchemaLookup := 
make(map[string]internal.ConstraintSchema)
+       for _, data := range tableConstraintsData {
+               var fullyQualifiedConstraintName string
+               if data.fk_constraintName.Valid {
+                       fullyQualifiedConstraintName = 
getFullyQualifiedConstraintName(data.fkDbName.String, data.fkSchema.String, 
data.fkTblName.String, data.fk_constraintName.String)
+                       if _, exists := 
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName]; !exists {
+                               
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName] = 
internal.ConstraintSchema{
+                                       ConstraintName:        
data.fk_constraintName.String,
+                                       ConstraintType:        
data.constraintType,
+                                       ConstraintColumnNames: 
[]string{data.fkColName.String},
+                                       ConstraintColumnUsages: 
[]internal.UsageSchema{
+                                               {
+                                                       ForeignKeyCatalog:  
data.dbName,
+                                                       ForeignKeyDbSchema: 
data.schema,
+                                                       ForeignKeyTable:    
data.tblName,
+                                                       ForeignKeyColName:  
data.colName,
+                                               },
+                                       },
+                               }
+                       } else {
+                               constraintInfo := 
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName]
+                               constraintInfo.ConstraintColumnNames = 
append(constraintInfo.ConstraintColumnNames, data.fkColName.String)
+
+                               constraintInfo.ConstraintColumnUsages = 
append(constraintInfo.ConstraintColumnUsages, internal.UsageSchema{
+                                       ForeignKeyCatalog:  data.dbName,
+                                       ForeignKeyDbSchema: data.schema,
+                                       ForeignKeyTable:    data.tblName,
+                                       ForeignKeyColName:  data.colName,
+                               })
+
+                               
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName] = 
constraintInfo
+                       }
+               } else {
+                       fullyQualifiedConstraintName = 
getFullyQualifiedConstraintName(data.dbName, data.schema, data.tblName, 
data.constraintName)
+                       if _, exists := 
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName]; !exists {
+                               
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName] = 
internal.ConstraintSchema{
+                                       ConstraintName:        
data.constraintName,
+                                       ConstraintType:        
data.constraintType,
+                                       ConstraintColumnNames: 
[]string{data.colName},
+                               }
+                       } else {
+                               constraintInfo := 
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName]
+                               constraintInfo.ConstraintColumnNames = 
append(constraintInfo.ConstraintColumnNames, data.colName)
+                               
fullyQualifiedConstraintSchemaLookup[fullyQualifiedConstraintName] = 
constraintInfo
+                       }
+               }
+       }
+
+       for key, constraintSchema := range fullyQualifiedConstraintSchemaLookup 
{
+               catalogSchemaTable := 
getCatalogSchemaTableFromFullyQualifiedConstraintName(key)
+               constraintLookup[catalogSchemaTable] = 
append(constraintLookup[catalogSchemaTable], constraintSchema)
+       }
+
+       return constraintLookup, nil
+}
+
+func (c *cnxn) getConstraintsData(ctx context.Context, depth adbc.ObjectDepth, 
metadataRecords []internal.Metadata) ([]TableConstraint, error) {
+       if depth == adbc.ObjectDepthCatalogs || depth == 
adbc.ObjectDepthDBSchemas {
+               return nil, nil
+       }
+       availableConstraintTypes := getAvailableConstraintTypes(metadataRecords)
+       availableFullyQualifiedConstraints := 
getAvailableFullyQualifiedConstraints(metadataRecords)
+
+       var uniqueConstraintsData []TableConstraint
+       var primaryKeyConstraintsData []TableConstraint
+       var foreignKeyConstraintsData []TableConstraint
+       var err error
+
+       if availableConstraintTypes != nil {
+               if _, exists := availableConstraintTypes[internal.Unique]; 
exists {
+                       uniqueConstraintsData, err = 
c.getUniqueConstraints(ctx, availableFullyQualifiedConstraints)
+                       if err != nil {
+                               return nil, errToAdbcErr(adbc.StatusIO, err)
+                       }
+               }
+
+               if _, exists := availableConstraintTypes[internal.PrimaryKey]; 
exists {
+                       primaryKeyConstraintsData, err = 
c.getPrimaryKeyConstraints(ctx, availableFullyQualifiedConstraints)
+                       if err != nil {
+                               return nil, errToAdbcErr(adbc.StatusIO, err)
+                       }
+               }
+
+               if _, exists := availableConstraintTypes[internal.ForeignKey]; 
exists {
+                       foreignKeyConstraintsData, err = 
c.getForeignKeyConstraints(ctx, availableFullyQualifiedConstraints)
+                       if err != nil {
+                               return nil, errToAdbcErr(adbc.StatusIO, err)
+                       }
+               }
+       }
+
+       tableConstraintsData := append(append(uniqueConstraintsData, 
primaryKeyConstraintsData...), foreignKeyConstraintsData...)
+
+       sort.Slice(tableConstraintsData, func(i, j int) bool {
+               if tableConstraintsData[i].constraintName == 
tableConstraintsData[j].constraintName {
+                       return tableConstraintsData[i].keySequence < 
tableConstraintsData[j].keySequence
+               }
+               return tableConstraintsData[i].constraintName == 
tableConstraintsData[j].constraintName
+       })

Review Comment:
   updated to use `slices.SortFunc`. Thanks for the suggestion!



##########
go/adbc/driver/snowflake/connection.go:
##########
@@ -408,7 +427,8 @@ func toField(name string, isnullable bool, dataType string, 
numPrec, numPrecRadi
 }
 
 func toXdbcDataType(dt arrow.DataType) (xdbcType internal.XdbcDataType) {
-       xdbcType = internal.XdbcDataType_XDBC_UNKNOWN_TYPE
+       // golangci-lint: ineffectual assignment to xdbcType (ineffassign)
+       // xdbcType = internal.XdbcDataType_XDBC_UNKNOWN_TYPE

Review Comment:
   removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to