visch
08/16/2022, 6:00 PMvisch
08/16/2022, 6:07 PMCREATE TEMP TABLE "temp_12345_etc" AS
SELECT * FROM "sometable" LIMIT 0
Nieve appraoch would be something like
def create_temp_table_from_table(self, from_table_name, temp_table_name):
self.connection.execute(f"CREATE TEMP TABLE \"{temp_table_name}\" AS SELECT * FROM \"{from_table_name}\" LIMIT 0")
Trying to be good about this is
def create_temp_table_from_table(self, from_table_name, temp_table_name):
create_temp_table_sql = sqlalchemy.text("""
CREATE TEMP TABLE :temp_table_name AS
SELECT * FROM :from_table_name LIMIT 0
""")
self.connection.execute(create_temp_table_sql,
temp_table_name=temp_table_name,
from_table_name=from_table_name)
Seems right from a distance, but this generates sql like
CREATE TEMP TABLE 'temp_table_name' AS
SELECT * FROM 'from_thistable' LIMIT 0
Issue with that is table names need to be double quotes. So we could do something like this to be cheeky
def create_temp_table_from_table(self, from_table_name, temp_table_name):
create_temp_table_sql = sqlalchemy.text("""
CREATE TEMP TABLE ":temp_table_name" AS
SELECT * FROM ":from_table_name" LIMIT 0
""")
self.connection.execute(create_temp_table_sql,
temp_table_name=temp_table_name,
from_table_name=from_table_name)
Which gives you
CREATE TEMP TABLE "'temp_table_name'" AS
SELECT * FROM "'from_thistable'" LIMIT 0
Which kind of works except for the fact that "'from_thistable'"
points to an absolute table name while "from_thistable"
points to a relative table (ie in your current schema)
Which leads me to diving into how we go from sqlalchemy.text
to SQL. Which gets you into the internals of SQLAlchemy with Bind Parameters re https://docs.sqlalchemy.org/en/14/core/sqlelement.html?highlight=bindparams#sqlalchemy.sql.expression.bindparam
Maybe we could specify a certain Type https://docs.sqlalchemy.org/en/14/core/type_api.html#sqlalchemy.types.TypeEngine and makeup a column https://docs.sqlalchemy.org/en/14/core/metadata.html?highlight=quote#sqlalchemy.schema.Column.params.quotevisch
08/16/2022, 6:09 PMvisch
08/16/2022, 6:10 PMedgar_ramirez_mondragon
08/16/2022, 6:55 PMDDL
? For example:
ddl = sqlalchemy.DDL(
"CREATE TEMP TABLE %(temp_table_name)s AS "
"SELECT * FROM %(table_name)s LIMIT 0",
{"temp_table_name": "test_temp", "table_name": "test"},
)
self.connection.execute(ddl)