Skip to content

BitmapBitPosition

Overview

The BitmapBitPosition expression calculates the bit position for a given long value in bitmap operations. This is a utility function used in bitmap-based data processing to determine the position of a bit within a bitmap structure based on the input value.

Syntax

bitmap_bit_position(value)
col("column_name").expr("bitmap_bit_position(column_name)")

Arguments

Argument Type Description
value BIGINT The long value for which to calculate the bit position

Return Type

BIGINT - Returns a long value representing the bit position.

Supported Data Types

  • Input: BIGINT (LongType)

  • Implicit casting is supported for compatible numeric types

Algorithm

  • Takes a single long value as input

  • Delegates computation to the BitmapExpressionUtils.bitmapBitPosition method via static invocation

  • Performs bit position calculation based on the input value

  • Returns the computed bit position as a long value

  • Does not return null for valid inputs (returnNullable = false)

Partitioning Behavior

This expression preserves partitioning behavior:

  • Does not require data shuffling

  • Can be evaluated independently on each partition

  • Does not affect existing partitioning schemes

Edge Cases

  • Null handling: Input nulls are handled according to standard Spark null propagation rules

  • Type safety: Automatic implicit casting ensures input is converted to LongType when possible

  • Non-nullable result: The expression is designed to always return a valid long value for non-null inputs

  • Deterministic behavior: Always produces the same output for the same input value

Code Generation

This expression uses runtime replacement and static method invocation:

  • Does not generate custom code via Tungsten code generation

  • Falls back to interpreted mode using static method calls

  • Leverages the RuntimeReplaceable trait for expression substitution

Examples

-- Calculate bit position for literal values
SELECT bitmap_bit_position(42) AS bit_pos;

-- Use with table columns
SELECT id, bitmap_bit_position(user_id) AS bit_position 
FROM user_events;

-- Use in WHERE clauses for filtering
SELECT * FROM data 
WHERE bitmap_bit_position(category_id) < 100;
// DataFrame API usage
import org.apache.spark.sql.functions._

// Calculate bit position for a column
df.select(expr("bitmap_bit_position(user_id)").alias("bit_pos"))

// Use in transformations
df.withColumn("bit_position", expr("bitmap_bit_position(category_id)"))

// Filter based on bit position
df.filter(expr("bitmap_bit_position(item_id) BETWEEN 10 AND 50"))

See Also

  • Other bitmap-related expressions in the misc_funcs group

  • BitmapExpressionUtils utility class for bitmap operations

  • Bitmap aggregation functions for data analysis