[
https://issues.apache.org/jira/browse/TRAFODION-2680?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16084972#comment-16084972
]
ASF GitHub Bot commented on TRAFODION-2680:
-------------------------------------------
Github user zellerh commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/1169#discussion_r127104673
--- 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 --
You mention the current lack of handling of embedded double quotes. Those
could be handled by adding a REPLACE(@A1, '"', '""'). Also, since neither
commas nor double quotes occur in booleans, datetimes, intervals, or numerics,
would it be ok to apply this transformation on character types only?
> 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)