Skip to content

Commit

Permalink
feat: add connector for CouchbaseDB
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush-couchbase committed Jun 12, 2024
1 parent 684e10d commit 4ee9d0f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions superset/db_engine_specs/couchbase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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 superset.db_engine_specs.base import BaseEngineSpec
from superset.constants import TimeGrain
from datetime import datetime
import sqlparse
from superset.db_engine_specs.base import BaseEngineSpec
from superset.superset_typing import ResultSetColumnType, SQLAColumnType
from flask_babel import gettext as __
from superset.db_engine_specs.base import BaseEngineSpec
from typing import (
Any
)

class CouchbaseEngineSpec(BaseEngineSpec):
engine = 'couchbase'
engine_name = 'couchbase'
allows_subqueries = False
_time_grain_expressions = {
None: "{col}",
TimeGrain.SECOND: "DATE_TRUNC_STR(TOSTRING({col}),'second')",
TimeGrain.MINUTE: "DATE_TRUNC_STR(TOSTRING({col}),'minute')",
TimeGrain.HOUR: "DATE_TRUNC_STR(TOSTRING({col}),'hour')",
TimeGrain.DAY: "DATE_TRUNC_STR(TOSTRING({col}),'day')",
TimeGrain.MONTH: "DATE_TRUNC_STR(TOSTRING({col}),'month')",
TimeGrain.YEAR: "DATE_TRUNC_STR(TOSTRING({col}),'year')",
TimeGrain.QUARTER: "DATE_TRUNC_STR(TOSTRING({col}),'quarter')"
}

@classmethod
def epoch_to_dttm(cls) -> str:
return "{col} * 1000"

Check warning on line 46 in superset/db_engine_specs/couchbase.py

View check run for this annotation

Codecov / codecov/patch

superset/db_engine_specs/couchbase.py#L46

Added line #L46 was not covered by tests

@classmethod
def epoch_ms_to_dttm(cls) -> str:
return "{col}"

Check warning on line 50 in superset/db_engine_specs/couchbase.py

View check run for this annotation

Codecov / codecov/patch

superset/db_engine_specs/couchbase.py#L50

Added line #L50 was not covered by tests

@classmethod
def convert_dttm(
cls, target_type: str, dttm: datetime, db_extra: dict[str, Any] | None = None
) -> str | None:
sqla_type = cls.get_sqla_column_type(target_type)
return f"DATETIME(DATE_FORMAT_STR(STR_TO_UTC('{dttm.date().isoformat()}'), 'iso8601'))"

Check warning on line 57 in superset/db_engine_specs/couchbase.py

View check run for this annotation

Codecov / codecov/patch

superset/db_engine_specs/couchbase.py#L56-L57

Added lines #L56 - L57 were not covered by tests

@classmethod
def parse_sql(cls, sql: str) -> list[str]:
sql.replace("`COUNT(*)`","COUNT(*)")
return [str(s).strip(" ;") for s in sqlparse.parse(sql)]

Check warning on line 62 in superset/db_engine_specs/couchbase.py

View check run for this annotation

Codecov / codecov/patch

superset/db_engine_specs/couchbase.py#L61-L62

Added lines #L61 - L62 were not covered by tests
1 change: 1 addition & 0 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"clickhouse": Dialects.CLICKHOUSE,
"clickhousedb": Dialects.CLICKHOUSE,
"cockroachdb": Dialects.POSTGRES,
"couchbase": Dialects.MYSQL,
# "crate": ???
# "databend": ???
"databricks": Dialects.DATABRICKS,
Expand Down

0 comments on commit 4ee9d0f

Please sign in to comment.