Implement Spark-compatible `conv`
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 65/100
Hướng nghiên cứu
Start by reading the sibling implementations hex, unhex and bin under crates/spark/src/function/math/ to copy the UDF registration and #[user_doc] patterns, then port Spark's NumberConverter using u64/unsigned division and wire the overflow error to datafusion.execution.enable_ansi_mode. Enable the commented queries in crates/sqllogictest/test_files/math/conv.slt (fix the second one's missing third argument) and extend it with the listed edge cases. Done means conv is registered, documented, and the slt file passes with ANSI both on and off.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Part of #174 and apache/datafusion-comet#7300.
What the function does
conv(num, from_base, to_base) converts the number written in num from base from_base to base to_base and returns it as a string. The logic is NumberConverter.convert, ported from Hive's UDFConv: the input is parsed into an unsigned 64-bit value; a positive to_base prints that value unsigned, a negative to_base prints it signed. Digits above 9 are printed in upper case. Available since 1.5.0.
num is implicitly cast to STRING (so conv(-10, 16, -10) works) and both bases to INT. The result type is the type of num (STRING; collation-aware in 4.0+). The result is NULL if any argument is NULL, if a base is out of range (from_base not in 2..36 or |to_base| not in 2..36), or if num is empty after trimming. Overflow while parsing gives -1 (all bits set) with ANSI off and an error with ANSI on.
Examples
SELECT conv('100', 2, 10);
-- 4
SELECT conv(-10, 16, -10);
-- -16
SELECT conv('3', 10, 2);
-- 11
SELECT conv('-15', 10, -16);
-- -F
SELECT conv('-15', 10, 16);
-- FFFFFFFFFFFFFFF1
SELECT conv('big', 36, 16);
-- 3A48
SELECT conv('11abc', 10, 16);
-- B
SELECT conv('9223372036854775808', 10, 16);
-- 8000000000000000
SELECT conv('92233720368547758070', 10, 16);
-- FFFFFFFFFFFFFFFF (ANSI off)
-- error ARITHMETIC_OVERFLOW (ANSI on)
SELECT conv('-9223372036854775807', 36, 10);
-- 18446744073709551615 (ANSI off)
Edge cases to test
- NULL in each argument returns NULL.
from_base1 or 37,to_base37 / -37 / 0 / 1 / -1 return NULL (conv('1234', 10, 37)is NULL).conv('', 10, 16)is NULL. - Trimming: only ASCII spaces (0x20) are removed (
UTF8String.trim).conv(' 10 ', 10, 2)is1010; a leading tab is an invalid digit, so the result is0. - Invalid digits: parsing stops at the first character that is not a digit in
from_base, and the valid prefix is converted (conv('11abc', 10, 16)isB). No valid digits gives0(conv('zz', 10, 10);conv('-', 10, 10)). - Case: lower- and upper-case letters are accepted as digits; output letters are upper case.
- Negative input with positive
to_baseis printed as an unsigned two's-complement value (FFFFFFFFFFFFFFF1); with negativeto_baseas signed (-F). - Overflow with ANSI off saturates to
-1(all bits) and then follows the sign rules:conv('9223372036854775807', 36, 10)andconv('-9223372036854775807', 36, 10)are18446744073709551615;conv('100000000000000000000000000000000000000000000000000000000000000000', 2, 8)is1777777777777777777777. - Overflow with ANSI on (
spark.sql.ansi.enabled=true, the 4.0+ default):ARITHMETIC_OVERFLOW(SQLSTATE 22003)Overflow in function conv(). If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error., also for-92233720368547758070. Long.MinValuewithto_base = -2: the longest output (64 digits plus-).- Non-ASCII characters in
numare invalid digits (the UTF-8 bytes are checked one at a time). - Integer and DECIMAL
numarguments are cast to string first (conv(1.5, 10, 2)parses1.5, stops at., and returns1). - Column bases (they are not required to be literals).
Differences across Spark versions
- 4.0: ANSI on by default, so overflow fails by default instead of returning
FFFFFFFFFFFFFFFF-style results; collation-awarenum(result type followsnum). - 3.4 to 4.2 share the same
NumberConverterlogic; ANSI overflow checking exists in all of them.
Implementation notes
- Spark:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala,Conv; logic insql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/NumberConverter.scala; error fromQueryExecutionErrors.overflowInConvError. - DataFusion core has no
conv.datafusion-sparkhashex,unhexandbinincrates/spark/src/function/math/; none does arbitrary-base conversion. - Port
NumberConverterdirectly (unsigned arithmetic withu64,Long.divideUnsignedequivalents), including the stop-at-invalid-digit and overflow-to--1behaviour. Usedatafusion.execution.enable_ansi_modefor the overflow error. - Tests: the placeholder
crates/sqllogictest/test_files/math/conv.sltexists with commented-out queries (note the second one drops the third argument); enable and extend it. Document with#[user_doc].
This issue was generated by an LLM (Claude) from the Apache Spark source code and tests. Examples come from Spark's ExpressionDescription examples, test suites and .sql.out golden files, but please verify them against Spark before relying on them.
- Ngôn ngữ chính
- Rust
- Star
- 7
- Fork
- 15
- Merge trung bình
- 16 giờ 43 phút
- Pull request đã merge (30 ngày)
- 26
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/datafusion-spark
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
apache/datafusion-spark#287 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
apache/datafusion-spark#266 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] make_dt_interval returns NULL for more than about 1.49 million days, Spark returns the intervalCó thể đã có người làm @Subodh-17 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/datafusion-spark#109 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
apache/datafusion-spark#299 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
apache/datafusion-spark#298 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của apache/datafusion-spark
Issue tương tự
-
comp-rbac
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ClickHouse/ClickHouse#131875 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug(python): read_transaction raises TypeError on a Rewrite with rewritten indicesCó thể đã có người làm @Borg93 đã nhận hôm nay. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
lance-format/lance#11117 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
storytold/photocraft#15232 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
forge: fork preflight reads still send `requireCanonical: false`, which Rootstock rejects (-32603)Có thể đã có người làm @Ynyesto đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
foundry-rs/foundry#18485 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
bondybondbond/rushcut#238 · 1 bình luận ·