<commit_before>CREATE OR REPLACE PACKAGE BODY json_core IS

    /* 
        Copyright 2018 <NAME>

        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.
    */
    
    TYPE t_json_values IS 
        TABLE OF json_values%ROWTYPE;

    TYPE t_integer_indexed_numbers IS 
        TABLE OF NUMBER 
        INDEX BY PLS_INTEGER;
    
    TYPE t_query_element_cache IS 
        TABLE OF t_query_elements 
        INDEX BY VARCHAR2(32000); 
        
    v_query_element_cache t_query_element_cache;
        
    TYPE t_query_statement_cache IS
        TABLE OF t_query_statement
        INDEX BY VARCHAR2(32000);
        
    v_query_statement_cache t_query_statement_cache;
    
    PROCEDURE register_messages IS
    BEGIN
        default_message_resolver.register_message('JDOC-00001', 'Unexpected character ":1"!');
        default_message_resolver.register_message('JDOC-00002', 'Unexpected end of the input!');
        default_message_resolver.register_message('JDOC-00003', 'Root can''t be modified!');
        default_message_resolver.register_message('JDOC-00004', 'Multiple values found at path :1!');
        default_message_resolver.register_message('JDOC-00005', 'Empty path specified!');
        default_message_resolver.register_message('JDOC-00006', 'Root requested as a property!');
        default_message_resolver.register_message('JDOC-00007', 'No container for property at path :1 could be found!');
        default_message_resolver.register_message('JDOC-00008', 'Scalar values and null can''t have properties!');
        default_message_resolver.register_message('JDOC-00009', 'Value :1 does not exist!');
        default_message_resolver.register_message('JDOC-00011', 'Property ":1" type mismatch!');
        default_message_resolver.register_message('JDOC-00013', 'Invalid array element index :1!');
        default_message_resolver.register_message('JDOC-00014', 'Requested target is not an array!');
        default_message_resolver.register_message('JDOC-00015', 'Unexpected :1 in a non-branching query!');
        default_message_resolver.register_message('JDOC-00016', 'Duplicate property/alias ":1"!');
        default_message_resolver.register_message('JDOC-00017', 'Alias too long!');
        default_message_resolver.register_message('JDOC-00018', 'Property name ":1" is too long to be a column name!');
        default_message_resolver.register_message('JDOC-00019', 'Alias not specified for a leaf wildcard property!');
        default_message_resolver.register_message('JDOC-00020', 'Variable name too long!');
        default_message_resolver.register_message('JDOC-00021', '');
        default_message_resolver.register_message('JDOC-00022', '');
        default_message_resolver.register_message('JDOC-00023', 'Column alias for a wildcard not specified!');
        default_message_resolver.register_message('JDOC-00024', 'Value :1 is locked!');
        default_message_resolver.register_message('JDOC-00025', 'Reserved field reference can''t be optional!');
        default_message_resolver.register_message('JDOC-00026', 'Reserved field reference can''t be branched!');
        default_message_resolver.register_message('JDOC-00027', 'Reserved field reference can''t have child elements!');
        default_message_resolver.register_message('JDOC-00028', 'Reserved field reference can''t be the topmost query element!');
        default_message_resolver.register_message('JDOC-00029', 'The topmost query element can''t be optional!');
    END;
    
    FUNCTION parse_query (
        p_query IN VARCHAR2,
        p_query_type IN PLS_INTEGER := c_VALUE_TABLE_QUERY
    )
    RETURN t_query_elements IS
    
        v_cache_key VARCHAR2(32000);
        v_query_elements t_query_elements;
        
        v_char CHAR;
        v_state VARCHAR2(30);
        
        TYPE t_stack_node IS RECORD (
            element_i PLS_INTEGER
           ,last_child_i PLS_INTEGER 
           ,branching BOOLEAN
        );
        
        TYPE t_stack IS TABLE OF t_stack_node;
        
        v_stack t_stack;
        
        v_value VARCHAR2(4000);
        
        TYPE t_aliases IS TABLE OF BOOLEAN INDEX BY VARCHAR2(30);
        
        v_aliases t_aliases;
        
        PROCEDURE init_stack IS
        BEGIN
        
            v_stack := t_stack();
            
            v_stack.EXTEND(1);
            v_stack(1).branching := FALSE;
            
        END;
        
        PROCEDURE push
            (p_type IN CHAR
            ,p_value IN VARCHAR2 := NULL) IS
            
            v_element_i PLS_INTEGER;
            
        BEGIN
        
            IF v_stack.COUNT > 1 AND v_query_elements(v_stack(v_stack.COUNT).element_i).type = 'F' THEN
                -- Reserved field reference can''t have child elements!
                error$.raise('JDOC-00027');
            END IF;
        
            v_query_elements.EXTEND(1);
            v_element_i := v_query_elements.COUNT;
            
            v_query_elements(v_element_i).type := p_type;
            v_query_elements(v_element_i).value := p_value;
            v_query_elements(v_element_i).optional := FALSE;
        
            IF v_stack(v_stack.COUNT).element_i IS NOT NULL
               AND v_query_elements(v_stack(v_stack.COUNT).element_i).first_child_i IS NULL THEN
               
                v_query_elements(v_stack(v_stack.COUNT).element_i).first_child_i := v_element_i;
               
            END IF;
        
            IF v_stack(v_stack.COUNT).last_child_i IS NOT NULL THEN
            
                v_query_elements(v_stack(v_stack.COUNT).last_child_i).next_sibling_i := v_element_i;
                
            END IF;
            
            v_stack(v_stack.COUNT).last_child_i := v_element_i;
        
            v_stack.EXTEND(1);
            v_stack(v_stack.COUNT).element_i := v_element_i;
            v_stack(v_stack.COUNT).branching := FALSE;
            
        END;
        
        PROCEDURE push_name (
            p_value IN VARCHAR2,
            p_simple IN BOOLEAN := FALSE
        ) IS
        BEGIN
        
            IF p_simple AND p_value = '$' THEN
            
                IF v_stack.COUNT > 1 THEN
                    -- 'Root requested as a property!'
                    error$.raise('JDOC-00006');
                END IF;
                
                push('R');
            
            ELSIF p_value IN ('_id', '_key', '_value') THEN<commit_msg>Restricted the special meaning of the reserved fields only to table queries<commit_after>CREATE OR REPLACE PACKAGE BODY json_core IS

    /* 
        Copyright 2018 <NAME>

        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.
    */
    
    TYPE t_json_values IS 
        TABLE OF json_values%ROWTYPE;

    TYPE t_integer_indexed_numbers IS 
        TABLE OF NUMBER 
        INDEX BY PLS_INTEGER;
    
    TYPE t_query_element_cache IS 
        TABLE OF t_query_elements 
        INDEX BY VARCHAR2(32000); 
        
    v_query_element_cache t_query_element_cache;
        
    TYPE t_query_statement_cache IS
        TABLE OF t_query_statement
        INDEX BY VARCHAR2(32000);
        
    v_query_statement_cache t_query_statement_cache;
    
    PROCEDURE register_messages IS
    BEGIN
        default_message_resolver.register_message('JDOC-00001', 'Unexpected character ":1"!');
        default_message_resolver.register_message('JDOC-00002', 'Unexpected end of the input!');
        default_message_resolver.register_message('JDOC-00003', 'Root can''t be modified!');
        default_message_resolver.register_message('JDOC-00004', 'Multiple values found at path :1!');
        default_message_resolver.register_message('JDOC-00005', 'Empty path specified!');
        default_message_resolver.register_message('JDOC-00006', 'Root requested as a property!');
        default_message_resolver.register_message('JDOC-00007', 'No container for property at path :1 could be found!');
        default_message_resolver.register_message('JDOC-00008', 'Scalar values and null can''t have properties!');
        default_message_resolver.register_message('JDOC-00009', 'Value :1 does not exist!');
        default_message_resolver.register_message('JDOC-00011', 'Property ":1" type mismatch!');
        default_message_resolver.register_message('JDOC-00013', 'Invalid array element index :1!');
        default_message_resolver.register_message('JDOC-00014', 'Requested target is not an array!');
        default_message_resolver.register_message('JDOC-00015', 'Unexpected :1 in a non-branching query!');
        default_message_resolver.register_message('JDOC-00016', 'Duplicate property/alias ":1"!');
        default_message_resolver.register_message('JDOC-00017', 'Alias too long!');
        default_message_resolver.register_message('JDOC-00018', 'Property name ":1" is too long to be a column name!');
        default_message_resolver.register_message('JDOC-00019', 'Alias not specified for a leaf wildcard property!');
        default_message_resolver.register_message('JDOC-00020', 'Variable name too long!');
        default_message_resolver.register_message('JDOC-00021', '');
        default_message_resolver.register_message('JDOC-00022', '');
        default_message_resolver.register_message('JDOC-00023', 'Column alias for a wildcard not specified!');
        default_message_resolver.register_message('JDOC-00024', 'Value :1 is locked!');
        default_message_resolver.register_message('JDOC-00025', 'Reserved field reference can''t be optional!');
        default_message_resolver.register_message('JDOC-00026', 'Reserved field reference can''t be branched!');
        default_message_resolver.register_message('JDOC-00027', 'Reserved field reference can''t have child elements!');
        default_message_resolver.register_message('JDOC-00028', 'Reserved field reference can''t be the topmost query element!');
        default_message_resolver.register_message('JDOC-00029', 'The topmost query element can''t be optional!');
    END;
    
    FUNCTION parse_query (
        p_query IN VARCHAR2,
        p_query_type IN PLS_INTEGER := c_VALUE_TABLE_QUERY
    )
    RETURN t_query_elements IS
    
        v_cache_key VARCHAR2(32000);
        v_query_elements t_query_elements;
        
        v_char CHAR;
        v_state VARCHAR2(30);
        
        TYPE t_stack_node IS RECORD (
            element_i PLS_INTEGER
           ,last_child_i PLS_INTEGER 
           ,branching BOOLEAN
        );
        
        TYPE t_stack IS TABLE OF t_stack_node;
        
        v_stack t_stack;
        
        v_value VARCHAR2(4000);
        
        TYPE t_aliases IS TABLE OF BOOLEAN INDEX BY VARCHAR2(30);
        
        v_aliases t_aliases;
        
        PROCEDURE init_stack IS
        BEGIN
        
            v_stack := t_stack();
            
            v_stack.EXTEND(1);
            v_stack(1).branching := FALSE;
            
        END;
        
        PROCEDURE push
            (p_type IN CHAR
            ,p_value IN VARCHAR2 := NULL) IS
            
            v_element_i PLS_INTEGER;
            
        BEGIN
        
            IF v_stack.COUNT > 1 AND v_query_elements(v_stack(v_stack.COUNT).element_i).type = 'F' THEN
                -- Reserved field reference can''t have child elements!
                error$.raise('JDOC-00027');
            END IF;
        
            v_query_elements.EXTEND(1);
            v_element_i := v_query_elements.COUNT;
            
            v_query_elements(v_element_i).type := p_type;
            v_query_elements(v_element_i).value := p_value;
            v_query_elements(v_element_i).optional := FALSE;
        
            IF v_stack(v_stack.COUNT).element_i IS NOT NULL
               AND v_query_elements(v_stack(v_stack.COUNT).element_i).first_child_i IS NULL THEN
               
                v_query_elements(v_stack(v_stack.COUNT).element_i).first_child_i := v_element_i;
               
            END IF;
        
            IF v_stack(v_stack.COUNT).last_child_i IS NOT NULL THEN
            
                v_query_elements(v_stack(v_stack.COUNT).last_child_i).next_sibling_i := v_element_i;
                
            END IF;
            
            v_stack(v_stack.COUNT).last_child_i := v_element_i;
        
            v_stack.EXTEND(1);
            v_stack(v_stack.COUNT).element_i := v_element_i;
            v_stack(v_stack.COUNT).branching := FALSE;
            
        END;
        
        PROCEDURE push_name (
            p_value IN VARCHAR2,
            p_simple IN BOOLEAN := FALSE
        ) IS
        BEGIN
        
            IF p_simple AND p_value = '$' THEN
            
                IF v_stack.COUNT > 1 THEN
                    -- 'Root requested as a property!'
                    error$.raise('JDOC-00006');
                END IF;
                
                push('R');
            
            ELSIF p_value IN ('_id', '_key', '_value') AND p_query_type IN (c_VALUE_TABLE_QUERY, c_X_VALUE_TABLE_QUERY) THEN
{'fraction_non_alphanumeric': 0.08235628508491437, 'fraction_numerical': 0.028565337881048817, 'mean_word_length': 2.3138686131386863, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1741965', 'n_tokens_mistral': 4382, 'n_tokens_neox': 3998, 'n_words': 953}