[
https://issues.apache.org/jira/browse/TRAFODION-2680?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16090072#comment-16090072
]
ASF GitHub Bot commented on TRAFODION-2680:
-------------------------------------------
Github user DaveBirdsall commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/1169#discussion_r127760681
--- Diff: core/sql/optimizer/BindItemExpr.cpp ---
@@ -10851,6 +10851,96 @@ ItemExpr *ZZZBinderFunction::bindNode(BindWA
*bindWA)
}
break;
+ case ITM_CSV_FORMAT:
+ {
+ bindChildren(bindWA);
+ if (bindWA->errStatus())
+ return this;
+
+ // The way the arguments of CSV_FORMAT are represented in
+ // the parse tree is as a tree of ItemList nodes; so
+ // CSV_FORMAT(a,b,c,d) is represented as
+ //
+ // this
+ // / \
+ // a ItemList
+ // / \
+ // b ItemList
+ // / \
+ // c d
+ //
+ // The code below traverses accordingly.
+
+ ItemExpr * next = child(0)->castToItemExpr();
+ ItemExpr * resultBoundTree = NULL;
+ while (next) // while arguments remain to process
+ {
+ ItemExpr * childk = NULL;
+ if (next->getOperatorType() == ITM_ITEM_LIST)
+ {
+ childk = next->child(0);
+ next = next->child(1);
+ }
+ else
+ {
+ childk = next;
+ next = NULL;
+ }
+
+ const NAType &type = childk->getValueId().getType();
+ switch (type.getTypeQualifier())
+ {
+ case NA_BOOLEAN_TYPE:
+ case NA_DATETIME_TYPE:
+ case NA_INTERVAL_TYPE:
+ case NA_NUMERIC_TYPE:
+ {
+ // TODO: Is VARCHAR(100) big enough? Consider bignums,
for example. We could
+ // probably be smarter about the length. E.g.
VARCHAR(6) is big enough for SMALLINT.
+ strcpy(buf,"CAST(@A1 AS VARCHAR(100))");
+ parseTree = parser.getItemExprTree(buf, strlen(buf),
BINDITEMEXPR_STMTCHARSET, 1, childk);
+ boundTree = parseTree->bindNode(bindWA);
+ if (bindWA->errStatus())
+ return this;
+ break;
+ }
+ case NA_CHARACTER_TYPE:
+ {
+ boundTree = childk;
+ break;
+ }
+ default:
+ {
+ // operand has an unsupported data type
+ *CmpCommon::diags() << DgSqlCode(-4018)
+ << DgString0("CSV_FORMAT")
+ <<
DgString1(type.getTypeName().data());
+ bindWA->setErrStatus();
+ return this;
+ }
+ }
+
+ strcpy(buf,"CASE WHEN POSITION(',' IN @A1) > 0 THEN '\"' ||
@A1 || '\"' ELSE @A1 END");
--- End diff --
Re: booleans and so on. I think you're right. I need to check if we have a
European form of CAST that uses a comma for the decimal point for numbers. Will
look into this.
Re: double quotes: Thanks very much for the tip. I need to do a little
experimentation with Excel spreadsheets to see how they behave with double
quotes. Then I'll make changes accordingly.
Re: white space: I wondered about that. I suspect programs such as Excel do
ignore trailing white space (not sure about leading). Seems like adding TRIM
would be redundant in that case? There is an aesthetic case to be made though
in getting rid of the extra white space in the output.
> CSV_FORMAT function
> -------------------
>
> Key: TRAFODION-2680
> URL: https://issues.apache.org/jira/browse/TRAFODION-2680
> Project: Apache Trafodion
> Issue Type: New Feature
> Components: sql-cmp
> Affects Versions: 2.2-incubating
> Environment: All
> Reporter: David Wayne Birdsall
> Assignee: David Wayne Birdsall
>
> To make it easier to get Trafodion data into spreadsheets, add a CSV_FORMAT
> function to Trafodion SQL.
> Syntax: CSV_FORMAT(argument1, ... ,argumentn)
> Semantics: The result of CSV_FORMAT is a VARCHAR string consisting of a
> comma-separated list of the argument values. If the argument itself contains
> a comma, it is surrounded with double quotes.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)