<commit_before>CREATE OR REPLACE PACKAGE BODY parse_util IS
   /*
   * Copyright 2011-2017 <NAME> <<EMAIL>>
   *
   * Licensed 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.
   */

   --
   -- parse_query
   --
   FUNCTION parse_query(
      in_parse_user IN VARCHAR2, 
      in_query      IN CLOB
   ) RETURN xmltype IS
      l_clob    CLOB;
      l_xml     xmltype;
   BEGIN
      IF in_query IS NOT NULL AND sys.dbms_lob.getlength(in_query) > 0 THEN
         sys.dbms_lob.createtemporary(l_clob, TRUE);
         
         -- parse query and get XML as CLOB
         -- parsing user must have access to objects in query
         sys.utl_xml.parsequery(in_parse_user, in_query, l_clob);
   
         -- create XMLTYPE from CLOB
         IF sys.dbms_lob.getlength(l_clob) > 0 THEN
            -- parse successful, calling user has rights to access underlying objects
            l_xml := sys.xmltype.createxml(l_clob);
         END IF;
         sys.dbms_lob.freetemporary(l_clob);
      END IF;
      RETURN l_xml;
   END parse_query;
   
   --
   -- get_insert_targets
   --
   FUNCTION get_insert_targets(
      in_parse_user IN VARCHAR2, 
      in_sql        IN CLOB
   ) RETURN t_obj_type
   IS
      t_obj t_obj_type := t_obj_type();
      l_xml xmltype;
   BEGIN
      IF upper(substr(ltrim(in_sql),1,6)) = 'INSERT' THEN
         l_xml := parse_query(in_parse_user => in_parse_user, in_query => in_sql);
         <<targets>>
         FOR r_tar IN (
            SELECT schema_name,
                   table_name
              FROM xmltable(q'{
                                 for $tar in /QUERY/FROM/FROM_ITEM
                                 return
                                    <target>
                                       <schemaName>{$tar/SCHEMA/text()}</schemaName>
                                       <tableName>{$tar/TABLE/text()}</tableName>
                                    </target>
                              }'
                      PASSING l_xml
                      COLUMNS schema_name VARCHAR2(128 CHAR) PATH '/target/schemaName',
                              table_name  VARCHAR2(128 CHAR) PATH '/target/tableName'
                   )
         ) LOOP
            t_obj.extend;
            t_obj(t_obj.count) := obj_type(
                                     r_tar.schema_name,
                                     NULL,
                                     r_tar.table_name
                                  );
         END LOOP targets;
      END IF;
      RETURN t_obj;
   END get_insert_targets;
   
   --
   -- get_insert_subquery
   --
   FUNCTION get_insert_subquery(in_sql IN CLOB) RETURN CLOB IS
      l_sql CLOB;
   BEGIN
      -- look for WITH...
      l_sql := regexp_substr(in_sql, '(\t|\n|\r|\ )+WITH(\t|\n|\r|\ )+(.)+',1,1,'i');
      IF l_sql IS NULL OR sys.dbms_lob.getlength(l_sql) = 0 THEN
         -- look for SELECT...
         l_sql := regexp_substr(in_sql, '(\t|\n|\r|\ )+SELECT(\t|\n|\r|\ )+(.)+',1,1,'i');
         IF l_sql IS NULL OR sys.dbms_lob.getlength(l_sql) = 0 THEN
            -- look for (SELECT...
            l_sql := regexp_substr(in_sql, '(\t|\n|\r|\ |\()+SELECT(\t|\n|\r|\ )+(.)+',1,1,'i');
         END IF;
      END IF;
      -- TODO: remove error_logging_clause
      RETURN l_sql;
   END get_insert_subquery;
   
   --
   -- get_dep_cols
   --
   FUNCTION get_dep_cols(
      in_parse_tree IN XMLTYPE,
      in_column_pos IN INTEGER<commit_msg>handle error_logging_clause in get_insert_subquery<commit_after>CREATE OR REPLACE PACKAGE BODY parse_util IS
   /*
   * Copyright 2011-2017 <NAME> <<EMAIL>>
   *
   * Licensed 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.
   */

   --
   -- parse_query
   --
   FUNCTION parse_query(
      in_parse_user IN VARCHAR2, 
      in_query      IN CLOB
   ) RETURN xmltype IS
      l_clob    CLOB;
      l_xml     xmltype;
   BEGIN
      IF in_query IS NOT NULL AND sys.dbms_lob.getlength(in_query) > 0 THEN
         sys.dbms_lob.createtemporary(l_clob, TRUE);
         
         -- parse query and get XML as CLOB
         -- parsing user must have access to objects in query
         sys.utl_xml.parsequery(in_parse_user, in_query, l_clob);
   
         -- create XMLTYPE from CLOB
         IF sys.dbms_lob.getlength(l_clob) > 0 THEN
            -- parse successful, calling user has rights to access underlying objects
            l_xml := sys.xmltype.createxml(l_clob);
         END IF;
         sys.dbms_lob.freetemporary(l_clob);
      END IF;
      RETURN l_xml;
   END parse_query;
   
   --
   -- get_insert_targets
   --
   FUNCTION get_insert_targets(
      in_parse_user IN VARCHAR2, 
      in_sql        IN CLOB
   ) RETURN t_obj_type
   IS
      t_obj t_obj_type := t_obj_type();
      l_xml xmltype;
   BEGIN
      IF upper(substr(ltrim(in_sql),1,6)) = 'INSERT' THEN
         l_xml := parse_query(in_parse_user => in_parse_user, in_query => in_sql);
         <<targets>>
         FOR r_tar IN (
            SELECT schema_name,
                   table_name
              FROM xmltable(q'{
                                 for $tar in /QUERY/FROM/FROM_ITEM
                                 return
                                    <target>
                                       <schemaName>{$tar/SCHEMA/text()}</schemaName>
                                       <tableName>{$tar/TABLE/text()}</tableName>
                                    </target>
                              }'
                      PASSING l_xml
                      COLUMNS schema_name VARCHAR2(128 CHAR) PATH '/target/schemaName',
                              table_name  VARCHAR2(128 CHAR) PATH '/target/tableName'
                   )
         ) LOOP
            t_obj.extend;
            t_obj(t_obj.count) := obj_type(
                                     r_tar.schema_name,
                                     NULL,
                                     r_tar.table_name
                                  );
         END LOOP targets;
      END IF;
      RETURN t_obj;
   END get_insert_targets;
   
   --
   -- get_insert_subquery
   --
   FUNCTION get_insert_subquery(in_sql IN CLOB) RETURN CLOB IS
      l_sql CLOB;
   BEGIN
      -- look for WITH...
      l_sql := regexp_substr(in_sql, '(\t|\n|\r|\ )+WITH(\t|\n|\r|\ )+(.)+', 1, 1, 'i');
      IF l_sql IS NULL OR sys.dbms_lob.getlength(l_sql) = 0 THEN
         -- look for SELECT...
         l_sql := regexp_substr(in_sql, '(\t|\n|\r|\ )+SELECT(\t|\n|\r|\ )+(.)+', 1, 1, 'i');
         IF l_sql IS NULL OR sys.dbms_lob.getlength(l_sql) = 0 THEN
            -- look for (SELECT...
            l_sql := regexp_substr(in_sql, '(\t|\n|\r|\ |\()+SELECT(\t|\n|\r|\ )+(.)+', 1, 1, 'i');
         END IF;
      END IF;
      -- remove error_logging_clause
      l_sql := regexp_replace(l_sql, '(.+)(LOG)(\t|\n|\r|\ )(ERRORS.+)', '\1', 1, 1, 'i');
      RETURN l_sql;
   END get_insert_subquery;
   
   --
   -- get_dep_cols
   --
   FUNCTION get_dep_cols(
      in_parse_tree IN XMLTYPE,
      in_column_pos IN INTEGER
{'fraction_non_alphanumeric': 0.10083509909011591, 'fraction_numerical': 0.008849557522123894, 'mean_word_length': 1.5505403687221868, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3613657', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2462, 'n_words': 635}