[ 
https://issues.apache.org/jira/browse/DRILL-5919?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16248188#comment-16248188
 ] 

ASF GitHub Bot commented on DRILL-5919:
---------------------------------------

Github user paul-rogers commented on a diff in the pull request:

    https://github.com/apache/drill/pull/1026#discussion_r150361681
  
    --- 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"), "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"), "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"), "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`");
    --- End diff --
    
    Consider referencing the constant for the symbol, rather than the option 
name itself:
    
    ```
    resetSystem(ExecConstants.YOUR_OPTION_NAME);
    ```


> Add non-numeric support for JSON processing
> -------------------------------------------
>
>                 Key: DRILL-5919
>                 URL: https://issues.apache.org/jira/browse/DRILL-5919
>             Project: Apache Drill
>          Issue Type: Improvement
>          Components: Storage - JSON
>    Affects Versions: 1.11.0
>            Reporter: Volodymyr Tkach
>            Assignee: Volodymyr Tkach
>              Labels: doc-impacting
>             Fix For: Future
>
>
> Add session options to allow drill working with non standard json strings 
> number literals like: NaN, Infinity, -Infinity. By default these options will 
> be switched off, the user will be able to toggle them during working session.
> *For documentation*
> 1. Added two session options {{store.json.reader.non_numeric_numbers}} and 
> {{store.json.reader.non_numeric_numbers}} that allow to read/write NaN and 
> Infinity as numbers. By default these options are set to false.
> 2. Extended signature of {{convert_toJSON}} and {{convert_fromJSON}} 
> functions by adding second optional parameter that enables read/write NaN and 
> Infinity.
> For example:
> {noformat}
> select convert_fromJSON('{"key": NaN}') from (values(1)); will result with 
> JsonParseException, but
> select convert_fromJSON('{"key": NaN}', true) from (values(1)); will parse 
> NaN as a number.
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to