Git Product home page Git Product logo

Comments (6)

wibbico avatar wibbico commented on September 28, 2024 1

try to use

sa.func.now()

from databricks-sql-python.

susodapop avatar susodapop commented on September 28, 2024 1

Hey folks thanks for the write-up and debugging! I'm pleased to see that you worked out how to hack this together using event.listens_for. The sqlalchemy events system is a powerful tool, since nearly every behaviour of a given dialect can be patched in this way. I agree that this belongs in the dialect itself.

@dhirschfeld would you shoot an email to [email protected] referencing this GitHub issue so we can track it and add it to the pipeline for enhancements to the dialect?

from databricks-sql-python.

dhirschfeld avatar dhirschfeld commented on September 28, 2024

Thanks to this thread I ws able to find out the magic syntax:

CREATE TABLE test.`ModelMetadata` (
	pkid BIGINT GENERATED ALWAYS AS IDENTITY, 
	name STRING NOT NULL, 
	version STRING NOT NULL, 
	insert_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, 
	PRIMARY KEY (pkid)
) USING DELTA
TBLPROPERTIES(
  'delta.minReaderVersion' = '1',
  'delta.minWriterVersion'='7',
  'delta.feature.allowColumnDefaults' = 'enabled'
);

...but I don't really know how to integrate that with sqlalchemy.

If that is required for defaults then I guess this projects needs to implement a custom compiler to add the required TBLPROPERTIES?

from databricks-sql-python.

dhirschfeld avatar dhirschfeld commented on September 28, 2024

With sa.func.now() sqlalchemy emits the below SQL:

CREATE TABLE test.`ModelMetadata` (
	pkid BIGINT GENERATED ALWAYS AS IDENTITY, 
	name STRING NOT NULL, 
	version STRING NOT NULL, 
	insert_timestamp TIMESTAMP DEFAULT now() NOT NULL, 
	PRIMARY KEY (pkid)
) USING DELTA;

...which gives the below error:

[WRONG_COLUMN_DEFAULTS_FOR_DELTA_FEATURE_NOT_ENABLED]
Failed to execute CREATE TABLE command because it assigned a column DEFAULT value, but the corresponding table feature was not enabled.
Please retry the command again after executing ALTER TABLE tableName SET TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported').

from databricks-sql-python.

dhirschfeld avatar dhirschfeld commented on September 28, 2024

If that is required for defaults then I guess this projects needs to implement a custom compiler to add the required TBLPROPERTIES?

Or, even better, just enable allowColumnDefaults by default so it Just Works without having to touch the sqlalchemy compiler.

from databricks-sql-python.

dhirschfeld avatar dhirschfeld commented on September 28, 2024

In my specific case it appears I can hack around it with:

@event.listens_for(Base.metadata, "after_create")
def set_default_insert_timestamp(target, connection, **kw):
    for table in target.tables:
        connection.execute(DDL(f"""
        ALTER TABLE {table} SET TBLPROPERTIES(
            'delta.feature.allowColumnDefaults' = 'enabled'
        );
        """))
        connection.execute(DDL(f"""
        ALTER TABLE {table}
        ALTER COLUMN insert_timestamp
        SET DEFAULT CURRENT_TIMESTAMP;
        """))

...but that's a bit gross 🤢

It would be nice for the standard sqlalchemy server_default to Just Work with no hacks required (by the end-user!)

from databricks-sql-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.