Ejemplo n.º 1
0
 def analyze(self, stmt: Statement) -> LineageResult:
     if stmt.get_type() == "DROP":
         self._extract_from_DDL_DROP(stmt)
     elif stmt.get_type() == "ALTER":
         self._extract_from_DDL_ALTER(stmt)
     elif (stmt.get_type() == "DELETE"
           or stmt.token_first(skip_cm=True).normalized == "TRUNCATE"
           or stmt.token_first(skip_cm=True).normalized.upper() == "REFRESH"
           or stmt.token_first(skip_cm=True).normalized == "CACHE"):
         pass
     else:
         # DML parsing logic also applies to CREATE DDL
         self._extract_from_DML(stmt)
     return self._lineage_result
Ejemplo n.º 2
0
    def analyze(self, stmt: Statement) -> LineageResult:
        """
        to analyze the Statement and store the result into :class:`LineageResult`.

        :param stmt: a SQL statement parsed by `sqlparse`
        """
        if stmt.get_type() == "DROP":
            self._extract_from_ddl_drop(stmt)
        elif stmt.get_type() == "ALTER":
            self._extract_from_ddl_alter(stmt)
        elif (stmt.get_type() == "DELETE"
              or stmt.token_first(skip_cm=True).normalized == "TRUNCATE"
              or stmt.token_first(skip_cm=True).normalized.upper() == "REFRESH"
              or stmt.token_first(skip_cm=True).normalized == "CACHE"):
            pass
        else:
            # DML parsing logic also applies to CREATE DDL
            self._extract_from_dml(stmt)
        return self._lineage_result
Ejemplo n.º 3
0
    def from_statement(cls, statement: token_groups.Statement) -> SQLQuery:
        """Extract an SQLQuery object from an SQL statement token.

        Params:
        -------
        statement: SQL token that contains the entire query.

        Returns:
        --------
        A new SQLQuery object.
        """
        first_token = statement.token_first(skip_cm=True, skip_ws=True)
        tables = cls._collect_tables(statement)

        if first_token.match(token_types.DML, "SELECT"):
            sql_instance = cls._build_select_query(statement, tables)

        if first_token.match(token_types.DML, "UPDATE"):
            assert len(tables) == 1
            table = tables[0]
            sql_instance = cls._build_update_query(statement, table)

        if first_token.match(token_types.DML, "INSERT"):
            assert len(tables) == 1
            table = tables[0]
            sql_instance = cls._build_insert_query(statement, table)

        if first_token.match(token_types.DML, "DELETE"):
            assert len(tables) == 1
            table = tables[0]
            sql_instance = cls._build_delete_query(statement, table)

        if sql_instance is None:
            raise exceptions.NotSupportedError(
                f"Unsupported query type {first_token}")

        _, where_group = statement.token_next_by(i=(token_groups.Where))
        filter_groups = sql_table.FilterGroup.from_where_group(where_group)
        for filter_group in filter_groups:
            sql_instance.add_filter_group(filter_group)

        return sql_instance