Skip to main content

polyglot_sql/dialects/
solr.rs

1//! Apache Solr SQL Dialect
2//!
3//! Solr-specific SQL dialect based on sqlglot patterns.
4//! Reference: https://solr.apache.org/guide/solr/latest/query-guide/sql-query.html
5//!
6//! Key characteristics:
7//! - Case insensitive normalization
8//! - Uses backticks for identifiers
9//! - Single quotes for strings only
10//! - Note: `||` is OR in Solr (not string concatenation)
11//! - Does not support SEMI/ANTI joins
12
13use super::{DialectImpl, DialectType};
14use crate::error::Result;
15use crate::expressions::{BinaryOp, Expression};
16use crate::generator::GeneratorConfig;
17use crate::tokens::TokenizerConfig;
18
19/// Apache Solr dialect
20pub struct SolrDialect;
21
22impl DialectImpl for SolrDialect {
23    fn dialect_type(&self) -> DialectType {
24        DialectType::Solr
25    }
26
27    fn tokenizer_config(&self) -> TokenizerConfig {
28        let mut config = TokenizerConfig::default();
29        // Solr uses backticks for identifiers
30        config.identifiers.insert('`', '`');
31        // Single quotes only for strings
32        // Note: Default tokenizer already handles single quotes for strings
33        config
34    }
35
36    fn generator_config(&self) -> GeneratorConfig {
37        use crate::generator::IdentifierQuoteStyle;
38        GeneratorConfig {
39            identifier_quote: '`',
40            identifier_quote_style: IdentifierQuoteStyle::BACKTICK,
41            dialect: Some(DialectType::Solr),
42            ..Default::default()
43        }
44    }
45
46    fn transform_expr(&self, expr: Expression) -> Result<Expression> {
47        // Solr has limited SQL support
48        // In Solr, || is OR, not string concatenation (DPIPE_IS_STRING_CONCAT = False in sqlglot)
49        match expr {
50            Expression::DPipe(dpipe) => {
51                // Transform DPipe (||) to Or
52                let left = self.transform_expr(*dpipe.this)?;
53                let right = self.transform_expr(*dpipe.expression)?;
54                Ok(Expression::Or(Box::new(BinaryOp::new(left, right))))
55            }
56            _ => Ok(expr),
57        }
58    }
59}