[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-17 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/nifi/pull/1983


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-13 Thread ijokarumawak
Github user ijokarumawak commented on a diff in the pull request:

https://github.com/apache/nifi/pull/1983#discussion_r127387210
  
--- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutSQL.java
 ---
@@ -828,10 +835,46 @@ private void setParameter(final PreparedStatement 
stmt, final String attrName, f
 stmt.setBigDecimal(parameterIndex, new 
BigDecimal(parameterValue));
 break;
 case Types.DATE:
-stmt.setDate(parameterIndex, new 
Date(Long.parseLong(parameterValue)));
+Date date;
+
+if (valueFormat.equals("")) {
+if 
(LONG_PATTERN.matcher(parameterValue).matches()) {
+date = new 
Date(Long.parseLong(parameterValue));
+} else {
+String dateFormatString = "-MM-dd";
+
+SimpleDateFormat dateFormat = new 
SimpleDateFormat(dateFormatString);
+java.util.Date parsedDate = 
dateFormat.parse(parameterValue);
+date = new Date(parsedDate.getTime());
+}
+} else {
+final DateTimeFormatter dtFormatter = 
getDateTimeFormatter(valueFormat);
+LocalDate parsedDate = 
LocalDate.parse(parameterValue, dtFormatter);
+date = new 
Date(Date.from(parsedDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()).getTime());
+}
+
+stmt.setDate(parameterIndex, date);
 break;
 case Types.TIME:
-stmt.setTime(parameterIndex, new 
Time(Long.parseLong(parameterValue)));
+Time time;
+
+if (valueFormat.equals("")) {
+if 
(LONG_PATTERN.matcher(parameterValue).matches()) {
+time = new 
Time(Long.parseLong(parameterValue));
+} else {
+String timeFormatString = "HH:mm:ss.SSS";
+
+SimpleDateFormat dateFormat = new 
SimpleDateFormat(timeFormatString);
+java.util.Date parsedDate = 
dateFormat.parse(parameterValue);
+time = new Time(parsedDate.getTime());
+}
+} else {
+final DateTimeFormatter dtFormatter = 
getDateTimeFormatter(valueFormat);
+LocalTime parsedTime = 
LocalTime.parse(parameterValue, dtFormatter);
+time = Time.valueOf(parsedTime);
--- End diff --

Some databases support fractional seconds (milli or microseconds). By 
creating java.sql.Time instance from LocalTime, even if the LocalTime has 
fractional seconds, it will be truncated because Time.valueOf only uses hours, 
minutes and seconds as follows:

```java
java.sql.Time
public static Time valueOf(LocalTime time) {
return new Time(time.getHour(), time.getMinute(), time.getSecond());
}
```

Can we have this like this instead? This way, we can preserve time 
precision at milliseconds if the database driver and database server supports 
it:

```java
final DateTimeFormatter dtFormatter = getDateTimeFormatter(valueFormat);
LocalTime parsedTime = LocalTime.parse(parameterValue, dtFormatter);
LocalDateTime localDateTime = parsedTime.atDate(LocalDate.ofEpochDay(0));
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
time = new Time(instant.toEpochMilli());
```

I confirmed this behavior with MySQL and PostgreSQL. With PostgreSQL, 
before changing this, when I passed "18:25:43.511" JSON value with 
ISO_LOCAL_TIME format, it's stored without milliseconds, "18:25:43". With above 
change, using new Time(Long) constructor, I was able to preserve milliseconds 
"18:25:43.511".

Unfortunately MySQL JDBC Driver has a known issue that it truncates 
milliseconds [76775](https://bugs.mysql.com/bug.php?id=76775), so we can't 
store millisecond with MySQL currently (TIMESTAMP works though).


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-10 Thread ijokarumawak
Github user ijokarumawak commented on a diff in the pull request:

https://github.com/apache/nifi/pull/1983#discussion_r126362972
  
--- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutSQL.java
 ---
@@ -828,10 +833,50 @@ private void setParameter(final PreparedStatement 
stmt, final String attrName, f
 stmt.setBigDecimal(parameterIndex, new 
BigDecimal(parameterValue));
 break;
 case Types.DATE:
-stmt.setDate(parameterIndex, new 
Date(Long.parseLong(parameterValue)));
+Date date;
+
+if (valueFormat.equals("")) {
+if(LONG_PATTERN.matcher(parameterValue).matches()){
+date = new 
Date(Long.parseLong(parameterValue));
+}else {
--- End diff --

We prefer to have a space here, like `} else {`. The existing code for 
TIMESTAMP has the similar code, too. 


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-10 Thread ijokarumawak
Github user ijokarumawak commented on a diff in the pull request:

https://github.com/apache/nifi/pull/1983#discussion_r126362595
  
--- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutSQL.java
 ---
@@ -110,11 +113,13 @@
 + "sql.args.1.value, sql.args.2.value, sql.args.3.value, 
and so on. The type of the sql.args.1.value Parameter is specified by the 
sql.args.1.type attribute."),
 @ReadsAttribute(attribute = "sql.args.N.format", description = 
"This attribute is always optional, but default options may not always work for 
your data. "
 + "Incoming FlowFiles are expected to be parametrized SQL 
statements. In some cases "
-+ "a format option needs to be specified, currently this 
is only applicable for binary data types and timestamps. For binary data types "
-+ "available options are 'ascii', 'base64' and 'hex'.  In 
'ascii' format each string character in your attribute value represents a 
single byte, this is the default format "
-+ "and the format provided by Avro Processors. In 'base64' 
format your string is a Base64 encoded string.  In 'hex' format the string is 
hex encoded with all "
-+ "letters in upper case and no '0x' at the beginning. For 
timestamps, the format can be specified according to 
java.time.format.DateTimeFormatter."
-+ "Customer and named patterns are accepted i.e. 
('-MM-dd','ISO_OFFSET_DATE_TIME')")
++ "a format option needs to be specified, currently this 
is only applicable for binary data types, dates, times and timestamps. Binary 
Data Types (defaults to 'ascii') - "
++ "ascii: each string character in your attribute value 
represents a single byte. This is the format provided by Avro Processors. "
++ "base64: the string is a Base64 encoded string that can 
be decoded to bytes. "
++ "hex: the string is hex encoded with all letters in 
upper case and no '0x' at the beginning. "
++ "Dates/Times/Timestamps - "
++ "Date, Time and Timestamp formats all support both 
custom formats or named format ('-MM-dd','ISO_OFFSET_DATE_TIME') "
++ "as specified according to 
java.time.format.DateTimeFormatter.")
--- End diff --

Nice documentation! How about adding default behavior as well? Such as "If 
not specified, a long value input is expected to be an unix epoch (milli 
seconds from 1970/1/1) or '-MM-dd' format is used."


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-10 Thread ijokarumawak
Github user ijokarumawak commented on a diff in the pull request:

https://github.com/apache/nifi/pull/1983#discussion_r126360491
  
--- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutSQL.java
 ---
@@ -828,10 +833,50 @@ private void setParameter(final PreparedStatement 
stmt, final String attrName, f
 stmt.setBigDecimal(parameterIndex, new 
BigDecimal(parameterValue));
 break;
 case Types.DATE:
-stmt.setDate(parameterIndex, new 
Date(Long.parseLong(parameterValue)));
+Date date;
+
+if (valueFormat.equals("")) {
+if(LONG_PATTERN.matcher(parameterValue).matches()){
+date = new 
Date(Long.parseLong(parameterValue));
+}else {
+String dateFormatString = "-MM-dd";
+if (!valueFormat.isEmpty()) {
--- End diff --

This condition won't be true as it already checks `valueFormat.equals("")` 
above. BTW, I personally prefer `isEmpty()`  than `equals("")`. Existing code 
for TIMESTAMP uses `equals("")` though.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-10 Thread ijokarumawak
Github user ijokarumawak commented on a diff in the pull request:

https://github.com/apache/nifi/pull/1983#discussion_r126361121
  
--- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutSQL.java
 ---
@@ -828,10 +833,50 @@ private void setParameter(final PreparedStatement 
stmt, final String attrName, f
 stmt.setBigDecimal(parameterIndex, new 
BigDecimal(parameterValue));
 break;
 case Types.DATE:
-stmt.setDate(parameterIndex, new 
Date(Long.parseLong(parameterValue)));
+Date date;
+
+if (valueFormat.equals("")) {
+if(LONG_PATTERN.matcher(parameterValue).matches()){
+date = new 
Date(Long.parseLong(parameterValue));
+}else {
+String dateFormatString = "-MM-dd";
+if (!valueFormat.isEmpty()) {
+dateFormatString = valueFormat;
+}
+SimpleDateFormat dateFormat = new 
SimpleDateFormat(dateFormatString);
+java.util.Date parsedDate = 
dateFormat.parse(parameterValue);
+date = new Date(parsedDate.getTime());
+}
+} else {
+final DateTimeFormatter dtFormatter = 
getDateTimeFormatter(valueFormat);
+LocalDate parsedDate = 
LocalDate.parse(parameterValue, dtFormatter);
+date = new 
Date(Date.from(parsedDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()).getTime());
+}
+
+stmt.setDate(parameterIndex, date);
 break;
 case Types.TIME:
-stmt.setTime(parameterIndex, new 
Time(Long.parseLong(parameterValue)));
+Time time;
+
+if (valueFormat.equals("")) {
+if 
(LONG_PATTERN.matcher(parameterValue).matches()) {
+time = new 
Time(Long.parseLong(parameterValue));
+} else {
+String timeFormatString = "HH:mm:ss.SSS";
+if (!valueFormat.isEmpty()) {
--- End diff --

Same as DATE, this won't be true.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-06 Thread yjhyjhyjh0
GitHub user yjhyjhyjh0 reopened a pull request:

https://github.com/apache/nifi/pull/1983

NiFi-2829: Add Date and Time Format Support for PutSQL

Fix unit test for Date and Time type time zone problem while testing PutSQL 
processor

@paulgibeault made the original PR #1073, #1468 
@patricker add support of **DATE** and **TIME** in Epoch format for PutSQL 
processor.
I’ve fix the unit test in different time zone problem.
The detail is list below

The originally problem with unit test happens because of different time 
zone.
Internally without specifying time zone, java.sql.Date and java.sql.Time 
will use local time zone to parse the time.
As a result, different time zone will have different format result for a 
given constant time value.
This is mentioned by @mattyb149 in https://github.com/apache/nifi/pull/1524

Currently solve the problem by giving time zone before insert and parse 
result with same time zone. (GMT)
Currently build and test successfully with NiFi newest version on GitHub 
which is 1.4.0-SNAPSHOT.


### For all changes:
- [x] Is there a JIRA ticket associated with this PR? Is it referenced 
 in the commit message?

- [x] Does your PR title start with NIFI- where  is the JIRA number 
you are trying to resolve? Pay particular attention to the hyphen "-" character.

- [x] Has your PR been rebased against the latest commit within the target 
branch (typically master)?

- [x] Is your initial contribution a single, squashed commit?

### For code changes:
- [ ] Have you ensured that the full suite of tests is executed via mvn 
-Pcontrib-check clean install at the root nifi folder?
- [x] Have you written or updated unit tests to verify your changes?
- [ ] If adding new dependencies to the code, are these dependencies 
licensed in a way that is compatible for inclusion under [ASF 
2.0](http://www.apache.org/legal/resolved.html#category-a)? 
- [ ] If applicable, have you updated the LICENSE file, including the main 
LICENSE file under nifi-assembly?
- [ ] If applicable, have you updated the NOTICE file, including the main 
NOTICE file found under nifi-assembly?
- [ ] If adding new Properties, have you added .displayName in addition to 
.name (programmatic access) for each of the new properties?

### For documentation related changes:
- [ ] Have you ensured that format looks appropriate for the output in 
which it is rendered?

### Note:
Please ensure that once the PR is submitted, you check travis-ci for build 
issues and submit an update to your PR as soon as possible.


You can merge this pull request into a Git repository by running:

$ git pull https://github.com/yjhyjhyjh0/nifi NIFI-2829

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/nifi/pull/1983.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 #1983


commit 1808ddbcfd68f7197446eb566b53076593ebdde5
Author: deonhuang 
Date:   2017-07-03T06:00:22Z

NiFi-2829: Add Date and Time Format Support for PutSQL

Fix unit test for Date and Time type time zone problem while testing PutSQL 
processor




---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-06 Thread yjhyjhyjh0
Github user yjhyjhyjh0 closed the pull request at:

https://github.com/apache/nifi/pull/1983


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---


[GitHub] nifi pull request #1983: NiFi-2829: Add Date and Time Format Support for Put...

2017-07-05 Thread yjhyjhyjh0
GitHub user yjhyjhyjh0 opened a pull request:

https://github.com/apache/nifi/pull/1983

NiFi-2829: Add Date and Time Format Support for PutSQL

Fix unit test for Date and Time type time zone problem while testing PutSQL 
processor

@paulgibeault made the original PR #1073, #1468 
@patricker add support of **DATE** and **TIME** in Epoch format for PutSQL 
processor.
I’ve fix the unit test in different time zone problem.
The detail is list below

The originally problem with unit test happens because of different time 
zone.
Internally without specifying time zone, java.sql.Date and java.sql.Time 
will use local time zone to parse the time.
As a result, different time zone will have different format result for a 
given constant time value.
This is mentioned by @mattyb149 in https://github.com/apache/nifi/pull/1524

Currently solve the problem by giving time zone before insert and parse 
result with same time zone. (GMT)
Currently build and test successfully with NiFi newest version on GitHub 
which is 1.4.0-SNAPSHOT.


### For all changes:
- [x] Is there a JIRA ticket associated with this PR? Is it referenced 
 in the commit message?

- [x] Does your PR title start with NIFI- where  is the JIRA number 
you are trying to resolve? Pay particular attention to the hyphen "-" character.

- [x] Has your PR been rebased against the latest commit within the target 
branch (typically master)?

- [x] Is your initial contribution a single, squashed commit?

### For code changes:
- [ ] Have you ensured that the full suite of tests is executed via mvn 
-Pcontrib-check clean install at the root nifi folder?
- [x] Have you written or updated unit tests to verify your changes?
- [ ] If adding new dependencies to the code, are these dependencies 
licensed in a way that is compatible for inclusion under [ASF 
2.0](http://www.apache.org/legal/resolved.html#category-a)? 
- [ ] If applicable, have you updated the LICENSE file, including the main 
LICENSE file under nifi-assembly?
- [ ] If applicable, have you updated the NOTICE file, including the main 
NOTICE file found under nifi-assembly?
- [ ] If adding new Properties, have you added .displayName in addition to 
.name (programmatic access) for each of the new properties?

### For documentation related changes:
- [ ] Have you ensured that format looks appropriate for the output in 
which it is rendered?

### Note:
Please ensure that once the PR is submitted, you check travis-ci for build 
issues and submit an update to your PR as soon as possible.


You can merge this pull request into a Git repository by running:

$ git pull https://github.com/yjhyjhyjh0/nifi NIFI-2829

Alternatively you can review and apply these changes as the patch at:

https://github.com/apache/nifi/pull/1983.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 #1983


commit c8e916df1ba39e6be52ae9274804740eba1d4df4
Author: deonhuang 
Date:   2017-07-03T06:00:22Z

NiFi-2829: Add Date and Time Format Support for PutSQL

Fix unit test for Date and Time type time zone problem while testing PutSQL 
processor




---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---