-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add date and timestamp support to to_json [databricks] #9600
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0989865
Add date and timestamp support to to_json
andygrove d4541b5
fall back to CPU for non-default date and timestamp format options
andygrove 9e982b3
add link to follow on issue for supporting custom formats
andygrove e7a1c13
update comment
andygrove 541c6f7
remove blank line
andygrove 09bdcaf
add support for Etc/UTC timezone
andygrove a14c5bf
generate docs
andygrove 3ffa025
upmerge
andygrove 08e3101
Merge remote-tracking branch 'nvidia/branch-23.12' into to_json_dates
andygrove ca8c926
Remove redundant cast
andygrove 02db97d
upmerge
andygrove 40f1085
fix merge conflict
andygrove ead3fd9
fix merge conflict
andygrove b69f257
fix merge conflict
andygrove 55ae3f1
fix merge conflict
andygrove fb6f8a9
Merge branch 'branch-23.12' into to_json_dates
andygrove f4bddca
Use parsed JSONOptions
andygrove cd8bc7a
use timestampFormatInWrite not InRead and add fallback tests
andygrove 881b75f
shims
andygrove 2c3c4ec
more tests
andygrove 9fb7166
update test
andygrove db9582b
move UTC timezone list to GpuOverrides
andygrove 95ccce3
upmerge
andygrove 635b3ef
upmerge
andygrove c4440ed
fix merge conflict
andygrove 13eb70c
remove hard-coded list of timezones
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -784,8 +784,8 @@ def test_read_case_col_name(spark_tmp_path, v1_enabled_list, col_name): | |
long_gen, | ||
pytest.param(float_gen, marks=pytest.mark.xfail(reason='https://github.com/NVIDIA/spark-rapids/issues/9350')), | ||
pytest.param(double_gen, marks=pytest.mark.xfail(reason='https://github.com/NVIDIA/spark-rapids/issues/9350')), | ||
pytest.param(date_gen, marks=pytest.mark.xfail(reason='https://github.com/NVIDIA/spark-rapids/issues/9515')), | ||
pytest.param(timestamp_gen, marks=pytest.mark.xfail(reason='https://github.com/NVIDIA/spark-rapids/issues/9515')), | ||
date_gen, | ||
timestamp_gen, | ||
StringGen('[A-Za-z0-9\r\n\'"\\\\]{0,10}', nullable=True) \ | ||
.with_special_case('\u1f600') \ | ||
.with_special_case('"a"') \ | ||
|
@@ -800,8 +800,13 @@ def test_read_case_col_name(spark_tmp_path, v1_enabled_list, col_name): | |
pytest.param(True, marks=pytest.mark.xfail(reason='https://github.com/NVIDIA/spark-rapids/issues/9517')), | ||
False | ||
]) | ||
@pytest.mark.parametrize('timezone', [ | ||
'UTC', | ||
'Etc/UTC', | ||
pytest.param('UTC+07:00', marks=pytest.mark.allow_non_gpu('ProjectExec')), | ||
]) | ||
@pytest.mark.xfail(condition = is_not_utc(), reason = 'xfail non-UTC time zone tests because of https://github.com/NVIDIA/spark-rapids/issues/9653') | ||
def test_structs_to_json(spark_tmp_path, data_gen, ignore_null_fields, pretty): | ||
def test_structs_to_json(spark_tmp_path, data_gen, ignore_null_fields, pretty, timezone): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a fallback test for non-utc? |
||
struct_gen = StructGen([ | ||
('a', data_gen), | ||
("b", StructGen([('child', data_gen)], nullable=True)), | ||
|
@@ -813,7 +818,8 @@ def test_structs_to_json(spark_tmp_path, data_gen, ignore_null_fields, pretty): | |
gen = StructGen([('my_struct', struct_gen)], nullable=False) | ||
|
||
options = { 'ignoreNullFields': ignore_null_fields, | ||
'pretty': pretty } | ||
'pretty': pretty, | ||
'timeZone': timezone} | ||
|
||
def struct_to_json(spark): | ||
df = gen_df(spark, gen) | ||
|
@@ -825,3 +831,141 @@ def struct_to_json(spark): | |
assert_gpu_and_cpu_are_equal_collect( | ||
lambda spark : struct_to_json(spark), | ||
conf=conf) | ||
|
||
@pytest.mark.parametrize('data_gen', [timestamp_gen], ids=idfn) | ||
@pytest.mark.parametrize('timestamp_format', [ | ||
'yyyy-MM-dd\'T\'HH:mm:ss[.SSS][XXX]', | ||
pytest.param('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX', marks=pytest.mark.allow_non_gpu('ProjectExec')), | ||
pytest.param('dd/MM/yyyy\'T\'HH:mm:ss[.SSS][XXX]', marks=pytest.mark.allow_non_gpu('ProjectExec')), | ||
]) | ||
@pytest.mark.parametrize('timezone', [ | ||
'UTC', | ||
'Etc/UTC', | ||
pytest.param('UTC+07:00', marks=pytest.mark.allow_non_gpu('ProjectExec')), | ||
]) | ||
def test_structs_to_json_timestamp(spark_tmp_path, data_gen, timestamp_format, timezone): | ||
struct_gen = StructGen([ | ||
("b", StructGen([('child', data_gen)], nullable=True)), | ||
], nullable=False) | ||
gen = StructGen([('my_struct', struct_gen)], nullable=False) | ||
|
||
options = { 'timestampFormat': timestamp_format, | ||
'timeZone': timezone} | ||
|
||
def struct_to_json(spark): | ||
df = gen_df(spark, gen) | ||
return df.withColumn("my_json", f.to_json("my_struct", options)) | ||
|
||
conf = copy_and_update(_enable_all_types_conf, | ||
{ 'spark.rapids.sql.expression.StructsToJson': True }) | ||
|
||
assert_gpu_and_cpu_are_equal_collect( | ||
lambda spark : struct_to_json(spark), | ||
conf=conf) | ||
|
||
@allow_non_gpu('ProjectExec') | ||
@pytest.mark.parametrize('data_gen', [timestamp_gen], ids=idfn) | ||
@pytest.mark.parametrize('timezone', ['UTC+07:00']) | ||
def test_structs_to_json_fallback_timezone(spark_tmp_path, data_gen, timezone): | ||
struct_gen = StructGen([ | ||
('a', data_gen), | ||
("b", StructGen([('child', data_gen)], nullable=True)), | ||
("c", ArrayGen(StructGen([('child', data_gen)], nullable=True))), | ||
("d", MapGen(LongGen(nullable=False), data_gen)), | ||
("d", MapGen(StringGen('[A-Za-z0-9]{0,10}', nullable=False), data_gen)), | ||
("e", ArrayGen(MapGen(LongGen(nullable=False), data_gen), nullable=True)), | ||
], nullable=False) | ||
gen = StructGen([('my_struct', struct_gen)], nullable=False) | ||
|
||
options = { 'timeZone': timezone } | ||
|
||
def struct_to_json(spark): | ||
df = gen_df(spark, gen) | ||
return df.withColumn("my_json", f.to_json("my_struct", options)).drop("my_struct") | ||
|
||
conf = copy_and_update(_enable_all_types_conf, | ||
{ 'spark.rapids.sql.expression.StructsToJson': True }) | ||
|
||
assert_gpu_fallback_collect( | ||
lambda spark : struct_to_json(spark), | ||
'ProjectExec', | ||
conf=conf) | ||
|
||
@allow_non_gpu('ProjectExec') | ||
@pytest.mark.parametrize('data_gen', [date_gen, timestamp_gen], ids=idfn) | ||
def test_structs_to_json_fallback_legacy(spark_tmp_path, data_gen): | ||
struct_gen = StructGen([ | ||
("a", StructGen([('child', data_gen)], nullable=True)), | ||
], nullable=False) | ||
gen = StructGen([('my_struct', struct_gen)], nullable=False) | ||
|
||
def struct_to_json(spark): | ||
df = gen_df(spark, gen) | ||
return df.withColumn("my_json", f.to_json("my_struct")).drop("my_struct") | ||
|
||
conf = copy_and_update(_enable_all_types_conf, | ||
{ 'spark.rapids.sql.expression.StructsToJson': True, | ||
'spark.sql.legacy.timeParserPolicy': 'LEGACY'}) | ||
|
||
assert_gpu_fallback_collect( | ||
lambda spark : struct_to_json(spark), | ||
'ProjectExec', | ||
conf=conf) | ||
|
||
@allow_non_gpu('ProjectExec') | ||
@pytest.mark.parametrize('data_gen', [date_gen], ids=idfn) | ||
@pytest.mark.parametrize('timezone', ['UTC']) | ||
@pytest.mark.parametrize('date_format', [ | ||
'yyyy-dd-MM', | ||
'dd/MM/yyyy', | ||
]) | ||
def test_structs_to_json_fallback_date_formats(spark_tmp_path, data_gen, timezone, date_format): | ||
struct_gen = StructGen([ | ||
('a', data_gen), | ||
("b", StructGen([('child', data_gen)], nullable=True)), | ||
], nullable=False) | ||
gen = StructGen([('my_struct', struct_gen)], nullable=False) | ||
|
||
options = { 'timeZone': timezone, | ||
'dateFormat': date_format } | ||
|
||
def struct_to_json(spark): | ||
df = gen_df(spark, gen) | ||
return df.withColumn("my_json", f.to_json("my_struct", options)).drop("my_struct") | ||
|
||
conf = copy_and_update(_enable_all_types_conf, | ||
{ 'spark.rapids.sql.expression.StructsToJson': True }) | ||
|
||
assert_gpu_fallback_collect( | ||
lambda spark : struct_to_json(spark), | ||
'ProjectExec', | ||
conf=conf) | ||
|
||
@allow_non_gpu('ProjectExec') | ||
@pytest.mark.parametrize('data_gen', [timestamp_gen], ids=idfn) | ||
@pytest.mark.parametrize('timezone', ['UTC']) | ||
@pytest.mark.parametrize('timestamp_format', [ | ||
'yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX', | ||
'dd/MM/yyyy\'T\'HH:mm:ss[.SSS][XXX]', | ||
]) | ||
def test_structs_to_json_fallback_date_formats(spark_tmp_path, data_gen, timezone, timestamp_format): | ||
struct_gen = StructGen([ | ||
('a', data_gen), | ||
("b", StructGen([('child', data_gen)], nullable=True)), | ||
], nullable=False) | ||
gen = StructGen([('my_struct', struct_gen)], nullable=False) | ||
|
||
options = { 'timeZone': timezone, | ||
'timestampFormat': timestamp_format } | ||
|
||
def struct_to_json(spark): | ||
df = gen_df(spark, gen) | ||
return df.withColumn("my_json", f.to_json("my_struct", options)).drop("my_struct") | ||
|
||
conf = copy_and_update(_enable_all_types_conf, | ||
{ 'spark.rapids.sql.expression.StructsToJson': True }) | ||
|
||
assert_gpu_fallback_collect( | ||
lambda spark : struct_to_json(spark), | ||
'ProjectExec', | ||
conf=conf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,64 @@ | |
package org.apache.spark.sql.rapids | ||
|
||
import ai.rapids.cudf.ColumnVector | ||
import com.nvidia.spark.rapids.{CastOptions, GpuCast, GpuColumnVector, GpuUnaryExpression} | ||
import com.nvidia.spark.rapids.{CastOptions, DataFromReplacementRule, GpuCast, GpuColumnVector, GpuExpression, GpuUnaryExpression, RapidsConf, RapidsMeta, UnaryExprMeta} | ||
import com.nvidia.spark.rapids.GpuOverrides | ||
import com.nvidia.spark.rapids.shims.LegacyBehaviorPolicyShim | ||
|
||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, StructsToJson} | ||
import org.apache.spark.sql.catalyst.json.GpuJsonUtils | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.{DataType, StringType, StructType} | ||
import org.apache.spark.sql.rapids.execution.TrampolineUtil | ||
import org.apache.spark.sql.types.{DataType, DateType, StringType, StructType, TimestampType} | ||
|
||
class GpuStructsToJsonMeta( | ||
expr: StructsToJson, | ||
conf: RapidsConf, | ||
parent: Option[RapidsMeta[_, _, _]], | ||
rule: DataFromReplacementRule | ||
) extends UnaryExprMeta[StructsToJson](expr, conf, parent, rule) { | ||
|
||
override def tagExprForGpu(): Unit = { | ||
if (expr.options.get("pretty").exists(_.equalsIgnoreCase("true"))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI. #9719 touches a little bit of timezone check. given |
||
willNotWorkOnGpu("to_json option pretty=true is not supported") | ||
} | ||
val options = GpuJsonUtils.parseJSONOptions(expr.options) | ||
val hasDates = TrampolineUtil.dataTypeExistsRecursively(expr.child.dataType, | ||
_.isInstanceOf[DateType]) | ||
if (hasDates) { | ||
GpuJsonUtils.dateFormatInWrite(options) match { | ||
case "yyyy-MM-dd" => | ||
case dateFormat => | ||
// we can likely support other formats but we would need to add tests | ||
// tracking issue is https://github.com/NVIDIA/spark-rapids/issues/9602 | ||
willNotWorkOnGpu(s"Unsupported dateFormat '$dateFormat' in to_json") | ||
} | ||
} | ||
val hasTimestamps = TrampolineUtil.dataTypeExistsRecursively(expr.child.dataType, | ||
_.isInstanceOf[TimestampType]) | ||
if (hasTimestamps) { | ||
GpuJsonUtils.timestampFormatInWrite(options) match { | ||
case "yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX]" => | ||
case timestampFormat => | ||
// we can likely support other formats but we would need to add tests | ||
// tracking issue is https://github.com/NVIDIA/spark-rapids/issues/9602 | ||
willNotWorkOnGpu(s"Unsupported timestampFormat '$timestampFormat' in to_json") | ||
} | ||
if (options.zoneId.normalized() != GpuOverrides.UTC_TIMEZONE_ID) { | ||
// we hard-code the timezone `Z` in GpuCast.castTimestampToJson | ||
// so we need to fall back if expr different timeZone is specified | ||
willNotWorkOnGpu(s"Unsupported timeZone '${options.zoneId}' in to_json") | ||
} | ||
} | ||
|
||
if (LegacyBehaviorPolicyShim.isLegacyTimeParserPolicy) { | ||
willNotWorkOnGpu("LEGACY timeParserPolicy is not supported in GpuJsonToStructs") | ||
} | ||
} | ||
|
||
override def convertToGpu(child: Expression): GpuExpression = | ||
GpuStructsToJson(expr.options, child, expr.timeZoneId) | ||
} | ||
|
||
case class GpuStructsToJson( | ||
options: Map[String, String], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some fallback tests for the options timestampFormat, dateFormat, and if "spark.sql.legacy.timeParserPolicy" is set to LEGACY? At least from the code all of these impact the format that is used to write the JSON output.