geido commented on code in PR #20683: URL: https://github.com/apache/superset/pull/20683#discussion_r925518497
########## tests/integration_tests/datasource_tests.py: ########## @@ -414,3 +416,157 @@ def test_get_datasource_invalid_datasource_failed(self): self.login(username="admin") resp = self.get_json_resp("/datasource/get/druid/500000/", raise_on_error=False) self.assertEqual(resp.get("error"), "'druid' is not a valid DatasourceType") + + def test_get_dataset_samples(self): + """ + Dataset API: Test get dataset samples + """ + if backend() == "sqlite": + return + + dataset = SqlaTable( + table_name="virtual_dataset", + sql=("SELECT 'foo' as foo, 'bar' as bar"), + database=get_example_database(), + ) + TableColumn(column_name="foo", type="VARCHAR(255)", table=dataset) + TableColumn(column_name="bar", type="VARCHAR(255)", table=dataset) + SqlMetric(metric_name="count", expression="count(*)", table=dataset) + + self.login(username="admin") + uri = f"datasource/samples?datasource_id={dataset.id}&datasource_type=table" + + # 1. should cache data + # feeds data + self.client.post(uri) + # get from cache + rv = self.client.post(uri) + rv_data = json.loads(rv.data) + assert rv.status_code == 200 + assert "result" in rv_data + assert rv_data["result"]["cached_dttm"] is not None + cache_key1 = rv_data["result"]["cache_key"] + assert QueryCacheManager.has(cache_key1, region=CacheRegion.DATA) + + # 2. should through cache Review Comment: Typo here ########## superset/views/datasource/utils.py: ########## @@ -0,0 +1,113 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from typing import Any, Dict, Optional + +from superset import app, db +from superset.common.chart_data import ChartDataResultType +from superset.common.query_context_factory import QueryContextFactory +from superset.common.utils.query_cache_manager import QueryCacheManager +from superset.constants import CacheRegion +from superset.datasets.commands.exceptions import DatasetSamplesFailedError +from superset.datasource.dao import DatasourceDAO +from superset.utils.core import QueryStatus +from superset.views.datasource.schemas import SamplesPayloadSchema + + +def get_limit_clause(page: Optional[int], per_page: Optional[int]) -> Dict[str, int]: + samples_row_limit = app.config.get("SAMPLES_ROW_LIMIT", 1000) + limit = samples_row_limit + offset = 0 + + if isinstance(page, int) and isinstance(per_page, int): + limit = int(per_page) + if limit < 0 or limit > samples_row_limit: + # reset limit value if input is invalid + limit = samples_row_limit + + offset = max((int(page) - 1) * limit, 0) + + return {"row_offset": offset, "row_limit": limit} + + +def get_samples( # pylint: disable=too-many-arguments,too-many-locals + datasource_type: str, + datasource_id: int, + force: bool = False, + page: int = 1, + per_page: int = 1000, Review Comment: Shouldn't use the `SAMPLES_ROW_LIMIT` here? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org