frictionlessdata / frictionless-py

Data management framework for Python that provides functionality to describe, extract, validate, and transform tabular data
https://framework.frictionlessdata.io
MIT License
709 stars 148 forks source link

Support sql output for the `frictionless describe` in CLI #1359

Open aborruso opened 1 year ago

aborruso commented 1 year ago

Hi, the frictionless describe output is one of the best tool to have CSV automatic inferencing of field types.

Is there a way to use frictionless to create a sql schema output?

If it's not possible via cli, is it possible via Python?

Thank you

CREATE TABLE a (
        a DECIMAL NOT NULL,
        b DECIMAL NOT NULL,
        c VARCHAR NOT NULL
);
roll commented 1 year ago

Hi @aborruso,

I think it will be a nice feature if we have (so I'm marking it as a feature request):

$ frictionless describe table.csv --sql

Currently, in the latest versions we're working on a static mapper:

import sqlalchemy as sa
from frictionless import Schema, formats

engine = sa.create_engine("sqlite://")
mapper = formats.sql.SqlMapper(engine)
schema = Schema.describe("data/table.csv")
table = mapper.write_schema(schema, table_name="table")
# here we have sqlalchemy.Table
print(repr(table))

@shashigharti @aivuk I didn't manage to get compiled "CREATE" statement -- maybe you know how to do so?

shashigharti commented 1 year ago

CreateTable prints the 'CREATE" statement

import sqlalchemy as sa
from frictionless import Schema, formats
from sqlalchemy.schema import CreateTable

engine = sa.create_engine("sqlite://")
mapper = formats.sql.SqlMapper(engine)
schema = Schema.describe("python/countries.csv")
print("Schema")
print(schema)
table = mapper.write_schema(schema, table_name="table")
# here we have sqlalchemy.Table
print(repr(table))
# create statement
print(CreateTable(table))
roll commented 1 year ago

Thanks @shashigharti! :tada:

aborruso commented 1 year ago

Thank you very much @shashigharti

For me it would be great to have this option available in the cli.

Thank you again and let me know if I have to close the issue

roll commented 1 year ago

Let's keep this issue as a feature request I've changed the title