Skip to content

Conversation

@cloud-fan
Copy link
Contributor

What changes were proposed in this pull request?

This is a followup of #48210 to fix correctness issues caused by pgsql filter pushdown. These datetime fields were picked wrongly before, see https://neon.tech/postgresql/postgresql-date-functions/postgresql-extract

Why are the changes needed?

bug fix

Does this PR introduce any user-facing change?

Yes, query result is corrected, but this bug is not released yet.

How was this patch tested?

updated test

Was this patch authored or co-authored using generative AI tooling?

no

@cloud-fan
Copy link
Contributor Author

cc @beliefer @MaxGekk

@github-actions github-actions bot added the SQL label Feb 27, 2025
@cloud-fan
Copy link
Contributor Author

and also cc @yaooqinn

case "DAY_OF_WEEK" => s"EXTRACT(DOW FROM $source)"
case _ => super.visitExtract(field, source)
// SECOND, MINUTE, HOUR, QUARTER, YEAR, DAY are identical on postgres and spark
// MONTH is different, postgres returns 0-11, spark returns 1-12.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the doc you provided, but it says 1-based for datetimes, 0-based for intervals on the pg side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, sorry I misread the doc.

Now looking into it again, Spark DS v2 pushdown does not support extracting fields from interval type yet (expressions like ExtractANSIIntervalMonths are not included in V2ExpressionBuilder). But even if we support it later, its semantic is the same as pgsql.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I believe they shall be identical as they all came from the work within the Postgres feature parity umbrella ticket.

I appreciate the confirmation.

// SECOND, MINUTE, HOUR, QUARTER, YEAR, DAY are identical on postgres and spark
// MONTH is different, postgres returns 0-11, spark returns 1-12.
// Postgres also returns 1-12 but just for interval columns, so without source
// data type, we cannot know how to push down it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

屏幕快照 2025-02-28 上午10 37 07

withClue("dayofweek") {
val dow = sql(s"SELECT dayofweek(date1) FROM $tbl WHERE name = 'alex'")
.collect().head.getInt(0)
val df = sql(s"SELECT name FROM $tbl WHERE dayofweek(date1) = $dow")
Copy link
Contributor Author

@cloud-fan cloud-fan Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better idea is probably having a test infra that automatically checks the result between JDBC pushdown on and off. I'll leave it to followups.

// but in V2ExpressionBuilder they converted DAY_OF_WEEK to DAY_OF_WEEK_ISO,
// so we need to push down ISODOW
// (ISO and standard day of weeks differs in starting day,
// Sunday is 0 on standard DOW extraction, while in ISO it's 7)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thank you for the explanation.

Copy link
Contributor

@beliefer beliefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I will check other dialects.

@cloud-fan
Copy link
Contributor Author

the streaming test failure is unrelated, thanks for the review, merging to master/4.0!

@cloud-fan cloud-fan closed this in 208a7ee Feb 28, 2025
cloud-fan added a commit that referenced this pull request Feb 28, 2025
…ushing down EXTRACT

### What changes were proposed in this pull request?

This is a followup of #48210 to fix correctness issues caused by pgsql filter pushdown. These datetime fields were picked wrongly before, see https://neon.tech/postgresql/postgresql-date-functions/postgresql-extract

### Why are the changes needed?

bug fix

### Does this PR introduce _any_ user-facing change?

Yes, query result is corrected, but this bug is not released yet.

### How was this patch tested?

updated test

### Was this patch authored or co-authored using generative AI tooling?

no

Closes #50101 from cloud-fan/pgsql.

Authored-by: Wenchen Fan <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
(cherry picked from commit 208a7ee)
Signed-off-by: Wenchen Fan <[email protected]>
zifeif2 pushed a commit to zifeif2/spark that referenced this pull request Nov 14, 2025
…ushing down EXTRACT

### What changes were proposed in this pull request?

This is a followup of apache#48210 to fix correctness issues caused by pgsql filter pushdown. These datetime fields were picked wrongly before, see https://neon.tech/postgresql/postgresql-date-functions/postgresql-extract

### Why are the changes needed?

bug fix

### Does this PR introduce _any_ user-facing change?

Yes, query result is corrected, but this bug is not released yet.

### How was this patch tested?

updated test

### Was this patch authored or co-authored using generative AI tooling?

no

Closes apache#50101 from cloud-fan/pgsql.

Authored-by: Wenchen Fan <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
(cherry picked from commit e65434e)
Signed-off-by: Wenchen Fan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants