From fc5f028c973f3983351c9914b676916983a371cf Mon Sep 17 00:00:00 2001 From: frank chen Date: Fri, 18 Jun 2021 14:51:00 +0800 Subject: [PATCH] Add more tests to ORC format --- .../example/test_json_path_functions.orc | Bin 0 -> 473 bytes .../druid/data/input/orc/OrcReaderTest.java | 58 +++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 extensions-core/orc-extensions/example/test_json_path_functions.orc diff --git a/extensions-core/orc-extensions/example/test_json_path_functions.orc b/extensions-core/orc-extensions/example/test_json_path_functions.orc new file mode 100644 index 0000000000000000000000000000000000000000..e3916d6a23308afc61f3c1a4573022ea9aab116b GIT binary patch literal 473 zcmeYdau#G@;9?VE;b074Fk@hNJn3mtDg&dUAfKD6o!+!5XJVWc4Kxo59$;XQ1}fy{ zVrF1qfT-36@`WLM9u8(9HV!@kAqj&31{07ekCT!am_erK+jX|3nwP&d3vUQuZ-gR@EpIhRB>n#>juW@i>K zR!r86JO1d5%ijk*NA`T_IpV|60W|Z|3`0N-JIhhsaGcUQPOA>Pw@?{4L;V_4Ym=ntRp9~%iJ2}X`CMgc~Nh6Vv8CI%i2cLVmOFU-vT HLCz8YkPMkW literal 0 HcmV?d00001 diff --git a/extensions-core/orc-extensions/src/test/java/org/apache/druid/data/input/orc/OrcReaderTest.java b/extensions-core/orc-extensions/src/test/java/org/apache/druid/data/input/orc/OrcReaderTest.java index 093ae7ab87e..9b2b8e7e6b1 100644 --- a/extensions-core/orc-extensions/src/test/java/org/apache/druid/data/input/orc/OrcReaderTest.java +++ b/extensions-core/orc-extensions/src/test/java/org/apache/druid/data/input/orc/OrcReaderTest.java @@ -112,7 +112,11 @@ public class OrcReaderTest ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.PATH, "struct_list_struct_int", "$.middle.list[1].int1"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "struct_list_struct_intlist", "$.middle.list[*].int1"), - new JSONPathFieldSpec(JSONPathFieldType.PATH, "struct_list_struct_middleListLength", "$.middle.list.length()"), + new JSONPathFieldSpec( + JSONPathFieldType.PATH, + "struct_list_struct_middleListLength", + "$.middle.list.length()" + ), new JSONPathFieldSpec(JSONPathFieldType.PATH, "list_struct_string", "$.list[0].string1"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "map_struct_int", "$.map.chani.int1") ) @@ -257,6 +261,58 @@ public class OrcReaderTest } } + /** + * schema: struct, ts:timestamp> + * data: {"dim1","[7,8,9]","2000-03-12 15:00:00"} + */ + @Test + public void testJsonPathFunctions() throws IOException + { + final OrcInputFormat inputFormat = new OrcInputFormat( + new JSONPathSpec( + true, + ImmutableList.of( + new JSONPathFieldSpec(JSONPathFieldType.PATH, "min", "$.list.min()"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "max", "$.list.max()"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "avg", "$.list.avg()"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "len", "$.list.length()"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "sum", "$.list.sum()"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "stddev", "$.list.stddev()"), + new JSONPathFieldSpec(JSONPathFieldType.PATH, "append", "$.list.append(10)") + ) + ), + null, + new Configuration() + ); + final InputEntityReader reader = createReader( + new TimestampSpec("ts", "millis", null), + new DimensionsSpec(null), + inputFormat, + "example/test_json_path_functions.orc" + ); + try (CloseableIterator iterator = reader.read()) { + int actualRowCount = 0; + + while (iterator.hasNext()) { + final InputRow row = iterator.next(); + actualRowCount++; + + Assert.assertEquals("7.0", Iterables.getOnlyElement(row.getDimension("min"))); + Assert.assertEquals("8.0", Iterables.getOnlyElement(row.getDimension("avg"))); + Assert.assertEquals("9.0", Iterables.getOnlyElement(row.getDimension("max"))); + Assert.assertEquals("24.0", Iterables.getOnlyElement(row.getDimension("sum"))); + Assert.assertEquals("3", Iterables.getOnlyElement(row.getDimension("len"))); + + //deviation of [7,8,9] is 1/3, stddev is sqrt(1/3), approximately 0.8165 + Assert.assertEquals(0.8165, Double.parseDouble(Iterables.getOnlyElement(row.getDimension("stddev"))), 0.0001); + + //append is not supported + Assert.assertEquals(Collections.emptyList(), row.getDimension("append")); + } + Assert.assertEquals(1, actualRowCount); + } + } + private InputEntityReader createReader( TimestampSpec timestampSpec, DimensionsSpec dimensionsSpec,