You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by arina-ielchiieva <gi...@git.apache.org> on 2017/11/09 09:35:04 UTC
[GitHub] drill pull request #1026: DRILL-5919: Add non-numeric support for JSON proce...
Github user arina-ielchiieva commented on a diff in the pull request:
https://github.com/apache/drill/pull/1026#discussion_r149903182
--- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/vector/complex/writer/TestJsonNonNumerics.java ---
@@ -0,0 +1,167 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to you 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.
+*/
+
+package org.apache.drill.exec.vector.complex.writer;
+
+import com.google.common.collect.ImmutableMap;
+import org.apache.commons.io.FileUtils;
+import org.apache.drill.BaseTestQuery;
+import org.apache.drill.common.exceptions.UserRemoteException;
+import org.apache.drill.common.expression.SchemaPath;
+import org.apache.drill.exec.record.RecordBatchLoader;
+import org.apache.drill.exec.record.VectorWrapper;
+import org.apache.drill.exec.rpc.user.QueryDataBatch;
+import org.apache.drill.exec.vector.VarCharVector;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.*;
+
+public class TestJsonNonNumerics extends BaseTestQuery {
+
+ @Test
+ public void testNonNumericSelect() throws Exception {
+ File file = new File(getTempDir(""), "nan_test.json");
+ String json = "{\"nan\":NaN, \"inf\":Infinity}";
+ String query = String.format("select * from dfs.`%s`",file.getAbsolutePath());
+ try {
+ FileUtils.writeStringToFile(file, json);
+ test("alter session set `store.json.reader.non_numeric_numbers` = true");
+ testBuilder()
+ .sqlQuery(query)
+ .unOrdered()
+ .baselineColumns("nan", "inf")
+ .baselineValues(Double.NaN, Double.POSITIVE_INFINITY)
+ .build()
+ .run();
+ } finally {
+ test("alter session reset `store.json.reader.non_numeric_numbers`");
+ FileUtils.deleteQuietly(file);
+ }
+ }
+
+ @Test(expected = UserRemoteException.class)
+ public void testNonNumericFailure() throws Exception {
+ File file = new File(getTempDir(""), "nan_test.json");
+ test("alter session set `store.json.reader.non_numeric_numbers` = false");
+ String json = "{\"nan\":NaN, \"inf\":Infinity}";
+ try {
+ FileUtils.writeStringToFile(file, json);
+ test("select * from dfs.`%s`;", file.getAbsolutePath());
+ } catch (UserRemoteException e) {
+ assertThat(e.getMessage(), containsString("Error parsing JSON"));
+ throw e;
+ } finally {
+ test("alter session reset `store.json.reader.non_numeric_numbers`");
+ FileUtils.deleteQuietly(file);
+ }
+ }
+
+ @Test
+ public void testCreateTableNonNumerics() throws Exception {
+ File file = new File(getTempDir(""), "nan_test.json");
+ String json = "{\"nan\":NaN, \"inf\":Infinity}";
+ String tableName = "ctas_test";
+ try {
+ FileUtils.writeStringToFile(file, json);
+ test("alter session set `store.json.reader.non_numeric_numbers` = true");
+ test("alter session set `store.json.writer.non_numeric_numbers` = true");
+ test("alter session set `store.format`='json'");
+ test("create table dfs_test.tmp.`%s` as select * from dfs.`%s`;", tableName, file.getAbsolutePath());
+
+ // ensuring that `NaN` and `Infinity` tokens ARE NOT enclosed with double quotes
+ File resultFile = new File(new File(getDfsTestTmpSchemaLocation(),tableName),"0_0_0.json");
+ String resultJson = FileUtils.readFileToString(resultFile);
+ int nanIndex = resultJson.indexOf("NaN");
+ assertFalse("`NaN` must not be enclosed with \"\" ", resultJson.charAt(nanIndex - 1) == '"');
+ assertFalse("`NaN` must not be enclosed with \"\" ", resultJson.charAt(nanIndex + "NaN".length()) == '"');
+ int infIndex = resultJson.indexOf("Infinity");
+ assertFalse("`Infinity` must not be enclosed with \"\" ", resultJson.charAt(infIndex - 1) == '"');
+ assertFalse("`Infinity` must not be enclosed with \"\" ", resultJson.charAt(infIndex + "Infinity".length()) == '"');
+ } finally {
+ test("drop table if exists dfs_test.tmp.`%s`", tableName);
+ test("alter session reset `store.json.reader.non_numeric_numbers`");
+ test("alter session reset `store.json.writer.non_numeric_numbers`");
+ FileUtils.deleteQuietly(file);
+ }
+ }
+
+ @Test
+ public void testConvertFromJsonFunction() throws Exception {
+ File file = new File(getTempDir(""), "nan_test.csv");
+ String csv = "col_0, {\"nan\":NaN}";
+ FileUtils.writeStringToFile(file, csv);
--- End diff --
Should be in try-catch.
---