JDarDagran commented on code in PR #31398: URL: https://github.com/apache/airflow/pull/31398#discussion_r1202553482
########## airflow/providers/openlineage/utils/sqlparser.py: ########## @@ -0,0 +1,263 @@ +# 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 __future__ import annotations + +from typing import TYPE_CHECKING, Callable, Iterable + +from attr import define + +from airflow.providers.openlineage.extractors.base import OperatorLineage +from airflow.providers.openlineage.utils.sql import ( + TablesHierarchy, + create_information_schema_query, + get_table_schemas, +) +from airflow.typing_compat import TypedDict +from openlineage.client.facet import ExtractionError, ExtractionErrorRunFacet, SqlJobFacet +from openlineage.client.run import Dataset +from openlineage.common.sql import DbTableMeta, SqlMeta, parse + +if TYPE_CHECKING: + from airflow.hooks.base import BaseHook + +DEFAULT_NAMESPACE = "default" +DEFAULT_INFORMATION_SCHEMA_COLUMNS = [ + "table_schema", + "table_name", + "column_name", + "ordinal_position", + "udt_name", +] +DEFAULT_INFORMATION_SCHEMA_TABLE_NAME = "information_schema.columns" + + +def default_normalize_name_method(name: str) -> str: + return name.lower() + + +class GetTableSchemasParams(TypedDict): + """get_table_schemas params""" + + normalize_name: Callable[[str], str] + is_cross_db: bool + information_schema_columns: list[str] + information_schema_table: str + is_uppercase_names: bool + allow_trailing_semicolon: bool + database: str | None + + +@define +class DatabaseInfo: + """ + Contains database specific information needed to process + SQL statement parse result. + + :param scheme: Scheme part of URI in OpenLineage namespace. + :param authority: Authority part of URI in OpenLineage namespace. + :param database: Takes precedence over parsed database name. + :param information_schema_columns: List of columns names from information schema table. + :param information_schema_table_name: Information schema table name. + :param is_information_schema_cross_db: Specifies if information schema contains + cross-database data. + :param is_uppercase_names: Specifies if database accepts only uppercase names (e.g. Snowflake). + :param allow_trailing_semicolon: For some databases such as Trino, + trailing semicolon can cause a syntax error. If True it adds semicolon at the end of query. + :param normalize_name_method: Method to normalize database, schema and table names. + Defaults to `name.lower()`. + """ + + scheme: str + authority: str | None = None + database: str | None = None + information_schema_columns: list[str] = DEFAULT_INFORMATION_SCHEMA_COLUMNS + information_schema_table_name: str = DEFAULT_INFORMATION_SCHEMA_TABLE_NAME Review Comment: I'd say both are valuable default values. ########## airflow/providers/openlineage/utils/sqlparser.py: ########## @@ -0,0 +1,263 @@ +# 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 __future__ import annotations + +from typing import TYPE_CHECKING, Callable, Iterable + +from attr import define + +from airflow.providers.openlineage.extractors.base import OperatorLineage +from airflow.providers.openlineage.utils.sql import ( + TablesHierarchy, + create_information_schema_query, + get_table_schemas, +) +from airflow.typing_compat import TypedDict +from openlineage.client.facet import ExtractionError, ExtractionErrorRunFacet, SqlJobFacet +from openlineage.client.run import Dataset +from openlineage.common.sql import DbTableMeta, SqlMeta, parse + +if TYPE_CHECKING: + from airflow.hooks.base import BaseHook + +DEFAULT_NAMESPACE = "default" +DEFAULT_INFORMATION_SCHEMA_COLUMNS = [ + "table_schema", + "table_name", + "column_name", + "ordinal_position", + "udt_name", +] +DEFAULT_INFORMATION_SCHEMA_TABLE_NAME = "information_schema.columns" + + +def default_normalize_name_method(name: str) -> str: + return name.lower() + + +class GetTableSchemasParams(TypedDict): + """get_table_schemas params""" + + normalize_name: Callable[[str], str] + is_cross_db: bool + information_schema_columns: list[str] + information_schema_table: str + is_uppercase_names: bool + allow_trailing_semicolon: bool + database: str | None + + +@define +class DatabaseInfo: + """ + Contains database specific information needed to process + SQL statement parse result. + + :param scheme: Scheme part of URI in OpenLineage namespace. + :param authority: Authority part of URI in OpenLineage namespace. + :param database: Takes precedence over parsed database name. + :param information_schema_columns: List of columns names from information schema table. + :param information_schema_table_name: Information schema table name. + :param is_information_schema_cross_db: Specifies if information schema contains + cross-database data. + :param is_uppercase_names: Specifies if database accepts only uppercase names (e.g. Snowflake). + :param allow_trailing_semicolon: For some databases such as Trino, + trailing semicolon can cause a syntax error. If True it adds semicolon at the end of query. + :param normalize_name_method: Method to normalize database, schema and table names. + Defaults to `name.lower()`. + """ + + scheme: str + authority: str | None = None + database: str | None = None + information_schema_columns: list[str] = DEFAULT_INFORMATION_SCHEMA_COLUMNS + information_schema_table_name: str = DEFAULT_INFORMATION_SCHEMA_TABLE_NAME Review Comment: I'd say both are valuable default values that are common for lots of SQL databases. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
