Return-Path: X-Original-To: apmail-drill-dev-archive@www.apache.org Delivered-To: apmail-drill-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DC55618582 for ; Mon, 16 Nov 2015 21:43:24 +0000 (UTC) Received: (qmail 99055 invoked by uid 500); 16 Nov 2015 21:43:24 -0000 Delivered-To: apmail-drill-dev-archive@drill.apache.org Received: (qmail 99008 invoked by uid 500); 16 Nov 2015 21:43:24 -0000 Mailing-List: contact dev-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@drill.apache.org Delivered-To: mailing list dev@drill.apache.org Received: (qmail 98992 invoked by uid 99); 16 Nov 2015 21:43:23 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Nov 2015 21:43:23 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C2B49E098F; Mon, 16 Nov 2015 21:43:23 +0000 (UTC) From: julienledem To: dev@drill.apache.org Reply-To: dev@drill.apache.org References: In-Reply-To: Subject: [GitHub] drill pull request: DRILL-4047: Select with options Content-Type: text/plain Message-Id: <20151116214323.C2B49E098F@git1-us-west.apache.org> Date: Mon, 16 Nov 2015 21:43:23 +0000 (UTC) Github user julienledem commented on a diff in the pull request: https://github.com/apache/drill/pull/246#discussion_r44987917 --- Diff: exec/java-exec/src/test/java/org/apache/drill/TestSelectWithOption.java --- @@ -0,0 +1,203 @@ +/** + * 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; + +import static java.lang.String.format; +import static org.apache.drill.TestBuilder.listOf; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory; +import org.junit.Ignore; +import org.junit.Test; + +public class TestSelectWithOption extends BaseTestQuery { + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(WorkspaceSchemaFactory.class); + + private File genCSVFile(String name, String... rows) throws IOException { + File file = new File(format("target/%s_%s.csv", this.getClass().getName(), name)); + try (FileWriter fw = new FileWriter(file)) { + for (int i = 0; i < rows.length; i++) { + fw.append(rows[i] + "\n"); + } + } + return file; + } + + private String genCSVTable(String name, String... rows) throws IOException { + File f = genCSVFile(name, rows); + return format("dfs.`${WORKING_PATH}/%s`", f.getPath()); + } + + private void testWithResult(String query, Object... expectedResult) throws Exception { + TestBuilder builder = testBuilder() + .sqlQuery(query) + .ordered() + .baselineColumns("columns"); + for (Object o : expectedResult) { + builder = builder.baselineValues(o); + } + builder.build().run(); + } + + @Test + public void testTextFieldDelimiter() throws Exception { + String tableName = genCSVTable("testTextFieldDelimiter", + "\"b\"|\"0\"", + "\"b\"|\"1\"", + "\"b\"|\"2\""); + + String queryTemplate = + "select columns from table(%s (type => 'TeXT', fieldDelimiter => '%s'))"; + testWithResult(format(queryTemplate, tableName, ","), + listOf("b\"|\"0"), + listOf("b\"|\"1"), + listOf("b\"|\"2") + ); + testWithResult(format(queryTemplate, tableName, "|"), + listOf("b", "0"), + listOf("b", "1"), + listOf("b", "2") + ); + } + + @Test @Ignore // It does not look like lineDelimiter is working + public void testTextLineDelimiter() throws Exception { + String tableName = genCSVTable("testTextLineDelimiter", + "\"b\"|\"0\"", + "\"b\"|\"1\"", + "\"b\"|\"2\""); + + testWithResult(format("select columns from table(%s(type => 'TeXT', lineDelimiter => '|'))", tableName), + listOf("\"b\""), + listOf("\"0\"", "\"b\""), + listOf("\"1\"", "\"b\""), + listOf("\"2\"") + ); + } + + @Test + public void testTextQuote() throws Exception { + String tableName = genCSVTable("testTextQuote", + "\"b\"|\"0\"", + "\"b\"|\"1\"", + "\"b\"|\"2\""); + + testWithResult(format("select columns from table(%s(type => 'TeXT', fieldDelimiter => '|', quote => '@'))", tableName), + listOf("\"b\"", "\"0\""), + listOf("\"b\"", "\"1\""), + listOf("\"b\"", "\"2\"") + ); + + String quoteTableName = genCSVTable("testTextQuote2", + "@b@|@0@", + "@b$@c@|@1@"); + // It seems that a parameter can not be called "escape" + testWithResult(format("select columns from table(%s(`escape` => '$', type => 'TeXT', fieldDelimiter => '|', quote => '@'))", quoteTableName), + listOf("b", "0"), + listOf("b$@c", "1") // shouldn't $ be removed here? + ); + } + + @Test + public void testTextComment() throws Exception { + String commentTableName = genCSVTable("testTextComment", + "b|0", + "@ this is a comment", + "b|1"); + testWithResult(format("select columns from table(%s(type => 'TeXT', fieldDelimiter => '|', comment => '@'))", commentTableName), + listOf("b", "0"), + listOf("b", "1") + ); + } + + @Test + public void testTextHeader() throws Exception { + String headerTableName = genCSVTable("testTextHeader", + "b|a", + "b|0", + "b|1"); + testWithResult(format("select columns from table(%s(type => 'TeXT', fieldDelimiter => '|', skipFirstLine => true))", headerTableName), + listOf("b", "0"), + listOf("b", "1") + ); + + testBuilder() + .sqlQuery(format("select a, b from table(%s(type => 'TeXT', fieldDelimiter => '|', extractHeader => true))", headerTableName)) + .ordered() + .baselineColumns("b", "a") + .baselineValues("b", "0") + .baselineValues("b", "1") + .build().run(); + } + + @Test + public void testVariationsCSV() throws Exception { + String csvTableName = genCSVTable("testVariationsCSV", + "a,b", + "c|d"); + // Using the defaults in TextFormatConfig (the field delimiter is neither "," not "|") + String[] csvQueries = { +// format("select columns from %s ('TeXT')", csvTableName), +// format("select columns from %s('TeXT')", csvTableName), + format("select columns from table(%s ('TeXT'))", csvTableName), --- End diff -- no, but the point was to use a capitalization that is not used anywhere in the code to make sure this was case insensitive. I can vary the spelling but I thought this was sufficient. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---