- Notifications
You must be signed in to change notification settings - Fork126
Parameterized queries: Add e2e tests for parameterized queries#227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
bd070da3ce76cec37f237b5e07263333ca0554623fa8841f8File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -613,10 +613,7 @@ def _create_arrow_table(self, t_row_set, lz4_compressed, schema_bytes, descripti | ||
| num_rows, | ||
| ) = convert_column_based_set_to_arrow_table(t_row_set.columns, description) | ||
| elif t_row_set.arrowBatches is not None: | ||
| (arrow_table, num_rows,) = convert_arrow_based_set_to_arrow_table( | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is just a formatting change unrelated to the bulk of the PR. | ||
| t_row_set.arrowBatches, lz4_compressed, schema_bytes | ||
| ) | ||
| else: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,9 +24,11 @@ def __some_example_requirement(self): | ||
| import sqlalchemy.testing.exclusions | ||
| import logging | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is a formatting change unrelated to the rest of this PR. | ||
| logger = logging.getLogger(__name__) | ||
| logger.warning("requirements.py is not currently employed by Databricks dialect") | ||
| class Requirements(sqlalchemy.testing.requirements.SuiteRequirements): | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import datetime | ||
| from decimal import Decimal | ||
| from typing import Dict, List, Tuple, Union | ||
| import pytz | ||
| from databricks.sql.client import Connection | ||
| from databricks.sql.utils import DbSqlParameter, DbSqlType | ||
| class PySQLParameterizedQueryTestSuiteMixin: | ||
| """Namespace for tests of server-side parameterized queries""" | ||
| QUERY = "SELECT :p AS col" | ||
| def _get_one_result(self, query: str, parameters: Union[Dict, List[Dict]]) -> Tuple: | ||
| with self.connection() as conn: | ||
| with conn.cursor() as cursor: | ||
| cursor.execute(query, parameters=parameters) | ||
| return cursor.fetchone() | ||
| def _quantize(self, input: Union[float, int], place_value=2) -> Decimal: | ||
| return Decimal(str(input)).quantize(Decimal("0." + "0" * place_value)) | ||
| def test_primitive_inferred_bool(self): | ||
| params = {"p": True} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == True | ||
| def test_primitive_inferred_integer(self): | ||
| params = {"p": 1} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == 1 | ||
| def test_primitive_inferred_double(self): | ||
| params = {"p": 3.14} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert self._quantize(result.col) == self._quantize(3.14) | ||
| def test_primitive_inferred_date(self): | ||
| # DATE in Databricks is mapped into a datetime.date object in Python | ||
| date_value = datetime.date(2023, 9, 6) | ||
| params = {"p": date_value} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
| def test_primitive_inferred_timestamp(self): | ||
| # TIMESTAMP in Databricks is mapped into a datetime.datetime object in Python | ||
| date_value = datetime.datetime(2023, 9, 6, 3, 14, 27, 843, tzinfo=pytz.UTC) | ||
| params = {"p": date_value} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
| def test_primitive_inferred_string(self): | ||
| params = {"p": "Hello"} | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == "Hello" | ||
| def test_dbsqlparam_inferred_bool(self): | ||
| params = [DbSqlParameter(name="p", value=True, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == True | ||
| def test_dbsqlparam_inferred_integer(self): | ||
| params = [DbSqlParameter(name="p", value=1, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == 1 | ||
| def test_dbsqlparam_inferred_double(self): | ||
| params = [DbSqlParameter(name="p", value=3.14, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert self._quantize(result.col) == self._quantize(3.14) | ||
| def test_dbsqlparam_inferred_date(self): | ||
| # DATE in Databricks is mapped into a datetime.date object in Python | ||
| date_value = datetime.date(2023, 9, 6) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
| def test_dbsqlparam_inferred_timestamp(self): | ||
| # TIMESTAMP in Databricks is mapped into a datetime.datetime object in Python | ||
| date_value = datetime.datetime(2023, 9, 6, 3, 14, 27, 843, tzinfo=pytz.UTC) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
| def test_dbsqlparam_inferred_string(self): | ||
| params = [DbSqlParameter(name="p", value="Hello", type=None)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == "Hello" | ||
| def test_dbsqlparam_explicit_bool(self): | ||
| params = [DbSqlParameter(name="p", value=True, type=DbSqlType.BOOLEAN)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == True | ||
| def test_dbsqlparam_explicit_integer(self): | ||
| params = [DbSqlParameter(name="p", value=1, type=DbSqlType.INTEGER)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == 1 | ||
| def test_dbsqlparam_explicit_double(self): | ||
| params = [DbSqlParameter(name="p", value=3.14, type=DbSqlType.FLOAT)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert self._quantize(result.col) == self._quantize(3.14) | ||
| def test_dbsqlparam_explicit_date(self): | ||
| # DATE in Databricks is mapped into a datetime.date object in Python | ||
| date_value = datetime.date(2023, 9, 6) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=DbSqlType.DATE)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
| def test_dbsqlparam_explicit_timestamp(self): | ||
| # TIMESTAMP in Databricks is mapped into a datetime.datetime object in Python | ||
| date_value = datetime.datetime(2023, 9, 6, 3, 14, 27, 843, tzinfo=pytz.UTC) | ||
| params = [DbSqlParameter(name="p", value=date_value, type=DbSqlType.TIMESTAMP)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == date_value | ||
| def test_dbsqlparam_explicit_string(self): | ||
| params = [DbSqlParameter(name="p", value="Hello", type=DbSqlType.STRING)] | ||
| result = self._get_one_result(self.QUERY, params) | ||
| assert result.col == "Hello" |