Skip to content

Commit

Permalink
fix: casing in description()
Browse files Browse the repository at this point in the history
introduced by 69f00a6
  • Loading branch information
tekumara committed Jan 28, 2024
1 parent f1187b1 commit db35a5d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
34 changes: 17 additions & 17 deletions fakesnow/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ def create_database(expression: exp.Expression, db_path: Path | None = None) ->
SQL_DESCRIBE_TABLE = Template(
"""
SELECT
column_name AS name,
column_name AS "name",
CASE WHEN data_type = 'NUMBER' THEN 'NUMBER(' || numeric_precision || ',' || numeric_scale || ')'
WHEN data_type = 'TEXT' THEN 'VARCHAR(' || coalesce(character_maximum_length,16777216) || ')'
WHEN data_type = 'TIMESTAMP_NTZ' THEN 'TIMESTAMP_NTZ(9)'
WHEN data_type = 'TIMESTAMP_TZ' THEN 'TIMESTAMP_TZ(9)'
WHEN data_type = 'TIME' THEN 'TIME(9)'
WHEN data_type = 'BINARY' THEN 'BINARY(8388608)'
ELSE data_type END AS type,
'COLUMN' AS kind,
ELSE data_type END AS "type",
'COLUMN' AS "kind",
CASE WHEN is_nullable = 'YES' THEN 'Y' ELSE 'N' END AS "null?",
column_default AS default,
column_default AS "default",
'N' AS "primary key",
'N' AS "unique key",
NULL AS check,
NULL AS expression,
NULL AS comment,
NULL AS "check",
NULL AS "expression",
NULL AS "comment",
NULL AS "policy name",
NULL AS "privacy domain",
FROM information_schema._fs_columns_snowflake
Expand Down Expand Up @@ -651,11 +651,11 @@ def set_schema(expression: exp.Expression, current_database: str | None) -> exp.

SQL_SHOW_OBJECTS = """
select
to_timestamp(0)::timestamptz as created_on,
table_name as name,
case when table_type='BASE TABLE' then 'TABLE' else table_type end as kind,
table_catalog as database_name,
table_schema as schema_name
to_timestamp(0)::timestamptz as 'created_on',
table_name as 'name',
case when table_type='BASE TABLE' then 'TABLE' else table_type end as 'kind',
table_catalog as 'database_name',
table_schema as 'schema_name'
from information_schema.tables
"""

Expand Down Expand Up @@ -697,11 +697,11 @@ def show_objects(expression: exp.Expression, current_database: str | None = None

SQL_SHOW_SCHEMAS = """
select
to_timestamp(0)::timestamptz as created_on,
schema_name as name,
NULL as kind,
catalog_name as database_name,
NULL as schema_name
to_timestamp(0)::timestamptz as 'created_on',
schema_name as 'name',
NULL as 'kind',
catalog_name as 'database_name',
NULL as 'schema_name'
from information_schema.schemata
where catalog_name not in ('memory', 'system', 'temp') and schema_name not in ('main', 'pg_catalog')
"""
Expand Down
17 changes: 16 additions & 1 deletion tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,20 @@ def test_describe_table(dcur: snowflake.connector.cursor.DictCursor):
assert dcur.execute("describe table example").fetchall() == expected
assert dcur.execute("describe table schema1.example").fetchall() == expected
assert dcur.execute("describe table db1.schema1.example").fetchall() == expected
assert len(dcur.description) == 12
assert [r.name for r in dcur.description] == [
"name",
"type",
"kind",
"null?",
"default",
"primary key",
"unique key",
"check",
"expression",
"comment",
"policy name",
"privacy domain",
]

assert dcur.execute("describe table db1.schema1.derived").fetchall() == [
# TODO: preserve varchar size when derived - this should be VARCHAR(20)
Expand Down Expand Up @@ -977,6 +990,7 @@ def test_show_objects(dcur: snowflake.connector.cursor.SnowflakeCursor):
"schema_name": "information_schema",
},
]
assert [r.name for r in dcur.description] == ["created_on", "name", "kind", "database_name", "schema_name"]


def test_show_schemas(dcur: snowflake.connector.cursor.SnowflakeCursor):
Expand All @@ -997,6 +1011,7 @@ def test_show_schemas(dcur: snowflake.connector.cursor.SnowflakeCursor):
"schema_name": None,
},
]
assert [r.name for r in dcur.description] == ["created_on", "name", "kind", "database_name", "schema_name"]


def test_sqlstate(cur: snowflake.connector.cursor.SnowflakeCursor):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,22 @@ def test_semi_structured_types() -> None:
def test_show_objects() -> None:
assert (
sqlglot.parse_one("show terse objects in database db1 limit 10", read="snowflake").transform(show_objects).sql()
== "SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS created_on, table_name AS name, CASE WHEN table_type = 'BASE TABLE' THEN 'TABLE' ELSE table_type END AS kind, table_catalog AS database_name, table_schema AS schema_name FROM information_schema.tables WHERE NOT (table_schema = 'information_schema' AND table_name LIKE '_fs_%%') AND table_catalog = 'db1' LIMIT 10" # noqa: E501
== """SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS "created_on", table_name AS "name", CASE WHEN table_type = 'BASE TABLE' THEN 'TABLE' ELSE table_type END AS "kind", table_catalog AS "database_name", table_schema AS "schema_name" FROM information_schema.tables WHERE NOT (table_schema = 'information_schema' AND table_name LIKE '_fs_%%') AND table_catalog = 'db1' LIMIT 10""" # noqa: E501
)
assert (
sqlglot.parse_one("show terse objects in db1.schema1", read="snowflake").transform(show_objects).sql()
== "SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS created_on, table_name AS name, CASE WHEN table_type = 'BASE TABLE' THEN 'TABLE' ELSE table_type END AS kind, table_catalog AS database_name, table_schema AS schema_name FROM information_schema.tables WHERE NOT (table_schema = 'information_schema' AND table_name LIKE '_fs_%%') AND table_catalog = 'db1' AND table_schema = 'schema1'" # noqa: E501
== """SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS "created_on", table_name AS "name", CASE WHEN table_type = 'BASE TABLE' THEN 'TABLE' ELSE table_type END AS "kind", table_catalog AS "database_name", table_schema AS "schema_name" FROM information_schema.tables WHERE NOT (table_schema = 'information_schema' AND table_name LIKE '_fs_%%') AND table_catalog = 'db1' AND table_schema = 'schema1'""" # noqa: E501
)
assert (
sqlglot.parse_one("show terse objects in database", read="snowflake").transform(show_objects).sql()
== "SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS created_on, table_name AS name, CASE WHEN table_type = 'BASE TABLE' THEN 'TABLE' ELSE table_type END AS kind, table_catalog AS database_name, table_schema AS schema_name FROM information_schema.tables WHERE NOT (table_schema = 'information_schema' AND table_name LIKE '_fs_%%')" # noqa: E501
== """SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS "created_on", table_name AS "name", CASE WHEN table_type = 'BASE TABLE' THEN 'TABLE' ELSE table_type END AS "kind", table_catalog AS "database_name", table_schema AS "schema_name" FROM information_schema.tables WHERE NOT (table_schema = 'information_schema' AND table_name LIKE '_fs_%%')""" # noqa: E501
)


def test_show_schemas() -> None:
assert (
sqlglot.parse_one("show terse schemas in database db1", read="snowflake").transform(show_schemas).sql()
== "SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS created_on, schema_name AS name, NULL AS kind, catalog_name AS database_name, NULL AS schema_name FROM information_schema.schemata WHERE NOT catalog_name IN ('memory', 'system', 'temp') AND NOT schema_name IN ('main', 'pg_catalog') AND catalog_name = 'db1'" # noqa: E501
== """SELECT CAST(UNIX_TO_TIME(0) AS TIMESTAMPTZ) AS "created_on", schema_name AS "name", NULL AS "kind", catalog_name AS "database_name", NULL AS "schema_name" FROM information_schema.schemata WHERE NOT catalog_name IN ('memory', 'system', 'temp') AND NOT schema_name IN ('main', 'pg_catalog') AND catalog_name = 'db1'""" # noqa: E501
)


Expand Down

0 comments on commit db35a5d

Please sign in to comment.