<commit_before>---
layout: global
title: ANSI Compliance
displayTitle: ANSI Compliance
license: |
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
---

Spark SQL has two options to comply with the SQL standard: `spark.sql.ansi.enabled` and `spark.sql.storeAssignmentPolicy` (See a table below for details).
When `spark.sql.ansi.enabled` is set to `true`, Spark SQL follows the standard in basic behaviours (e.g., arithmetic operations, type conversion, and SQL parsing).
Moreover, Spark SQL has an independent option to control implicit casting behaviours when inserting rows in a table.
The casting behaviours are defined as store assignment rules in the standard.
When `spark.sql.storeAssignmentPolicy` is set to `ANSI`, Spark SQL complies with the ANSI store assignment rules.

<table class="table">
<tr><th>Property Name</th><th>Default</th><th>Meaning</th></tr>
<tr>
  <td><code>spark.sql.ansi.enabled</code></td>
  <td>false</td>
  <td>
    When true, Spark tries to conform to the ANSI SQL specification:
    1. Spark will throw a runtime exception if an overflow occurs in any operation on integral/decimal field.
    2. Spark will forbid using the reserved keywords of ANSI SQL as identifiers in the SQL parser.
  </td>
</tr>
<tr>
  <td><code>spark.sql.storeAssignmentPolicy</code></td>
  <td>ANSI</td>
  <td>
    When inserting a value into a column with different data type, Spark will perform type coercion.
    Currently, we support 3 policies for the type coercion rules: ANSI, legacy and strict. With ANSI policy,
    Spark performs the type coercion as per ANSI SQL. In practice, the behavior is mostly the same as PostgreSQL.<commit_msg>[SPARK-30703][SQL][DOCS][FOLLOWUP] Declare the ANSI SQL compliance options as experimental<commit_after>---
layout: global
title: ANSI Compliance
displayTitle: ANSI Compliance
license: |
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
---

Since Spark 3.0, Spark SQL introduces two experimental options to comply with the SQL standard: `spark.sql.ansi.enabled` and `spark.sql.storeAssignmentPolicy` (See a table below for details).

When `spark.sql.ansi.enabled` is set to `true`, Spark SQL follows the standard in basic behaviours (e.g., arithmetic operations, type conversion, and SQL parsing).
Moreover, Spark SQL has an independent option to control implicit casting behaviours when inserting rows in a table.
The casting behaviours are defined as store assignment rules in the standard.

When `spark.sql.storeAssignmentPolicy` is set to `ANSI`, Spark SQL complies with the ANSI store assignment rules. This is a separate configuration because its default value is `ANSI`, while the configuration `spark.sql.ansi.enabled` is disabled by default.

<table class="table">
<tr><th>Property Name</th><th>Default</th><th>Meaning</th></tr>
<tr>
  <td><code>spark.sql.ansi.enabled</code></td>
  <td>false</td>
  <td>
    (Experimental) When true, Spark tries to conform to the ANSI SQL specification:
    1. Spark will throw a runtime exception if an overflow occurs in any operation on integral/decimal field.
    2. Spark will forbid using the reserved keywords of ANSI SQL as identifiers in the SQL parser.
  </td>
</tr>
<tr>
  <td><code>spark.sql.storeAssignmentPolicy</code></td>
  <td>ANSI</td>
  <td>
    (Experimental) When inserting a value into a column with different data type, Spark will perform type coercion.
    Currently, we support 3 policies for the type coercion rules: ANSI, legacy and strict. With ANSI policy,
    Spark performs the type coercion as per ANSI SQL. In practice, the behavior is mostly the same as PostgreSQL.
{'fraction_non_alphanumeric': 0.07313698359359558, 'fraction_numerical': 0.004151017987744613, 'mean_word_length': 4.292887029288703, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5426211', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1303, 'n_words': 672}