diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java index 649bb63..3c43a43 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java @@ -92,6 +92,7 @@ private String initFile = null; private String authType = null; private char delimiterForDSV = DEFAULT_DELIMITER_FOR_DSV; + private boolean disableQuotingForSV = false; private Map hiveVariables = new HashMap(); private Map hiveConfVariables = new HashMap(); @@ -511,5 +512,13 @@ public char getDelimiterForDSV() { public void setDelimiterForDSV(char delimiterForDSV) { this.delimiterForDSV = delimiterForDSV; } + + public boolean isDisableQuotingForSV() { + return disableQuotingForSV; + } + + public void setDisableQuotingForSV(boolean disableQuotingForSV) { + this.disableQuotingForSV = disableQuotingForSV; + } } diff --git a/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java b/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java index a2c18c7..acadd6b 100644 --- a/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java +++ b/beeline/src/java/org/apache/hive/beeline/SeparatedValuesOutputFormat.java @@ -37,10 +37,12 @@ * */ private final BeeLine beeLine; + private char separator; private CsvPreference csvPreference; SeparatedValuesOutputFormat(BeeLine beeLine, char separator) { this.beeLine = beeLine; + this.separator = separator; csvPreference = new CsvPreference.Builder('"', separator, "").build(); } @@ -54,6 +56,7 @@ private void updateCsvPreference() { // "" is passed as the end of line symbol in following function, as // beeline itself adds newline csvPreference = new CsvPreference.Builder('"', newDel, "").build(); + separator = newDel; } } } @@ -61,10 +64,14 @@ private void updateCsvPreference() { @Override public int print(Rows rows) { updateCsvPreference(); - + boolean isQuotingDisabled = beeLine.getOpts().isDisableQuotingForSV(); int count = 0; while (rows.hasNext()) { - printRow(rows, (Rows.Row) rows.next()); + if (isQuotingDisabled) { + printRowWithoutQuote((Rows.Row) rows.next()); + } else { + printRowWithQuote((Rows.Row) rows.next()); + } count++; } return count - 1; // sans header row @@ -85,9 +92,19 @@ private String getFormattedStr(String[] vals) { return strWriter.toString(); } - public void printRow(Rows rows, Rows.Row row) { + private void printRowWithQuote(Rows.Row row) { String[] vals = row.values; String formattedStr = getFormattedStr(vals); beeLine.output(formattedStr); } + + private void printRowWithoutQuote(Rows.Row row) { + String[] vals = row.values; + StringBuilder buf = new StringBuilder(); + for (int i = 0; i < vals.length; i++) { + buf.append(buf.length() == 0 ? "" : separator) + .append(vals[i] == null ? "" : vals[i]); + } + beeLine.output(buf.toString()); + } } diff --git a/beeline/src/main/resources/BeeLine.properties b/beeline/src/main/resources/BeeLine.properties index 2e987fd..23d1ac4 100644 --- a/beeline/src/main/resources/BeeLine.properties +++ b/beeline/src/main/resources/BeeLine.properties @@ -70,6 +70,7 @@ help-columns: List all the columns for the specified table help-properties: Connect to the database specified in the properties file(s) help-outputformat: Set the output format for displaying results (table,vertical,csv2,dsv,tsv2,xmlattrs,xmlelements, and deprecated formats(csv, tsv)) help-delimiterForDSV: Set the delimiter for dsv output format +help-disableQuotingForSV: Disable the quoting for the tsv/csv/dsv output format altogether help-nullemptystring: Set to true to get historic behavior of printing null as empty string. Default is false. jline-missing: The JLine jar was not found. Please ensure it is installed. @@ -174,6 +175,7 @@ cmd-usage: Usage: java org.apache.hive.cli.beeline.BeeLine \n \ \ --delimiterForDSV=DELIMITER specify the delimiter for delimiter-separated values output format (default: |)\n \ \ --isolation=LEVEL set the transaction isolation level\n \ \ --nullemptystring=[true/false] set to true to get historic behavior of printing null as empty string\n \ +\ --disableQuotingForSV=[true/false] set to true to disable quotes for TSV, CSV and DSV output format altogether\n \ \ --help display this message diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java index 1f3e484..509caac 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeeLineWithArgs.java @@ -382,6 +382,76 @@ public void testTSVOutput() throws Throwable { testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); } + /** + * Test writing output using new TSV format + */ + @Test + public void testTSV2OutputWithoutQuote() throws Throwable { + String SCRIPT_TEXT = getFormatTestQueryForDisableQuotes(); + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=tsv2"); + argList.add("--disableQuotingForSV=true"); + + final String EXPECTED_PATTERN = "1\tNULL\tdefg\tab\"c\t\"aa\"\t1.0"; + testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } + + /** + * Test writing output using TSV deprecated format + */ + @Test + public void testTSVOutputWithoutQuote() throws Throwable { + String SCRIPT_TEXT = getFormatTestQueryForDisableQuotes(); + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=tsv"); + argList.add("--disableQuotingForSV=true"); + + final String EXPECTED_PATTERN = "'1'\t'NULL'\t'defg'\t'ab\"c'\t'\"aa\"'\t'1.0'"; + testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } + + /** + * Test writing output using new CSV format + */ + @Test + public void testCSV2OutputWithoutQuote() throws Throwable { + String SCRIPT_TEXT = getFormatTestQueryForDisableQuotes(); + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=csv2"); + argList.add("--disableQuotingForSV=true"); + + final String EXPECTED_PATTERN = "1,NULL,defg,ab\"c,\"aa\",1.0"; + testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } + + /** + * Test writing output using CSV deprecated format + */ + @Test + public void testCSVOutputWithoutQuote() throws Throwable { + String SCRIPT_TEXT = getFormatTestQueryForDisableQuotes(); + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=csv"); + argList.add("--disableQuotingForSV=true"); + + final String EXPECTED_PATTERN = "'1','NULL','defg','ab\"c','\"aa\"','1.0'"; + testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } + + /** + * Test writing output using DSV format, with custom delimiter ";" + */ + @Test + public void testDSVOutputWithoutQuote() throws Throwable { + String SCRIPT_TEXT = getFormatTestQueryForDisableQuotes(); + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("--outputformat=dsv"); + argList.add("--delimiterForDSV=;"); + argList.add("--disableQuotingForSV=true"); + + final String EXPECTED_PATTERN = "1;NULL;defg;ab\"c;\"aa\";1.0"; + testScriptFile(SCRIPT_TEXT, EXPECTED_PATTERN, true, argList); + } /** * Test writing output using TSV deprecated format @@ -428,6 +498,12 @@ private String getFormatTestQuery() { return "set hive.support.concurrency = false;\n" + "select 1, null, 'defg', 'ab\"c', 1.0D from " + tableName + " limit 1 ;\n"; } + + private String getFormatTestQueryForDisableQuotes() { + return "set hive.support.concurrency = false;\n" + + "select 1, null, 'defg', 'ab\"c', '\"aa\"', 1.0D from " + tableName + " limit 1 ;\n"; + } + /** * Select null from table , check if setting null to empty string works - Using beeling cmd line * argument.