You are viewing a plain text version of this content. The canonical link for it is here.
Posted to codereview@trafodion.apache.org by DaveBirdsall <gi...@git.apache.org> on 2017/07/06 23:00:30 UTC
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
GitHub user DaveBirdsall opened a pull request:
https://github.com/apache/incubator-trafodion/pull/1169
[TRAFODION-2680] Add CSV_FORMAT function to Trafodion SQL
This change adds a new function, CSV_FORMAT, 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 is a prototype for evaluation. For example, it does not escape double quotes within a double quoted string; the need for that is to be determined. Also, a regression test for this needs to be added.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/DaveBirdsall/incubator-trafodion CsvFormat
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/incubator-trafodion/pull/1169.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #1169
----
commit 3d19d7bd03698cfd758eb0281a963e7b77c1bb7d
Author: Dave Birdsall <db...@apache.org>
Date: 2017-07-06T22:57:28Z
[TRAFODION-2680] Add CSV_FORMAT function to Trafodion SQL
----
---
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.
---
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
Posted by zellerh <gi...@git.apache.org>.
Github user zellerh commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/1169#discussion_r127243745
--- 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 --
One more thought: I think that in a CSV file, white space between fields is ignored. So, it may be best if this code adds a TRIM, so that the padding around fixed chars is not included.
---
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.
---
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
Posted by zellerh <gi...@git.apache.org>.
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?
---
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.
---
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
Posted by zellerh <gi...@git.apache.org>.
Github user zellerh commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/1169#discussion_r127105300
--- 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.
--- End diff --
There is a class ItemExprList that can convert such trees into a collection class. Other binder methods use that to traverse lists.
---
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.
---
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
Posted by DaveBirdsall <gi...@git.apache.org>.
Github user DaveBirdsall closed the pull request at:
https://github.com/apache/incubator-trafodion/pull/1169
---
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.
---
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
Posted by DaveBirdsall <gi...@git.apache.org>.
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.
---
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.
---
[GitHub] incubator-trafodion pull request #1169: [TRAFODION-2680] Add CSV_FORMAT func...
Posted by DaveBirdsall <gi...@git.apache.org>.
Github user DaveBirdsall commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/1169#discussion_r127759569
--- 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.
--- End diff --
Thank you, Hans. Will use that.
---
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.
---