From 119f7590c8c898ff44538e194f9e1a19141b0955 Mon Sep 17 00:00:00 2001 From: Thomas Schmidt Date: Thu, 21 Dec 2023 07:36:21 +0100 Subject: [PATCH 1/2] Added array column types --- src/sql_mock/bigquery/column_mocks.py | 13 +++++++++++++ src/sql_mock/clickhouse/column_mocks.py | 13 +++++++++++++ src/sql_mock/column_mocks.py | 6 +++++- tests/sql_mock/bigquery/test_column_mocks.py | 15 ++++++++++++++- tests/sql_mock/clickhouse/test_column_mocks.py | 15 ++++++++++++++- tests/sql_mock/test_column_mocks.py | 14 ++++++++++++++ 6 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/sql_mock/bigquery/column_mocks.py b/src/sql_mock/bigquery/column_mocks.py index b5c4a40..0f9f2ec 100644 --- a/src/sql_mock/bigquery/column_mocks.py +++ b/src/sql_mock/bigquery/column_mocks.py @@ -25,3 +25,16 @@ class Decimal(BigQueryColumnMock): def __init__(self, default, precision, scale, nullable=False) -> None: self.dtype = f"Decimal({precision}, {scale})" super().__init__(default, nullable) + + +class Array(BigQueryColumnMock): + use_quotes_for_casting = False + + def __init__( + self, + inner_dtype, + default, + nullable=False, + ) -> None: + self.dtype = f"Array<{inner_dtype}>" + super().__init__(default, nullable) diff --git a/src/sql_mock/clickhouse/column_mocks.py b/src/sql_mock/clickhouse/column_mocks.py index 4d49de9..5eac499 100644 --- a/src/sql_mock/clickhouse/column_mocks.py +++ b/src/sql_mock/clickhouse/column_mocks.py @@ -40,3 +40,16 @@ class Decimal(ClickhouseColumnMock): def __init__(self, default, precision, scale, nullable=False) -> None: self.dtype = f"Decimal({precision}, {scale})" super().__init__(default, nullable) + + +class Array(ClickhouseColumnMock): + use_quotes_for_casting = False + + def __init__( + self, + inner_dtype, + default, + nullable=False, + ) -> None: + self.dtype = f"Array({inner_dtype})" + super().__init__(default, nullable) diff --git a/src/sql_mock/column_mocks.py b/src/sql_mock/column_mocks.py index 804c1c3..dda4e5c 100644 --- a/src/sql_mock/column_mocks.py +++ b/src/sql_mock/column_mocks.py @@ -9,11 +9,13 @@ class ColumnMock: dtype (str): The data type of the column. nullable: Indicator whether the column can be null default: The default value for the column. + use_quotes_for_casting (bool): Indicator whether the value needs to be quoted (e.g. in the final cast) """ dtype = None nullable = False default = None + use_quotes_for_casting = True def __init__(self, default=None, nullable=False) -> None: """ @@ -34,7 +36,9 @@ def to_sql(self, column_name: str, value=NO_INPUT) -> str: # In case the val is None, we convert it to NULL if val is None: return f"cast(NULL AS {self.dtype}) AS {column_name}" - return f"cast('{val}' AS {self.dtype}) AS {column_name}" + + val = f"'{val}'" if self.use_quotes_for_casting else val + return f"cast({val} AS {self.dtype}) AS {column_name}" def cast_field(self, column_name): return f"cast({column_name} AS {self.dtype}) AS {column_name}" diff --git a/tests/sql_mock/bigquery/test_column_mocks.py b/tests/sql_mock/bigquery/test_column_mocks.py index 91de411..5a7aabf 100644 --- a/tests/sql_mock/bigquery/test_column_mocks.py +++ b/tests/sql_mock/bigquery/test_column_mocks.py @@ -1,4 +1,4 @@ -from sql_mock.bigquery.column_mocks import BigQueryColumnMock, Decimal +from sql_mock.bigquery.column_mocks import Array, BigQueryColumnMock, Decimal def test_init_not_nullable(): @@ -45,3 +45,16 @@ def test_decimal_initialization_nullable(self): assert decimal_col.dtype == "Decimal(10, 2)" assert decimal_col.default is None assert decimal_col.nullable + + +def test_array_column_inner_dtype(): + """Ensure that the inner dtype is processed correctly""" + string_array_col = Array(inner_dtype="String", default=["a", "b"], nullable=True) + int_array_col = Array(inner_dtype="Integer", default=[1, 2], nullable=False) + + assert string_array_col.dtype == "Array" + assert string_array_col.default == ["a", "b"] + assert string_array_col.nullable + assert int_array_col.dtype == "Array" + assert int_array_col.default == [1, 2] + assert int_array_col.nullable is False diff --git a/tests/sql_mock/clickhouse/test_column_mocks.py b/tests/sql_mock/clickhouse/test_column_mocks.py index 75694ef..dfd8cfb 100644 --- a/tests/sql_mock/clickhouse/test_column_mocks.py +++ b/tests/sql_mock/clickhouse/test_column_mocks.py @@ -1,4 +1,4 @@ -from sql_mock.clickhouse.column_mocks import ClickhouseColumnMock, Decimal +from sql_mock.clickhouse.column_mocks import Array, ClickhouseColumnMock, Decimal def test_init_not_nullable(): @@ -45,3 +45,16 @@ def test_decimal_initialization_nullable(): assert decimal_col.dtype == "Nullable(Decimal(10, 2))" assert decimal_col.default == 0.0 assert decimal_col.nullable + + +def test_array_column_inner_dtype(): + """Ensure that the inner dtype is processed correctly""" + string_array_col = Array(inner_dtype="String", default=["a", "b"], nullable=True) + int_array_col = Array(inner_dtype="Integer", default=[1, 2], nullable=False) + + assert string_array_col.dtype == "Nullable(Array(String))" + assert string_array_col.default == ["a", "b"] + assert string_array_col.nullable + assert int_array_col.dtype == "Array(Integer)" + assert int_array_col.default == [1, 2] + assert int_array_col.nullable is False diff --git a/tests/sql_mock/test_column_mocks.py b/tests/sql_mock/test_column_mocks.py index a559175..c872400 100644 --- a/tests/sql_mock/test_column_mocks.py +++ b/tests/sql_mock/test_column_mocks.py @@ -75,3 +75,17 @@ class ColumnTestMock(ColumnMock): column = ColumnTestMock(default=None, nullable=True) sql = column.to_sql("company") assert sql == "cast(NULL AS String) AS company" + + +def test_to_sql_not_use_quotes_for_casting(): + """ + ...then it should not quote the value in the cast expression. + """ + + class ColumnTestMock(ColumnMock): + dtype = "Integer" + use_quotes_for_casting = False + + column = ColumnTestMock(default=3.14) + sql = column.to_sql("price", value=42) + assert sql == "cast(42 AS Integer) AS price" From a4bda89d32367a548fffd6596abb346a5bd334d2 Mon Sep 17 00:00:00 2001 From: Thomas Schmidt Date: Thu, 21 Dec 2023 07:37:53 +0100 Subject: [PATCH 2/2] Added documentation for Array columns --- docs/genindex.html | 34 +++++++++++++++++++------------ docs/objects.inv | Bin 1356 -> 1383 bytes docs/searchindex.js | 2 +- docs/sitemap.xml | 2 +- docs/sql_mock.bigquery.html | 11 ++++++++++ docs/sql_mock.clickhouse.html | 27 ++++++++++--------------- docs/sql_mock.html | 37 +++++++++++++++++++++++++--------- 7 files changed, 73 insertions(+), 40 deletions(-) diff --git a/docs/genindex.html b/docs/genindex.html index c741f51..3cb63fe 100644 --- a/docs/genindex.html +++ b/docs/genindex.html @@ -130,10 +130,16 @@

_

A

-
  • StringArray (class in sql_mock.clickhouse.column_mocks) -
  • @@ -517,22 +519,28 @@

    T

    U

    +
    • user (sql_mock.clickhouse.settings.ClickHouseSettings attribute)
    • diff --git a/docs/objects.inv b/docs/objects.inv index eece63ab50163d7b3fead53f2c2769b5428c8643..ea142ee8d9ffec55ce3a1e1ec69be0a4919311c3 100644 GIT binary patch delta 1277 zcmV$r7uFp2<)ouCHlBCdiKIW1`JNG42?E0Wt< zx4uSRuTN6`?60){OI-vlIs9fg91e%OYR}@GATKCA-0Vb>F+gvS${V9+y<^||=8GTD z#j5OhqT782^3Xo4=p7y(5y7$@ca172EP5{`cJ>j5l|olt#DB4l%61ggc4RR){Tw7^f+t1ZygI zOe7@0HN{K?@qfpuQZsvCF%f)2w`Wzcp}>-5v(Wg-_$N%xudzytZSMSH+ovurF$?zc zEKH_vx2x*!wyY*Ie7G$D_8f4>)bY5fhukbpAB13lc{tN^;6T2ag}yD@2ae@g^v?w;D@B&%d zUmi_7T*`yr<-A{QTW^sx)yNiZ^emJP=7lx|qspRiiUx|K5Q4F3q3oL7mE=Uo@` zUoKWKSVvCW0rgt4ve{LVGtd3O0=F;*1*eXt+Lz_f^s%u+{9tf+GXe`=z+(wOV%F_e#sei!#V z&Gg9VfmZ^ly|+$PSPhK+#Ed5H^7~Or;r4pj+pm3aS9hv!+y$|M{(E4r=H~dw211;e z>pPHvke>py9kFo|4vaM68U~4#gVG{)#_WSWntw_9Iwl%o*k_mpBL<>b!#MW$oK%X` zpbv9d8!2`sU`W1a7un@l)qa@BDmf2%R7~6mjq&5^sp-Ihx@iiq{4UX#Ua!Zoe2;|9 z5bx(M-um%3i@d|Ld|w-#pD%NLR3|OSI1P-i0DN``VV48NfvjV;(iss8qhJA|{uH+; z=6~xr2|NJ_$)f3QLB}s}Fhz>Z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XLRD9sH+kV=Syna70RPL;9JsuxMv9Hl z4K2RKkNA-bq^V}`(@FuBWZ%@_+q~%7_kY1VC_}A|ZG=(zgi=gi>O-oJ+ihbTpMN`0 z%LnmOTJ0l98}H;|idsLO -https://deeplcom.github.io/sql-mock/en/getting_started/quickstart.htmlhttps://deeplcom.github.io/sql-mock/en/index.htmlhttps://deeplcom.github.io/sql-mock/en/modules.htmlhttps://deeplcom.github.io/sql-mock/en/sql_mock.htmlhttps://deeplcom.github.io/sql-mock/en/sql_mock.bigquery.htmlhttps://deeplcom.github.io/sql-mock/en/sql_mock.clickhouse.htmlhttps://deeplcom.github.io/sql-mock/en/usage/default_values.htmlhttps://deeplcom.github.io/sql-mock/en/usage/defining_table_mocks.htmlhttps://deeplcom.github.io/sql-mock/en/genindex.htmlhttps://deeplcom.github.io/sql-mock/en/py-modindex.htmlhttps://deeplcom.github.io/sql-mock/en/search.html \ No newline at end of file +https://deeplcom.github.io/sql-mock/en/getting_started/quickstart.htmlhttps://deeplcom.github.io/sql-mock/en/index.htmlhttps://deeplcom.github.io/sql-mock/en/modules.htmlhttps://deeplcom.github.io/sql-mock/en/sql_mock.htmlhttps://deeplcom.github.io/sql-mock/en/sql_mock.bigquery.htmlhttps://deeplcom.github.io/sql-mock/en/sql_mock.clickhouse.htmlhttps://deeplcom.github.io/sql-mock/en/genindex.htmlhttps://deeplcom.github.io/sql-mock/en/py-modindex.htmlhttps://deeplcom.github.io/sql-mock/en/search.html \ No newline at end of file diff --git a/docs/sql_mock.bigquery.html b/docs/sql_mock.bigquery.html index b8bf1b6..1ce2c13 100644 --- a/docs/sql_mock.bigquery.html +++ b/docs/sql_mock.bigquery.html @@ -116,6 +116,17 @@

      Submodules

      sql_mock.bigquery.column_mocks module

      +
      +
      +class sql_mock.bigquery.column_mocks.Array(inner_dtype, default, nullable=False)
      +

      Bases: BigQueryColumnMock

      +
      +
      +use_quotes_for_casting = False
      +
      + +
      +
      class sql_mock.bigquery.column_mocks.BigQueryColumnMock(default=None, nullable=False)
      diff --git a/docs/sql_mock.clickhouse.html b/docs/sql_mock.clickhouse.html index a57edec..1684a38 100644 --- a/docs/sql_mock.clickhouse.html +++ b/docs/sql_mock.clickhouse.html @@ -115,6 +115,17 @@

      Submodules

      sql_mock.clickhouse.column_mocks module

      +
      +
      +class sql_mock.clickhouse.column_mocks.Array(inner_dtype, default, nullable=False)
      +

      Bases: ClickhouseColumnMock

      +
      +
      +use_quotes_for_casting = False
      +
      + +
      +
      class sql_mock.clickhouse.column_mocks.Boolean(default, nullable=False)
      @@ -204,22 +215,6 @@

      Submodules -
      -class sql_mock.clickhouse.column_mocks.StringArray(default, nullable=False)
      -

      Bases: ClickhouseColumnMock

      -
      -
      -dtype = 'Array(String)'
      -
      - -
      -
      -to_sql(column_name: str, value=<sql_mock.constants.NoInput object>) str
      -
      - -

      -

      sql_mock.clickhouse.settings module

      diff --git a/docs/sql_mock.html b/docs/sql_mock.html index 760666b..3078b3a 100644 --- a/docs/sql_mock.html +++ b/docs/sql_mock.html @@ -136,6 +136,10 @@

      Subpackagessql_mock.bigquery package

      +
      cast_field(column_name)
      @@ -292,6 +306,11 @@

      Submodulesto_sql(column_name: str, value=<sql_mock.constants.NoInput object>) str

      +
      +
      +use_quotes_for_casting = True
      +
      + @@ -472,13 +491,13 @@

      Submodules -
      -query: str
      +
      +query: str
      -
      -table_ref: str
      +
      +table_ref: str