Skip to content

Commit

Permalink
add more tests for object_agg
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkinulas committed Apr 5, 2024
1 parent 5d31975 commit 3559816
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion fakesnow/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _execute(
.transform(transforms.timestamp_ntz_ns)
.transform(transforms.float_to_double)
.transform(transforms.integer_precision)
# TODO(selman): Broken, failes on CTAS queries with CASTs;
# TODO(selman): Broken, fails on CTAS queries with CASTs;
# CREATE TABLE SOME_TABLE AS (
# SELECT
# R1 AS C1,
Expand Down
8 changes: 4 additions & 4 deletions fakesnow/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,13 +1425,13 @@ def to_json_extract_scalar(expression: exp.JSONExtract) -> exp.Expression:
left = expression.left
right = expression.right

# <json-extact> = <string-literal>
# <json-extract> = <string-literal>
if is_json_extract(left) and isinstance(right, exp.Literal) and right.is_string:
json_extract_scalar = exp.Paren(this=to_json_extract_scalar(cast(exp.JSONExtract, left)))

return exp.EQ(this=json_extract_scalar, expression=right)

# <string-literal> = <json-extact>
# <string-literal> = <json-extract>
elif is_json_extract(right) and isinstance(left, exp.Literal) and left.is_string:
json_extract_scalar = exp.Paren(this=to_json_extract_scalar(cast(exp.JSONExtract, right)))

Expand All @@ -1441,7 +1441,7 @@ def to_json_extract_scalar(expression: exp.JSONExtract) -> exp.Expression:


def json_extract_in_string_literals(expression: exp.Expression) -> exp.Expression:
"""Snowflake does implicit casting on JSON extract value on an IN caluse;
"""Snowflake does implicit casting on JSON extract value on an IN clause;
Snowflake;
SELECT
Expand Down Expand Up @@ -1483,7 +1483,7 @@ def json_extract_in_string_literals(expression: exp.Expression) -> exp.Expressio
via `->` operator thus FALSE in first case of the first query.
To keep things simple, if all values in IN clause are string literals, we
can extract JSON value as string/VARCHAR to achive similar behaviour.
can extract JSON value as string/VARCHAR to achieve similar behaviour.
SELECT
TO_JSON({'k': '10'}) AS D,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,15 @@ def test_json_extract_cast_as_varchar(dcur: snowflake.connector.cursor.DictCurso
dcur.execute("SELECT j:str::number as c_str_number, j:number::number as c_num_number FROM example")
assert dcur.fetchall() == [{"C_STR_NUMBER": 100, "C_NUM_NUMBER": 100}]

def test_json_group_object(dcur: snowflake.connector.cursor.DictCursor):
dcur.execute("create table table1 (id number, key varchar, value varchar)")
values = [(1, "a", "1"), (1, "b", "2"), (1, "c", "3"), (2, "e", "1"), (2, "f", "1"), (3, "a", "2")]

dcur.executemany("insert into table1 values (%s, %s, %s)", values)
expected = [{'ID': 1, 'OBJ': '{\n "a": "1",\n "b": "2",\n "c": "3"\n}'}, {'ID': 2, 'OBJ': '{\n "e": "1",\n "f": "1"\n}'}, {'ID': 3, 'OBJ': '{\n "a": "2"\n}'}]
dcur.execute("select id, object_agg(key, value) as obj from table1 group by 1")
assert dindent(dcur.fetchall()) == expected


def test_write_pandas_quoted_column_names(conn: snowflake.connector.SnowflakeConnection):
with conn.cursor(snowflake.connector.cursor.DictCursor) as dcur:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_info_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_info_schema_columns_other(cur: snowflake.connector.cursor.SnowflakeCurs
]


@pytest.mark.xfail(reason="NOTE(selman): removed extact_extract_text_length transformation")
@pytest.mark.xfail(reason="NOTE(selman): removed extract_extract_text_length transformation")
def test_info_schema_columns_text(cur: snowflake.connector.cursor.SnowflakeCursor):
# see https://docs.snowflake.com/en/sql-reference/data-types-text
cur.execute(
Expand Down

0 comments on commit 3559816

Please sign in to comment.