Compile Spreadsheet Formulas Into Warehouse SQL Expressions

Last updated: November 29, 2026

Quick Overview

Write a compiler that turns a Sigma-style formula such as `If([Revenue] > 1000, "big", "small")` or `[Revenue] - [Cost] * 2` into a SQL expression with correct operator precedence, quoted identifiers mapped to physical columns, safely escaped string literals, and clear errors for bad input.

Sigma Computing
Coding & Algorithms
Software Engineer
Sigma Computing
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Write a compiler that turns a Sigma-style formula such as `If([Revenue] > 1000, "big", "small")` or `[Revenue] - [Cost] * 2` into a SQL expression with correct operator precedence, quoted identifiers mapped to physical columns, safely escaped string literals, and clear errors for bad input.

Sigma's core trick is that every formula a user types runs as SQL inside the customer's warehouse, so formula-to-SQL compilation is close to the heart of the product. The interviewer checks tokenizing, precedence parsing, injection-safe output, and whether errors are useful to a business user.

What the Interviewer Expects
  • Separate tokenizing from parsing and keep both small
  • Handle precedence and left associativity (10 - 3 - 2 is 5, not 9)
  • Map display column names to quoted physical identifiers and reject unknown columns
  • Escape string literals so user text can never break out of the SQL
  • Translate functions that have no direct SQL twin, such as If to CASE WHEN
  • Give errors with positions or names rather than generic failures
Key Topics to Cover
Pratt parsing
operator precedence
SQL injection safety
tokenization
code generation
error reporting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add type checking so `[Region] + 1` fails before it reaches the warehouse?
  • How would you emit different SQL for Snowflake versus BigQuery for the same function?
  • How would you strip redundant parentheses without changing meaning?
  • Where would aggregate functions like Sum([Revenue]) change the compilation, given they force a GROUP BY?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Support numbers, double-quoted strings, [Column] references, + - * / & (string concat), comparisons, unary minus, parentheses, and a few functions (If, Coalesce, Round, Lower). Output is a...

High-Level Design

A regex tokenizer yields (kind, value) pairs. A Pratt parser handles precedence with a binding-power table: comparisons 10, & 20, + - 30, * / 40, unary minus 45. Each binary node is emitted fu...


Submit Your Answer
Markdown supported

Related Questions