diff --git a/include/s3select.h b/include/s3select.h index 6bbd0f14..d560f0c0 100644 --- a/include/s3select.h +++ b/include/s3select.h @@ -3225,10 +3225,6 @@ class json_object : public base_s3object f_push_key_value_into_scratch_area_per_star_operation = [this](s3selectEngine::scratch_area::json_key_value_t& key_value) {return push_key_value_into_scratch_area_per_star_operation(key_value);}; - //setting the container for all json-variables, to be extracted by the json reader - JsonHandler.set_statement_json_variables(query->get_json_variables_access()); - - //calling to getMatchRow. processing a single row per each call. JsonHandler.set_s3select_processing_callback(f_sql); //upon excat match between input-json-key-path and sql-statement-variable-path the callback pushes to scratch area @@ -3270,6 +3266,9 @@ class json_object : public base_s3object } m_sa->set_parquet_type();//TODO json type + + //setting the container for all json-variables, to be extracted by the json reader + JsonHandler.set_statement_json_variables(query->get_json_variables_access()); } json_object(s3select* query):base_s3object(query),m_processed_bytes(0),m_end_of_stream(false),m_row_count(0),star_operation_ind(false),m_init_json_processor_ind(false) diff --git a/test/s3select_test.cpp b/test/s3select_test.cpp index e81ee89b..73e28f35 100644 --- a/test/s3select_test.cpp +++ b/test/s3select_test.cpp @@ -3482,6 +3482,105 @@ input_json_data = R"( } +TEST(TestS3selectFunctions, json_access_array_with_differential_from_clause) +{ +//purpose: to test the json-parser matcher(combination of the from-clause and projection variables) whether it +//access correctly to different key-values in a nested JSON input. +//the tests, run queries with different combinations of from-clause, and key-path. + + +std::string input_json_data = R"( +{ + "root": { + "hello": "world", + "t": "true", + "f": "false", + "n": "null", + "i": 123, + "pi": 3.1416, + "nested_obj": { + "hello2": "world", + "t2": true, + "nested2": { + "c1": "c1_value", + "array_nested2": [ + 10, + 20, + 30, + 40 + ], + "c2": "c2_valuec2_value" + }, + "nested3": { + "hello3": "world", + "t2": true, + "nested4": { + "c1": "c1_value", + "array_nested3": [ + 100, + 200, + 300, + 400 + ] + } + } + }, + "array_1": [ + 1, + 2, + 3, + 4 + ] + } +} +)"; + + std::string expected_result=R"(100,400,null +)"; + std::string input_query = "select _1.array_nested3[0],_1.array_nested3[3],_1.array_nested3[5] from s3object[*].root.nested_obj.nested3.nested4;"; + std::string result; + + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + + expected_result=R"(100,400,null +)"; + input_query = "select _1.nested3.nested4.array_nested3[0], _1.nested3.nested4.array_nested3[3], _1.nested3.nested4.array_nested3[5] from s3object[*].root.nested_obj;"; + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + + expected_result=R"(10,40,null +)"; + input_query = "select _1.array_nested2[0],_1.array_nested2[3],_1.array_nested2[4] from s3object[*].root.nested_obj.nested2;"; + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + + expected_result=R"(10,40,null +)"; + input_query = "select _1.nested_obj.nested2.array_nested2[0],_1.nested_obj.nested2.array_nested2[3],_1.nested_obj.nested2.array_nested2[4] from s3object[*].root;"; + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + + expected_result=R"(10,40,null +)"; + input_query = "select _1.root.nested_obj.nested2.array_nested2[0],_1.root.nested_obj.nested2.array_nested2[3],_1.root.nested_obj.nested2.array_nested2[4] from s3object[*];"; + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + + expected_result=R"(c1_value +)"; + input_query = "select _1.root.nested_obj.nested2.c1 from s3object[*];"; + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + + expected_result=R"(c1_value +)"; + input_query = "select _1.c1 from s3object[*].root.nested_obj.nested2;"; + run_json_query(input_query.c_str(), input_json_data, result); + ASSERT_EQ(result,expected_result); + +} + TEST(TestS3selectFunctions, json_queries_format) { std::string result;