Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

apache spark - Count of all dates in an array which are present between two other columns

My JSON looks like this

{"array":["2021-01-17","2021-01-25"], "beginDate": "2021-01-01", "endDate": "2021-01-20"}

I want to find the count of all dates in the array which are present between beginDate and endDate.

I was trying to create a sequence of dates between begin and end date and doing an array_intersect. Similar to this:

select size(array_intersect(array("2021-12-02","2021-12-05","2021-12-10"),(sequence(to_date("2021-12-01"), date_add(to_date("2021-12-06"),-1), interval 1 day))))

But I am getting:

array_intersect should have been two arrays with same element type, but it's [array<string>, array<date>]

When I run it on my JSON.

So I believe I need to convert my array to array<date>. Any suggestions?

question from:https://stackoverflow.com/questions/65926552/count-of-all-dates-in-an-array-which-are-present-between-two-other-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make sure that both arrays are array<date>. In your example, the first array was array<string>, which caused an error when you intersect it with an array<date>.

select 
    size(
        array_intersect(
            transform(array("2021-12-02","2021-12-05","2021-12-10"), x -> date(x)),
            sequence(to_date("2021-12-01"), date_add(to_date("2021-12-06"), -1), interval 1 day)
        )
    )
;

This query gives 2.


For your initial question, you can try this query:

select 
    size(
        array_intersect(
            transform(array, x -> date(x)),
            sequence(to_date(begin), date_add(to_date(end), -1), interval 1 day)
        )
    )
;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...