mack icon indicating copy to clipboard operation
mack copied to clipboard

Provide an API to add constraints to a Delta Table via Python

Open robertkossendey opened this issue 2 years ago • 6 comments

Correct me if I am wrong, but the only way to currently add table constraints is through the SQL interface. We should provide an API that allows the user to add, maybe event update and delete constraints from a table via Python.

robertkossendey avatar Mar 13 '23 20:03 robertkossendey

@robertkossendey - this would be great, but it really feels like this one belongs in Delta itself. Can you open an issue in delta-io/delta and see if we can get it added there? If that's not possible, then I suppose we can add it to mack.

MrPowers avatar Mar 13 '23 21:03 MrPowers

@MrPowers I asked Denny about the Roadmap and whether we should work on such features for mack. I agree that this should be part of the core delta-spark but it is not on the Roadmap yet.

robertkossendey avatar Mar 13 '23 21:03 robertkossendey

Actually, because constraints end up as properties of the Delta table, you can add constraints with the following syntax. It's not elegant but it's an optional workaround:

target_table = (DeltaTable.create(spark)
  .tableName("table_with_constraints")
  .addColumn("col1", dataType = "INT", nullable = False)
  .addColumn("col2", dataType = "STRING", nullable = True)
  .addColumn("col3", dataType = "STRING", nullable = False)
  .property("delta.constraints.col1_constraint","col1 > 0" )
  .execute()
)

cs23andris avatar Mar 16 '23 09:03 cs23andris

Ahhh, thank you very much for letting me know @cs23andris! I always forget about the DeltaTableBuilder API. This does only work on creation though, right?

robertkossendey avatar Mar 16 '23 09:03 robertkossendey

Yes, that's true. It seems that a DeltaTableBuilder object can only be created with create(), createIfNotExists(), replace() or createOrReplace() methods there's no alter() function.

cs23andris avatar Mar 16 '23 10:03 cs23andris

@robertkossendey - yea, think we can go ahead and add this feature. Feel free to propose your suggested API and we can brainstorm before it gets built. Thank you!

MrPowers avatar Mar 17 '23 12:03 MrPowers